create-workframe 0.1.12 → 0.1.14
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 +1 -1
- package/bin/create-workframe.js +2 -2
- package/bin/workframe.js +130 -2
- package/docs/security.md +2 -2
- package/package.json +1 -1
- package/scripts/bundle-workframe-ui.mjs +9 -0
- package/scripts/sync-canonical-to-package.mjs +1 -0
- package/scripts/verify-public-deploy.sh +65 -3
- package/workframe-api/action_proxy.py +23 -17
- package/workframe-api/activity_feed.py +798 -0
- package/workframe-api/api_meta.py +157 -0
- package/workframe-api/auth_gate.py +566 -0
- package/workframe-api/avatar_registry.py +343 -0
- package/workframe-api/broker_audit.py +164 -0
- package/workframe-api/cell_authority.py +231 -0
- package/workframe-api/chat_bind.py +319 -0
- package/workframe-api/chat_sessions.py +509 -0
- package/workframe-api/chat_stream.py +694 -0
- package/workframe-api/cleanup_dogfood_smoke.py +252 -0
- package/workframe-api/credential_broker.py +162 -0
- package/workframe-api/credential_resolve.py +202 -0
- package/workframe-api/credential_store.py +245 -0
- package/workframe-api/crew_registry.py +250 -0
- package/workframe-api/db_schema.py +755 -0
- package/workframe-api/docker_gateway.py +166 -0
- package/workframe-api/doctor_runtime.py +109 -0
- package/workframe-api/domain/__init__.py +47 -0
- package/workframe-api/domain/entities.py +252 -0
- package/workframe-api/domain/schema/workframe-domain.schema.json +278 -0
- package/workframe-api/egress_policy.py +42 -0
- package/workframe-api/handler_modules/__init__.py +17 -0
- package/workframe-api/handler_modules/handler_admin.py +542 -0
- package/workframe-api/handler_modules/handler_auth.py +512 -0
- package/workframe-api/handler_modules/handler_chat.py +641 -0
- package/workframe-api/handler_modules/handler_install.py +178 -0
- package/workframe-api/handler_modules/handler_provider.py +325 -0
- package/workframe-api/handler_modules/handler_workspace.py +1420 -0
- package/workframe-api/health_monitor.py +80 -0
- package/workframe-api/hermes_admin.py +756 -0
- package/workframe-api/hermes_profiles.py +1328 -0
- package/workframe-api/install_api.py +123 -0
- package/workframe-api/kanban_cron.py +473 -0
- package/workframe-api/lane_bindings.py +423 -0
- package/workframe-api/llm_proxy.py +17 -43
- package/workframe-api/mention_helpers.py +180 -0
- package/workframe-api/mention_invoke.py +431 -0
- package/workframe-api/model_surface.py +1139 -0
- package/workframe-api/oauth_pending.py +85 -0
- package/workframe-api/oauth_redirect.py +381 -0
- package/workframe-api/package.json +2 -2
- package/workframe-api/profile_api_lifecycle.py +66 -0
- package/workframe-api/profile_gateway.py +436 -0
- package/workframe-api/provider_bindings.py +673 -0
- package/workframe-api/provider_bootstrap.py +347 -0
- package/workframe-api/provider_catalog.py +180 -0
- package/workframe-api/rooms.py +1613 -0
- package/workframe-api/route_registry.py +331 -191
- package/workframe-api/run-typecheck.mjs +2 -1
- package/workframe-api/run_authority.py +287 -0
- package/workframe-api/run_ledger.py +470 -0
- package/workframe-api/run_surface_wiring.py +344 -0
- package/workframe-api/runtime_cohort.py +752 -0
- package/workframe-api/runtime_tokens.py +127 -0
- package/workframe-api/server.py +1453 -19513
- package/workframe-api/snapshot_feed.py +49 -0
- package/workframe-api/stack_config.py +33 -1
- package/workframe-api/supervisor_client.py +154 -0
- package/workframe-api/test_api_meta_build_stamp.py +34 -0
- package/workframe-api/test_cell_authority.py +79 -0
- package/workframe-api/test_chat_bind.py +48 -0
- package/workframe-api/test_credential_lease_matrix.py +171 -0
- package/workframe-api/test_credential_resolve.py +51 -0
- package/workframe-api/test_credential_store.py +27 -0
- package/workframe-api/test_dogfood_flows.py +346 -0
- package/workframe-api/test_domain_entities.py +152 -0
- package/workframe-api/test_exception_hygiene.py +12 -0
- package/workframe-api/test_hermes_admin.py +72 -0
- package/workframe-api/test_install_wizard_resume.py +78 -0
- package/workframe-api/test_local_bootstrap.py +67 -0
- package/workframe-api/test_mention_helpers.py +35 -0
- package/workframe-api/test_mention_invoke.py +26 -0
- package/workframe-api/test_model_surface_consistency.py +1 -0
- package/workframe-api/test_oauth_pending.py +41 -0
- package/workframe-api/test_provider_bindings.py +52 -0
- package/workframe-api/test_provider_catalog.py +28 -0
- package/workframe-api/test_route_registry.py +171 -35
- package/workframe-api/test_run_authority.py +112 -0
- package/workframe-api/test_run_ledger.py +157 -0
- package/workframe-api/test_run_surface_wiring.py +130 -0
- package/workframe-api/test_runtime_cohort.py +85 -0
- package/workframe-api/test_secure_mode_docker_boundary.py +39 -0
- package/workframe-api/test_server_reexports.py +99 -0
- package/workframe-api/test_stack_config_install_smtp.py +7 -0
- package/workframe-api/turn_credentials.py +28 -13
- package/workframe-api/turn_overlay.py +715 -0
- package/workframe-api/updates.py +16 -1
- package/workframe-api/user_prefs.py +387 -0
- package/workframe-api/wf032_extract_remaining_handlers.py +417 -0
- package/workframe-api/workspace_bootstrap.py +536 -0
- package/workframe-api/workspace_files.py +344 -0
- package/workframe-api/workspace_messaging.py +206 -0
- package/workframe-api/zk_auth.py +3 -2
- package/workframe-supervisor/server.py +10 -1
- package/workframe-supervisor/test_supervisor_negative.py +75 -0
- package/workframe-ui/public/assets/{arc-B0OFRGmJ.js → arc-BT9iZUSd.js} +1 -1
- package/workframe-ui/public/assets/architecture-7EHR7CIX-m1aPUQ_r.js +1 -0
- package/workframe-ui/public/assets/{architectureDiagram-3BPJPVTR-DeBNltHS.js → architectureDiagram-3BPJPVTR-CaEDd3np.js} +1 -1
- package/workframe-ui/public/assets/{blockDiagram-GPEHLZMM-BDhCHu7I.js → blockDiagram-GPEHLZMM-DHJhMrIS.js} +1 -1
- package/workframe-ui/public/assets/{c4Diagram-AAUBKEIU-olcDYvtI.js → c4Diagram-AAUBKEIU-Cdyz9hIV.js} +1 -1
- package/workframe-ui/public/assets/channel-3M69ekE5.js +1 -0
- package/workframe-ui/public/assets/{chunk-2J33WTMH-D0obCBn_.js → chunk-2J33WTMH-BbZx76LX.js} +1 -1
- package/workframe-ui/public/assets/{chunk-3OPIFGDE-DX93f-ZZ.js → chunk-3OPIFGDE-DcSxjTIP.js} +1 -1
- package/workframe-ui/public/assets/{chunk-4BX2VUAB-BX9B5wUs.js → chunk-4BX2VUAB-B7F-kTY1.js} +1 -1
- package/workframe-ui/public/assets/{chunk-55IACEB6-C4DGc6RO.js → chunk-55IACEB6-BwwEiRTK.js} +1 -1
- package/workframe-ui/public/assets/{chunk-5ZQYHXKU-Crlja9Lf.js → chunk-5ZQYHXKU-DAi_NksX.js} +1 -1
- package/workframe-ui/public/assets/{chunk-727SXJPM-JYSm3szI.js → chunk-727SXJPM-BIaF4XCN.js} +1 -1
- package/workframe-ui/public/assets/{chunk-AQP2D5EJ-_KslxVEl.js → chunk-AQP2D5EJ-DjYSk1dG.js} +1 -1
- package/workframe-ui/public/assets/{chunk-BSJP7CBP-CpTO-jU8.js → chunk-BSJP7CBP-_twnyfgV.js} +1 -1
- package/workframe-ui/public/assets/{chunk-CSCIHK7Q-JGUkCmbI.js → chunk-CSCIHK7Q-CkNmdy5s.js} +2 -2
- package/workframe-ui/public/assets/{chunk-FMBD7UC4-BscBwGaC.js → chunk-FMBD7UC4-CjLqd-gl.js} +1 -1
- package/workframe-ui/public/assets/{chunk-KSCS5N6A-5ZTZ0bpX.js → chunk-KSCS5N6A-_6oNqFjj.js} +1 -1
- package/workframe-ui/public/assets/{chunk-L5ZTLDWV-DxvitYbW.js → chunk-L5ZTLDWV-CeO9Am_D.js} +1 -1
- package/workframe-ui/public/assets/chunk-LZXEDZCA-sRM4YeIe.js +2 -0
- package/workframe-ui/public/assets/{chunk-ND2GUHAM-Cayl0iV9.js → chunk-ND2GUHAM-CH8tMb21.js} +1 -1
- package/workframe-ui/public/assets/{chunk-NZK2D7GU-_7dyBR5G.js → chunk-NZK2D7GU-DAI1bsII.js} +1 -1
- package/workframe-ui/public/assets/{chunk-O5CBEL6O--xTpD0yi.js → chunk-O5CBEL6O-B2e-5a2G.js} +1 -1
- package/workframe-ui/public/assets/chunk-QZHKN3VN-BMl9ed8P.js +1 -0
- package/workframe-ui/public/assets/chunk-WU5MYG2G-C08HH6BU.js +1 -0
- package/workframe-ui/public/assets/{chunk-XPW4576I-CwxJQaeK.js → chunk-XPW4576I-DM2-0q37.js} +1 -1
- package/workframe-ui/public/assets/classDiagram-4FO5ZUOK-BFcOE4Aq.js +1 -0
- package/workframe-ui/public/assets/classDiagram-v2-Q7XG4LA2-BFcOE4Aq.js +1 -0
- package/workframe-ui/public/assets/{cose-bilkent-S5V4N54A-CCspENYv.js → cose-bilkent-S5V4N54A-BnbbewLv.js} +1 -1
- package/workframe-ui/public/assets/{dagre-BM42HDAG-Dv8V6R60.js → dagre-BM42HDAG-C4zmB1Vt.js} +1 -1
- package/workframe-ui/public/assets/{diagram-2AECGRRQ-Da48hFPT.js → diagram-2AECGRRQ-n-BJmsiC.js} +1 -1
- package/workframe-ui/public/assets/{diagram-5GNKFQAL-dTihc7-3.js → diagram-5GNKFQAL-BzQGQnZi.js} +1 -1
- package/workframe-ui/public/assets/{diagram-KO2AKTUF-D_Jqu699.js → diagram-KO2AKTUF-Cx_De-wS.js} +1 -1
- package/workframe-ui/public/assets/{diagram-LMA3HP47-Bt8PtEaZ.js → diagram-LMA3HP47-CIOI3Lok.js} +1 -1
- package/workframe-ui/public/assets/{diagram-OG6HWLK6-BtmPjlh0.js → diagram-OG6HWLK6-DkRuPLWY.js} +1 -1
- package/workframe-ui/public/assets/{dist-Cz2turIT.js → dist-XhCT1Q-V.js} +1 -1
- package/workframe-ui/public/assets/{erDiagram-TEJ5UH35-BxMGCflX.js → erDiagram-TEJ5UH35-C3GwhDU1.js} +1 -1
- package/workframe-ui/public/assets/eventmodeling-FCH6USID-DIVBL6CF.js +1 -0
- package/workframe-ui/public/assets/{flowDiagram-I6XJVG4X-ivyroIZt.js → flowDiagram-I6XJVG4X-CLWH8z2e.js} +1 -1
- package/workframe-ui/public/assets/{ganttDiagram-6RSMTGT7-B9llwShx.js → ganttDiagram-6RSMTGT7-D4h66Vy3.js} +1 -1
- package/workframe-ui/public/assets/{gitGraph-WXDBUCRP-BFQHk4bw.js → gitGraph-WXDBUCRP-IW5F-Q6c.js} +1 -1
- package/workframe-ui/public/assets/{gitGraphDiagram-PVQCEYII-ursegLbc.js → gitGraphDiagram-PVQCEYII-BjNIbmRM.js} +1 -1
- package/workframe-ui/public/assets/index-DL2vJP4L.css +1 -0
- package/workframe-ui/public/assets/index-ElS_D59P.js +130 -0
- package/workframe-ui/public/assets/{info-J43DQDTF-DQpifAsB.js → info-J43DQDTF-CcJspM79.js} +1 -1
- package/workframe-ui/public/assets/{infoDiagram-5YYISTIA-C16XP2Q7.js → infoDiagram-5YYISTIA-DP4xR9jt.js} +1 -1
- package/workframe-ui/public/assets/{ishikawaDiagram-YF4QCWOH-CiJoz7rP.js → ishikawaDiagram-YF4QCWOH-D4cZb21V.js} +1 -1
- package/workframe-ui/public/assets/{journeyDiagram-JHISSGLW-CVwEvvUL.js → journeyDiagram-JHISSGLW-DsLuHkIJ.js} +1 -1
- package/workframe-ui/public/assets/{kanban-definition-UN3LZRKU-CWQ6hX5i.js → kanban-definition-UN3LZRKU-Wu65nOsR.js} +1 -1
- package/workframe-ui/public/assets/{line-CgQsmU35.js → line-ByBDzRpy.js} +1 -1
- package/workframe-ui/public/assets/{linear-Bxbc77dL.js → linear-DzXi2vnq.js} +1 -1
- package/workframe-ui/public/assets/{mermaid-parser.core-C_jMeF0a.js → mermaid-parser.core-CaB-1Ckn.js} +2 -2
- package/workframe-ui/public/assets/{mermaid.core-4RKV9MZP.js → mermaid.core-D7HTHC17.js} +3 -3
- package/workframe-ui/public/assets/{mindmap-definition-RKZ34NQL-DZ7y7Sz0.js → mindmap-definition-RKZ34NQL-BfKsC9aj.js} +1 -1
- package/workframe-ui/public/assets/{packet-YPE3B663-B9h2I7Vq.js → packet-YPE3B663-ClpU98TO.js} +1 -1
- package/workframe-ui/public/assets/{pie-LRSECV5Y-B2mP5uHm.js → pie-LRSECV5Y-KeOyY_-u.js} +1 -1
- package/workframe-ui/public/assets/{pieDiagram-4H26LBE5-DyYKyKiP.js → pieDiagram-4H26LBE5-CUpjnzbs.js} +1 -1
- package/workframe-ui/public/assets/{quadrantDiagram-W4KKPZXB-CL_6nej-.js → quadrantDiagram-W4KKPZXB-CJdPTszF.js} +1 -1
- package/workframe-ui/public/assets/{radar-GUYGQ44K-CNKNDQ7S.js → radar-GUYGQ44K-C9BpandZ.js} +1 -1
- package/workframe-ui/public/assets/{requirementDiagram-4Y6WPE33-eWU_mhFa.js → requirementDiagram-4Y6WPE33-CHDZPwry.js} +1 -1
- package/workframe-ui/public/assets/{sankeyDiagram-5OEKKPKP-DrjcXvEQ.js → sankeyDiagram-5OEKKPKP-CMJJh91s.js} +1 -1
- package/workframe-ui/public/assets/{sequenceDiagram-3UESZ5HK-BHWR1xTJ.js → sequenceDiagram-3UESZ5HK-BsV4GppP.js} +1 -1
- package/workframe-ui/public/assets/{src-DfJB8kV1.js → src-WHks82Up.js} +1 -1
- package/workframe-ui/public/assets/{stateDiagram-AJRCARHV-BGJbSd3I.js → stateDiagram-AJRCARHV-eSX9P8z0.js} +1 -1
- package/workframe-ui/public/assets/stateDiagram-v2-BHNVJYJU-FzMK4PkM.js +1 -0
- package/workframe-ui/public/assets/{timeline-definition-PNZ67QCA-DkwoCjiZ.js → timeline-definition-PNZ67QCA-Dg1UkhGo.js} +1 -1
- package/workframe-ui/public/assets/{treeView-BLDUP644-GxfhiqoW.js → treeView-BLDUP644-B7yGsBuj.js} +1 -1
- package/workframe-ui/public/assets/{treemap-LRROVOQU-DxrOsars.js → treemap-LRROVOQU-j_G2oyQ_.js} +1 -1
- package/workframe-ui/public/assets/{vennDiagram-CIIHVFJN-BsWhpUfr.js → vennDiagram-CIIHVFJN-BRrT0jqC.js} +1 -1
- package/workframe-ui/public/assets/{wardley-L42UT6IY-CrFZWMHS.js → wardley-L42UT6IY-DEdUqez2.js} +1 -1
- package/workframe-ui/public/assets/{wardleyDiagram-YWT4CUSO-DV8Xpf5I.js → wardleyDiagram-YWT4CUSO-C-bBK9Bb.js} +1 -1
- package/workframe-ui/public/assets/{xychartDiagram-2RQKCTM6-DqMqnwdZ.js → xychartDiagram-2RQKCTM6-DhH5xuR2.js} +1 -1
- package/workframe-ui/public/index.html +11 -7
- package/workframe-ui/public/workframe-build.json +5 -0
- package/workframe-ui/public/assets/architecture-7EHR7CIX-Dwwi9yA0.js +0 -1
- package/workframe-ui/public/assets/channel-fNHbDpV4.js +0 -1
- package/workframe-ui/public/assets/chunk-LZXEDZCA-DMfdMUwz.js +0 -2
- package/workframe-ui/public/assets/chunk-QZHKN3VN-ewTgEQK8.js +0 -1
- package/workframe-ui/public/assets/chunk-WU5MYG2G-D4Tg-A0l.js +0 -1
- package/workframe-ui/public/assets/classDiagram-4FO5ZUOK-CFAgBpEl.js +0 -1
- package/workframe-ui/public/assets/classDiagram-v2-Q7XG4LA2-CFAgBpEl.js +0 -1
- package/workframe-ui/public/assets/eventmodeling-FCH6USID-BIJTP5k-.js +0 -1
- package/workframe-ui/public/assets/index-8CuZDEIG.css +0 -1
- package/workframe-ui/public/assets/index-xS9lFekI.js +0 -129
- package/workframe-ui/public/assets/stateDiagram-v2-BHNVJYJU-CMLuNyeC.js +0 -1
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
"""WF-032 extract: @mention agent invoke streaming in space rooms."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sqlite3
|
|
7
|
+
import time
|
|
8
|
+
import urllib.parse
|
|
9
|
+
import urllib.request
|
|
10
|
+
import uuid
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
import chat_stream
|
|
14
|
+
import lane_bindings
|
|
15
|
+
import profile_gateway
|
|
16
|
+
import rooms
|
|
17
|
+
import run_authority
|
|
18
|
+
import run_ledger
|
|
19
|
+
from domain.entities import RunStatus
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _srv():
|
|
23
|
+
import server as srv
|
|
24
|
+
|
|
25
|
+
return srv
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _room_recent_transcript(conn: sqlite3.Connection, room_id: str, limit: int = 24) -> str:
|
|
29
|
+
rows = conn.execute(
|
|
30
|
+
"""
|
|
31
|
+
SELECT m.content, u.display_name AS user_name, ap.display_name AS agent_name
|
|
32
|
+
FROM messages m
|
|
33
|
+
LEFT JOIN users u ON u.id = m.sender_user_id
|
|
34
|
+
LEFT JOIN agent_profiles ap ON ap.id = m.sender_agent_id
|
|
35
|
+
WHERE m.room_id = ? AND m.deleted_at IS NULL
|
|
36
|
+
ORDER BY m.created_at DESC
|
|
37
|
+
LIMIT ?
|
|
38
|
+
""",
|
|
39
|
+
(room_id, limit),
|
|
40
|
+
).fetchall()
|
|
41
|
+
lines: list[str] = []
|
|
42
|
+
for row in reversed(rows):
|
|
43
|
+
name = str(row["user_name"] or row["agent_name"] or "Member")
|
|
44
|
+
lines.append(f"{name}: {row['content']}")
|
|
45
|
+
return "\n".join(lines)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _inject_turn_credentials(
|
|
49
|
+
turn_body: dict[str, Any],
|
|
50
|
+
user_id: str,
|
|
51
|
+
workspace_id: str,
|
|
52
|
+
provider: str = "openrouter",
|
|
53
|
+
) -> None:
|
|
54
|
+
resolved = _srv()._resolve_credential(user_id, workspace_id, provider)
|
|
55
|
+
if not resolved:
|
|
56
|
+
return
|
|
57
|
+
turn_body["_credential_override"] = resolved["credential_ref"]
|
|
58
|
+
turn_body["_credential_scope"] = resolved["scope"]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _invoke_room_agent_mention(
|
|
63
|
+
room_id: str,
|
|
64
|
+
workspace_id: str,
|
|
65
|
+
agent_row: dict[str, Any],
|
|
66
|
+
triggered_by_user_id: str,
|
|
67
|
+
parent_message_id: str,
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Stream a tagged agent turn in a shared space room; fan out live updates to room subscribers."""
|
|
70
|
+
template_slug = str(agent_row.get("slug") or "").strip()
|
|
71
|
+
agent_db_id = str(agent_row.get("id") or "").strip()
|
|
72
|
+
agent_name = str(agent_row.get("display_name") or template_slug).strip() or template_slug
|
|
73
|
+
if triggered_by_user_id:
|
|
74
|
+
personal = _srv()._runtime_display_label(triggered_by_user_id, template_slug, workspace_id)
|
|
75
|
+
if personal:
|
|
76
|
+
agent_name = personal
|
|
77
|
+
if not template_slug or not agent_db_id:
|
|
78
|
+
return
|
|
79
|
+
hermes_slug = _srv()._runtime_profile_slug(triggered_by_user_id, template_slug)
|
|
80
|
+
_srv().ensure_runtime_profile(hermes_slug, template_slug, triggered_by_user_id, workspace_id)
|
|
81
|
+
|
|
82
|
+
turn_id = str(uuid.uuid4())
|
|
83
|
+
segments: list[dict[str, Any]] = []
|
|
84
|
+
last_flush = 0.0
|
|
85
|
+
flush_interval = 0.05
|
|
86
|
+
run_id = str(uuid.uuid4())
|
|
87
|
+
session_id = ""
|
|
88
|
+
user_text = ""
|
|
89
|
+
provider = ""
|
|
90
|
+
auth_decision: run_authority.RunAuthorityDecision | None = None
|
|
91
|
+
run_completed_ok = False
|
|
92
|
+
|
|
93
|
+
def publish_update(*, force: bool = False, status: str | None = None) -> None:
|
|
94
|
+
nonlocal last_flush
|
|
95
|
+
now = time.time()
|
|
96
|
+
if not force and (now - last_flush) < flush_interval:
|
|
97
|
+
return
|
|
98
|
+
payload: dict[str, Any] = {
|
|
99
|
+
"type": "turn.update",
|
|
100
|
+
"turn_id": turn_id,
|
|
101
|
+
"room_id": room_id,
|
|
102
|
+
"segments": segments,
|
|
103
|
+
}
|
|
104
|
+
if status:
|
|
105
|
+
payload["status"] = status
|
|
106
|
+
rooms._room_live_publish(room_id, payload)
|
|
107
|
+
rooms._room_live_set_turn(
|
|
108
|
+
{
|
|
109
|
+
"type": "turn.snapshot",
|
|
110
|
+
"turn_id": turn_id,
|
|
111
|
+
"room_id": room_id,
|
|
112
|
+
"agent_slug": hermes_slug,
|
|
113
|
+
"agent_name": agent_name,
|
|
114
|
+
"agent_profile_id": agent_db_id,
|
|
115
|
+
"segments": segments,
|
|
116
|
+
"status": status or "",
|
|
117
|
+
}
|
|
118
|
+
)
|
|
119
|
+
last_flush = now
|
|
120
|
+
|
|
121
|
+
def finish_turn(message_id: str = "") -> None:
|
|
122
|
+
rooms._room_live_publish(
|
|
123
|
+
room_id,
|
|
124
|
+
{
|
|
125
|
+
"type": "turn.complete",
|
|
126
|
+
"turn_id": turn_id,
|
|
127
|
+
"room_id": room_id,
|
|
128
|
+
"message_id": message_id,
|
|
129
|
+
},
|
|
130
|
+
)
|
|
131
|
+
rooms._room_live_clear_turn(turn_id)
|
|
132
|
+
|
|
133
|
+
def fail_turn(error: str, *, persist: bool = True) -> None:
|
|
134
|
+
segments[:] = chat_stream._live_reduce_stream_event(segments, "error", {"error": error})
|
|
135
|
+
publish_update(force=True, status="error")
|
|
136
|
+
if persist:
|
|
137
|
+
try:
|
|
138
|
+
conn = _srv()._workframe_db()
|
|
139
|
+
try:
|
|
140
|
+
now_ts = str(int(time.time()))
|
|
141
|
+
mid = str(uuid.uuid4())
|
|
142
|
+
conn.execute(
|
|
143
|
+
"""
|
|
144
|
+
INSERT INTO messages (
|
|
145
|
+
id, room_id, sender_user_id, sender_agent_id, parent_message_id,
|
|
146
|
+
content, content_type, is_edited, created_at, updated_at
|
|
147
|
+
) VALUES (?,?,?,?,?,?,?,?,?,?)
|
|
148
|
+
""",
|
|
149
|
+
(
|
|
150
|
+
mid,
|
|
151
|
+
room_id,
|
|
152
|
+
None,
|
|
153
|
+
agent_db_id,
|
|
154
|
+
parent_message_id,
|
|
155
|
+
error,
|
|
156
|
+
"text",
|
|
157
|
+
0,
|
|
158
|
+
now_ts,
|
|
159
|
+
now_ts,
|
|
160
|
+
),
|
|
161
|
+
)
|
|
162
|
+
conn.execute("UPDATE rooms SET updated_at = ? WHERE id = ?", (now_ts, room_id))
|
|
163
|
+
conn.commit()
|
|
164
|
+
_srv()._bump_workspace_event_state()
|
|
165
|
+
finish_turn(mid)
|
|
166
|
+
return
|
|
167
|
+
finally:
|
|
168
|
+
conn.close()
|
|
169
|
+
except Exception: # noqa: BLE001
|
|
170
|
+
pass
|
|
171
|
+
rooms._room_live_publish(
|
|
172
|
+
room_id,
|
|
173
|
+
{
|
|
174
|
+
"type": "turn.error",
|
|
175
|
+
"turn_id": turn_id,
|
|
176
|
+
"room_id": room_id,
|
|
177
|
+
"error": error,
|
|
178
|
+
"segments": segments,
|
|
179
|
+
},
|
|
180
|
+
)
|
|
181
|
+
finish_turn()
|
|
182
|
+
|
|
183
|
+
try:
|
|
184
|
+
conn = _srv()._workframe_db()
|
|
185
|
+
try:
|
|
186
|
+
parent = conn.execute(
|
|
187
|
+
"SELECT content FROM messages WHERE id = ? AND room_id = ? AND deleted_at IS NULL",
|
|
188
|
+
(parent_message_id, room_id),
|
|
189
|
+
).fetchone()
|
|
190
|
+
user_text = str(parent["content"] or "").strip() if parent else ""
|
|
191
|
+
finally:
|
|
192
|
+
conn.close()
|
|
193
|
+
|
|
194
|
+
rooms._room_live_publish(
|
|
195
|
+
room_id,
|
|
196
|
+
{
|
|
197
|
+
"type": "turn.started",
|
|
198
|
+
"turn_id": turn_id,
|
|
199
|
+
"room_id": room_id,
|
|
200
|
+
"agent_slug": hermes_slug,
|
|
201
|
+
"agent_name": agent_name,
|
|
202
|
+
"agent_profile_id": agent_db_id,
|
|
203
|
+
"triggered_by_user_id": triggered_by_user_id,
|
|
204
|
+
},
|
|
205
|
+
)
|
|
206
|
+
rooms._room_live_set_turn(
|
|
207
|
+
{
|
|
208
|
+
"type": "turn.snapshot",
|
|
209
|
+
"turn_id": turn_id,
|
|
210
|
+
"room_id": room_id,
|
|
211
|
+
"agent_slug": hermes_slug,
|
|
212
|
+
"agent_name": agent_name,
|
|
213
|
+
"agent_profile_id": agent_db_id,
|
|
214
|
+
"segments": segments,
|
|
215
|
+
"status": "starting",
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
session = lane_bindings.profile_chat_session(
|
|
220
|
+
hermes_slug,
|
|
221
|
+
{
|
|
222
|
+
"room_id": room_id,
|
|
223
|
+
"source_id": "room",
|
|
224
|
+
"client_id": room_id,
|
|
225
|
+
},
|
|
226
|
+
triggered_by_user_id,
|
|
227
|
+
)
|
|
228
|
+
session_id = str(session.get("session_id") or "").strip()
|
|
229
|
+
if not session_id:
|
|
230
|
+
raise ValueError("session_bootstrap_failed: could not start agent session for this room")
|
|
231
|
+
|
|
232
|
+
turn_body = profile_gateway._profile_turn_payload(hermes_slug, user_text, room_id)
|
|
233
|
+
provider = str(agent_row.get("model_provider") or "").strip()
|
|
234
|
+
if not provider:
|
|
235
|
+
provider = _srv()._llm_billing_provider(
|
|
236
|
+
hermes_slug,
|
|
237
|
+
user_id=triggered_by_user_id,
|
|
238
|
+
workspace_id=workspace_id,
|
|
239
|
+
)
|
|
240
|
+
if triggered_by_user_id:
|
|
241
|
+
run_ledger.ensure_schema()
|
|
242
|
+
auth_req = run_authority.mention_run_request(
|
|
243
|
+
triggering_user_id=triggered_by_user_id,
|
|
244
|
+
profile_slug=hermes_slug,
|
|
245
|
+
workspace_id=workspace_id,
|
|
246
|
+
provider=provider,
|
|
247
|
+
room_id=room_id,
|
|
248
|
+
)
|
|
249
|
+
auth_ctx = chat_stream._run_authority_context_for_chat(
|
|
250
|
+
triggered_by_user_id, workspace_id, provider,
|
|
251
|
+
)
|
|
252
|
+
auth_decision = run_authority.evaluate_run_authority(
|
|
253
|
+
auth_req, auth_ctx, run_id=run_id,
|
|
254
|
+
)
|
|
255
|
+
conn = _srv()._workframe_db()
|
|
256
|
+
try:
|
|
257
|
+
run_ledger.record_authority_decision(
|
|
258
|
+
conn,
|
|
259
|
+
run_id=run_id,
|
|
260
|
+
request_surface=auth_req.surface,
|
|
261
|
+
actor_type=auth_req.actor_type,
|
|
262
|
+
actor_id=auth_req.actor_id,
|
|
263
|
+
triggering_user_id=triggered_by_user_id,
|
|
264
|
+
workspace_id=workspace_id or "default",
|
|
265
|
+
agent_id=auth_req.agent_id,
|
|
266
|
+
runtime_binding_id=auth_req.runtime_binding_id,
|
|
267
|
+
profile_slug=hermes_slug,
|
|
268
|
+
provider=provider,
|
|
269
|
+
room_id=room_id,
|
|
270
|
+
session_id=session_id,
|
|
271
|
+
decision=auth_decision,
|
|
272
|
+
)
|
|
273
|
+
conn.commit()
|
|
274
|
+
finally:
|
|
275
|
+
conn.close()
|
|
276
|
+
if not auth_decision.allowed:
|
|
277
|
+
fail_turn(
|
|
278
|
+
"I can't reply yet — connect an LLM provider in Profile → Connect accounts.",
|
|
279
|
+
persist=False,
|
|
280
|
+
)
|
|
281
|
+
return
|
|
282
|
+
|
|
283
|
+
_srv().ensure_profile_api(
|
|
284
|
+
hermes_slug,
|
|
285
|
+
triggered_by_user_id,
|
|
286
|
+
workspace_id,
|
|
287
|
+
bootstrap_providers=False,
|
|
288
|
+
)
|
|
289
|
+
_srv()._overlay_turn_provider_env(hermes_slug, triggered_by_user_id, workspace_id, provider, run_id)
|
|
290
|
+
_srv()._overlay_turn_user_env(hermes_slug, triggered_by_user_id, workspace_id, run_id)
|
|
291
|
+
_inject_turn_credentials(turn_body, triggered_by_user_id, workspace_id, provider)
|
|
292
|
+
_srv()._ensure_profile_proxy_headers(hermes_slug)
|
|
293
|
+
|
|
294
|
+
upstream_body = json.dumps(turn_body).encode("utf-8")
|
|
295
|
+
port = _srv()._profile_api_port(hermes_slug)
|
|
296
|
+
url = f"http://gateway:{port}/api/sessions/{urllib.parse.quote(session_id, safe='')}/chat/stream"
|
|
297
|
+
headers = {
|
|
298
|
+
"Authorization": f"Bearer {_srv()._profile_api_key(hermes_slug)}",
|
|
299
|
+
"Content-Type": "application/json",
|
|
300
|
+
}
|
|
301
|
+
req = urllib.request.Request(url, data=upstream_body, headers=headers, method="POST")
|
|
302
|
+
upstream = urllib.request.urlopen(req, timeout=3600)
|
|
303
|
+
|
|
304
|
+
final_text = ""
|
|
305
|
+
try:
|
|
306
|
+
publish_update(force=True, status="thinking")
|
|
307
|
+
for event_name, data in chat_stream._iter_profile_stream_frames(upstream):
|
|
308
|
+
segments[:] = chat_stream._live_reduce_stream_event(segments, event_name, data)
|
|
309
|
+
if event_name in ("message.delta", "assistant.delta"):
|
|
310
|
+
final_text += chat_stream._live_stream_text(data)
|
|
311
|
+
publish_update(status="writing")
|
|
312
|
+
elif event_name in ("thinking.delta", "reasoning.delta"):
|
|
313
|
+
publish_update(status="thinking")
|
|
314
|
+
elif event_name == "tool.started":
|
|
315
|
+
publish_update(force=True, status=f"tool:{data.get('tool_name') or 'tool'}")
|
|
316
|
+
elif event_name in ("tool.completed", "tool.failed", "tool.progress"):
|
|
317
|
+
publish_update(force=True)
|
|
318
|
+
elif event_name in ("message.complete", "assistant.completed"):
|
|
319
|
+
final_text = str(data.get("content") or data.get("text") or final_text)
|
|
320
|
+
publish_update(force=True, status="writing")
|
|
321
|
+
publish_update(force=True)
|
|
322
|
+
finally:
|
|
323
|
+
upstream.close()
|
|
324
|
+
|
|
325
|
+
assistant = final_text.strip() or chat_stream._segments_to_reply_text(segments)
|
|
326
|
+
if not assistant:
|
|
327
|
+
raise ValueError("empty assistant response")
|
|
328
|
+
|
|
329
|
+
conn = _srv()._workframe_db()
|
|
330
|
+
try:
|
|
331
|
+
now_ts = str(int(time.time()))
|
|
332
|
+
mid = str(uuid.uuid4())
|
|
333
|
+
conn.execute(
|
|
334
|
+
"""
|
|
335
|
+
INSERT INTO messages (
|
|
336
|
+
id, room_id, sender_user_id, sender_agent_id, parent_message_id,
|
|
337
|
+
content, content_type, is_edited, created_at, updated_at
|
|
338
|
+
) VALUES (?,?,?,?,?,?,?,?,?,?)
|
|
339
|
+
""",
|
|
340
|
+
(
|
|
341
|
+
mid,
|
|
342
|
+
room_id,
|
|
343
|
+
None,
|
|
344
|
+
agent_db_id,
|
|
345
|
+
parent_message_id,
|
|
346
|
+
assistant,
|
|
347
|
+
"text",
|
|
348
|
+
0,
|
|
349
|
+
now_ts,
|
|
350
|
+
now_ts,
|
|
351
|
+
),
|
|
352
|
+
)
|
|
353
|
+
conn.execute("UPDATE rooms SET updated_at = ? WHERE id = ?", (now_ts, room_id))
|
|
354
|
+
conn.commit()
|
|
355
|
+
finally:
|
|
356
|
+
conn.close()
|
|
357
|
+
_srv()._bump_workspace_event_state()
|
|
358
|
+
run_completed_ok = True
|
|
359
|
+
finish_turn(mid)
|
|
360
|
+
_srv()._log_agent_run(
|
|
361
|
+
run_id,
|
|
362
|
+
agent_db_id,
|
|
363
|
+
room_id,
|
|
364
|
+
triggered_by_user_id,
|
|
365
|
+
session_id,
|
|
366
|
+
"completed",
|
|
367
|
+
provider,
|
|
368
|
+
str(agent_row.get("model_name") or "default"),
|
|
369
|
+
error=None,
|
|
370
|
+
)
|
|
371
|
+
lane_bindings.chat_dispatch(
|
|
372
|
+
{
|
|
373
|
+
"profile": hermes_slug,
|
|
374
|
+
"session_id": session_id,
|
|
375
|
+
"gateway_session_id": f"api:{hermes_slug}:{session_id}",
|
|
376
|
+
"room_id": room_id,
|
|
377
|
+
"user_id": triggered_by_user_id,
|
|
378
|
+
"source_id": "room",
|
|
379
|
+
"client_id": room_id,
|
|
380
|
+
"text": user_text[:240],
|
|
381
|
+
}
|
|
382
|
+
)
|
|
383
|
+
except Exception as exc: # noqa: BLE001
|
|
384
|
+
err_text = str(exc)
|
|
385
|
+
if "no_llm_provider_for_user" in err_text:
|
|
386
|
+
reply = (
|
|
387
|
+
"I can't reply yet — the member who @mentioned me needs to connect an LLM provider "
|
|
388
|
+
"(Profile → Connect accounts → OpenRouter or another model provider)."
|
|
389
|
+
)
|
|
390
|
+
else:
|
|
391
|
+
reply = err_text
|
|
392
|
+
try:
|
|
393
|
+
_srv()._log_agent_run(
|
|
394
|
+
run_id,
|
|
395
|
+
agent_db_id,
|
|
396
|
+
room_id,
|
|
397
|
+
triggered_by_user_id,
|
|
398
|
+
session_id,
|
|
399
|
+
"failed",
|
|
400
|
+
str(agent_row.get("model_provider") or "openrouter"),
|
|
401
|
+
str(agent_row.get("model_name") or "default"),
|
|
402
|
+
error=err_text,
|
|
403
|
+
)
|
|
404
|
+
except Exception: # noqa: BLE001
|
|
405
|
+
pass
|
|
406
|
+
fail_turn(reply)
|
|
407
|
+
finally:
|
|
408
|
+
_srv()._revoke_turn_credential_lease(run_id, hermes_slug)
|
|
409
|
+
if triggered_by_user_id and auth_decision and auth_decision.allowed:
|
|
410
|
+
try:
|
|
411
|
+
conn = _srv()._workframe_db()
|
|
412
|
+
try:
|
|
413
|
+
existing = run_ledger.get_run(conn, run_id)
|
|
414
|
+
if existing and existing.status == RunStatus.RUNNING:
|
|
415
|
+
if run_completed_ok:
|
|
416
|
+
run_ledger.complete_run(
|
|
417
|
+
conn,
|
|
418
|
+
run_id,
|
|
419
|
+
model=str(agent_row.get("model_name") or ""),
|
|
420
|
+
provider=provider,
|
|
421
|
+
funding_source=auth_decision.funding_source,
|
|
422
|
+
payer_user_id=auth_decision.payer_user_id or triggered_by_user_id,
|
|
423
|
+
receipt={"room_id": room_id, "session_id": session_id},
|
|
424
|
+
)
|
|
425
|
+
else:
|
|
426
|
+
run_ledger.fail_run(conn, run_id, reason="mention_failed")
|
|
427
|
+
conn.commit()
|
|
428
|
+
finally:
|
|
429
|
+
conn.close()
|
|
430
|
+
except Exception: # noqa: BLE001
|
|
431
|
+
pass
|