openany-cli-dev 0.1.3 → 0.1.5
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/README.md +10 -0
- package/package.json +1 -1
- package/skills/openany-provider/SKILL.md +8 -4
- package/skills/openany-provider/SKILL.zh-CN.md +8 -4
- package/skills/openany-provider/references/apis.md +2 -0
- package/skills/openany-provider/references/apis.zh-CN.md +2 -0
- package/skills/openany-provider/references/application.md +1 -1
- package/skills/openany-provider/references/application.zh-CN.md +1 -1
- package/skills/openany-provider/references/connections.md +4 -4
- package/skills/openany-provider/references/connections.zh-CN.md +4 -4
- package/skills/openany-provider/references/hermes-auto-files.md +121 -0
- package/skills/openany-provider/references/hermes-files.md +133 -0
- package/skills/openany-provider/references/imports.md +2 -0
- package/skills/openany-provider/references/imports.zh-CN.md +2 -0
- package/skills/openany-provider/references/login.md +3 -3
- package/skills/openany-provider/references/login.zh-CN.md +3 -3
- package/skills/openany-provider/references/model-import.md +1 -1
- package/skills/openany-provider/references/model-import.zh-CN.md +1 -1
- package/skills/openany-provider/references/models.md +3 -1
- package/skills/openany-provider/references/models.zh-CN.md +3 -1
- package/skills/openany-provider/references/publishing.md +7 -3
- package/skills/openany-provider/references/publishing.zh-CN.md +6 -4
- package/skills/openany-provider/references/reviews.md +2 -0
- package/skills/openany-provider/references/reviews.zh-CN.md +2 -0
- package/skills/openany-provider/templates/provider-agent-api-template.json +1 -1
- package/skills/openany-provider/templates/seller-agent-api-import-template.json +1 -1
- package/src/artifact-collector.js +72 -0
- package/src/artifact-files.js +45 -0
- package/src/cli.js +7 -0
- package/src/hermes-delivery.js +228 -0
- package/src/http.js +7 -0
- package/src/poll-artifacts.js +48 -0
- package/src/provider-api-import-v2.js +1 -1
- package/src/provider-import-format.js +5 -5
- package/src/provider-imports.js +1 -1
- package/src/provider-models.js +3 -3
- package/src/provider-state.js +31 -14
- package/src/provider.js +8 -0
|
@@ -2,19 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
Use the environment, account, and supplier confirmed in the [main entry](../SKILL.md) before collecting resource materials. If no supplier is available, follow [Seller application](application.md) once authorized. For historical reviews only, read [Review results](reviews.md).
|
|
4
4
|
|
|
5
|
+
Follow the [main entry](../SKILL.md) form-first rule for every missing input, choice, or confirmation in this workflow. Ask through the native form once and wait for the answer.
|
|
6
|
+
|
|
5
7
|
## 1. Identify the type, connection, and source
|
|
6
8
|
|
|
7
9
|
Route an explicit “list an API” request to the [standard API workflow](apis.md) only, and an explicit “list a model” request to the [model workflow](models.md) only. Skip the resource type question for both.
|
|
8
10
|
|
|
9
|
-
For a request to list a resource, if the request and already confirmed information leave the type unclear, first
|
|
11
|
+
For a request to list a resource, if the request and already confirmed information leave the type unclear, first open a native form asking “What type of resource do you want to list?” Offer **Skill, Bot, Service, Data, and Goods**. If the types exceed the form's option limit, show the full list in the question and use follow-up selection forms to capture the exact type. Use the form's text input only if selectable options are unavailable. Reuse a type the user has already specified; API-based delivery alone does not make the listing an API. Use the form to clarify the stopping point (draft only or verify and publish) only if it is unclear.
|
|
10
12
|
|
|
11
13
|
For Skill, Bot, Service, Data, or Goods, use the [Agent API template](../templates/seller-agent-api-import-template.json) and [Imports](imports.md). Set `catalogType` to the selected type: `skill`, `bot`, `service`, `data`, or `digital`, respectively. The template's `service` value is an example.
|
|
12
14
|
|
|
13
15
|
When a connection is needed and still undecided, use the native form to choose an existing connection or add one. Follow [Connections](connections.md), reusing confirmed connections and using the secure dialog when no connection fields have been collected. For a connection-only task, stop once you confirm the ID and revision.
|
|
14
16
|
|
|
15
|
-
Confirm the required connection before requesting or reading source materials. When the source is missing, open a separate **Source materials** form with the options applicable to the chosen type: resource or API documentation link, sanitized cURL, file upload, or absolute local path. If the form allows only three options, group the two file options and ask how to provide the file next. In the form prompt, show the matching [Agent API](../templates/seller-agent-api-import-template.json), [standard API](../templates/provider-agent-api-template.json), or [model](../templates/models-v2.json) template location and say: “The template location is provided here. If you do not have a copy, download the template, fill it in, then upload the completed file or enter its absolute path.” Use an accessible absolute URL or verified local absolute path. If the location is inaccessible, attach the template and name it in the form. For Agent API listings, point to `fieldGuide` and the examples in `apis[0]`.
|
|
17
|
+
Confirm the required connection before requesting or reading source materials. When the source is missing, open a separate **Source materials** form with the options applicable to the chosen type: resource or API documentation link, sanitized cURL, file upload, or absolute local path. If the form allows only three options, group the two file options and ask how to provide the file next. In the form prompt, show the matching [Agent API](../templates/seller-agent-api-import-template.json), [standard API](../templates/provider-agent-api-template.json), or [model](../templates/models-v2.json) template location and say: “The template location is provided here. If you do not have a copy, download the template, fill it in, then upload the completed file or enter its absolute path.” Use an accessible absolute URL or verified local absolute path. If the location is inaccessible, attach the template and name it in the form. For Agent API listings, point to `fieldGuide` and the examples in `apis[0]`. Use a follow-up form for input the source selector cannot collect. For a file upload, direct the user to the attachment control.
|
|
18
|
+
|
|
19
|
+
Wait for the user to identify the source before inspecting any source materials. Do not scan the current directory or workspace for candidates, even during CLI setup. If the user chooses a local file without supplying its path, use a text-input form to ask only for the file's absolute path. Once the user provides the path, check only that file. If the path is invalid or points to a directory, use the form to request the correct file path.
|
|
16
20
|
|
|
17
|
-
Read the chosen source and ask only about omissions or conflicts. Collect the included content, usage instructions, effect examples, and how the resource is delivered. If its delivery is unsupported by the CLI, explain the limitation before preparing a draft. Preserve the supplied methods, paths, mappings, and billing terms. Validate imports against the template. Fetch the schema once for direct creation.
|
|
21
|
+
Read the chosen source and use the form to ask only about omissions or conflicts. Collect the included content, usage instructions, effect examples, and how the resource is delivered. If its delivery is unsupported by the CLI, explain the limitation before preparing a draft. Preserve the supplied methods, paths, mappings, and billing terms. Validate imports against the template. Fetch the schema once for direct creation.
|
|
18
22
|
|
|
19
23
|
Use top-level `categories` for APIs and `configuration.categories` in the metadata for OpenAPI imports. Use at most 16 distinct IDs. Use `routes[].familyId` for new public models, with choices from `items[].families` in the import preview. Preserve existing assignments. If the family is missing, ask the user to pick from the current options. Allow independently complete items to proceed.
|
|
20
24
|
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
# API / 模型 / 资源上架与连接
|
|
2
2
|
|
|
3
|
-
沿用[主入口](../SKILL.zh-CN.md)确认的环境、账号和供应商,再收集资源资料。无可用供应商时,获授权后转入[供应商申请](application.zh-CN.md)。仅查历史审核时读[审核结果](reviews.zh-CN.md)
|
|
3
|
+
沿用[主入口](../SKILL.zh-CN.md)确认的环境、账号和供应商,再收集资源资料。无可用供应商时,获授权后转入[供应商申请](application.zh-CN.md)。仅查历史审核时读[审核结果](reviews.zh-CN.md)。与用户的交互优先使用表单交互,尽量不要在对话里提问。
|
|
4
4
|
|
|
5
5
|
## 1. 确定类型、连接和资料来源
|
|
6
6
|
|
|
7
7
|
用户明确要求「上架 API」时,只走[普通 API 流程](apis.zh-CN.md)。「上架模型」只走[模型流程](models.zh-CN.md)。这两种请求均跳过资源类型提问。
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
用户要求上架资源时,如果请求和已明确的信息仍未指定类型,先用原生表单问「你想上架哪种资源?」列出 **Skill(技能)、Bot(机器人)、Service(服务)、Data(数据)、Goods(商品)**。类型超过表单选项上限时,在题面列出完整类型,再用后续选择表单收集具体类型。只有表单不支持可选项时才用表单文本输入。用户已明确类型就直接沿用;通过 API 交付不等于上架 API。仅在停止点不明确时用表单补问是只建草稿,还是验证并发布。
|
|
10
10
|
|
|
11
11
|
Skill、Bot、Service、Data、Goods 使用 [Agent API 模板](../templates/seller-agent-api-import-template.json),走[导入流程](imports.zh-CN.md),并按用户选定类型将 `catalogType` 分别填写为 `skill`、`bot`、`service`、`data`、`digital`。模板中的 `service` 只是示例。
|
|
12
12
|
|
|
13
13
|
需要连接且用户未指定时,用原生表单选择复用已有连接或新建。按[连接流程](connections.zh-CN.md)复用已确认的连接,按录入进度选择安全弹框或补齐字段。仅建连接时,确认 ID 和修订即结束。
|
|
14
14
|
|
|
15
|
-
先确认本次需要的连接,再索取或读取资源资料。缺来源时,单独打开**资料来源**表单:按已选类型提供适用的资源或接口文档链接、脱敏 cURL、上传文件或本地绝对路径选项。表单最多支持三项时合并文件选项,下一步再问上传或路径。在表单题面显示对应的 [Agent API](../templates/seller-agent-api-import-template.json)、[普通 API](../templates/provider-agent-api-template.json)或[模型](../templates/models-v2.json)模板位置,并注明「这里已提供模板位置。如果你还没有模板,请先下载并填写,再上传填好的文件或输入本地绝对路径。」使用可访问的完整 URL 或已核实的本地绝对路径,位置不可访问时提供附件并在表单标出文件名。Agent API 资源请参考 `fieldGuide` 和 `apis[0]`
|
|
15
|
+
先确认本次需要的连接,再索取或读取资源资料。缺来源时,单独打开**资料来源**表单:按已选类型提供适用的资源或接口文档链接、脱敏 cURL、上传文件或本地绝对路径选项。表单最多支持三项时合并文件选项,下一步再问上传或路径。在表单题面显示对应的 [Agent API](../templates/seller-agent-api-import-template.json)、[普通 API](../templates/provider-agent-api-template.json)或[模型](../templates/models-v2.json)模板位置,并注明「这里已提供模板位置。如果你还没有模板,请先下载并填写,再上传填好的文件或输入本地绝对路径。」使用可访问的完整 URL 或已核实的本地绝对路径,位置不可访问时提供附件并在表单标出文件名。Agent API 资源请参考 `fieldGuide` 和 `apis[0]` 中的示例。来源选择表单无法接收所选资料时,用后续表单收集。选择上传文件时,引导用户使用附件上传入口。
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
用户确认来源后才检查源资料。包括 CLI 准备阶段在内,禁止扫描当前目录或工作区寻找候选文件。用户选择本地文件但未给出路径时,只用文本输入表单索取该文件的绝对路径。收到路径后只检查指定文件。路径无效或指向目录时,通过表单请用户提供正确的文件路径。
|
|
18
|
+
|
|
19
|
+
读取指定资料后只通过表单补问缺项或冲突,保留原方法、路径、映射和计费。收集交付内容、使用说明、效果示例和交付方式。如果 CLI 不支持该交付方式,建草稿前先说明限制。导入按模板校验,直接创建时取对应 schema 一次。
|
|
18
20
|
|
|
19
21
|
API 分类用顶层 `categories`,OpenAPI 导入写入元数据的 `configuration.categories`,最多 16 个不重复 ID。新公共模型用 `routes[].familyId`,选项来自导入预览的 `items[].families`;已有归属保持原样。缺失时请用户从当前选项选择,缺配置的项目不阻塞独立完整项。
|
|
20
22
|
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Resource reviews are retired. Historical `reviews list|get` records remain readable, but do not determine current publication. Do not submit, approve, or withdraw resource reviews. Read the model/API itself for current activation and runtime synchronization. Supplier admission remains required.
|
|
4
4
|
|
|
5
|
+
Follow the [main entry](../SKILL.md) form-first rule if recovery needs the user's input or confirmation.
|
|
6
|
+
|
|
5
7
|
## Recover without duplicate side effects
|
|
6
8
|
|
|
7
9
|
Keep operation names unique across independent writes and stable across recovery. Preserve the original payload, account, application, resource ID, and any returned receipt IDs. Never treat matching names as proof of resource ownership.
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
可以查询历史 `reviews list|get` 记录。当前启用和运行时同步状态以 API 或模型本身为准。供应商须先准入,资源审核的提交、审批和撤回流程已停用。
|
|
4
4
|
|
|
5
|
+
恢复过程中需要用户补充或确认时,按[主入口](../SKILL.zh-CN.md)的表单优先规则处理。
|
|
6
|
+
|
|
5
7
|
## 恢复结果不确定的操作
|
|
6
8
|
|
|
7
9
|
独立写入使用各自的操作名,恢复时保留原操作名、载荷、账号、供应商、资源 ID 和回执 ID。核对实际归属。操作与原账号、供应商、HTTP 方法、路径和 JSON 载荷绑定。变更后复用操作名会报 `IDEMPOTENCY_CONFLICT`。以原参数重跑已完成的操作只会返回已保存的结果。
|
|
@@ -543,7 +543,7 @@
|
|
|
543
543
|
"type": "object",
|
|
544
544
|
"required": true,
|
|
545
545
|
"description": "Enter non-sensitive request headers.",
|
|
546
|
-
"constraints": "
|
|
546
|
+
"constraints": "Authorization may contain a placeholder such as Bearer $OPENANY_DEV_API_KEY in request examples. Configure real credentials through the connection. Use {} if there are no extra headers.",
|
|
547
547
|
"example": {}
|
|
548
548
|
},
|
|
549
549
|
"apis[].config.requestResponseExamples[].request.body": {
|
|
@@ -594,7 +594,7 @@
|
|
|
594
594
|
"type": "object",
|
|
595
595
|
"required": true,
|
|
596
596
|
"description": "Enter non-sensitive request headers.",
|
|
597
|
-
"constraints": "
|
|
597
|
+
"constraints": "Authorization may contain a placeholder such as Bearer $OPENANY_DEV_API_KEY in request examples. Configure real credentials through the connection. Use {} if there are no extra headers.",
|
|
598
598
|
"example": {}
|
|
599
599
|
},
|
|
600
600
|
"apis[].config.requestResponseExamples[].request.body": {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { extname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { inside, MAX_FILE_BYTES, readStableArtifact, sha256 } from './artifact-files.js';
|
|
5
|
+
import { CLIError } from './errors.js';
|
|
6
|
+
|
|
7
|
+
const mime = {'.pdf':'application/pdf','.zip':'application/zip','.docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
8
|
+
'.xlsx':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','.xls':'application/vnd.ms-excel',
|
|
9
|
+
'.json':'application/json','.txt':'text/plain','.csv':'text/csv'};
|
|
10
|
+
const extensions = /\.(?:pdf|zip|docx?|xlsx?|csv|txt|json|png|jpe?g|gif|webp|bmp|tiff|svg|mp[34]|mov|avi|mkv|webm|wav|ogg|m4a|flac|odt|rtf|md|tsv|xml|ya?ml|pptx?|odp|tar|gz|tgz|bz2|7z|html?)$/i;
|
|
11
|
+
|
|
12
|
+
export function replyArtifactPaths(output) {
|
|
13
|
+
// Code examples are not declarations of final artifacts (same convention as Hermes).
|
|
14
|
+
const text = String(output || '').replace(/(```|~~~)[\s\S]*?\1/g, '').replace(/`+[^`\n]*`+/g, '');
|
|
15
|
+
return [...new Set((text.match(/(?:~\/|\/)[^\s<>"'`,。;!?()【】]+/g) || [])
|
|
16
|
+
.map(p => p.replace(/[)\],;:!?]+$/, '')).filter(p => extensions.test(p)))];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// STORE-only ZIP: fixed timestamps, safe relative names and no external archiver dependency.
|
|
20
|
+
function crc32(bytes) {
|
|
21
|
+
let crc = 0xffffffff;
|
|
22
|
+
for (const byte of bytes) { crc ^= byte; for (let n=0;n<8;n++) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); }
|
|
23
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
24
|
+
}
|
|
25
|
+
export function zipArtifacts(files) {
|
|
26
|
+
const local = [], central = []; let offset = 0;
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const name = Buffer.from(file.name); const crc = crc32(file.bytes);
|
|
29
|
+
const h = Buffer.alloc(30); h.writeUInt32LE(0x04034b50); h.writeUInt16LE(20,4); h.writeUInt16LE(0x800,6);
|
|
30
|
+
h.writeUInt16LE(33,12); h.writeUInt32LE(crc,14); h.writeUInt32LE(file.bytes.length,18); h.writeUInt32LE(file.bytes.length,22); h.writeUInt16LE(name.length,26);
|
|
31
|
+
const c = Buffer.alloc(46); c.writeUInt32LE(0x02014b50); c.writeUInt16LE(20,4); c.writeUInt16LE(20,6); c.writeUInt16LE(0x800,8);
|
|
32
|
+
c.writeUInt16LE(33,14); c.writeUInt32LE(crc,16); c.writeUInt32LE(file.bytes.length,20); c.writeUInt32LE(file.bytes.length,24); c.writeUInt16LE(name.length,28); c.writeUInt32LE(offset,42);
|
|
33
|
+
local.push(h,name,file.bytes); central.push(c,name); offset += h.length + name.length + file.bytes.length;
|
|
34
|
+
}
|
|
35
|
+
const directory = Buffer.concat(central); const end = Buffer.alloc(22);
|
|
36
|
+
end.writeUInt32LE(0x06054b50); end.writeUInt16LE(files.length,8); end.writeUInt16LE(files.length,10); end.writeUInt32LE(directory.length,12); end.writeUInt32LE(offset,16);
|
|
37
|
+
return Buffer.concat([...local,directory,end]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function collectArtifacts(taskDir, output) {
|
|
41
|
+
let paths;
|
|
42
|
+
const manifest = join(taskDir, 'openany-artifacts.json');
|
|
43
|
+
try {
|
|
44
|
+
await fs.lstat(manifest);
|
|
45
|
+
const value = JSON.parse((await readStableArtifact(taskDir, manifest, 65536)).toString('utf8'));
|
|
46
|
+
if (!Array.isArray(value.files) || !value.files.length || value.files.length > 32 || value.files.some(p => typeof p !== 'string' || !p))
|
|
47
|
+
throw new CLIError('INVALID_MANIFEST', 'Expected a nonempty files array with at most 32 paths');
|
|
48
|
+
paths = value.files;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error.code !== 'ENOENT') throw error;
|
|
51
|
+
paths = replyArtifactPaths(output);
|
|
52
|
+
}
|
|
53
|
+
if (!paths.length || paths.length > 32) throw new CLIError('ARTIFACTS_MISSING', 'No final artifact declaration found');
|
|
54
|
+
const files = []; const seen = new Set(); let size = 0;
|
|
55
|
+
for (const path of paths) {
|
|
56
|
+
if (/[\\\x00-\x1f\x7f]/.test(path)) throw new CLIError('UNSAFE_PATH', 'Artifact paths cannot contain backslashes or control characters');
|
|
57
|
+
const absolute = path.startsWith('~/') ? resolve(homedir(),path.slice(2)) : isAbsolute(path) ? path : resolve(taskDir,path);
|
|
58
|
+
if (!inside(taskDir, absolute) || relative(taskDir,absolute).split(/[\\/]/).some(p => p.startsWith('.openany-')) || absolute === manifest)
|
|
59
|
+
throw new CLIError('UNSAFE_PATH', 'Only declared files inside the task directory can be delivered');
|
|
60
|
+
const canonical = await fs.realpath(absolute);
|
|
61
|
+
if (seen.has(canonical)) continue;
|
|
62
|
+
seen.add(canonical);
|
|
63
|
+
const bytes = await readStableArtifact(taskDir, absolute); size += bytes.length;
|
|
64
|
+
if (size > MAX_FILE_BYTES) throw new CLIError('INVALID_INPUT', 'Combined artifacts exceed the file limit');
|
|
65
|
+
files.push({name:relative(taskDir,absolute).split('\\').join('/'),bytes});
|
|
66
|
+
}
|
|
67
|
+
files.sort((a,b) => a.name.localeCompare(b.name,'en'));
|
|
68
|
+
const singleType = files.length === 1 && mime[extname(files[0].name).toLowerCase()];
|
|
69
|
+
const bytes = singleType ? files[0].bytes : zipArtifacts(files);
|
|
70
|
+
if (bytes.length > MAX_FILE_BYTES) throw new CLIError('INVALID_INPUT', 'Delivery archive exceeds the file limit');
|
|
71
|
+
return {bytes, content_type: singleType || 'application/zip', name:singleType ? 'delivery'+extname(files[0].name).toLowerCase() : 'delivery.zip', size:bytes.length, sha256:sha256(bytes)};
|
|
72
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import fs, { realpath } from 'node:fs/promises';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { isAbsolute, relative, sep } from 'node:path';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { CLIError } from './errors.js';
|
|
6
|
+
|
|
7
|
+
export const MAX_FILE_BYTES = 10_000_000;
|
|
8
|
+
export const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
9
|
+
export const inside = (root, file) => {
|
|
10
|
+
const rel = relative(root, file);
|
|
11
|
+
return rel !== '' && rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
|
|
12
|
+
};
|
|
13
|
+
const unchanged = (a, b) => ['dev','ino','size','mtimeNs','ctimeNs'].every(k => a[k] === b[k]);
|
|
14
|
+
|
|
15
|
+
// File contents and the resolved task boundary must remain stable for the whole read.
|
|
16
|
+
// Process/UID isolation remains necessary against a hostile process with the same UID.
|
|
17
|
+
export async function readStableArtifact(root, file, limit = MAX_FILE_BYTES) {
|
|
18
|
+
let handle;
|
|
19
|
+
try {
|
|
20
|
+
const boundary = await realpath(root);
|
|
21
|
+
const resolved = await realpath(file);
|
|
22
|
+
if (boundary !== root || !inside(root, resolved)) throw new CLIError('UNSAFE_PATH', 'File must remain inside its task directory');
|
|
23
|
+
const before = await fs.stat(resolved, {bigint:true});
|
|
24
|
+
handle = await fs.open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
25
|
+
const opened = await handle.stat({bigint:true});
|
|
26
|
+
if (!unchanged(before, opened) || !opened.isFile()) throw new CLIError('FILE_CHANGED', 'File changed while opening');
|
|
27
|
+
if (opened.size < 1 || opened.size > BigInt(limit)) throw new CLIError('INVALID_INPUT', 'Invalid artifact size');
|
|
28
|
+
const buffer = Buffer.alloc(Number(opened.size) + 1);
|
|
29
|
+
let size = 0;
|
|
30
|
+
while (size < buffer.length) {
|
|
31
|
+
const part = await handle.read(buffer, size, buffer.length - size, size);
|
|
32
|
+
if (!part.bytesRead) break;
|
|
33
|
+
size += part.bytesRead;
|
|
34
|
+
}
|
|
35
|
+
const after = await handle.stat({bigint:true});
|
|
36
|
+
const pathAfter = await fs.stat(resolved, {bigint:true});
|
|
37
|
+
if (BigInt(size) !== opened.size || !unchanged(opened, after) || !unchanged(after, pathAfter) ||
|
|
38
|
+
await realpath(root) !== boundary || await realpath(file) !== resolved)
|
|
39
|
+
throw new CLIError('FILE_CHANGED', 'File changed during reading; retry after generation has finished');
|
|
40
|
+
return buffer.subarray(0, size);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof CLIError) throw error;
|
|
43
|
+
throw new CLIError('FILE_UNAVAILABLE', 'Cannot read the task file');
|
|
44
|
+
} finally { await handle?.close(); }
|
|
45
|
+
}
|
package/src/cli.js
CHANGED
|
@@ -43,6 +43,12 @@ Model execution uses the OpenAny gateway with the path and query from curlExampl
|
|
|
43
43
|
Multiple providers require --provider; multiple requests require --example.
|
|
44
44
|
Supply the complete JSON body, including model; route.provider is bound from the catalog.
|
|
45
45
|
Audio transcription is not supported. Task polling is explicit, never automatic.
|
|
46
|
+
seller hermes serve [-j] (loopback native Runs relay with durable automatic file delivery)
|
|
47
|
+
seller hermes status --id TASK_ID [-j] (local delivery state; no credentials or business output)
|
|
48
|
+
Requires OPENANY_HERMES_URL, OPENANY_HERMES_API_KEY, OPENANY_HERMES_STATE_DIR,
|
|
49
|
+
OPENANY_TASK_ROOT, OPENANY_DEV_SELLER_KEY; optional OPENANY_HERMES_PORT (default 8790).
|
|
50
|
+
seller tasks upload --id TASK_ID --file FILE --content-type MIME [-j]
|
|
51
|
+
Requires OPENANY_DEV_SELLER_KEY and absolute OPENANY_TASK_ROOT; does not settle the task.
|
|
46
52
|
seller status [--application ID]
|
|
47
53
|
seller applications list|create --file REGISTRATION_JSON --operation NAME
|
|
48
54
|
seller connections list|get|create --application ID [--id ID]
|
|
@@ -165,6 +171,7 @@ const valuedOptions = new Set([
|
|
|
165
171
|
"resource-id",
|
|
166
172
|
"verification-id",
|
|
167
173
|
"timeout-seconds",
|
|
174
|
+
"content-type",
|
|
168
175
|
]);
|
|
169
176
|
export function parseArgs(argv) {
|
|
170
177
|
const options = {},
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { randomUUID, timingSafeEqual } from 'node:crypto';
|
|
5
|
+
import { GATEWAY_ORIGIN } from './http.js';
|
|
6
|
+
import { CLIError } from './errors.js';
|
|
7
|
+
import { inside, readStableArtifact, sha256 } from './artifact-files.js';
|
|
8
|
+
import { collectArtifacts } from './artifact-collector.js';
|
|
9
|
+
import { uploadPollArtifact } from './poll-artifacts.js';
|
|
10
|
+
|
|
11
|
+
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
12
|
+
const runId = /^[A-Za-z0-9_-]{1,128}$/;
|
|
13
|
+
const done = new Set(['stored','failed','expired']);
|
|
14
|
+
const fail = (code, status=422) => { throw new CLIError(code, code, {status}); };
|
|
15
|
+
const MAX_JSON = 1_048_576;
|
|
16
|
+
|
|
17
|
+
async function syncDir(path) { const h = await fs.open(path,'r'); try { await h.sync(); } finally { await h.close(); } }
|
|
18
|
+
async function atomicWrite(path, bytes) {
|
|
19
|
+
const temp = path+'.'+randomUUID()+'.tmp';
|
|
20
|
+
const h = await fs.open(temp,'wx',0o600);
|
|
21
|
+
try { await h.writeFile(bytes); await h.sync(); } finally { await h.close(); }
|
|
22
|
+
await fs.rename(temp,path); await syncDir(resolve(path,'..'));
|
|
23
|
+
}
|
|
24
|
+
async function jsonFile(path) { return JSON.parse(await fs.readFile(path,'utf8')); }
|
|
25
|
+
async function readBounded(stream, limit=MAX_JSON) {
|
|
26
|
+
const chunks=[]; let size=0;
|
|
27
|
+
for await (const chunk of stream) { size+=chunk.length; if (size>limit) fail('BODY_TOO_LARGE',413); chunks.push(chunk); }
|
|
28
|
+
return Buffer.concat(chunks);
|
|
29
|
+
}
|
|
30
|
+
function equal(a,b) { const x=Buffer.from(a || ''); const y=Buffer.from(b); return x.length===y.length && timingSafeEqual(x,y); }
|
|
31
|
+
|
|
32
|
+
export class HermesDelivery {
|
|
33
|
+
constructor({ctx, fetch=globalThis.fetch, now=Date.now}) {
|
|
34
|
+
this.ctx=ctx; this.env=ctx.env; this.fetch=fetch; this.now=now; this.jobs=new Map(); this.serial=Promise.resolve();
|
|
35
|
+
this.key=this.env.OPENANY_HERMES_API_KEY?.trim();
|
|
36
|
+
if (!this.key || !/^[\x21-\x7e]+$/.test(this.key) || !this.env.OPENANY_DEV_SELLER_KEY?.trim()) fail('CONFIG_REQUIRED');
|
|
37
|
+
ctx.secrets.push(this.key,this.env.OPENANY_DEV_SELLER_KEY.trim());
|
|
38
|
+
for (const name of ['OPENANY_TASK_ROOT','OPENANY_HERMES_STATE_DIR']) if (!isAbsolute(this.env[name] || '')) fail('CONFIG_REQUIRED');
|
|
39
|
+
this.root=resolve(this.env.OPENANY_TASK_ROOT); this.stateDir=resolve(this.env.OPENANY_HERMES_STATE_DIR);
|
|
40
|
+
if (this.root===this.stateDir || inside(this.root,this.stateDir) || inside(this.stateDir,this.root)) fail('UNSAFE_STATE_DIRECTORY');
|
|
41
|
+
try { this.url=new URL(this.env.OPENANY_HERMES_URL); } catch { fail('INVALID_HERMES_URL'); }
|
|
42
|
+
if (this.url.username || this.url.password || this.url.search || this.url.hash || this.url.pathname !== '/' ||
|
|
43
|
+
!(this.url.protocol==='https:' || this.url.protocol==='http:' && ['127.0.0.1','[::1]','localhost'].includes(this.url.hostname))) fail('INVALID_HERMES_URL');
|
|
44
|
+
this.ctx.env={...this.env,OPENANY_TASK_ROOT:this.root};
|
|
45
|
+
}
|
|
46
|
+
locked(fn) { const next=this.serial.then(fn); this.serial=next.catch(()=>{}); return next; }
|
|
47
|
+
async init() {
|
|
48
|
+
for (const path of [this.root,this.stateDir]) {
|
|
49
|
+
await fs.mkdir(path,{recursive:true,mode:0o700});
|
|
50
|
+
const stat=await fs.lstat(path);
|
|
51
|
+
if (stat.isSymbolicLink() || stat.mode & 0o077 || stat.uid!==process.getuid?.()) fail('UNSAFE_STATE_DIRECTORY');
|
|
52
|
+
}
|
|
53
|
+
this.root=await fs.realpath(this.root); this.stateDir=await fs.realpath(this.stateDir);
|
|
54
|
+
this.ctx.env.OPENANY_TASK_ROOT=this.root;
|
|
55
|
+
if (this.root===this.stateDir || inside(this.root,this.stateDir) || inside(this.stateDir,this.root)) fail('UNSAFE_STATE_DIRECTORY');
|
|
56
|
+
this.lockPath=join(this.stateDir,'worker.lock');
|
|
57
|
+
for (let n=0;n<2;n++) {
|
|
58
|
+
try { this.lock=await fs.open(this.lockPath,'wx',0o600); await this.lock.writeFile(String(process.pid)); await this.lock.sync(); break; }
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error.code!=='EEXIST' || n) fail('COLLECTOR_ALREADY_RUNNING');
|
|
61
|
+
const pid=Number(await fs.readFile(this.lockPath,'utf8'));
|
|
62
|
+
if (!Number.isSafeInteger(pid) || pid<1) fail('COLLECTOR_LOCK_UNCERTAIN');
|
|
63
|
+
try { process.kill(pid,0); fail('COLLECTOR_ALREADY_RUNNING'); }
|
|
64
|
+
catch (e) { if (e.code!=='ESRCH') throw e; await fs.unlink(this.lockPath); }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
// Changing a Hermes credential changes its idempotency scope. Require reconciliation first.
|
|
69
|
+
const binding={root:this.root,hermes:this.url.href,key_hash:sha256(this.key),environment:GATEWAY_ORIGIN};
|
|
70
|
+
const path=join(this.stateDir,'binding.json');
|
|
71
|
+
try { if (JSON.stringify(await jsonFile(path))!==JSON.stringify(binding)) fail('COLLECTOR_BINDING_CHANGED'); }
|
|
72
|
+
catch (e) { if(e.code!=='ENOENT') throw e; await atomicWrite(path,JSON.stringify(binding)); }
|
|
73
|
+
for (const entry of await fs.readdir(this.stateDir)) if (/^[0-9a-f-]{36}\.json$/.test(entry)) {
|
|
74
|
+
const job=await jsonFile(join(this.stateDir,entry));
|
|
75
|
+
if (!uuid.test(job.task_id) || entry!==job.task_id+'.json' || job.version!==1 || !Number.isFinite(job.deadline)) fail('INVALID_DELIVERY_STATE');
|
|
76
|
+
this.jobs.set(job.task_id,job);
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
} catch(e) { await this.close(); throw e; }
|
|
80
|
+
}
|
|
81
|
+
async close() { await this.serial; if(this.lock) { await this.lock.close(); this.lock=null; await fs.unlink(this.lockPath); } }
|
|
82
|
+
async save(job) { await atomicWrite(join(this.stateDir,job.task_id+'.json'),JSON.stringify(job)); this.jobs.set(job.task_id,job); }
|
|
83
|
+
async request(path,{method='GET',body,id,deadline=this.now()+10000}={}) {
|
|
84
|
+
const response=await this.fetch(new URL(path,this.url),{method,headers:{authorization:'Bearer '+this.key,'content-type':'application/json',...(id?{'idempotency-key':id}:{})},
|
|
85
|
+
body,redirect:'error',signal:AbortSignal.timeout(Math.max(1,Math.min(10000,deadline-this.now())))});
|
|
86
|
+
const bytes=await readBounded(response.body);
|
|
87
|
+
let value; try { value=JSON.parse(bytes.toString()); } catch { fail('INVALID_HERMES_RESPONSE',502); }
|
|
88
|
+
if (!response.ok) fail('HERMES_HTTP_'+response.status,response.status);
|
|
89
|
+
return {status:response.status,body:value};
|
|
90
|
+
}
|
|
91
|
+
async checkCapabilities(deadline) {
|
|
92
|
+
const {body}=await this.request('/v1/capabilities',{deadline}); const idem=body.features?.runs_idempotency;
|
|
93
|
+
if (idem?.supported!==true || idem?.durable!==true || !(idem.retention_seconds*1000 >= deadline-this.now()+10000)) fail('DURABLE_RUNS_REQUIRED',503);
|
|
94
|
+
}
|
|
95
|
+
async admit(id,deadlineText,body,idem) { return this.locked(async()=>{
|
|
96
|
+
const deadline=Date.parse(deadlineText);
|
|
97
|
+
if (!uuid.test(id || '') || idem!==id || !Number.isFinite(deadline) || deadline<=this.now() || !body || typeof body!=='object' || Array.isArray(body)) fail('INVALID_TASK_CONTEXT');
|
|
98
|
+
const original=JSON.stringify(body); let job=this.jobs.get(id);
|
|
99
|
+
if (job && (job.request_hash!==sha256(original) || job.deadline!==deadline)) fail('TASK_CONTEXT_CONFLICT',409);
|
|
100
|
+
if (!job) {
|
|
101
|
+
await this.checkCapabilities(deadline);
|
|
102
|
+
const taskDir=join(this.root,id); await fs.mkdir(taskDir,{recursive:true,mode:0o700});
|
|
103
|
+
if (await fs.realpath(taskDir)!==taskDir || (await fs.lstat(taskDir)).mode & 0o077) fail('UNSAFE_TASK_DIRECTORY');
|
|
104
|
+
const native={...body,instructions:(body.instructions || '')+'\n\nOpenAny 自动文件交付:仅在 '+taskDir+' 中生成本任务最终成果。完成后在回复中以纯文本列出成果的绝对路径(不放在代码块或反引号中),并提供非空摘要;或者写入 '+join(taskDir,'openany-artifacts.json')+',格式为 {"files":["report.pdf"]}。只声明最终文件,勿列出中间产物。不要自行执行上传命令,自动收集器负责上传。'};
|
|
105
|
+
job={version:1,task_id:id,deadline,request_hash:sha256(original),native:JSON.stringify(native),state:'dispatching',attempts:0,next_at:0};
|
|
106
|
+
// Persist the exact admitted payload BEFORE dispatch. Unknown outcomes replay only this payload/key.
|
|
107
|
+
await this.save(job);
|
|
108
|
+
}
|
|
109
|
+
if (!job.run_id && !done.has(job.state)) await this.dispatch(job);
|
|
110
|
+
if (!job.run_id) fail('ADMISSION_UNCONFIRMED',503);
|
|
111
|
+
return {status:202,body:{run_id:job.run_id,status:'queued',replayed:job.attempts>1}};
|
|
112
|
+
}); }
|
|
113
|
+
async dispatch(job) {
|
|
114
|
+
if (this.now()>=job.deadline) { job.state='expired'; await this.save(job); return; }
|
|
115
|
+
// Recheck durable admission after a restart before replaying an uncertain POST.
|
|
116
|
+
await this.checkCapabilities(job.deadline);
|
|
117
|
+
const response=await this.request('/v1/runs',{method:'POST',body:job.native,id:job.task_id,deadline:job.deadline});
|
|
118
|
+
if (!runId.test(response.body.run_id || '') || response.status!==202) fail('INVALID_HERMES_ADMISSION',502);
|
|
119
|
+
job.run_id=response.body.run_id; job.state='running'; job.attempts++; await this.save(job);
|
|
120
|
+
}
|
|
121
|
+
async freeze(job,output) {
|
|
122
|
+
const taskDir=join(this.root,job.task_id); const dir=join(taskDir,'.openany-delivery');
|
|
123
|
+
try {
|
|
124
|
+
const meta=await jsonFile(join(dir,'snapshot.json'));
|
|
125
|
+
if (!/^delivery\.[a-z0-9]+$/.test(meta.name) || !Number.isSafeInteger(meta.size) || !/^[0-9a-f]{64}$/.test(meta.sha256)) fail('INVALID_SNAPSHOT');
|
|
126
|
+
const bytes=await readStableArtifact(taskDir,join(dir,meta.name));
|
|
127
|
+
if (sha256(bytes)!==meta.sha256 || bytes.length!==meta.size) fail('SNAPSHOT_CORRUPT');
|
|
128
|
+
return {...meta,file:join(dir,meta.name)};
|
|
129
|
+
} catch(e) { if(e.code!=='ENOENT') throw e; }
|
|
130
|
+
// Atomic directory publication makes recovery reuse the same immutable ZIP after a crash.
|
|
131
|
+
const artifact=await collectArtifacts(taskDir,output); const staging=join(taskDir,'.openany-staging-'+randomUUID());
|
|
132
|
+
await fs.mkdir(staging,{mode:0o700});
|
|
133
|
+
const {bytes,...meta}=artifact;
|
|
134
|
+
try {
|
|
135
|
+
await atomicWrite(join(staging,meta.name),bytes); await atomicWrite(join(staging,'snapshot.json'),JSON.stringify(meta));
|
|
136
|
+
await fs.rename(staging,dir); await syncDir(taskDir);
|
|
137
|
+
} catch(e) { await fs.rm(staging,{recursive:true,force:true}); throw e; }
|
|
138
|
+
return {...meta,file:join(dir,meta.name)};
|
|
139
|
+
}
|
|
140
|
+
async step() { return this.locked(async()=>{
|
|
141
|
+
for (const job of this.jobs.values()) {
|
|
142
|
+
if (done.has(job.state) || job.next_at>this.now() && this.now()<job.deadline) continue;
|
|
143
|
+
try {
|
|
144
|
+
if (this.now()>=job.deadline) { job.state='expired'; job.last_code='DEADLINE_EXCEEDED'; await this.save(job); continue; }
|
|
145
|
+
if (!job.run_id) await this.dispatch(job);
|
|
146
|
+
if (!job.run_id) continue;
|
|
147
|
+
if (!job.frozen) {
|
|
148
|
+
const {body}=await this.request('/v1/runs/'+job.run_id,{deadline:job.deadline});
|
|
149
|
+
if (body.run_id!==job.run_id) fail('RUN_ID_MISMATCH');
|
|
150
|
+
if (['failed','cancelled','interrupted'].includes(body.status)) { job.state='failed'; job.last_code='HERMES_RUN_FAILED'; await this.save(job); continue; }
|
|
151
|
+
if (body.status!=='completed') { job.next_at=this.now()+2000; await this.save(job); continue; }
|
|
152
|
+
if (typeof body.output!=='string' || !body.output.trim()) fail('RESULT_MISSING');
|
|
153
|
+
job.frozen=await this.freeze(job,body.output); job.state='uploading'; await this.save(job);
|
|
154
|
+
}
|
|
155
|
+
if(this.now()>=job.deadline) fail('DEADLINE_EXCEEDED');
|
|
156
|
+
// Reuse the very same uploader as `seller tasks upload`, including scope and receipt checks.
|
|
157
|
+
const receipt=await uploadPollArtifact(['seller','tasks','upload'],{...this.ctx,
|
|
158
|
+
options:{id:job.task_id,file:job.frozen.file,'content-type':job.frozen.content_type},expectedArtifact:job.frozen,
|
|
159
|
+
http:{gateway:(path,options)=>this.ctx.http.gateway(path,{...options,timeoutMs:Math.max(1,Math.min(10000,job.deadline-this.now()))})}});
|
|
160
|
+
job.receipt=receipt; job.state='stored'; job.last_code=null; delete job.native; await this.save(job);
|
|
161
|
+
} catch(e) {
|
|
162
|
+
job.attempts++; job.last_code=/^[A-Z0-9_]+$/.test(e.code || '')?e.code:'DELIVERY_RETRY';
|
|
163
|
+
const permanent=['UNSAFE_PATH','INVALID_INPUT','INVALID_MANIFEST','SNAPSHOT_CORRUPT','INVALID_SNAPSHOT','FILE_CHANGED'].includes(e.code) && job.frozen ||
|
|
164
|
+
[400,401,403,409,410,413,422].includes(e.details?.status);
|
|
165
|
+
job.state=this.now()>=job.deadline?'expired':permanent?'failed':job.run_id?'retry':'dispatching';
|
|
166
|
+
job.next_at=this.now()+Math.min(30000,1000*2**Math.min(job.attempts,5)); await this.save(job);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}); }
|
|
170
|
+
publicStatus(id) {
|
|
171
|
+
const job=this.jobs.get(id); if(!job) fail('TASK_NOT_FOUND',404);
|
|
172
|
+
return Object.fromEntries(['task_id','run_id','state','attempts','last_code','deadline'].map(k=>[k,job[k] ?? null]));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function createHermesRelay(options) {
|
|
177
|
+
const delivery=await new HermesDelivery(options).init();
|
|
178
|
+
const send=(res,status,body)=>{res.writeHead(status,{'content-type':'application/json','cache-control':'no-store'});res.end(JSON.stringify(body));};
|
|
179
|
+
const server=createServer(async(req,res)=>{
|
|
180
|
+
try {
|
|
181
|
+
if (!equal(req.headers.authorization,'Bearer '+delivery.key)) fail('AUTHENTICATION_INVALID',401);
|
|
182
|
+
if (req.url==='/health' && req.method==='GET') return send(res,200,{ok:true});
|
|
183
|
+
const status=/^\/openany\/delivery\/([0-9a-f-]{36})$/.exec(req.url);
|
|
184
|
+
if (status && req.method==='GET') return send(res,200,delivery.publicStatus(status[1]));
|
|
185
|
+
if (req.url==='/v1/runs' && req.method==='POST') {
|
|
186
|
+
let body; try { body=JSON.parse((await readBounded(req)).toString()); } catch(e) { if(e instanceof CLIError) throw e; fail('INVALID_JSON',400); }
|
|
187
|
+
if (req.headers['x-openany-artifact-delivery']==='auto_cli') {
|
|
188
|
+
const r=await delivery.admit(req.headers['x-openany-task-id'],req.headers['x-openany-deadline'],body,req.headers['idempotency-key']);
|
|
189
|
+
return send(res,r.status,r.body);
|
|
190
|
+
}
|
|
191
|
+
if (Object.keys(req.headers).some(k=>k.startsWith('x-openany-'))) fail('INVALID_TASK_CONTEXT');
|
|
192
|
+
const r=await delivery.request('/v1/runs',{method:'POST',body:JSON.stringify(body),id:req.headers['idempotency-key']}); return send(res,r.status,r.body);
|
|
193
|
+
}
|
|
194
|
+
if (req.method==='GET' && (/^\/v1\/runs\/[A-Za-z0-9_-]{1,128}$/.test(req.url) || ['/v1/skills','/v1/capabilities'].includes(req.url))) {
|
|
195
|
+
const r=await delivery.request(req.url); return send(res,r.status,r.body);
|
|
196
|
+
}
|
|
197
|
+
fail('NOT_FOUND',404);
|
|
198
|
+
} catch(e) { send(res,e.details?.status || 503,{error:{code:/^[A-Z0-9_]+$/.test(e.code || '')?e.code:'DELIVERY_UNAVAILABLE'}}); }
|
|
199
|
+
});
|
|
200
|
+
server.requestTimeout=15000; server.headersTimeout=10000;
|
|
201
|
+
let ticking=false;
|
|
202
|
+
const tick=()=>{if(ticking)return;ticking=true;delivery.step().catch(()=>options.ctx.stderr?.write('{"error":"DELIVERY_STATE_WRITE_FAILED"}\n')).finally(()=>ticking=false);};
|
|
203
|
+
const timer=setInterval(tick,1000); timer.unref(); tick();
|
|
204
|
+
return {server,delivery,async close(){clearInterval(timer);server.closeIdleConnections();await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));await delivery.close();}};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function runHermesDelivery(args,ctx) {
|
|
208
|
+
if (args.length!==3 || !['serve','status'].includes(args[2]) || Object.keys(ctx.options).some(k=>!['id','json'].includes(k)))
|
|
209
|
+
throw new CLIError('INVALID_INPUT','Use seller hermes serve [-j] or seller hermes status --id TASK_ID [-j]');
|
|
210
|
+
if (args[2]==='status') {
|
|
211
|
+
const id=ctx.options.id;
|
|
212
|
+
if (!uuid.test(id || '') || !isAbsolute(ctx.env.OPENANY_HERMES_STATE_DIR || '')) fail('CONFIG_REQUIRED');
|
|
213
|
+
const job=await jsonFile(join(ctx.env.OPENANY_HERMES_STATE_DIR,id+'.json'));
|
|
214
|
+
return Object.fromEntries(['task_id','run_id','state','attempts','last_code','deadline'].map(k=>[k,job[k] ?? null]));
|
|
215
|
+
}
|
|
216
|
+
const port=Number(ctx.env.OPENANY_HERMES_PORT || 8790);
|
|
217
|
+
if (!Number.isInteger(port) || port<1024 || port>65535 || ctx.options.id) fail('INVALID_INPUT');
|
|
218
|
+
const relay=await createHermesRelay({ctx});
|
|
219
|
+
try {
|
|
220
|
+
await new Promise((resolve,reject)=>{relay.server.once('error',reject);relay.server.listen(port,'127.0.0.1',resolve);});
|
|
221
|
+
ctx.stdout.write(JSON.stringify({ready:true,address:'127.0.0.1',port})+'\n');
|
|
222
|
+
await new Promise(resolve=>{
|
|
223
|
+
const stop=()=>{process.off('SIGTERM',stop);process.off('SIGINT',stop);resolve();};
|
|
224
|
+
process.once('SIGTERM',stop);process.once('SIGINT',stop);
|
|
225
|
+
});
|
|
226
|
+
} finally { await relay.close(); }
|
|
227
|
+
return {stopped:true};
|
|
228
|
+
}
|
package/src/http.js
CHANGED
|
@@ -94,6 +94,7 @@ export class HttpClient {
|
|
|
94
94
|
body,
|
|
95
95
|
headers: extra,
|
|
96
96
|
credential,
|
|
97
|
+
sellerKey,
|
|
97
98
|
auth,
|
|
98
99
|
allowStatuses = [],
|
|
99
100
|
timeoutMs = this.timeoutMs,
|
|
@@ -114,6 +115,11 @@ export class HttpClient {
|
|
|
114
115
|
"CREDENTIAL_SCOPE",
|
|
115
116
|
"API key is not allowed on this backend endpoint",
|
|
116
117
|
);
|
|
118
|
+
if (sellerKey !== undefined && (
|
|
119
|
+
origin !== GATEWAY_ORIGIN || method !== "POST" || auth || credential ||
|
|
120
|
+
!/^\/v1\/agent-api-tasks\/[0-9a-f-]{36}\/artifacts$/.test(path) ||
|
|
121
|
+
typeof sellerKey !== "string" || !/^[\x21-\x7e]+$/.test(sellerKey)))
|
|
122
|
+
throw new CLIError("CREDENTIAL_SCOPE", "Seller upload credentials are scoped to task artifact uploads");
|
|
117
123
|
const url = new URL(path, origin);
|
|
118
124
|
if (rawSearch) url.search = rawSearch;
|
|
119
125
|
for (const [key, value] of Object.entries(query || {}))
|
|
@@ -140,6 +146,7 @@ export class HttpClient {
|
|
|
140
146
|
});
|
|
141
147
|
headers.set("Authorization", `Bearer ${this.apiKey}`);
|
|
142
148
|
}
|
|
149
|
+
if (sellerKey) headers.set("Authorization", `Bearer ${sellerKey}`);
|
|
143
150
|
if (credential?.token)
|
|
144
151
|
headers.set("Authorization", `Bearer ${credential.token}`);
|
|
145
152
|
if (credential?.workspaceId)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { realpath } from 'node:fs/promises';
|
|
2
|
+
import { basename, isAbsolute, join, relative } from 'node:path';
|
|
3
|
+
import { CLIError } from './errors.js';
|
|
4
|
+
import { inside, readStableArtifact, sha256 } from './artifact-files.js';
|
|
5
|
+
|
|
6
|
+
export const types = new Set(['application/pdf', 'application/zip',
|
|
7
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
8
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
9
|
+
'application/vnd.ms-excel', 'application/json', 'text/plain', 'text/csv']);
|
|
10
|
+
|
|
11
|
+
export async function uploadPollArtifact(args, ctx) {
|
|
12
|
+
const { options, env, http, secrets } = ctx;
|
|
13
|
+
const keyName = 'OPENANY_DEV_SELLER_KEY';
|
|
14
|
+
const key = env[keyName]?.trim();
|
|
15
|
+
if (key) secrets.push(key);
|
|
16
|
+
if (args.length !== 3 || args[2] !== 'upload' ||
|
|
17
|
+
Object.keys(options).some(k => !['id','file','content-type','json','output'].includes(k)))
|
|
18
|
+
throw new CLIError('INVALID_INPUT', 'Use seller tasks upload --id TASK_ID --file FILE --content-type MIME [-j]');
|
|
19
|
+
const id = options.id?.toLowerCase();
|
|
20
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(id || ''))
|
|
21
|
+
throw new CLIError('INVALID_INPUT', 'Use the OpenAny task UUID, not the Hermes run_id');
|
|
22
|
+
if (!key || !/^[\x21-\x7e]+$/.test(key))
|
|
23
|
+
throw new CLIError('CONFIG_REQUIRED', `Configure ${keyName} in the execution environment`);
|
|
24
|
+
if (!types.has(options['content-type']))
|
|
25
|
+
throw new CLIError('INVALID_INPUT', 'Unsupported file MIME type; use ZIP for multiple files');
|
|
26
|
+
if (!env.OPENANY_TASK_ROOT || !isAbsolute(env.OPENANY_TASK_ROOT) || !options.file)
|
|
27
|
+
throw new CLIError('INVALID_INPUT', 'Configure absolute OPENANY_TASK_ROOT and provide --file');
|
|
28
|
+
const root = await realpath(env.OPENANY_TASK_ROOT);
|
|
29
|
+
const taskDir = await realpath(join(root, id));
|
|
30
|
+
if (!inside(root, taskDir) || relative(root, taskDir) !== id)
|
|
31
|
+
throw new CLIError('UNSAFE_PATH', 'Task directory must remain inside OPENANY_TASK_ROOT');
|
|
32
|
+
const bytes = await readStableArtifact(taskDir, options.file);
|
|
33
|
+
const hash = sha256(bytes);
|
|
34
|
+
if (ctx.expectedArtifact && (ctx.expectedArtifact.sha256 !== hash || ctx.expectedArtifact.size !== bytes.length))
|
|
35
|
+
throw new CLIError('FILE_CHANGED', 'Frozen artifact no longer matches its delivery record');
|
|
36
|
+
const body = new FormData();
|
|
37
|
+
body.set('metadata', JSON.stringify({status:'completed'}));
|
|
38
|
+
body.set('files', new Blob([bytes], {type:options['content-type']}), basename(options.file));
|
|
39
|
+
const response = await http.gateway(`/v1/agent-api-tasks/${id}/artifacts`, {
|
|
40
|
+
method:'POST', body, sellerKey:key,
|
|
41
|
+
});
|
|
42
|
+
if (response.body?.task_id !== id || response.body?.status !== 'stored' ||
|
|
43
|
+
typeof response.body?.artifact?.artifact_id !== 'string' ||
|
|
44
|
+
response.body.artifact.size !== bytes.length || response.body.artifact.sha256 !== hash ||
|
|
45
|
+
response.body.artifact.content_type !== options['content-type'])
|
|
46
|
+
throw new CLIError('INVALID_RESPONSE', 'Upload outcome is uncertain; retry the same task and unchanged file');
|
|
47
|
+
return response.body;
|
|
48
|
+
}
|
|
@@ -42,7 +42,7 @@ export function apiImportV2(document) {
|
|
|
42
42
|
return document.apis.map((entry, index) => {
|
|
43
43
|
const path = `apis[${index}]`;
|
|
44
44
|
const api = rename(entry, fields.api, path);
|
|
45
|
-
checkConfiguration(api);
|
|
45
|
+
checkConfiguration(api, "api");
|
|
46
46
|
validateApiImportCategories(api.categories, path);
|
|
47
47
|
if (!["GET", "POST", "PUT", "PATCH", "DELETE"].includes(api.method) ||
|
|
48
48
|
typeof api.pathTemplate !== "string" || !api.pathTemplate.startsWith("/") || api.pathTemplate.length > maxPathLength)
|
|
@@ -18,7 +18,7 @@ export function manifest(value, name, type) {
|
|
|
18
18
|
"IMPORT_FORMAT",
|
|
19
19
|
`${name} requires schemaVersion 1 and at most 100 items`,
|
|
20
20
|
);
|
|
21
|
-
assertNoSecrets(value);
|
|
21
|
+
assertNoSecrets(value, "$", type);
|
|
22
22
|
const seen = new Set();
|
|
23
23
|
for (const item of value.items) {
|
|
24
24
|
if (
|
|
@@ -77,7 +77,7 @@ export function parseImport(
|
|
|
77
77
|
document,
|
|
78
78
|
metadata = { schemaVersion: 1, items: [] },
|
|
79
79
|
) {
|
|
80
|
-
assertNoSecrets(document);
|
|
80
|
+
assertNoSecrets(document, "$", type);
|
|
81
81
|
const websiteModel = type === "model" && (document?.schemaVersion === modelImportVersion || document?.models !== undefined);
|
|
82
82
|
if (websiteModel)
|
|
83
83
|
document = modelImportV2(document);
|
|
@@ -86,13 +86,13 @@ export function parseImport(
|
|
|
86
86
|
);
|
|
87
87
|
let items = [];
|
|
88
88
|
if (type === "model") {
|
|
89
|
-
items = manifest(document, "Model manifest").map((item) => {
|
|
89
|
+
items = manifest(document, "Model manifest", type).map((item) => {
|
|
90
90
|
if (
|
|
91
91
|
Object.keys(item).some((k) => !["itemId", "configuration"].includes(k))
|
|
92
92
|
)
|
|
93
93
|
throw new CLIError("IMPORT_FORMAT", "Unexpected model item field");
|
|
94
94
|
const c = item.configuration;
|
|
95
|
-
checkConfiguration(c);
|
|
95
|
+
checkConfiguration(c, type);
|
|
96
96
|
if (!object(c.price) || !Array.isArray(c.routes) || !c.routes.length)
|
|
97
97
|
throw new CLIError(
|
|
98
98
|
"IMPORT_FORMAT",
|
|
@@ -183,7 +183,7 @@ export function parseImport(
|
|
|
183
183
|
"Metadata cannot override the source method or path",
|
|
184
184
|
{ itemId },
|
|
185
185
|
);
|
|
186
|
-
checkConfiguration(extra);
|
|
186
|
+
checkConfiguration(extra, type);
|
|
187
187
|
const security = operation.security ?? document.security;
|
|
188
188
|
items.push({
|
|
189
189
|
itemId,
|
package/src/provider-imports.js
CHANGED
|
@@ -312,7 +312,7 @@ export async function runImports(action, client, ctx) {
|
|
|
312
312
|
"Completed or unknown items cannot change",
|
|
313
313
|
);
|
|
314
314
|
for (let i = 0; i < bound.length; i++) {
|
|
315
|
-
if (bound[i].configuration) checkConfiguration(bound[i].configuration);
|
|
315
|
+
if (bound[i].configuration) checkConfiguration(bound[i].configuration, batch.type);
|
|
316
316
|
if (
|
|
317
317
|
digest(bound[i].configuration) !==
|
|
318
318
|
digest(batch.items[i].configuration)
|