@dazitech/cli 3.0.9 → 3.1.1

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 (67) hide show
  1. package/README.md +1 -1
  2. package/dist/clis/dazi-app.js +2 -2
  3. package/dist/clis/dazi-flow.js +2 -2
  4. package/dist/clis/dazi-onto.js +324 -41
  5. package/dist/clis/dazi.js +407 -185
  6. package/dist/docs/flow/flow-project-guide.md +1 -1
  7. package/dist/docs/guides/cli-reference.md +16 -3
  8. package/dist/docs/guides/troubleshooting.md +3 -3
  9. package/dist/docs/index.json +3 -15
  10. package/dist/docs/onto/dazi_script_sdk_reference.md +246 -244
  11. package/dist/docs/onto/dazi_script_seed_data_guide.md +1 -1
  12. package/dist/docs/onto/function-guide.md +123 -123
  13. package/dist/docs/onto//346/234/254/344/275/223/345/210/206/347/261/273/350/247/204/345/210/222/344/270/216SDK/346/211/251/345/261/225/346/226/271/346/241/210.md +169 -168
  14. package/dist/docs/onto//346/234/254/344/275/223/345/221/275/345/220/215/350/247/204/350/214/203_/347/211/251/347/220/206/350/241/250Cube/344/270/216/345/257/271/350/261/241.md +403 -402
  15. package/dist/docs/onto//346/234/254/344/275/223/345/274/200/345/217/221/344/274/230/345/214/226/346/200/273/347/273/223.md +257 -0
  16. package/dist/docs/onto//346/234/254/344/275/223/350/204/232/346/234/254/347/274/226/345/206/231/346/214/207/345/215/227.md +339 -311
  17. package/dist/docs/onto//346/234/254/344/275/223/350/247/204/345/210/222/346/214/207/345/215/227.md +305 -281
  18. package/dist/docs/onto//350/204/232/346/234/254/350/277/220/350/241/214/345/270/270/350/247/201/351/224/231/350/257/257/345/244/204/347/220/206.md +297 -0
  19. package/dist/examples/index.json +222 -6
  20. package/dist/examples/onto/README.md +34 -36
  21. package/dist/examples/onto/_templates/onto_preflight.ps1 +84 -0
  22. package/dist/examples/onto/index.json +53 -0
  23. package/dist/examples/onto/index.yaml +29 -0
  24. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/README.md +23 -0
  25. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/save_test_arguments.ps1 +12 -11
  26. package/dist/{docs/onto → examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/plans}//350/247/204/345/210/222/347/244/272/344/276/213_/345/210/251/346/266/246/345/210/206/346/236/220/346/234/254/344/275/223/346/226/271/346/241/210.md +37 -35
  27. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_category_mount.py +85 -0
  28. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_ontology_init.py +3 -66
  29. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/README.md +24 -0
  30. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_availability_analysis.py +84 -0
  31. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_downtime_breakdown.py +119 -0
  32. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_energy_intensity.py +98 -0
  33. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_get_summary.py +125 -0
  34. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_maintenance_compliance.py +77 -0
  35. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_mom_analysis.py +118 -0
  36. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_oee_analysis.py +126 -0
  37. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_plan_vs_actual.py +105 -0
  38. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_top_fault_equipment.py +104 -0
  39. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_unit_comparison.py +120 -0
  40. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/equip_ops_fn_yoy_analysis.py +115 -0
  41. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/save_test_arguments.ps1 +42 -0
  42. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.availability_analysis.json +7 -0
  43. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.downtime_breakdown.json +8 -0
  44. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.energy_intensity.json +8 -0
  45. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.get_summary.json +7 -0
  46. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.maintenance_compliance.json +7 -0
  47. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.mom_analysis.json +8 -0
  48. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.oee_analysis.json +8 -0
  49. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.plan_vs_actual.json +8 -0
  50. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.top_fault_equipment.json +8 -0
  51. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.unit_comparison.json +8 -0
  52. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/functions/test_arguments/equip_ops.fn.yoy_analysis.json +8 -0
  53. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/plans//345/214/226/345/267/245/350/256/276/345/244/207/350/277/220/350/220/245/345/210/206/346/236/220/346/234/254/344/275/223/346/226/271/346/241/210.md +735 -0
  54. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/setup/equip_ops_category_mount.py +106 -0
  55. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/setup/equip_ops_ontology_init.py +1077 -0
  56. package/dist/examples/onto//350/256/276/345/244/207/350/277/220/350/220/245/setup/equip_ops_seed_data.py +552 -0
  57. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/README.md +23 -0
  58. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/save_test_arguments.ps1 +13 -12
  59. package/dist/{docs/onto → examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/plans}//350/247/204/345/210/222/347/244/272/344/276/213_/344/272/247/345/223/201/351/224/200/345/224/256/346/234/254/344/275/223/350/247/204/345/210/222/346/226/271/346/241/210.md +34 -34
  60. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_category_mount.py +82 -0
  61. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_ontology_init.py +3 -54
  62. package/dist/prompts/index.json +8 -1
  63. package/dist/prompts/onto/function-design.md +73 -73
  64. package/dist/prompts/onto/planning-design.md +226 -0
  65. package/dist/prompts/onto/script-publish-run.md +231 -208
  66. package/package.json +1 -1
  67. package/dist/docs/onto//350/204/232/346/234/254/350/277/220/350/241/214/347/272/240/351/224/231_/345/225/206/345/212/241/346/210/220/346/234/254/346/226/271/346/241/210/345/274/200/345/217/221/350/277/207/347/250/213.md +0 -213
@@ -1,7 +1,7 @@
1
1
  # 数据流程项目开发指南
2
2
 
3
3
  **文档 ID**: `flow/flow-project-guide`
