viatris-exam-cli 1.6.0 → 1.8.0
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 +21 -12
- package/package.json +2 -2
- package/skills/viatris-exam-cli/SKILL.md +6 -3
- package/skills/viatris-exam-cli/references/cli-reference.md +30 -10
- package/skills/viatris-exam-cli.zip +0 -0
- package/src/app.js +14 -2
- package/src/command-handlers/knowledge.js +19 -3
- package/src/command-handlers/products.js +2 -1
- package/src/config.js +17 -7
- package/src/credentials.js +100 -0
- package/src/help.js +9 -4
- package/src/metadata.js +17 -4
- package/src/sanitizers/documents.js +113 -0
package/README.md
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
```bash
|
|
8
8
|
npm link
|
|
9
9
|
export VIATRIS_API_KEY="replace-with-real-key"
|
|
10
|
+
viatris-exam login
|
|
11
|
+
unset VIATRIS_API_KEY
|
|
10
12
|
```
|
|
11
13
|
|
|
12
14
|
默认连接线上环境:
|
|
@@ -29,25 +31,27 @@ https://apaaspsc-app_17gbe211qq5-1879207268486311.aiforce.run/app/app_17gbe211qq
|
|
|
29
31
|
|
|
30
32
|
`VIATRIS_API_ENV` 支持 `production`、`development`,以及别名 `prod`、`dev`。如需临时连接其他部署,可设置 `VIATRIS_API_BASE_URL`;该变量的优先级高于环境预设。
|
|
31
33
|
|
|
32
|
-
CLI 不接受 `--api-key`
|
|
34
|
+
`login` 将 Key 写入 `~/.viatris-exam/.credentials.json`。隐藏目录权限为 `0700`,凭证文件权限为 `0600`。CLI 不接受 `--api-key` 参数,也不会输出 Key。
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
X-API-Key: <VIATRIS_API_KEY>
|
|
36
|
-
```
|
|
36
|
+
读取顺序:
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
1. 优先读取隐藏凭证文件。
|
|
39
|
+
2. 文件不存在时读取 `VIATRIS_API_KEY`。
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
默认认证格式:
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
Authorization: Bearer <API key>
|
|
43
45
|
```
|
|
44
46
|
|
|
47
|
+
无需设置 `VIATRIS_API_KEY_HEADER` 或 `VIATRIS_API_KEY_PREFIX`。
|
|
48
|
+
|
|
45
49
|
## Agent 接口约定
|
|
46
50
|
|
|
47
51
|
- API 成功响应以 JSON 写入 `stdout`。
|
|
48
52
|
- 错误以单行结构化 JSON 写入 `stderr`。
|
|
49
53
|
- POST/PUT 请求从文件、`stdin` 或 `--json` 读取 JSON 对象。
|
|
50
|
-
- API Key
|
|
54
|
+
- API Key 优先从隐藏凭证文件读取,环境变量作为兜底。
|
|
51
55
|
- 默认使用线上环境,可通过 `VIATRIS_API_ENV=development` 切换到开发环境。
|
|
52
56
|
- `VIATRIS_API_BASE_URL` 可覆盖当前环境地址。
|
|
53
57
|
- GET 请求遇到网络错误、HTTP 429 或 5xx 时自动重试,POST/PUT/PATCH/DELETE 不自动重试。
|
|
@@ -114,6 +118,8 @@ viatris-exam knowledge documents delete <document-id>
|
|
|
114
118
|
|
|
115
119
|
`/knowledge` CRUD 响应中的 `level` 是物理存储层级(产品根为 1、固定维度为 2);产品生成接口使用业务层级(产品隐藏、固定维度为 L1、动态节点为 L2-L4)。
|
|
116
120
|
|
|
121
|
+
所有文档写入都会在请求发出前从 `title`、`url`、`summary` 中移除“小荷”“小荷医疗”和“小荷 AI 医生”及其空格变体。当前写接口只接受 `title` 和 `summary`,输入中的 `url` 会在净化后从出站载荷移除,但 URL 净化能力作为永久兼容规则保留。该规则覆盖文档创建、批量 upsert、单文档更新和整树回写中的嵌套文档;标题清洗后为空会直接返回校验错误。
|
|
122
|
+
|
|
117
123
|
### 出题任务
|
|
118
124
|
|
|
119
125
|
```bash
|
|
@@ -196,14 +202,14 @@ printf '%s' '{"query":"阿托伐他汀 适应症","limit":20}' |
|
|
|
196
202
|
|
|
197
203
|
```text
|
|
198
204
|
--base-url <url> 覆盖当前环境地址
|
|
199
|
-
--key-header <name>
|
|
200
|
-
--key-prefix <value>
|
|
205
|
+
--key-header <name> 覆盖认证头,默认 Authorization
|
|
206
|
+
--key-prefix <value> 覆盖认证前缀,默认 "Bearer "
|
|
201
207
|
--timeout <ms> 超时时间,默认 30000
|
|
202
208
|
--retries <n> GET 重试次数,默认 2
|
|
203
209
|
--compact 输出单行紧凑 JSON
|
|
204
210
|
```
|
|
205
211
|
|
|
206
|
-
地址选择优先级为 `--base-url`、`VIATRIS_API_BASE_URL`、`VIATRIS_API_ENV` 对应的预设地址。认证 Key
|
|
212
|
+
地址选择优先级为 `--base-url`、`VIATRIS_API_BASE_URL`、`VIATRIS_API_ENV` 对应的预设地址。认证 Key 优先使用隐藏凭证文件,文件不存在时使用 `VIATRIS_API_KEY`。
|
|
207
213
|
|
|
208
214
|
## 项目结构
|
|
209
215
|
|
|
@@ -217,7 +223,10 @@ src/
|
|
|
217
223
|
├── args.js # 参数解析与校验
|
|
218
224
|
├── input.js # JSON 文件、stdin 与内联输入
|
|
219
225
|
├── config.js # 环境、地址与认证配置
|
|
226
|
+
├── credentials.js # 隐藏凭证文件读写
|
|
220
227
|
├── http.js # HTTP、重试与响应处理
|
|
228
|
+
├── sanitizers/
|
|
229
|
+
│ └── documents.js # 文档永久净化规则
|
|
221
230
|
├── metadata.js # 版本、命令和环境元数据
|
|
222
231
|
├── help.js # 帮助文本
|
|
223
232
|
├── output.js # 标准 JSON 输出
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viatris-exam-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Agent-friendly CLI for the Viatris exam OpenAPI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
14
|
"test": "node --test",
|
|
15
|
-
"check": "node --check src/*.js && node --check src/command-handlers/*.js",
|
|
15
|
+
"check": "node --check src/*.js && node --check src/command-handlers/*.js && node --check src/sanitizers/*.js",
|
|
16
16
|
"check:versions": "node scripts/check-versions.js",
|
|
17
17
|
"check:skill": "node scripts/check-skill.js",
|
|
18
18
|
"pack:skill": "cd skills && /bin/rm -f viatris-exam-cli.zip && /usr/bin/zip -qr viatris-exam-cli.zip viatris-exam-cli -x '*.DS_Store'"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: viatris-exam-cli
|
|
3
|
-
description: Operate the Viatris exam OpenAPI through its agent-friendly CLI. Use for products, knowledge
|
|
4
|
-
version: 1.
|
|
3
|
+
description: Operate the Viatris exam OpenAPI through its agent-friendly CLI. Use for login, products, knowledge, generation tasks, exam records, scoring, exams, questions, or medical search.
|
|
4
|
+
version: 1.8.0
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# Viatris Exam CLI
|
|
@@ -11,7 +11,7 @@ Use the repository-local CLI for every supported API operation. Do not call the
|
|
|
11
11
|
## Start
|
|
12
12
|
|
|
13
13
|
1. Run commands from the repository root.
|
|
14
|
-
2.
|
|
14
|
+
2. Prefer the saved credential. To initialize or rotate it, set `VIATRIS_API_KEY` and run `node src/cli.js login`; never place the key in command arguments, output, or logs.
|
|
15
15
|
3. Use production by default. Set `VIATRIS_API_ENV=development` only when the user requests development or testing.
|
|
16
16
|
4. Run `node src/cli.js describe --compact` when capability discovery or exact machine-readable metadata is needed.
|
|
17
17
|
5. Read [references/cli-reference.md](references/cli-reference.md) before constructing a write payload or handling a nonzero exit code.
|
|
@@ -22,6 +22,7 @@ Use the repository-local CLI for every supported API operation. Do not call the
|
|
|
22
22
|
- Prefer `--input <file>` for existing payloads and stdin for generated payloads.
|
|
23
23
|
- Parse successful `stdout` as JSON.
|
|
24
24
|
- Parse failed `stderr` as JSON and branch on both exit code and `error.type`.
|
|
25
|
+
- Authentication defaults to `Authorization: Bearer <key>`. Read the hidden credential file first and use `VIATRIS_API_KEY` only when no saved credential exists.
|
|
25
26
|
- Use `products list` to resolve product master IDs and stable codes before product-specific workflows. `products create` requires only a Chinese name and also initializes the canonical knowledge-tree root.
|
|
26
27
|
- Use `products update` only for verified optional medical master data. Do not infer English names, generic names, indications, dosage forms, or remarks without evidence.
|
|
27
28
|
- Preserve `externalRef` when retrying question submission. It is the idempotency key.
|
|
@@ -29,6 +30,8 @@ Use the repository-local CLI for every supported API operation. Do not call the
|
|
|
29
30
|
- Apply every returned generation rule. Product-scoped rules identify applicable product codes through `products`; global rules return an empty `products` array.
|
|
30
31
|
- Prefer scoring rule V2 for generated questions and scoring results. Use stable point and deduction-rule IDs.
|
|
31
32
|
- Treat product master records as outside business knowledge-tree levels. Fixed dimensions are business L1; generated nodes are L2-L4. Product detail lookup also accepts a historical knowledge-tree root ID.
|
|
33
|
+
- Document writes automatically remove 小荷 source-brand labels, including 小荷医疗 and whitespace variants of 小荷 AI 医生, from title, URL, and summary. Never reintroduce these labels after CLI sanitization.
|
|
34
|
+
- Current document write APIs accept only `title` and `summary`; the CLI drops an input `url` before sending. Keep URL sanitization in the permanent sanitizer for backward and future compatibility.
|
|
32
35
|
- Treat knowledge-tree and recursive node deletion as destructive. Inspect the target first and perform them only when the user explicitly requests deletion.
|
|
33
36
|
- Do not automatically retry POST, PUT, PATCH, or DELETE commands. Inspect the returned error and determine whether the server accepted the request.
|
|
34
37
|
- Keep temporary payloads outside the repository unless they are requested deliverables.
|
|
@@ -2,12 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## Authentication And Environment
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Login once:
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
export VIATRIS_API_KEY="..."
|
|
9
|
+
node src/cli.js login
|
|
10
|
+
unset VIATRIS_API_KEY
|
|
9
11
|
```
|
|
10
12
|
|
|
13
|
+
Login writes `~/.viatris-exam/.credentials.json` with file mode `0600`; its containing directory uses mode `0700`. The command never prints the key. Run login again with a new environment value to rotate the saved key.
|
|
14
|
+
|
|
15
|
+
API key lookup order:
|
|
16
|
+
|
|
17
|
+
1. Hidden credential file.
|
|
18
|
+
2. `VIATRIS_API_KEY` environment variable when the file does not exist.
|
|
19
|
+
|
|
11
20
|
Environment selection:
|
|
12
21
|
|
|
13
22
|
| Value | Target |
|
|
@@ -17,17 +26,17 @@ Environment selection:
|
|
|
17
26
|
|
|
18
27
|
`VIATRIS_API_BASE_URL` overrides the selected environment. Use it only for an explicitly supplied custom deployment or local mock.
|
|
19
28
|
|
|
20
|
-
Default authentication
|
|
29
|
+
Default authentication:
|
|
21
30
|
|
|
22
31
|
```text
|
|
23
|
-
|
|
32
|
+
Authorization: Bearer <API key>
|
|
24
33
|
```
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
No header or prefix environment variables are required. Advanced overrides remain available when connecting to a different compatible service:
|
|
27
36
|
|
|
28
37
|
```bash
|
|
29
|
-
export VIATRIS_API_KEY_HEADER="
|
|
30
|
-
export VIATRIS_API_KEY_PREFIX="
|
|
38
|
+
export VIATRIS_API_KEY_HEADER="Custom-Header"
|
|
39
|
+
export VIATRIS_API_KEY_PREFIX=""
|
|
31
40
|
```
|
|
32
41
|
|
|
33
42
|
## Capability Discovery
|
|
@@ -126,7 +135,6 @@ Payload shape:
|
|
|
126
135
|
"documents": [
|
|
127
136
|
{
|
|
128
137
|
"title": "药品说明书",
|
|
129
|
-
"url": "https://example.com/source",
|
|
130
138
|
"summary": "与该知识点直接对应的证据摘录"
|
|
131
139
|
}
|
|
132
140
|
]
|
|
@@ -190,15 +198,27 @@ Manage documents:
|
|
|
190
198
|
```bash
|
|
191
199
|
node src/cli.js knowledge documents list "$NODE_ID"
|
|
192
200
|
node src/cli.js knowledge documents create "$NODE_ID" \
|
|
193
|
-
--json '{"title":"说明书","
|
|
201
|
+
--json '{"title":"说明书","summary":"摘录"}'
|
|
194
202
|
node src/cli.js knowledge documents upsert "$NODE_ID" \
|
|
195
203
|
--json '{"items":[{"title":"说明书","summary":"更新后的摘录"}]}'
|
|
196
204
|
node src/cli.js knowledge documents update "$DOCUMENT_ID" \
|
|
197
|
-
--json '{"
|
|
205
|
+
--json '{"summary":"更新后的摘要"}'
|
|
198
206
|
node src/cli.js knowledge documents delete "$DOCUMENT_ID"
|
|
199
207
|
```
|
|
200
208
|
|
|
201
|
-
Create reuses a document with the same title under the node. Batch upsert accepts 1-20 items and updates `
|
|
209
|
+
Create reuses a document with the same title under the node. Batch upsert accepts 1-20 items and updates `summary` for existing titles. A single-document update changes only supplied fields; send `summary: null` to clear the summary. Current write contracts accept only `title` and `summary`; if input contains `url`, the CLI drops it before sending. Read responses may still contain historical URL data. Inspect a document before an explicitly requested delete.
|
|
210
|
+
|
|
211
|
+
### Document Sanitization
|
|
212
|
+
|
|
213
|
+
Before every document write, the CLI removes source-brand labels from `title`, `url`, and `summary`. This applies to document create, batch upsert, single-document update, and documents nested in `products update-knowledge-tree`. URL filtering remains a permanent compatibility rule even though the current API drops URL from write payloads.
|
|
214
|
+
|
|
215
|
+
Removed forms include:
|
|
216
|
+
|
|
217
|
+
- `小荷`
|
|
218
|
+
- `小荷医疗`, including whitespace between `小荷` and `医疗`
|
|
219
|
+
- `小荷AI医生` with arbitrary whitespace, including `小荷 AI 医生`, `小荷 AI医生`, and `小荷AI 医生`
|
|
220
|
+
|
|
221
|
+
Whitespace is normalized after removal, and leading or trailing title separators are removed. If a supplied title becomes empty, the CLI returns `validation_error` without sending the request.
|
|
202
222
|
|
|
203
223
|
List generation tasks:
|
|
204
224
|
|
|
Binary file
|
package/src/app.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
assertNoExtraPositionals,
|
|
3
|
+
assertOptions,
|
|
4
|
+
parseArgs,
|
|
5
|
+
} from "./args.js";
|
|
2
6
|
import { resolveCommand } from "./commands.js";
|
|
3
7
|
import { configFrom } from "./config.js";
|
|
8
|
+
import { loginWithEnvironmentKey } from "./credentials.js";
|
|
4
9
|
import { helpText } from "./help.js";
|
|
5
10
|
import { request } from "./http.js";
|
|
6
11
|
import { describeCli, VERSION } from "./metadata.js";
|
|
@@ -28,9 +33,16 @@ export async function run(
|
|
|
28
33
|
writeJson(stdout, describeCli(), options.compact);
|
|
29
34
|
return;
|
|
30
35
|
}
|
|
36
|
+
if (positionals[0] === "login") {
|
|
37
|
+
assertNoExtraPositionals(positionals, 1);
|
|
38
|
+
assertOptions(options);
|
|
39
|
+
const result = await loginWithEnvironmentKey(environmentVariables);
|
|
40
|
+
writeJson(stdout, result, options.compact);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
31
43
|
|
|
32
44
|
const command = await resolveCommand(positionals, options);
|
|
33
|
-
const config = configFrom(options, environmentVariables);
|
|
45
|
+
const config = await configFrom(options, environmentVariables);
|
|
34
46
|
const response = await request(command, config);
|
|
35
47
|
writeJson(stdout, response, options.compact);
|
|
36
48
|
}
|
|
@@ -4,10 +4,23 @@ import {
|
|
|
4
4
|
requiredId,
|
|
5
5
|
} from "../args.js";
|
|
6
6
|
import { readJsonInput } from "../input.js";
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
sanitizeDocumentBatchPayload,
|
|
9
|
+
sanitizeDocumentPayload,
|
|
10
|
+
} from "../sanitizers/documents.js";
|
|
11
|
+
|
|
12
|
+
async function jsonCommand(
|
|
13
|
+
method,
|
|
14
|
+
path,
|
|
15
|
+
options,
|
|
16
|
+
transform = (body) => body,
|
|
17
|
+
) {
|
|
9
18
|
assertOptions(options, ["input", "json"]);
|
|
10
|
-
return
|
|
19
|
+
return {
|
|
20
|
+
method,
|
|
21
|
+
path,
|
|
22
|
+
body: transform(await readJsonInput(options)),
|
|
23
|
+
};
|
|
11
24
|
}
|
|
12
25
|
|
|
13
26
|
function nodePath(positionals, label) {
|
|
@@ -96,6 +109,7 @@ export async function resolveKnowledge(positionals, options) {
|
|
|
96
109
|
"POST",
|
|
97
110
|
`/openapi/knowledge/nodes/${id}/documents`,
|
|
98
111
|
options,
|
|
112
|
+
sanitizeDocumentPayload,
|
|
99
113
|
);
|
|
100
114
|
}
|
|
101
115
|
|
|
@@ -106,6 +120,7 @@ export async function resolveKnowledge(positionals, options) {
|
|
|
106
120
|
"PUT",
|
|
107
121
|
`/openapi/knowledge/nodes/${id}/documents`,
|
|
108
122
|
options,
|
|
123
|
+
sanitizeDocumentBatchPayload,
|
|
109
124
|
);
|
|
110
125
|
}
|
|
111
126
|
|
|
@@ -116,6 +131,7 @@ export async function resolveKnowledge(positionals, options) {
|
|
|
116
131
|
"PUT",
|
|
117
132
|
`/openapi/knowledge/documents/${id}`,
|
|
118
133
|
options,
|
|
134
|
+
sanitizeDocumentPayload,
|
|
119
135
|
);
|
|
120
136
|
}
|
|
121
137
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
requiredId,
|
|
5
5
|
} from "../args.js";
|
|
6
6
|
import { readJsonInput } from "../input.js";
|
|
7
|
+
import { sanitizeKnowledgeTreePayload } from "../sanitizers/documents.js";
|
|
7
8
|
|
|
8
9
|
export async function resolveProducts(positionals, options) {
|
|
9
10
|
const [resource, action] = positionals;
|
|
@@ -50,7 +51,7 @@ export async function resolveProducts(positionals, options) {
|
|
|
50
51
|
return {
|
|
51
52
|
method: "PUT",
|
|
52
53
|
path: `/openapi/products/${id}/knowledge-tree`,
|
|
53
|
-
body: await readJsonInput(options),
|
|
54
|
+
body: sanitizeKnowledgeTreePayload(await readJsonInput(options)),
|
|
54
55
|
};
|
|
55
56
|
}
|
|
56
57
|
|
package/src/config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { integerOption } from "./args.js";
|
|
2
|
+
import { readStoredApiKey } from "./credentials.js";
|
|
2
3
|
import { CliError } from "./errors.js";
|
|
3
4
|
import {
|
|
4
5
|
API_ENVIRONMENT_ALIASES,
|
|
@@ -7,7 +8,10 @@ import {
|
|
|
7
8
|
DEFAULT_TIMEOUT_MS,
|
|
8
9
|
} from "./metadata.js";
|
|
9
10
|
|
|
10
|
-
export function configFrom(
|
|
11
|
+
export async function configFrom(
|
|
12
|
+
options,
|
|
13
|
+
environmentVariables = process.env,
|
|
14
|
+
) {
|
|
11
15
|
const baseUrlOverride =
|
|
12
16
|
options.base_url ?? environmentVariables.VIATRIS_API_BASE_URL;
|
|
13
17
|
let environment = null;
|
|
@@ -28,13 +32,16 @@ export function configFrom(options, environmentVariables = process.env) {
|
|
|
28
32
|
baseUrl = API_ENVIRONMENTS[environment];
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
const
|
|
35
|
+
const storedApiKey = await readStoredApiKey(environmentVariables);
|
|
36
|
+
const apiKey = storedApiKey ?? environmentVariables.VIATRIS_API_KEY;
|
|
32
37
|
const keyHeader =
|
|
33
38
|
options.key_header ??
|
|
34
39
|
environmentVariables.VIATRIS_API_KEY_HEADER ??
|
|
35
|
-
"
|
|
40
|
+
"Authorization";
|
|
36
41
|
const keyPrefix =
|
|
37
|
-
options.key_prefix ??
|
|
42
|
+
options.key_prefix ??
|
|
43
|
+
environmentVariables.VIATRIS_API_KEY_PREFIX ??
|
|
44
|
+
"Bearer ";
|
|
38
45
|
const timeoutMs = integerOption(
|
|
39
46
|
options.timeout ?? String(DEFAULT_TIMEOUT_MS),
|
|
40
47
|
"timeout",
|
|
@@ -63,9 +70,11 @@ export function configFrom(options, environmentVariables = process.env) {
|
|
|
63
70
|
);
|
|
64
71
|
}
|
|
65
72
|
if (!apiKey) {
|
|
66
|
-
throw new CliError(
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
throw new CliError(
|
|
74
|
+
"config_error",
|
|
75
|
+
"API key not found. Run `viatris-exam login` with VIATRIS_API_KEY set, or set VIATRIS_API_KEY for this request.",
|
|
76
|
+
{ exitCode: 3 },
|
|
77
|
+
);
|
|
69
78
|
}
|
|
70
79
|
if (!/^[A-Za-z0-9-]+$/.test(keyHeader)) {
|
|
71
80
|
throw new CliError("config_error", "API key header name is invalid.", {
|
|
@@ -77,6 +86,7 @@ export function configFrom(options, environmentVariables = process.env) {
|
|
|
77
86
|
baseUrl: baseUrl.replace(/\/+$/, ""),
|
|
78
87
|
environment,
|
|
79
88
|
apiKey,
|
|
89
|
+
apiKeySource: storedApiKey === null ? "environment" : "credential_file",
|
|
80
90
|
keyHeader,
|
|
81
91
|
keyPrefix,
|
|
82
92
|
timeoutMs,
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmod,
|
|
3
|
+
mkdir,
|
|
4
|
+
readFile,
|
|
5
|
+
rename,
|
|
6
|
+
rm,
|
|
7
|
+
writeFile,
|
|
8
|
+
} from "node:fs/promises";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { CliError } from "./errors.js";
|
|
12
|
+
|
|
13
|
+
export const CREDENTIAL_FILE_DISPLAY =
|
|
14
|
+
"~/.viatris-exam/.credentials.json";
|
|
15
|
+
|
|
16
|
+
export function credentialFilePath(environmentVariables = process.env) {
|
|
17
|
+
const home = environmentVariables.HOME || homedir();
|
|
18
|
+
return join(home, ".viatris-exam", ".credentials.json");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function readStoredApiKey(
|
|
22
|
+
environmentVariables = process.env,
|
|
23
|
+
) {
|
|
24
|
+
const path = credentialFilePath(environmentVariables);
|
|
25
|
+
let content;
|
|
26
|
+
try {
|
|
27
|
+
content = await readFile(path, "utf8");
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error.code === "ENOENT") return null;
|
|
30
|
+
throw new CliError(
|
|
31
|
+
"config_error",
|
|
32
|
+
`Unable to read the credential file: ${error.message}`,
|
|
33
|
+
{ exitCode: 3, details: { credentialFile: path } },
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const credentials = JSON.parse(content);
|
|
39
|
+
if (
|
|
40
|
+
credentials === null ||
|
|
41
|
+
typeof credentials !== "object" ||
|
|
42
|
+
typeof credentials.apiKey !== "string" ||
|
|
43
|
+
!credentials.apiKey
|
|
44
|
+
) {
|
|
45
|
+
throw new Error("apiKey is missing or invalid");
|
|
46
|
+
}
|
|
47
|
+
return credentials.apiKey;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw new CliError(
|
|
50
|
+
"config_error",
|
|
51
|
+
`Invalid credential file: ${error.message}`,
|
|
52
|
+
{ exitCode: 3, details: { credentialFile: path } },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function loginWithEnvironmentKey(
|
|
58
|
+
environmentVariables = process.env,
|
|
59
|
+
) {
|
|
60
|
+
const apiKey = environmentVariables.VIATRIS_API_KEY;
|
|
61
|
+
if (!apiKey) {
|
|
62
|
+
throw new CliError(
|
|
63
|
+
"config_error",
|
|
64
|
+
"VIATRIS_API_KEY is required for login.",
|
|
65
|
+
{ exitCode: 3 },
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const path = credentialFilePath(environmentVariables);
|
|
70
|
+
const directory = dirname(path);
|
|
71
|
+
const temporaryPath = join(
|
|
72
|
+
directory,
|
|
73
|
+
`.credentials.${process.pid}.${Date.now()}.tmp`,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
78
|
+
await chmod(directory, 0o700);
|
|
79
|
+
await writeFile(
|
|
80
|
+
temporaryPath,
|
|
81
|
+
`${JSON.stringify({ version: 1, apiKey }, null, 2)}\n`,
|
|
82
|
+
{ encoding: "utf8", mode: 0o600 },
|
|
83
|
+
);
|
|
84
|
+
await rename(temporaryPath, path);
|
|
85
|
+
await chmod(path, 0o600);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
await rm(temporaryPath, { force: true });
|
|
88
|
+
throw new CliError(
|
|
89
|
+
"config_error",
|
|
90
|
+
`Unable to save the credential file: ${error.message}`,
|
|
91
|
+
{ exitCode: 3, details: { credentialFile: path } },
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
status: "saved",
|
|
97
|
+
credentialFile: path,
|
|
98
|
+
permissions: "0600",
|
|
99
|
+
};
|
|
100
|
+
}
|
package/src/help.js
CHANGED
|
@@ -5,9 +5,11 @@ export function helpText() {
|
|
|
5
5
|
|
|
6
6
|
Usage:
|
|
7
7
|
viatris-exam <resource> <action> [id|query] [options]
|
|
8
|
+
viatris-exam login
|
|
8
9
|
viatris-exam describe
|
|
9
10
|
|
|
10
11
|
Commands:
|
|
12
|
+
login
|
|
11
13
|
products list
|
|
12
14
|
products create --input <file|->
|
|
13
15
|
products get <product-id>
|
|
@@ -48,8 +50,8 @@ Medical search:
|
|
|
48
50
|
|
|
49
51
|
Global options:
|
|
50
52
|
--base-url <url> Override the selected environment URL.
|
|
51
|
-
--key-header <name> Override
|
|
52
|
-
--key-prefix <value> Override
|
|
53
|
+
--key-header <name> Override the auth header (default: Authorization).
|
|
54
|
+
--key-prefix <value> Override the auth prefix (default: "Bearer ").
|
|
53
55
|
--timeout <ms> Request timeout (default: 30000).
|
|
54
56
|
--retries <n> GET retries for 429/5xx/network errors (default: 2).
|
|
55
57
|
--compact Emit compact JSON.
|
|
@@ -59,9 +61,12 @@ Global options:
|
|
|
59
61
|
Environment:
|
|
60
62
|
VIATRIS_API_ENV API environment: production (default) or development.
|
|
61
63
|
VIATRIS_API_BASE_URL Optional URL override with highest priority.
|
|
62
|
-
VIATRIS_API_KEY
|
|
64
|
+
VIATRIS_API_KEY Login input or fallback key when no saved key exists.
|
|
63
65
|
VIATRIS_API_KEY_HEADER Optional authentication header name.
|
|
64
|
-
VIATRIS_API_KEY_PREFIX Optional header value prefix
|
|
66
|
+
VIATRIS_API_KEY_PREFIX Optional authentication header value prefix.
|
|
67
|
+
|
|
68
|
+
Credentials:
|
|
69
|
+
~/.viatris-exam/.credentials.json (read before VIATRIS_API_KEY, mode 0600)
|
|
65
70
|
|
|
66
71
|
Agent contract:
|
|
67
72
|
Successful API JSON is written to stdout. Structured errors are written to
|
package/src/metadata.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { CREDENTIAL_FILE_DISPLAY } from "./credentials.js";
|
|
2
|
+
import { DOCUMENT_SANITIZATION_POLICY } from "./sanitizers/documents.js";
|
|
3
|
+
|
|
4
|
+
export const VERSION = "1.8.0";
|
|
2
5
|
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
3
6
|
export const DEFAULT_RETRIES = 2;
|
|
4
7
|
|
|
@@ -15,6 +18,12 @@ export const API_ENVIRONMENT_ALIASES = Object.freeze({
|
|
|
15
18
|
});
|
|
16
19
|
|
|
17
20
|
export const COMMANDS = Object.freeze([
|
|
21
|
+
{
|
|
22
|
+
command: "login",
|
|
23
|
+
local: true,
|
|
24
|
+
input: "VIATRIS_API_KEY environment variable",
|
|
25
|
+
summary: "Save the API key to the local hidden credential file.",
|
|
26
|
+
},
|
|
18
27
|
{
|
|
19
28
|
command: "products list",
|
|
20
29
|
method: "GET",
|
|
@@ -113,7 +122,7 @@ export const COMMANDS = Object.freeze([
|
|
|
113
122
|
command: "knowledge documents create <node-id>",
|
|
114
123
|
method: "POST",
|
|
115
124
|
path: "/openapi/knowledge/nodes/{id}/documents",
|
|
116
|
-
input: "{title,
|
|
125
|
+
input: "{title, summary?}",
|
|
117
126
|
summary: "Idempotently create a document under a node.",
|
|
118
127
|
},
|
|
119
128
|
{
|
|
@@ -127,7 +136,7 @@ export const COMMANDS = Object.freeze([
|
|
|
127
136
|
command: "knowledge documents update <document-id>",
|
|
128
137
|
method: "PUT",
|
|
129
138
|
path: "/openapi/knowledge/documents/{id}",
|
|
130
|
-
input: "{title?,
|
|
139
|
+
input: "{title?, summary?}",
|
|
131
140
|
summary: "Update supplied fields on a knowledge document.",
|
|
132
141
|
},
|
|
133
142
|
{
|
|
@@ -235,7 +244,10 @@ export function describeCli() {
|
|
|
235
244
|
authentication: {
|
|
236
245
|
keyEnvironmentVariable: "VIATRIS_API_KEY",
|
|
237
246
|
baseUrlEnvironmentVariable: "VIATRIS_API_BASE_URL",
|
|
238
|
-
defaultHeader: "
|
|
247
|
+
defaultHeader: "Authorization",
|
|
248
|
+
defaultPrefix: "Bearer ",
|
|
249
|
+
credentialFile: CREDENTIAL_FILE_DISPLAY,
|
|
250
|
+
keySourcePriority: ["credential_file", "environment"],
|
|
239
251
|
},
|
|
240
252
|
environments: {
|
|
241
253
|
environmentVariable: "VIATRIS_API_ENV",
|
|
@@ -244,6 +256,7 @@ export function describeCli() {
|
|
|
244
256
|
urls: API_ENVIRONMENTS,
|
|
245
257
|
baseUrlOverrideEnvironmentVariable: "VIATRIS_API_BASE_URL",
|
|
246
258
|
},
|
|
259
|
+
documentSanitization: DOCUMENT_SANITIZATION_POLICY,
|
|
247
260
|
commands: COMMANDS,
|
|
248
261
|
exitCodes: EXIT_CODES,
|
|
249
262
|
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
|
|
3
|
+
export const DOCUMENT_SANITIZATION_POLICY = Object.freeze({
|
|
4
|
+
fields: ["title", "url", "summary"],
|
|
5
|
+
writableFields: ["title", "summary"],
|
|
6
|
+
droppedWriteFields: ["url"],
|
|
7
|
+
removedLabels: [
|
|
8
|
+
"小荷",
|
|
9
|
+
"小荷医疗",
|
|
10
|
+
"小荷AI医生(AI 与医生之间允许任意空格)",
|
|
11
|
+
],
|
|
12
|
+
appliesTo: [
|
|
13
|
+
"products update-knowledge-tree",
|
|
14
|
+
"knowledge documents create",
|
|
15
|
+
"knowledge documents upsert",
|
|
16
|
+
"knowledge documents update",
|
|
17
|
+
],
|
|
18
|
+
emptyTitle: "validation_error",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const PROHIBITED_LABEL = /小荷\s*(?:AI\s*医生|医疗)?/giu;
|
|
22
|
+
const TITLE_EDGE_SEPARATOR = /^[\s\-—–_::||·]+|[\s\-—–_::||·]+$/gu;
|
|
23
|
+
|
|
24
|
+
function sanitizeText(value) {
|
|
25
|
+
return value
|
|
26
|
+
.replace(PROHIBITED_LABEL, "")
|
|
27
|
+
.replace(/[ \t]{2,}/g, " ")
|
|
28
|
+
.trim();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function sanitizeTitle(value) {
|
|
32
|
+
return sanitizeText(value).replace(TITLE_EDGE_SEPARATOR, "").trim();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function sanitizeDocument(document, location = "document") {
|
|
36
|
+
if (
|
|
37
|
+
document === null ||
|
|
38
|
+
Array.isArray(document) ||
|
|
39
|
+
typeof document !== "object"
|
|
40
|
+
) {
|
|
41
|
+
return document;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const sanitized = { ...document };
|
|
45
|
+
if (typeof sanitized.title === "string") {
|
|
46
|
+
sanitized.title = sanitizeTitle(sanitized.title);
|
|
47
|
+
if (!sanitized.title) {
|
|
48
|
+
throw new CliError(
|
|
49
|
+
"validation_error",
|
|
50
|
+
`${location}.title is empty after prohibited source labels are removed.`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const field of ["url", "summary"]) {
|
|
55
|
+
if (typeof sanitized[field] === "string") {
|
|
56
|
+
sanitized[field] = sanitizeText(sanitized[field]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return sanitized;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function prepareDocumentForWrite(document, location) {
|
|
63
|
+
const sanitized = sanitizeDocument(document, location);
|
|
64
|
+
if (
|
|
65
|
+
sanitized === null ||
|
|
66
|
+
Array.isArray(sanitized) ||
|
|
67
|
+
typeof sanitized !== "object"
|
|
68
|
+
) {
|
|
69
|
+
return sanitized;
|
|
70
|
+
}
|
|
71
|
+
const { url: _unsupportedUrl, ...writableDocument } = sanitized;
|
|
72
|
+
return writableDocument;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function sanitizeDocumentPayload(body) {
|
|
76
|
+
return prepareDocumentForWrite(body, "document");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function sanitizeDocumentBatchPayload(body) {
|
|
80
|
+
if (!Array.isArray(body.items)) return body;
|
|
81
|
+
return {
|
|
82
|
+
...body,
|
|
83
|
+
items: body.items.map((document, index) =>
|
|
84
|
+
prepareDocumentForWrite(document, `items[${index}]`),
|
|
85
|
+
),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function sanitizeKnowledgeTreePayload(body) {
|
|
90
|
+
if (!Array.isArray(body.nodes)) return body;
|
|
91
|
+
return {
|
|
92
|
+
...body,
|
|
93
|
+
nodes: body.nodes.map((node, nodeIndex) => {
|
|
94
|
+
if (
|
|
95
|
+
node === null ||
|
|
96
|
+
Array.isArray(node) ||
|
|
97
|
+
typeof node !== "object" ||
|
|
98
|
+
!Array.isArray(node.documents)
|
|
99
|
+
) {
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
...node,
|
|
104
|
+
documents: node.documents.map((document, documentIndex) =>
|
|
105
|
+
prepareDocumentForWrite(
|
|
106
|
+
document,
|
|
107
|
+
`nodes[${nodeIndex}].documents[${documentIndex}]`,
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
};
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|