mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-19 07:32:02 +00:00
277982ead3
- 蓝图:bp_* 操作和逐卡白名单、蓝图检查、留痕、按蓝图判权限;五个场景模板加自由形(骨架、关节、应用骨架、定时任务),实例化与骨架比对;BlueprintApplier / TemplateApplier(只收模板声明过的关节)与自查;look 新类别口径卡;蓝图对话和填关节两个流式接口,不同组的关节并行填、出错的组串行重试;模板列表 - 试运行与发布单:四种起点(对话、模板、问数据的一轮、已发布版本);试运行是探索运行,草稿应用按这次运行解析、印章写试运行,流程类派给自己、不触发升级;发布单九步、预检只读、每步留痕、停下后接着做、核对卡片指纹和应用管理权、启动时整理中断的单;白话层覆盖全部问题编号 - 记录表与文件:平台托管只追加的记录表、按高水位固定数据、连接上的临时视图做行级可见、写入走动作(records_append)、表版本在发布时切换、草稿不影响线上、记录列表遮敏感列;文件按哈希存、两步上传、提交时绑定运行、下载按读权限、清理、file_text、按配方导入一期(table_import_period) - 个人应用:存成个人应用(问数据的约束)、刷新(遮罩、只读、每日上限)、只有拥有者看得见、提交审核与共享副本、开关下发到认人接口;页面编辑的候选;隐藏组件不下发;三条新检查
163 lines
8.6 KiB
Python
163 lines
8.6 KiB
Python
"""records_append / records_lookup 的执行(平台内置能力,capabilities/platform.py)。
|
|
|
|
由 process/actions.send 经 registry.endpoint_request 调到这里:生效记录已经先写好(pending → sent),这里只管写那一行、
|
|
交回回执。幂等键就是动作键:发送用的是「动作键:第几次」,这里只取动作键,同一个动作不论第几次发都只落一行
|
|
(记录表 store.append 按它去重)。写下的行里有文件字段的,把那几个文件绑到这一行(下载按行级可见核对)。
|
|
|
|
写之前核对三样(写进去的东西谁都看得到,不能凭图里写的一个名字就写):
|
|
- 正式运行只写自己的记录表:表出自哪个蓝图,就只收那个蓝图发布出来的工作流的运行(没有蓝图的表,收钉着它的应用里的
|
|
工作流)——不然一张手改过的图就能往别的应用的表里写(records.table_foreign);
|
|
- 更正(supersedes)只能更正提交的人看得到的那一行:按行级可见(records/access),使用者只能更正自己提交的;
|
|
- 文件只绑这次运行提交时绑上的(StoredFile.bound_run_id 是这次运行):值里随便写一个文件编号,不会把别人的文件挂到
|
|
这一行上、让看得到这一行的人都能下载。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
from typing import Any
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.db.base import SessionLocal
|
|
from app.records import store
|
|
|
|
|
|
def _result(status: int | None, body: Any = None, *, error: str | None = None, environment: str = "prod") -> Any:
|
|
from app.capabilities.registry import EndpointResult
|
|
|
|
return EndpointResult(status=status, body=body, error=error, environment=environment)
|
|
|
|
|
|
def action_key_of(idem_key: str | None) -> str:
|
|
"""「动作键:第几次」→ 动作键。"""
|
|
return str(idem_key or "").rsplit(":", 1)[0]
|
|
|
|
|
|
async def append_action(args: dict[str, Any], *, idem_key: str | None, run_id: str | None, on_behalf: Any,
|
|
environment: str) -> Any:
|
|
from app.capabilities.platform import actor_of
|
|
from app.records.tables import columns_of, find_table, path_of, refuse_retired
|
|
|
|
values = args.get("values")
|
|
if isinstance(values, str):
|
|
try:
|
|
values = json.loads(values)
|
|
except ValueError:
|
|
return _result(None, error="要写入的内容需要是一组字段和值(JSON 对象)", environment=environment)
|
|
actor = actor_of(on_behalf)
|
|
supersedes = str(args.get("supersedes") or "").strip() or None
|
|
async with SessionLocal() as session:
|
|
table = await find_table(session, str(args.get("table") or ""))
|
|
source = None
|
|
if table is not None and table.source_id:
|
|
from app.db.models import DataSource
|
|
|
|
source = await session.get(DataSource, table.source_id)
|
|
try:
|
|
if table is not None:
|
|
refuse_retired(table)
|
|
await _check_writer(session, table, run_id)
|
|
if supersedes:
|
|
await _check_supersedes(session, table, supersedes, actor)
|
|
except store.RecordsRefused as e:
|
|
return _result(None, error=e.message, environment=environment)
|
|
if table is None:
|
|
return _result(None, error=f"找不到记录表「{args.get('table')}」,可能已被删除,请重新发布应用",
|
|
environment=environment)
|
|
try:
|
|
columns = columns_of(table)
|
|
done = await asyncio.to_thread(
|
|
store.append, path_of(table), columns, values, version=table.version, run_id=run_id,
|
|
actor=actor, action_key=action_key_of(idem_key), supersedes=supersedes)
|
|
except store.RecordsRefused as e:
|
|
return _result(None, error=e.message, environment=environment)
|
|
if not done.duplicate:
|
|
await _bind_files(table, columns, done, run_id)
|
|
return _result(200, {"table": table.table_key, "version": table.version, "source": source.name if source else None,
|
|
**done.receipt()}, environment=environment)
|
|
|
|
|
|
async def _check_writer(session: Any, table: Any, run_id: str | None) -> None:
|
|
"""正式运行只写自己的记录表:表出自蓝图的,运行的工作流要是那个蓝图发布出来的;没有蓝图的,要是钉着这张表的应用里的
|
|
工作流。不是正式运行的(探索运行在 actions.prepare 就定成演练,走不到这里)、没有运行的(平台内部直接调)不核对。"""
|
|
from app.db.models import Run
|
|
from app.db.models_apps import App
|
|
from app.db.models_blueprints import Blueprint
|
|
|
|
run = await session.get(Run, run_id) if run_id else None
|
|
if run is None or run.run_class != "formal":
|
|
return
|
|
owners: set[str] = set()
|
|
if table.blueprint_id:
|
|
bp = await session.get(Blueprint, table.blueprint_id)
|
|
if bp is not None and bp.workflow_id:
|
|
owners.add(bp.workflow_id)
|
|
elif table.app_id:
|
|
app = await session.get(App, table.app_id)
|
|
pins = ((app.draft or {}).get("pins") or []) if app is not None else []
|
|
owners |= {str(p.get("workflow_id")) for p in pins if isinstance(p, dict) and p.get("workflow_id")}
|
|
if run.workflow_id not in owners:
|
|
raise store.RecordsRefused("records.table_foreign", f"记录表「{table.name}」不属于这个应用:正式运行只能写自己应用的"
|
|
"记录表,请检查写记录表那一步选的表")
|
|
|
|
|
|
async def _check_supersedes(session: Any, table: Any, row_id: str, actor: str | None) -> None:
|
|
"""更正只能更正提交的人看得到的那一行(行级可见同记录列表:使用者只看得到自己提交的)。看不到的说「不在这张表里」,
|
|
不说它在不在。"""
|
|
from app.records.access import scope_of_run
|
|
from app.records.tables import columns_of, path_of
|
|
|
|
scope = await scope_of_run(session, table.app_id, run_class=None, started_by=actor)
|
|
if scope.get("rows") == "all":
|
|
return
|
|
row = await asyncio.to_thread(store.row_by_id, path_of(table), columns_of(table), row_id)
|
|
if scope.get("rows") != "mine" or row is None or row.get("_submitted_by") != actor:
|
|
raise store.RecordsRefused("records.supersede_missing", "要更正的那一行不在这张记录表里")
|
|
|
|
|
|
async def _bind_files(table: Any, columns: list[Any], done: Any, run_id: str | None) -> None:
|
|
"""这一行的文件字段:把文件绑到这一行(StoredFile.bound_record)。下载时读得到这一行的人才能下(files/store.py)。
|
|
只绑这次运行提交时绑上的文件(bound_run_id 是这次运行):值里写的别的文件编号不认。"""
|
|
from app.db.models_blueprints import StoredFile
|
|
|
|
ids = []
|
|
for column in columns:
|
|
if column.type != "file":
|
|
continue
|
|
raw = done.values.get(column.name)
|
|
try:
|
|
ref = json.loads(raw) if isinstance(raw, str) else raw
|
|
except ValueError:
|
|
continue
|
|
if isinstance(ref, dict) and isinstance(ref.get("file_id"), str):
|
|
ids.append(ref["file_id"])
|
|
if not ids:
|
|
return
|
|
async with SessionLocal() as session:
|
|
for row in (await session.execute(select(StoredFile).where(StoredFile.id.in_(ids)))).scalars():
|
|
if row.bound_record is None and run_id and row.bound_run_id == run_id:
|
|
row.bound_record = {"table_id": table.id, "row_id": done.row_id}
|
|
await session.commit()
|
|
|
|
|
|
async def lookup_action(args: dict[str, Any], *, environment: str) -> Any:
|
|
"""结果未知时按幂等键查:找到这次动作的生效记录,从它的预览里取写的是哪张表,再按动作键查那一行。"""
|
|
from app.db.models import ActionRecord
|
|
from app.records.tables import columns_of, find_table, path_of
|
|
|
|
idem = str(args.get("idempotency_key") or "")
|
|
async with SessionLocal() as session:
|
|
rec = (await session.execute(select(ActionRecord).where(ActionRecord.idem_key == idem))).scalar_one_or_none()
|
|
preview = rec.preview if rec is not None and isinstance(rec.preview, dict) else {}
|
|
table = await find_table(session, str(preview.get("table") or "")) if preview else None
|
|
if table is None:
|
|
return _result(404, {"found": False}, environment=environment)
|
|
try:
|
|
got = await asyncio.to_thread(store.lookup, path_of(table), columns_of(table), action_key_of(idem))
|
|
except store.RecordsRefused as e:
|
|
return _result(None, error=e.message, environment=environment)
|
|
if got is None:
|
|
return _result(404, {"found": False}, environment=environment)
|
|
return _result(200, {"table": table.table_key, "version": table.version, **got}, environment=environment)
|