4
- **适用**: `dazi-vscode` v3.0.9+、`dazi-work` 工作区、业务项目 `项目/<业务名>/`
4
+ **适用**: `dazi-vscode` v3.1.1+、`dazi-work` 工作区、业务项目 `项目/<业务名>/`
5
5
 
6
6
  ---
7
7
 
@@ -22,15 +22,28 @@
22
22
  | `dazi prompt list` | 列出提示词 |
23
23
  | `dazi prompt show <id>` | 显示提示词 |
24
24
  | `dazi prompt sync` | 同步内置提示词到 `资源/prompts/` |
25
- | `dazi examples list` | 列出示例脚本 |
26
- | `dazi examples open <id>` | 显示示例内容 |
27
- | `dazi examples sync` | 同步内置示例到 `资源/examples/` |
25
+ | `dazi examples list` | 列出示例脚本(`--category onto` 过滤本体类) |
26
+ | `dazi examples show <id>` | 显示示例内容(如 `onto/sales/plan`) |
27
+ | `dazi examples sync` | 同步内置示例到 `资源/examples/`(含完整 `onto/` 树) |
28
+ | `dazi examples onto list` | 列出本体完整示例注册表(读 `onto/index.json`) |
29
+ | `dazi examples onto suggest [关键词]` | 按业务域推荐对照示例(规划前选用) |
30
+ | `dazi examples onto show <id>` | 显示示例路径;`--plan` / `--readme` 输出全文 |
28
31
  | `dazi migrate workspace` | 迁移工作区 |
29
32
  | `dazi migrate config` | 迁移旧配置 |
30
33
  | `dazi onto <args>` | 本体 CLI(**勿用** `dazi-onto`) |
31
34
  | `dazi flow <args>` | 流程 CLI |
32
35
  | `dazi app <args>` | 应用 CLI(亦可在应用项目根用 `pnpm run dazi-app`) |
33
36
 
37
+ **本体规划阶段常用**:
38
+
39
+ ```powershell
40
+ dazi examples sync
41
+ dazi examples onto suggest 设备 OEE
42
+ dazi examples onto show equip-ops --plan
43
+ ```
44
+
45
+ 索引真源:`examples/onto/index.yaml`(人工维护)→ `index.json`(`copy:bundled` 时生成)。详见 [本体规划指南](../onto/本体规划指南.md)。
46
+
34
47
  ## 本体(`dazi onto ...`)
35
48
 
36
49
  | 命令 | 说明 |
@@ -84,7 +84,7 @@ dazi doctor --workspace-root D:\path\to\dazi-work
84
84
  检查 `.cursor/mcp.json` 配置。MCP 需能调用 bundled CLI,示例:
85
85
 
86
86
  ```powershell
87
- $env:DAZI_BUNDLED_DIR = "$env:USERPROFILE\.cursor\extensions\dazitech.dazi-vscode-3.0.9\bundled\clis"
87
+ $env:DAZI_BUNDLED_DIR = "$env:USERPROFILE\.cursor\extensions\dazitech.dazi-vscode-3.1.1\bundled\clis"
88
88
  node "$env:DAZI_BUNDLED_DIR\dazi.js" mcp stdio
89
89
  ```
90
90
 
@@ -104,9 +104,9 @@ pnpm run dazi-app -- --version
104
104
 
105
105
  | 现象 | 原因 | 处理 |
106
106
  | ---- | ---- | ---- |
107
- | init 报 `sync_metrics` / `cubes` 不存在 | API 误用 | 见 [脚本运行纠错](../onto/脚本运行纠错_商务成本方案开发过程.md) §1 |
107
+ | init 报 `sync_metrics` / `cubes` 不存在 | API 误用 | 见 [脚本运行常见错误处理](../onto/脚本运行常见错误处理.md) §1 |
108
108
  | 函数聚合 SQL 报 `float` 无 `.get()` | `query_one` 用于多列聚合 | 改用 `query()` 取 `rows[0]`,见纠错文档 §2 |
109
109
  | `function run --arguments-json-file` unknown | 该子命令不支持此参数 | 先 `save-test-arguments`,再 `function run <function_id>` |
110
110
  | `save-test-arguments` 404 | 传了 `function_id` 而非 `ofn_xxx` | `dazi onto function list` 取内部 `id` |
111
111
 
112
- 完整 CLI 清单与商务成本方案实录:`dazi docs show onto/script-run-troubleshooting`(`dazi docs sync` 后位于 `资源/docs/onto/`)。
112
+ 完整常见错误与 CLI 说明:`dazi docs show onto/script-run-troubleshooting`(`dazi docs sync` 后位于 `资源/docs/onto/`)。
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.0.9",
2
+ "version": "3.1.1",
3
3
  "updatedAt": "2026-06-06T12:00:00.000Z",
