models.py
6.42 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
91
92
93
94
95
96
97
98
99
100
101
102
103
"""能力层的表:能力定义(技能 / 动作 / 外部端点)、动作运行、审批、人工任务。
内置函数与工具在代码里注册(registry.py),不进表;表里只放用户可配置的能力。
"""
from __future__ import annotations
import datetime as dt
from sqlalchemy import DateTime, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from ..core.db import _LONGTEXT, Base
from ..core.dbx import now
class CapabilityDef(Base):
__tablename__ = "otlg_capability"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
kind: Mapped[str] = mapped_column(String(16), index=True) # skill / action / endpoint
name: Mapped[str] = mapped_column(String(128), index=True)
display_name: Mapped[str] = mapped_column(String(255), default="")
description: Mapped[str] = mapped_column(Text, default="")
spec_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
status: Mapped[str] = mapped_column(String(16), default="draft") # draft / published / disabled
version: Mapped[int] = mapped_column(Integer, default=1)
risk_level: Mapped[str] = mapped_column(String(16), default="low")
created_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, onupdate=now)
class ActionRun(Base):
"""一次动作:从提议到执行(或驳回)的完整记录。"""
__tablename__ = "otlg_action_run"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
action_id: Mapped[str] = mapped_column(String(64), index=True)
action_name: Mapped[str] = mapped_column(String(255), default="")
action_version: Mapped[int] = mapped_column(Integer, default=1)
kind: Mapped[str] = mapped_column(String(16), default="normal") # normal / revert
revert_of: Mapped[str] = mapped_column(String(64), default="")
params_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
target_ref: Mapped[str] = mapped_column(String(255), default="", index=True) # object:<type>:<key>
# 作用对象拆开存:动作记录是本体里的系统对象,要能按对象关联、按对象类型统计
target_type: Mapped[str] = mapped_column(String(64), default="", index=True)
target_key: Mapped[str] = mapped_column(String(255), default="", index=True)
target_label: Mapped[str] = mapped_column(String(255), default="")
initiator_kind: Mapped[str] = mapped_column(String(16), default="user") # user / ai / sop / watch
initiator_id: Mapped[str] = mapped_column(String(128), default="")
submitted_by: Mapped[str] = mapped_column(String(128), default="")
source_ref: Mapped[str] = mapped_column(String(255), default="", index=True) # sop_step:<id> / turn:<id>
reason: Mapped[str] = mapped_column(Text, default="")
status: Mapped[str] = mapped_column(String(24), default="pending_approval", index=True)
preview_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
result_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
error: Mapped[str] = mapped_column(Text, default="")
idempotency_key: Mapped[str] = mapped_column(String(64), default="", index=True)
priority: Mapped[str] = mapped_column(String(8), default="P2", index=True) # P0 最紧急 ~ P3
evidence_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]") # 依据:数据 / 规则 / 原文
impact_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}") # 影响面:关系网上会波及谁
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, index=True)
decided_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
finished_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
class ApprovalTask(Base):
__tablename__ = "otlg_approval"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
run_id: Mapped[str] = mapped_column(String(64), index=True)
approver_roles_json: Mapped[str] = mapped_column(Text, default="[]")
stage: Mapped[int] = mapped_column(Integer, default=0) # 第几段审批(从 0 起)
stage_name: Mapped[str] = mapped_column(String(64), default="")
status: Mapped[str] = mapped_column(String(16), default="pending", index=True)
decided_by: Mapped[str] = mapped_column(String(128), default="")
comment: Mapped[str] = mapped_column(Text, default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
decided_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
class Task(Base):
"""人工任务:由审批过的动作(task.create)或 SOP 的人工步骤产生。"""
__tablename__ = "otlg_task"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
title: Mapped[str] = mapped_column(String(512), default="")
description: Mapped[str] = mapped_column(Text, default="")
role: Mapped[str] = mapped_column(String(128), default="", index=True)
assignee: Mapped[str] = mapped_column(String(128), default="", index=True)
form_json: Mapped[str] = mapped_column(Text, default="[]")
checklist_json: Mapped[str] = mapped_column(Text, default="[]")
context_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
object_ref: Mapped[str] = mapped_column(String(255), default="", index=True)
status: Mapped[str] = mapped_column(String(16), default="open", index=True) # open / done / cancelled
result_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
source_type: Mapped[str] = mapped_column(String(16), default="") # action / sop
source_id: Mapped[str] = mapped_column(String(64), default="", index=True)
due_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
created_by: Mapped[str] = mapped_column(String(128), default="")
completed_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, index=True)
completed_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)