bootstrap.py
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""模块装配:把各层之间的扩展点接起来。
各模块之间通过「注册」解耦,而不是互相 import:
- 内核的引用分析 / 对象 360 / YAML 导入导出,由能力层与 SOP 层注册扩展;
- 审核队列的采纳处理器、发布闸门,由各自模块在导入时注册。
这里是唯一需要知道所有模块的地方。
"""
from __future__ import annotations
_done = False
def setup() -> None:
global _done
if _done:
return
_done = True
# 导入即注册:内置函数与工具、SOP 函数、审核处理器、发布闸门
from .capability import builtins as _cap_builtins # noqa: F401
from .evals import service as _evals # noqa: F401
from .kernel import propose as _propose # noqa: F401
from .sop import engine as _sop_engine # noqa: F401
from .sop import functions as _sop_functions # noqa: F401
from .sop import service as _sop_service # noqa: F401
from .capability import actions, defs
from .kernel import api as kernel_api
from .kernel import service as ksvc
from .kernel import templates
from .sop import engine as sop_engine
from .sop import service as sop_service
ksvc.register_usage_provider(defs.usage_provider)
ksvc.register_usage_provider(sop_service.usage_provider)
kernel_api.register_object_section(actions.object_section)
kernel_api.register_object_section(sop_engine.object_section)
templates.register_section("capabilities", _import_capabilities, _export_capabilities)
templates.register_section("sops", _import_sops, _export_sops)
def _export_capabilities() -> list[dict]:
from .capability import defs
return [{"kind": d["kind"], "name": d["name"], "display_name": d["display_name"],
"description": d["description"], "spec": d["spec"]}
for d in defs.list_defs() if d["kind"] in ("skill", "action")]
def _import_capabilities(items: list[dict]) -> dict:
from fastapi import HTTPException
from .capability import defs
ok, errors = [], []
for it in items:
try:
cur = defs.find(it["name"], it.get("kind"))
spec = it.get("spec") or {}
if it.get("kind") == "action" and spec.get("target_type", "").startswith("@"):
from .kernel import service as ksvc
found = ksvc.find(spec["target_type"][1:], "object_type")
spec["target_type"] = found["id"] if found else spec["target_type"][1:]
defs.upsert(it["kind"], it["name"], display_name=it.get("display_name", ""),
description=it.get("description", ""), spec=spec,
cap_id=cur["id"] if cur else "")
ok.append(it["name"])
except HTTPException as e:
errors.append(f"{it.get('name')}:{e.detail}")
return {"imported": ok, "errors": errors}
def _export_sops() -> list[dict]:
from .sop import service as sop_service
out = []
for d in sop_service.list_defs():
full = sop_service.get(d["id"])
out.append({"title": full["title"], "description": full["description"],
"owner_role": full["owner_role"], "spec": full["spec"]})
return out
def _import_sops(items: list[dict]) -> dict:
from .sop import service as sop_service
existing = {d["title"] for d in sop_service.list_defs()}
created = []
for it in items:
if it.get("title") in existing:
continue
sop_service.create(it.get("title", ""), it.get("description", ""), it.get("spec"),
owner_role=it.get("owner_role", ""))
created.append(it.get("title"))
return {"created": created}