4
4
  "docs": [
5
5
  {
@@ -98,18 +98,6 @@
98
98
  "category": "onto",
99
99
  "file": "onto/本体分类规划与SDK扩展方案.md"
100
100
  },
101
- {
102
- "id": "onto/planning-example-profit",
103
- "title": "规划示例:利润分析本体(GL 域)",
104
- "category": "onto",
105
- "file": "onto/规划示例_利润分析本体方案.md"
106
- },
107
- {
108
- "id": "onto/planning-example-sales",
109
- "title": "规划示例:产品销售本体",
110
- "category": "onto",
111
- "file": "onto/规划示例_产品销售本体规划方案.md"
112
- },
113
101
  {
114
102
  "id": "onto/script-authoring",
115
103
  "title": "本体脚本编写指南(含发布)",
@@ -130,9 +118,9 @@
130
118
  },
131
119
  {
132
120
  "id": "onto/script-run-troubleshooting",
133
- "title": "【必读】脚本运行纠错(商务成本实录)",
121
+ "title": "【必读】脚本运行常见错误处理",
134
122
  "category": "onto",
135
- "file": "onto/脚本运行纠错_商务成本方案开发过程.md",
123
+ "file": "onto/脚本运行常见错误处理.md",
136
124
  "featured": true
137
125
  },
138
126
  {
@@ -1,244 +1,246 @@
1
- # DaziScript SDK 参考
2
-
3
- **文档 ID**: `onto/dazi-script-sdk-reference`
4
- **适用**: dazi-vscode v3 + 搭子平台 DaziScript(ClickHouse 数据空间)
5
-
6
- > 给 LLM 与开发者提供精简、可执行的 SDK 规范。脚本目录、类型与 **`dazi onto script publish`** 等见 **[本体脚本编写指南](./本体脚本编写指南.md)**。
7
- > **执行脚本前必读(强制)**:[脚本运行纠错](./脚本运行纠错_商务成本方案开发过程.md)。
8
-
9
- ## 1. 工作区与脚本放置(dazi-vscode)
10
-
11
- | 用途 | 路径 |
12
- | -------------------- | ----------------------------------------------------------------------------------------------------------- |
13
- | **日常开发**(推荐) | `<工作区根>/项目/<业务名>/本体/ontos/<实现名>/setup/*.py`(初始化、灌数) |
14
- | | `<工作区根>/项目/<业务名>/本体/ontos/<实现名>/functions/*.py`(本体函数、动作) |
15
- | **空间 ID** | `项目/<业务名>/本体/ontos/<实现名>/README.md` 中的数据空间 ID |
16
- | **参考示例** | **`资源/examples/onto/利润示例/`**、**`资源/examples/onto/销售示例/`**(侧栏 **帮助 → 示例** 或 `dazi examples sync`;总览 `onto/README.md`) |
17
- | **本文档** | `资源/docs/onto/dazi_script_sdk_reference.md`(`dazi docs sync` 后) |
18
-
19
- - **禁止**将 `onto/<space_id>/editorial/` 作为 v3 本地开发约定(历史路径,仅 CLI `script sync` 可能拉回平台副本)。
20
- - 脚本入口仅需定义 **`main()`**;平台执行时自动调用,**不要**写 `if __name__ == "__main__":`。
21
-
22
- ## 2. 基本约束
23
-
24
- - 数据引擎:**ClickHouse**
25
- - 新建空间须显式指定(若脚本内创建空间):
26
- - `storage_engine="clickhouse"`
27
- - `connection_config={"database": "<db_name>"}`
28
- - 执行前确认已登录(`dazi auth whoami`)且 `dazi.serverUrl` 正确。
29
-
30
- ## 3. 顶层对象与推荐调用
31
-
32
- 常用对象:
33
-
34
- - `space`:空间管理与切换
35
- - `s.sql`:原始 SQL
36
- - `s.tables`:表注册与列同步
37
- - `s.register_cube(...)`:Cube 注册
38
- - `s.onto`:本体定义(对象、属性、链接、函数、活动)
39
- - `s.ontology`:对象中心能力(objects/features)
40
- - `s.ontology_rules`:规则集与规则
41
- - `s.scripts`:脚本记录管理
42
- - `output`:打印与成功提示(`output.print` / `output.success`)
43
-
44
- 命名规范(便于 LLM 推断):
45
-
46
- - 读操作:`exists_*`、`get_*`、`require_*`、`list_*`
47
- - 写操作:`create_*`、`ensure_*`、`update_*`、`delete_*`
48
- - 派生/同步:`create_from_*`、`sync_*`
49
-
50
- 推荐本体落库路径(与 **[本体规划指南](./本体规划指南.md)** 一致):
51
-
52
- 1. `s.onto.define_object_type` → `s.onto.bind_source(..., "dazi_cube", config={"cube": ...})`
53
- 2. `s.onto.define_property`(`dimension` / `measure` 须带与 Cube 成员一致的 `qualified_name`)
54
- 3. `s.onto.define_link_type`(两端为对象类型 **`code`**,非 Cube 名)
55
- 4. `s.onto.register_function` / `s.onto.define_action`
56
- 5. `s.ontology.features.attach(object_code, feature_type, feature_id)`(`feature_type`: `function | action | rule`)
57
-
58
- **关于 `s.ontology.objects.create_from_cubes`**:SDK 仍提供,**易与业务语义脱节**;规划文档、示例包与交付脚本**不应依赖**。仅隔离烟测按需使用。
59
-
60
- ## 4. 返回结构规范
61
-
62
- ### 4.1 批量派生返回(ResultBatch)
63
-
64
- ```json
65
- {
66
- "ok": true,
67
- "created": [],
68
- "updated": [],
69
- "skipped": [],
70
- "errors": [],
71
- "summary": {
72
- "requested": 0,
73
- "created": 0,
74
- "updated": 0,
75
- "skipped": 0,
76
- "errors": 0
77
- }
78
- }
79
- ```
80
-
81
- ### 4.2 特征绑定返回(示例)
82
-
83
- ```json
84
- {
85
- "ok": true,
86
- "id": "xxx",
87
- "created": true,
88
- "updated": false,
89
- "message": ""
90
- }
91
- ```
92
-
93
- ## 5. 模块速查
94
-
95
- ### 5.1 `space`
96
-
97
- - `s = space.create(name, space_id=None, storage_engine="clickhouse", connection_config={"database": ...}, ...)`
98
- - `s = space.get(name_or_id)`
99
- - `space.use(handle_or_name)`
100
-
101
- ### 5.2 `s.sql`
102
-
103
- - `s.sql.query(sql)` / `s.sql.query_one(sql)` / `s.sql.execute(sql)`
104
- - `s.sql.insert_rows(table, rows)`(`rows` 为 `list[dict]`)
105
-
106
- **`query_one` 与多列聚合**
107
-
108
- - `query_one` 适合 `SELECT count()` 等**单列标量**;对 `SELECT sum(a), count(b) ...` 等多列聚合,**勿**对返回值调用 `.get()`(可能得到 float 而非 dict)。
109
- - **推荐**:`rows = s.sql.query(sql); row = rows[0] if rows else {}`(函数脚本内为 `p.sql.query`)。详见 [脚本运行纠错](./脚本运行纠错_商务成本方案开发过程.md#2-函数聚合查询返回类型错误)。
110
-
111
- ### 5.2.1 本体函数输出(`ontology_function`)
112
-
113
- 与 setup/seed 不同,本体函数运行时平台注入 `ctx`、`space`、`onto`:
114
-
115
- | 要点 | 说明 |
116
- | ---- | ---- |
117
- | 入口 | `def main():`(**无** `params` 形参) |
118
- | 入参 | `ctx.params` → `p.get_params()` |
119
- | 出参 | **`return p.function_result(columns=..., data=..., row_count=...)`** |
120
- | 禁止 | `output.print_json()`(`OutputModule` 无此方法)、裸 `return {"k": v}` |
121
-
122
- 标准模板:`资源/examples/onto/_templates/ontology_function_template.py`。详见 [function-guide](./function-guide.md#函数脚本结构标准模板)、[脚本运行纠错 §3](./脚本运行纠错_商务成本方案开发过程.md#3-函数输出方式错误outputprint_json)。
123
-
124
- **ClickHouse:`INSERT ... VALUES` 与注释**
125
-
126
- - **`VALUES` 与各元组之间禁止 SQL 行注释 `--`**,否则易出现 `Code: 27` 等解析错误。
127
- - **推荐**:大批量灌数用 **`s.sql.insert_rows`**;详见 **[dazi_script_seed_data_guide](./dazi_script_seed_data_guide.md)**。
128
-
129
- ### 5.3 `s.tables`
130
-
131
- - `s.tables.register(table_name, display_name=..., description=..., label=..., category_347=...)` — `label` 为 `display_name` 简写别名;已注册时可刷新表显示名与说明;`category_347` 为 347 标准中文名(如 `"维度表"`),注册后**即时挂载**平台分类
132
- - `s.tables.sync_columns(table_name)` — 从物理库同步列;未显式传入元数据时仅**推断** `display_name`(token 词典),**不**写入业务 `description`
133
- - `s.tables.set_column_meta(table_name, columns=[{name, display_name, description, business_role}], force=False)` — 批量写入列显示名与说明(须先 register + sync_columns)
134
- - `s.tables.register_with_meta(table_name, display_name=..., description=..., columns=[...], force_column_meta=False, category_347=...)` — 上三者合一(**推荐** setup 脚本使用)
135
- - `s.tables.list()` / `s.tables.discover()`
136
- - `s.tables.add_relationship(from_table, to_table, join_sql, relationship_type="many_to_one", join_keys=None, description=None, category_347=...)` — 注册**数据空间表间关系**(幂等);`category_347` 如 `"主数据关联"`、`"时间关联"`
137
-
138
- ### 5.4 Cube
139
-
140
- - `s.register_cube(name, table, title, measures, dimensions, category_347=...)` — `category_347` 如 `"流程型"`、`"主体型"`
141
-
142
- ### 5.5 `s.onto`
143
-
144
- - `s.onto.define_object_type(code, name, ..., category_347=...)` — 如 `category_347="主数据"`
145
- - `s.onto.bind_source(object_type_code, "dazi_cube", config={"cube": "CubeName"})`
146
- - `s.onto.define_property(...)`
147
- - `s.onto.define_link_type(code, name, from_object_type_code, to_object_type_code, ..., category_347=...)` — 如 `category_347="归属关系"`
148
- - `s.onto.register_function(function_id, adapter, ..., category_347=...)` — 如 `category_347="总览分析"`
149
- - `s.onto.define_action(action_code, ...)`
150
-
151
- **常见误用**(setup 脚本):不存在 `s.onto.sync_metrics()`、`s.cubes.upsert`;`define_object_type` 第二参数为 `name` 非 `label`;链接类型参数为 `from_object_type_code` / `to_object_type_code`。对照表见 [脚本运行纠错](./脚本运行纠错_商务成本方案开发过程.md#1-setup-脚本-api-误用多次尝试才正确)。
152
-
153
- ### 5.6 `s.categories`(347 对齐分类)
154
-
155
- 挂载到 `ads_categories` 默认根下的平级目录,桥表关联表/Cube/对象/关系/链接/函数。分类名必须与 [347 规范](./本体命名规范_物理表Cube与对象.md) 标准中文名一致。详见 [349 方案](./本体分类规划与SDK扩展方案.md)。
156
-
157
- - `s.categories` `CategoryManager` 实例(按空间懒加载)
158
- - `s.categories.ensure_347(kind, category)` — 确保平级分类目录存在(幂等);`kind` 支持 `table` / `cube` / `object` / `relation` / `link` / `function`
159
- - `s.categories.assign_table(category, table_name)` — 挂载物理表
160
- - `s.categories.assign_cube(category, cube_name)` — 挂载 Cube
161
- - `s.categories.assign_object(category, object_type_code)` — 挂载对象类型
162
- - `s.categories.assign_relation(category, from_table, to_table)` — 挂载表间关系
163
- - `s.categories.assign_link(category, link_code)` — 挂载链接类型
164
- - `s.categories.assign_function(category, function_id)` — 挂载本体函数(如 `profit.fn.get_summary`)
165
- - `s.categories.auto_assign_tables(table_names)` — 按表名前缀自动推断 347 类别并挂载
166
- - `s.categories.apply_registry(CATEGORY_REGISTRY, skip_missing=True)` — 批量应用 init 顶部注册表
167
-
168
- **`CATEGORY_REGISTRY` 结构**(与规划附录 B 对齐):
169
-
170
- ```python
171
- CATEGORY_REGISTRY = {
172
- "table": {"维度表": ["dim_account"], "事实表": ["fact_gl_journal_entry"]},
173
- "cube": {"流程型": ["ActualCube"], "主体型": ["AccountActualCube"]},
174
- "object": {"主数据": ["Account"], "事务": ["JournalEntry"], "分析": ["ProfitAnalysis"]},
175
- "relation": {"时间关联": [("fact_x", "dim_date")], "主数据关联": [("fact_x", "dim_y")]},
176
- "link": {"归属关系": ["entry_belongs_account"], "分析归因": ["analysis_by_account"]},
177
- "function": {"总览分析": ["profit.fn.get_summary"]}, # 函数未注册时须 skip_missing=True
178
- }
179
- ```
180
-
181
- **两种用法**(可并存,挂载幂等):
182
-
183
- 1. **批量**(推荐 init 末尾):`s.categories.apply_registry(CATEGORY_REGISTRY, skip_missing=True)`
184
- 2. **内联**(注册时即时挂载):
185
-
186
- ```python
187
- s.tables.register_with_meta("dim_product", display_name="产品维表", category_347="维度表", ...)
188
- s.register_cube("SalesCube", table=fact, title="...", category_347="流程型", ...)
189
- s.onto.define_object_type("Product", "产品", category_347="主数据")
190
- s.tables.add_relationship(..., category_347="主数据关联")
191
- s.onto.define_link_type("order_contains_product", "...", ..., category_347="归属关系")
192
- ```
193
-
194
- ### 5.7 `s.ontology` / `s.ontology_rules` / `s.scripts`
195
-
196
- 见上文;规则:`ensure_rule_set` + `upsert_rule`;脚本记录:`create` / `ensure` / `list` 等。
197
-
198
- ## 6. 标准初始化流程(建议)
199
-
200
- 1. 确认 `space_id`(实现单元 README)
201
- 2. 建表与灌数(`s.sql`;灌数规范见 seed 指南)
202
- 3. `s.tables.register_with_meta`(`TABLE_REGISTRY` 含表/列 `display_name`、`description`)
203
- 4. **`s.tables.add_relationship`**(与规划「表间关系」一致;**勿省略**)
204
- 5. `s.register_cube`
205
- 6. `s.onto` 定义对象、属性、链接
206
- 7. 注册函数/动作并 `features.attach`;**发布后** CLI `save-test-arguments` 写入 `test_arguments`(见 [function-guide](./function-guide.md))
207
- 8. `s.categories.apply_registry(CATEGORY_REGISTRY, skip_missing=True)`(347 对齐分类)
208
- 9. 配置规则(如需要)
209
-
210
- ## 7. 在 dazi-vscode 中发布与运行
211
-
212
- 开发完成后,在工作区根目录:
213
-
214
- ```bash
215
- # 预检
216
- dazi onto script publish-preview 项目/<业务名>/本体/ontos/<实现名>/setup/my_setup.py --space <space-id>
217
-
218
- # 发布(初始化/灌数类脚本)
219
- dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/setup/my_setup.py --space <space-id>
220
-
221
- # 发布并注册为本体函数
222
- dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/functions/my_func.py \
223
- --space <space-id> \
224
- --register-function-id my_func
225
-
226
- # 运行已入库函数
227
- dazi onto function run my_func --space <space-id> --params '{}'
228
- ```
229
-
230
- 亦可用侧栏 **Onto 本体** → 发布函数 / 运行函数。
231
-
232
- ## 8. 回归与 JSON 摘要
233
-
234
- - 支持 `--json` 时,输出前缀统一:`__JSON_SUMMARY__`
235
- - 非 0 退出码即失败
236
- - 会改写数据的步骤须提供跳过/禁用开关
237
-
238
- **内置参考示例**(在用户工作区:**侧栏 帮助 → 示例 → 下载所有示例**,或执行 `dazi examples sync`,得到 **`资源/examples/`**;可复制到 **`项目/<业务名>/本体/ontos/<实现名>/setup/`** 或 **`functions/`** 再改,勿直接改写同步下来的只读备份):
239
-
240
- - **规划全文(推荐)**:**`资源/docs/onto/规划示例_产品销售本体规划方案.md`**(含表间关系、test_arguments)
241
- - **完整落地参考**(init / seed / 7 个 sales 函数):**`项目/潘达石化/本体/ontos/产品销售本体方案/`**
242
- - **利润示例(GL 域)**:`资源/examples/onto/利润示例/setup/profit_ontology_init.py`、`profit_seed_data.py`、`functions/profit_fn_*.py`、`functions/test_arguments/`
243
- - **销售示例(推荐,含表间关系 + test_arguments)**:`资源/examples/onto/销售示例/setup/sales_ontology_init.py`、`sales_seed_data.py`、`functions/sales_fn_*.py`
244
- - **脚本运行纠错(实录)**:`资源/docs/onto/脚本运行纠错_商务成本方案开发过程.md` — setup API、`query_one`、CLI test_arguments 踩坑
1
+ # DaziScript SDK 参考
2
+
3
+ **文档 ID**: `onto/dazi-script-sdk-reference`
4
+ **适用**: dazi-vscode v3 + 搭子平台 DaziScript(ClickHouse 数据空间)
5
+
6
+ > 给 LLM 与开发者提供精简、可执行的 SDK 规范。脚本目录、类型与 **`dazi onto script publish`** 等见 **[本体脚本编写指南](./本体脚本编写指南.md)**。
7
+ > **执行脚本前必读(强制)**:[脚本运行常见错误处理](./脚本运行常见错误处理.md)。
8
+
9
+ ## 1. 工作区与脚本放置(dazi-vscode)
10
+
11
+ | 用途 | 路径 |
12
+ | -------------------- | ----------------------------------------------------------------------------------------------------------- |
13
+ | **日常开发**(推荐) | `<工作区根>/项目/<业务名>/本体/ontos/<实现名>/setup/*.py`(初始化、灌数) |
14
+ | | `<工作区根>/项目/<业务名>/本体/ontos/<实现名>/functions/*.py`(本体函数、动作) |
15
+ | **空间 ID** | `项目/<业务名>/本体/ontos/<实现名>/README.md` 中的数据空间 ID |
16
+ | **参考示例** | **`资源/examples/onto/利润示例/`**、**`资源/examples/onto/销售示例/`**(侧栏 **帮助 → 示例** 或 `dazi examples sync`;总览 `onto/README.md`) |
17
+ | **本文档** | `资源/docs/onto/dazi_script_sdk_reference.md`(`dazi docs sync` 后) |
18
+
19
+ - **禁止**将 `onto/<space_id>/editorial/` 作为 v3 本地开发约定(历史路径,仅 CLI `script sync` 可能拉回平台副本)。
20
+ - 脚本入口仅需定义 **`main()`**;平台执行时自动调用,**不要**写 `if __name__ == "__main__":`。
21
+
22
+ ## 2. 基本约束
23
+
24
+ - 数据引擎:**ClickHouse**
25
+ - 新建空间须显式指定(若脚本内创建空间):
26
+ - `storage_engine="clickhouse"`
27
+ - `connection_config={"database": "<db_name>"}`
28
+ - 执行前确认已登录(`dazi auth whoami`)且 `dazi.serverUrl` 正确。
29
+
30
+ ## 3. 顶层对象与推荐调用
31
+
32
+ 常用对象:
33
+
34
+ - `space`:空间管理与切换
35
+ - `s.sql`:原始 SQL
36
+ - `s.tables`:表注册与列同步
37
+ - `s.register_cube(...)`:Cube 注册
38
+ - `s.onto`:本体定义(对象、属性、链接、函数、活动)
39
+ - `s.ontology`:对象中心能力(objects/features)
40
+ - `s.ontology_rules`:规则集与规则
41
+ - `s.scripts`:脚本记录管理
42
+ - `output`:打印与成功提示(`output.print` / `output.success`)
43
+
44
+ 命名规范(便于 LLM 推断):
45
+
46
+ - 读操作:`exists_*`、`get_*`、`require_*`、`list_*`
47
+ - 写操作:`create_*`、`ensure_*`、`update_*`、`delete_*`
48
+ - 派生/同步:`create_from_*`、`sync_*`
49
+
50
+ 推荐本体落库路径(与 **[本体规划指南](./本体规划指南.md)** 一致):
51
+
52
+ 1. `s.onto.define_object_type` → `s.onto.bind_source(..., "dazi_cube", config={"cube": ...})`
53
+ 2. `s.onto.define_property`(`dimension` / `measure` 须带与 Cube 成员一致的 `qualified_name`)
54
+ 3. `s.onto.define_link_type`(两端为对象类型 **`code`**,非 Cube 名)
55
+ 4. `s.onto.register_function` / `s.onto.define_action`
56
+ 5. `s.ontology.features.attach(object_code, feature_type, feature_id)`(`feature_type`: `function | action | rule`)
57
+
58
+ **关于 `s.ontology.objects.create_from_cubes`**:SDK 仍提供,**易与业务语义脱节**;规划文档、示例包与交付脚本**不应依赖**。仅隔离烟测按需使用。
59
+
60
+ ## 4. 返回结构规范
61
+
62
+ ### 4.1 批量派生返回(ResultBatch)
63
+
64
+ ```json
65
+ {
66
+ "ok": true,
67
+ "created": [],
68
+ "updated": [],
69
+ "skipped": [],
70
+ "errors": [],
71
+ "summary": {
72
+ "requested": 0,
73
+ "created": 0,
74
+ "updated": 0,
75
+ "skipped": 0,
76
+ "errors": 0
77
+ }
78
+ }
79
+ ```
80
+
81
+ ### 4.2 特征绑定返回(示例)
82
+
83
+ ```json
84
+ {
85
+ "ok": true,
86
+ "id": "xxx",
87
+ "created": true,
88
+ "updated": false,
89
+ "message": ""
90
+ }
91
+ ```
92
+
93
+ ## 5. 模块速查
94
+
95
+ ### 5.1 `space`
96
+
97
+ - `s = space.create(name, space_id=None, storage_engine="clickhouse", connection_config={"database": ...}, ...)`
98
+ - `s = space.get(name_or_id)`
99
+ - `space.use(handle_or_name)`
100
+
101
+ ### 5.2 `s.sql`
102
+
103
+ - `s.sql.query(sql)` / `s.sql.query_one(sql)` / `s.sql.execute(sql)`
104
+ - `s.sql.insert_rows(table, rows)`(`rows` 为 `list[dict]`)
105
+
106
+ **`query_one` 与多列聚合**
107
+
108
+ - `query_one` 适合 `SELECT count()` 等**单列标量**;对 `SELECT sum(a), count(b) ...` 等多列聚合,**勿**对返回值调用 `.get()`(可能得到 float 而非 dict)。
109
+ - **推荐**:`rows = s.sql.query(sql); row = rows[0] if rows else {}`(函数脚本内为 `p.sql.query`)。详见 [脚本运行常见错误处理](./脚本运行常见错误处理.md#2-sql-聚合查询query_one-返回类型)。
110
+
111
+ ### 5.2.1 本体函数输出(`ontology_function`)
112
+
113
+ 与 setup/seed 不同,本体函数运行时平台注入 `ctx`、`space`、`onto`:
114
+
115
+ | 要点 | 说明 |
116
+ | ---- | ---- |
117
+ | 入口 | `def main():`(**无** `params` 形参) |
118
+ | 入参 | `ctx.params` → `p.get_params()` |
119
+ | 出参 | **`return p.function_result(columns=..., data=..., row_count=...)`** |
120
+ | 禁止 | `output.print_json()`(`OutputModule` 无此方法)、裸 `return {"k": v}` |
121
+
122
+ 标准模板:`资源/examples/onto/_templates/ontology_function_template.py`。详见 [function-guide](./function-guide.md#函数脚本结构标准模板)、[脚本运行常见错误处理 §3](./脚本运行常见错误处理.md#3-函数输出禁止-outputprint_json)。
123
+
124
+ **ClickHouse:`INSERT ... VALUES` 与注释**
125
+
126
+ - **`VALUES` 与各元组之间禁止 SQL 行注释 `--`**,否则易出现 `Code: 27` 等解析错误。
127
+ - **推荐**:大批量灌数用 **`s.sql.insert_rows`**;详见 **[dazi_script_seed_data_guide](./dazi_script_seed_data_guide.md)**。
128
+
129
+ ### 5.3 `s.tables`
130
+
131
+ - `s.tables.register(table_name, display_name=..., description=..., label=..., category_347=...)` — `label` 为 `display_name` 简写别名;已注册时可刷新表显示名与说明;`category_347` 为 平台标准分类中文名(如 `"维度表"`),注册后**即时挂载**平台分类
132
+ - `s.tables.sync_columns(table_name)` — 从物理库同步列;未显式传入元数据时仅**推断** `display_name`(token 词典),**不**写入业务 `description`
133
+ - `s.tables.set_column_meta(table_name, columns=[{name, display_name, description, business_role}], force=False)` — 批量写入列显示名与说明(须先 register + sync_columns)
134
+ - `s.tables.register_with_meta(table_name, display_name=..., description=..., columns=[...], force_column_meta=False, category_347=...)` — 上三者合一(**推荐** setup 脚本使用)
135
+ - `s.tables.list()` / `s.tables.discover()`
136
+ - `s.tables.add_relationship(from_table, to_table, join_sql, relationship_type="many_to_one", join_keys=None, description=None, category_347=...)` — 注册**数据空间表间关系**(幂等);`category_347` 如 `"主数据关联"`、`"时间关联"`
137
+
138
+ ### 5.4 Cube
139
+
140
+ - `s.register_cube(name, table, title, measures, dimensions, category_347=...)` — `category_347` 如 `"流程型"`、`"主体型"`
141
+
142
+ ### 5.5 `s.onto`
143
+
144
+ - `s.onto.define_object_type(code, name, ..., category_347=...)` — 如 `category_347="主数据"`
145
+ - `s.onto.bind_source(object_type_code, "dazi_cube", config={"cube": "CubeName"})`
146
+ - `s.onto.define_property(...)`
147
+ - `s.onto.define_link_type(code, name, from_object_type_code, to_object_type_code, ..., category_347=...)` — 如 `category_347="归属关系"`
148
+ - `s.onto.register_function(function_id, adapter, ..., category_347=...)` — 如 `category_347="总览分析"`
149
+ - `s.onto.define_action(action_code, ...)`
150
+
151
+ **常见误用**(setup 脚本):不存在 `s.onto.sync_metrics()`、`s.cubes.upsert`;`define_object_type` 第二参数为 `name` 非 `label`;链接类型参数为 `from_object_type_code` / `to_object_type_code`。对照表见 [脚本运行常见错误处理](./脚本运行常见错误处理.md#1-setup-脚本-api-误用)。
152
+
153
+ ### 5.6 `s.categories`(平台分类挂载)
154
+
155
+ 挂载到 `ads_categories` 默认根下的平级目录,桥表关联表/Cube/对象/关系/链接/函数。分类名必须与 [本体命名规范](./本体命名规范_物理表Cube与对象.md) 中的**平台标准分类中文名**一致(如「维度表」「流程型」「总览分析」)。详见 [本体分类规划与SDK扩展方案](./本体分类规划与SDK扩展方案.md)。
156
+
157
+ > **术语说明**:SDK 参数 `category_347`、`ensure_347` CLI `--register-platform-category` 均表示**平台标准分类**(侧栏分组名),与内部文档编号无关;`category_347` 为历史参数名,请传入标准中文分类名。
158
+
159
+ - `s.categories` — `CategoryManager` 实例(按空间懒加载)
160
+ - `s.categories.ensure_347(kind, category)` — 按平台标准分类名创建平级目录(幂等);`kind` 支持 `table` / `cube` / `object` / `relation` / `link` / `function`
161
+ - `s.categories.assign_table(category, table_name)` — 挂载物理表
162
+ - `s.categories.assign_cube(category, cube_name)` — 挂载 Cube
163
+ - `s.categories.assign_object(category, object_type_code)` — 挂载对象类型
164
+ - `s.categories.assign_relation(category, from_table, to_table)` — 挂载表间关系
165
+ - `s.categories.assign_link(category, link_code)` — 挂载链接类型
166
+ - `s.categories.assign_function(category, function_id)` — 挂载本体函数(如 `profit.fn.get_summary`)
167
+ - `s.categories.auto_assign_tables(table_names)` — 按表名前缀自动推断平台分类并挂载
168
+ - `s.categories.apply_registry(CATEGORY_REGISTRY, skip_missing=True, kinds=None)` 批量应用注册表;`kinds` 可限定资源类(默认六类全挂;推荐在 `*_category_mount.py` 中一次性执行)
169
+
170
+ **`CATEGORY_REGISTRY` 结构**(与规划附录 B 对齐):
171
+
172
+ ```python
173
+ CATEGORY_REGISTRY = {
174
+ "table": {"维度表": ["dim_account"], "事实表": ["fact_gl_journal_entry"]},
175
+ "cube": {"流程型": ["ActualCube"], "主体型": ["AccountActualCube"]},
176
+ "object": {"主数据": ["Account"], "事务": ["JournalEntry"], "分析": ["ProfitAnalysis"]},
177
+ "relation": {"时间关联": [("fact_x", "dim_date")], "主数据关联": [("fact_x", "dim_y")]},
178
+ "link": {"归属关系": ["entry_belongs_account"], "分析归因": ["analysis_by_account"]},
179
+ "function": {"总览分析": ["profit.fn.get_summary"]}, # 函数未注册时须 skip_missing=True
180
+ }
181
+ ```
182
+
183
+ **两种用法**(可并存,挂载幂等):
184
+
185
+ 1. **批量 — `*_category_mount.py`**(推荐):全量 `CATEGORY_REGISTRY` + `apply_registry`(init + seed + 全部函数 publish **之后**)
186
+ 2. **内联**(注册时即时挂载,可与 1 并存):
187
+
188
+ ```python
189
+ s.tables.register_with_meta("dim_product", display_name="产品维表", category_347="维度表", ...)
190
+ s.register_cube("SalesCube", table=fact, title="...", category_347="流程型", ...)
191
+ s.onto.define_object_type("Product", "产品", category_347="主数据")
192
+ s.tables.add_relationship(..., category_347="主数据关联")
193
+ s.onto.define_link_type("order_contains_product", "...", ..., category_347="归属关系")
194
+ ```
195
+
196
+ ### 5.7 `s.ontology` / `s.ontology_rules` / `s.scripts`
197
+
198
+ 见上文;规则:`ensure_rule_set` + `upsert_rule`;脚本记录:`create` / `ensure` / `list` 等。
199
+
200
+ ## 6. 标准初始化流程(建议)
201
+
202
+ 1. 确认 `space_id`(实现单元 README)
203
+ 2. 建表与灌数(`s.sql`;灌数规范见 seed 指南)
204
+ 3. `s.tables.register_with_meta`(`TABLE_REGISTRY` 含表/列 `display_name`、`description`)
205
+ 4. **`s.tables.add_relationship`**(与规划「表间关系」一致;**勿省略**)
206
+ 5. `s.register_cube`
207
+ 6. `s.onto` 定义对象、属性、链接
208
+ 7. 注册函数/动作:`dazi onto script publish ... --register-function-id`(可选 `--register-platform-category`);**发布后** `save-test-arguments`
209
+ 8. 函数齐后 run `*_category_mount.py`(全量分类;或依赖步骤 7 内联 `--register-platform-category`)
210
+ 9. 配置规则(如需要)
211
+
212
+ ## 7. 在 dazi-vscode 中发布与运行
213
+
214
+ 开发完成后,在工作区根目录:
215
+
216
+ ```bash
217
+ # 预检
218
+ dazi onto script publish-preview 项目/<业务名>/本体/ontos/<实现名>/setup/my_setup.py --space <space-id>
219
+
220
+ # 发布(初始化/灌数类脚本)
221
+ dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/setup/my_setup.py --space <space-id>
222
+
223
+ # 发布并注册为本体函数
224
+ dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/functions/my_func.py \
225
+ --space <space-id> \
226
+ --register-function-id my_func
227
+
228
+ # 运行已入库函数
229
+ dazi onto function run my_func --space <space-id> --params '{}'
230
+ ```
231
+
232
+ 亦可用侧栏 **Onto 本体** 发布函数 / 运行函数。
233
+
234
+ ## 8. 回归与 JSON 摘要
235
+
236
+ - 支持 `--json` 时,输出前缀统一:`__JSON_SUMMARY__`
237
+ - 非 0 退出码即失败
238
+ - 会改写数据的步骤须提供跳过/禁用开关
239
+
240
+ **内置参考示例**(在用户工作区:**侧栏 帮助 → 示例 → 下载所有示例**,或执行 `dazi examples sync`,得到 **`资源/examples/`**;可复制到 **`项目/<业务名>/本体/ontos/<实现名>/setup/`** 或 **`functions/`** 再改,勿直接改写同步下来的只读备份):
241
+
242
+ - **规划全文**:`资源/examples/onto/<示例>/plans/*.md`(推荐 `销售示例/plans/规划示例_产品销售本体规划方案.md`)
243
+ - **常见错误**(,**非**通用模板):`脚本运行常见错误处理.md` 中的已废弃客户路径 — 使用通用占位符
244
+ - **利润示例(GL 域)**:`资源/examples/onto/利润示例/setup/profit_ontology_init.py`、`profit_seed_data.py`、`functions/profit_fn_*.py`、`functions/test_arguments/`
245
+ - **销售示例(推荐,含表间关系 + test_arguments)**:`资源/examples/onto/销售示例/setup/sales_ontology_init.py`、`sales_seed_data.py`、`functions/sales_fn_*.py`
246
+ - **脚本运行纠错(实录)**:`资源/docs/onto/脚本运行常见错误处理.md` — setup API、`query_one`、CLI test_arguments 踩坑
@@ -143,7 +143,7 @@ dazi onto script run --script-id <script-id> --space <space-id> --params '{}'
143
143
 
144
144
  **参考完整初始化+灌数**:
145
145
 
146
- - **推荐(已落地)**:`资源/examples/onto/销售示例/setup/sales_ontology_init.py`、`sales_seed_data.py`(见 [规划示例_产品销售本体规划方案.md](./规划示例_产品销售本体规划方案.md))
146
+ - **推荐(已落地)**:`资源/examples/onto/销售示例/setup/sales_ontology_init.py`、`sales_seed_data.py`(规划见同目录 `../plans/规划示例_产品销售本体规划方案.md`)
147
147
  - **通用模板**:复制 `资源/examples/onto/利润示例/setup/profit_ontology_init.py` 到 `项目/<业务名>/本体/ontos/<实现名>/setup/` 后按空间改 `space_id` 与表名,再发布执行。
148
148
 
149
149
  > **已废弃**:`dazi-agent run --file "spaces/.../editorial/..."` — 请改用上表 `dazi onto script publish` / `function run`。