ltcai 1.0.1 → 1.2.0

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.
@@ -0,0 +1,748 @@
1
+ """Workspace OS + Organization Workspace API router.
2
+
3
+ Extracted from ``server_app.py`` in v1.2.0. Routes are unchanged (`/workspace`
4
+ and `/workspace/*`); request/response shapes are preserved. Permission
5
+ guardrails for workspace-scoped reads/writes are centralized in
6
+ :class:`latticeai.services.workspace_service.WorkspaceService`.
7
+
8
+ The factory mirrors the existing ``create_auth_router`` / ``create_admin_router``
9
+ convention: server_app constructs the dependency callables/objects and passes
10
+ them in, so this module never imports the FastAPI app (no import cycle).
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import asyncio
16
+ import logging
17
+ from datetime import datetime
18
+ from pathlib import Path
19
+ from typing import Any, Callable, Dict, List, Optional
20
+
21
+ from fastapi import APIRouter, HTTPException, Request
22
+ from pydantic import BaseModel
23
+
24
+
25
+ # ── Request models (workspace-only; moved verbatim from server_app) ──────────
26
+
27
+ class WorkspaceOnboardingStepRequest(BaseModel):
28
+ step: str
29
+ status: str = "complete"
30
+ data: Dict = {}
31
+ error: str = ""
32
+
33
+
34
+ class WorkspaceOnboardingCompleteRequest(BaseModel):
35
+ data: Dict = {}
36
+
37
+
38
+ class WorkspaceSnapshotRequest(BaseModel):
39
+ name: str = "Workspace snapshot"
40
+
41
+
42
+ class WorkspaceSnapshotCompareRequest(BaseModel):
43
+ before_id: str
44
+ after_id: str
45
+
46
+
47
+ class WorkspaceMemoryRequest(BaseModel):
48
+ kind: str
49
+ content: str
50
+ tags: List[str] = []
51
+ memory_id: Optional[str] = None
52
+ metadata: Dict = {}
53
+
54
+
55
+ class WorkspaceAgentRunRequest(BaseModel):
56
+ agent_id: str = "agent:executor"
57
+ status: str = "ok"
58
+ input: str = ""
59
+ output: str = ""
60
+ timeline: List[Dict] = []
61
+ relationships: List[str] = []
62
+
63
+
64
+ class WorkspaceWorkflowRequest(BaseModel):
65
+ name: str
66
+ steps: List[Dict] = []
67
+ metadata: Dict = {}
68
+
69
+
70
+ class WorkspaceWorkflowEventRequest(BaseModel):
71
+ event_type: str
72
+ payload: Dict = {}
73
+
74
+
75
+ class WorkspaceComputerMemoryRequest(BaseModel):
76
+ enabled: bool = False
77
+ consent: Dict = {}
78
+ scopes: List[str] = []
79
+
80
+
81
+ class WorkspaceComputerActivityRequest(BaseModel):
82
+ activity: Dict = {}
83
+
84
+
85
+ class WorkspaceSkillActionRequest(BaseModel):
86
+ skill: str
87
+ plugin: Optional[str] = None
88
+ enabled: Optional[bool] = None
89
+ version: Optional[str] = None
90
+ metadata: Dict = {}
91
+
92
+
93
+ class WorkspaceVSCodeRequest(BaseModel):
94
+ action: str
95
+ file_path: Optional[str] = None
96
+ language: Optional[str] = None
97
+ content: str = ""
98
+ selection: str = ""
99
+ prompt: str = ""
100
+
101
+
102
+ class WorkspaceCreateRequest(BaseModel):
103
+ name: str
104
+ settings: Dict = {}
105
+
106
+
107
+ class WorkspaceUpdateRequest(BaseModel):
108
+ name: Optional[str] = None
109
+ settings: Optional[Dict] = None
110
+
111
+
112
+ class WorkspaceMemberRequest(BaseModel):
113
+ user_id: str
114
+ role: str = "member"
115
+
116
+
117
+ class WorkspaceMemberRoleRequest(BaseModel):
118
+ role: str
119
+
120
+
121
+ class WorkspaceActivateRequest(BaseModel):
122
+ workspace_id: str
123
+
124
+
125
+ def _workspace_scope_from_request(request: Request) -> Optional[str]:
126
+ """Resolve a requested workspace id from header/query, or None.
127
+
128
+ ``None`` lets the service fall back to the active workspace (Personal by
129
+ default), preserving pre-1.1 behaviour for clients that send no header.
130
+ """
131
+ header = request.headers.get("X-Workspace-Id")
132
+ if header and header.strip():
133
+ return header.strip()
134
+ query = request.query_params.get("workspace_id")
135
+ return query.strip() if query and query.strip() else None
136
+
137
+
138
+ def create_workspace_router(
139
+ *,
140
+ service,
141
+ require_user: Callable[[Request], str],
142
+ require_admin: Callable[[Request], Any],
143
+ get_current_user: Callable[[Request], Optional[str]],
144
+ append_audit_event: Callable[..., None],
145
+ graph_stats: Callable[[], Dict],
146
+ workspace_models: Callable[[], Dict],
147
+ workspace_settings: Callable[[], Dict],
148
+ get_history: Callable[[], List[Dict]],
149
+ get_audit_log: Callable[[], List[Dict]],
150
+ require_graph: Callable[[], Any],
151
+ workspace_graph: Callable[[], Any],
152
+ knowledge_graph: Any,
153
+ local_kg_watcher: Any,
154
+ load_users: Callable[[], Dict],
155
+ scan_environment: Callable[[], Any],
156
+ local_sysinfo: Callable[[Request], Any],
157
+ get_recommendations: Callable[[Any], Any],
158
+ fetch_skills_marketplace: Callable[..., Any],
159
+ install_skill: Callable[..., Any],
160
+ remove_skill_directory: Callable[..., Dict],
161
+ redact_secret_text: Callable[[str], str],
162
+ skills_dir: Path,
163
+ capability_registry: Any,
164
+ ui_file_response: Callable[[Path], Any],
165
+ static_dir: Path,
166
+ local_model: Optional[str],
167
+ public_model: Optional[str],
168
+ ) -> APIRouter:
169
+ router = APIRouter()
170
+
171
+ # Bind injected deps to the names the moved handler bodies expect.
172
+ svc = service
173
+ WORKSPACE_OS = service.store
174
+ _workspace_graph = workspace_graph
175
+ _graph_stats_safe = graph_stats
176
+ _workspace_models_payload = workspace_models
177
+ _workspace_settings_payload = workspace_settings
178
+ _require_graph = require_graph
179
+ KNOWLEDGE_GRAPH = knowledge_graph
180
+ LOCAL_KG_WATCHER = local_kg_watcher
181
+ SKILLS_DIR = skills_dir
182
+ STATIC_DIR = static_dir
183
+ LOCAL_MODEL = local_model
184
+ PUBLIC_MODEL = public_model
185
+ _fetch_skills_marketplace = fetch_skills_marketplace
186
+ _workspace_scope = _workspace_scope_from_request
187
+
188
+ def _gate_read(request: Request):
189
+ try:
190
+ return svc.resolve_read_scope(_workspace_scope(request), get_current_user(request))
191
+ except PermissionError as exc:
192
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
193
+
194
+ def _gate_write(request: Request):
195
+ try:
196
+ return svc.resolve_write_scope(_workspace_scope(request), get_current_user(request))
197
+ except PermissionError as exc:
198
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
199
+
200
+ # ── Workspace UI pages ────────────────────────────────────────────────
201
+
202
+ @router.get("/workspace")
203
+ async def workspace_page(request: Request):
204
+ require_user(request)
205
+ workspace_path = STATIC_DIR / "workspace.html"
206
+ if not workspace_path.exists():
207
+ raise HTTPException(status_code=404, detail="Workspace OS UI not found.")
208
+ return ui_file_response(workspace_path)
209
+
210
+ @router.get("/onboarding")
211
+ async def onboarding_page(request: Request):
212
+ require_user(request)
213
+ workspace_path = STATIC_DIR / "workspace.html"
214
+ if not workspace_path.exists():
215
+ raise HTTPException(status_code=404, detail="Workspace OS UI not found.")
216
+ return ui_file_response(workspace_path)
217
+
218
+ # ── Workspace OS summary / onboarding ─────────────────────────────────
219
+
220
+ @router.get("/workspace/os")
221
+ async def workspace_os_summary(request: Request):
222
+ user = require_user(request)
223
+ summary = svc.summary(user or None)
224
+ summary["graph"] = _graph_stats_safe()
225
+ summary["models"] = _workspace_models_payload()
226
+ summary["edition"] = capability_registry.describe()
227
+ return summary
228
+
229
+ @router.get("/workspace/onboarding/status")
230
+ async def workspace_onboarding_status(request: Request):
231
+ require_user(request)
232
+ return WORKSPACE_OS.onboarding_status(load_users(), _graph_stats_safe())
233
+
234
+ @router.post("/workspace/onboarding/step")
235
+ async def workspace_onboarding_step(req: WorkspaceOnboardingStepRequest, request: Request):
236
+ current_user = require_user(request)
237
+ return WORKSPACE_OS.update_onboarding_step(
238
+ req.step,
239
+ status=req.status,
240
+ data=req.data,
241
+ error=req.error,
242
+ user_email=current_user or None,
243
+ )
244
+
245
+ @router.post("/workspace/onboarding/complete")
246
+ async def workspace_onboarding_complete(req: WorkspaceOnboardingCompleteRequest, request: Request):
247
+ current_user = require_user(request)
248
+ append_audit_event("onboarding_complete", user_email=current_user, platform="AI Workspace OS")
249
+ return WORKSPACE_OS.complete_onboarding(req.data, user_email=current_user or None)
250
+
251
+ @router.get("/workspace/onboarding/hardware")
252
+ async def workspace_onboarding_hardware(request: Request):
253
+ require_user(request)
254
+ env = await asyncio.to_thread(scan_environment)
255
+ sysinfo = await local_sysinfo(request)
256
+ payload = {"environment": env, "sysinfo": sysinfo, "scanned_at": datetime.now().isoformat()}
257
+ WORKSPACE_OS.update_onboarding_step("hardware", status="complete", data=payload, user_email=get_current_user(request))
258
+ return payload
259
+
260
+ @router.get("/workspace/onboarding/model-recommendations")
261
+ async def workspace_onboarding_model_recommendations(request: Request):
262
+ require_user(request)
263
+ env = await asyncio.to_thread(scan_environment)
264
+ recommendations = get_recommendations(env)
265
+ payload = {
266
+ "environment": env,
267
+ "recommendations": recommendations,
268
+ "default_local_model": LOCAL_MODEL,
269
+ "default_public_model": PUBLIC_MODEL,
270
+ }
271
+ WORKSPACE_OS.update_onboarding_step("model_recommendation", status="complete", data=payload, user_email=get_current_user(request))
272
+ return payload
273
+
274
+ # ── Graph traces ──────────────────────────────────────────────────────
275
+
276
+ @router.get("/workspace/traces")
277
+ async def workspace_traces(request: Request, conversation_id: Optional[str] = None, limit: int = 50):
278
+ require_user(request)
279
+ scope = _gate_read(request)
280
+ return WORKSPACE_OS.list_traces(conversation_id=conversation_id, limit=limit, workspace_id=scope)
281
+
282
+ # ── Local indexing dashboard (graph is machine-global shared state) ───
283
+
284
+ @router.get("/workspace/indexing")
285
+ async def workspace_indexing_dashboard(request: Request):
286
+ require_user(request)
287
+ graph = _workspace_graph()
288
+ watcher_status = LOCAL_KG_WATCHER.status() if LOCAL_KG_WATCHER else {"available": False, "active": {}}
289
+ return WORKSPACE_OS.build_indexing_dashboard(graph, watcher_status)
290
+
291
+ @router.post("/workspace/indexing/{source_id}/pause")
292
+ async def workspace_indexing_pause(source_id: str, request: Request):
293
+ require_user(request)
294
+ _require_graph()
295
+ return WORKSPACE_OS.pause_indexing(KNOWLEDGE_GRAPH, source_id, LOCAL_KG_WATCHER)
296
+
297
+ @router.post("/workspace/indexing/{source_id}/resume")
298
+ async def workspace_indexing_resume(source_id: str, request: Request):
299
+ require_user(request)
300
+ _require_graph()
301
+ return WORKSPACE_OS.resume_indexing(KNOWLEDGE_GRAPH, source_id, LOCAL_KG_WATCHER)
302
+
303
+ @router.post("/workspace/indexing/{source_id}/remove")
304
+ async def workspace_indexing_remove(source_id: str, request: Request):
305
+ require_user(request)
306
+ _require_graph()
307
+ return WORKSPACE_OS.remove_index_source(KNOWLEDGE_GRAPH, source_id, LOCAL_KG_WATCHER)
308
+
309
+ # ── Snapshots / Time Machine / Knowledge Diff ─────────────────────────
310
+
311
+ @router.get("/workspace/snapshots")
312
+ async def workspace_snapshots(request: Request):
313
+ require_user(request)
314
+ scope = _gate_read(request)
315
+ return WORKSPACE_OS.list_snapshots(workspace_id=scope)
316
+
317
+ @router.post("/workspace/snapshots")
318
+ async def workspace_snapshot_create(req: WorkspaceSnapshotRequest, request: Request):
319
+ current_user = require_user(request)
320
+ scope = _gate_write(request)
321
+ result = WORKSPACE_OS.create_snapshot(
322
+ name=req.name,
323
+ graph=_workspace_graph(),
324
+ history=get_history(),
325
+ settings=_workspace_settings_payload(),
326
+ models=_workspace_models_payload(),
327
+ workspace_id=scope,
328
+ )
329
+ append_audit_event("workspace_snapshot", user_email=current_user, snapshot_id=result["snapshot"]["id"])
330
+ return result
331
+
332
+ @router.post("/workspace/snapshots/compare")
333
+ async def workspace_snapshot_compare(req: WorkspaceSnapshotCompareRequest, request: Request):
334
+ require_user(request)
335
+ try:
336
+ return WORKSPACE_OS.compare_snapshots(req.before_id, req.after_id)
337
+ except FileNotFoundError as exc:
338
+ raise HTTPException(status_code=404, detail=f"Snapshot not found: {exc}") from exc
339
+
340
+ @router.get("/workspace/snapshots/{snapshot_id}")
341
+ async def workspace_snapshot_get(snapshot_id: str, request: Request):
342
+ require_user(request)
343
+ try:
344
+ return WORKSPACE_OS.get_snapshot(snapshot_id)
345
+ except FileNotFoundError as exc:
346
+ raise HTTPException(status_code=404, detail=f"Snapshot not found: {exc}") from exc
347
+
348
+ @router.get("/workspace/snapshots/{snapshot_id}/{area}")
349
+ async def workspace_snapshot_area(snapshot_id: str, area: str, request: Request):
350
+ require_user(request)
351
+ try:
352
+ return WORKSPACE_OS.snapshot_view(snapshot_id, area)
353
+ except FileNotFoundError as exc:
354
+ raise HTTPException(status_code=404, detail=f"Snapshot not found: {exc}") from exc
355
+
356
+ @router.post("/workspace/snapshots/{snapshot_id}/export")
357
+ async def workspace_snapshot_export(snapshot_id: str, request: Request):
358
+ current_user = require_user(request)
359
+ try:
360
+ result = WORKSPACE_OS.export_snapshot(snapshot_id)
361
+ except FileNotFoundError as exc:
362
+ raise HTTPException(status_code=404, detail=f"Snapshot not found: {exc}") from exc
363
+ append_audit_event("workspace_snapshot_export", user_email=current_user, snapshot_id=snapshot_id, path=result.get("export_path"))
364
+ return result
365
+
366
+ @router.get("/workspace/time-machine")
367
+ async def workspace_time_machine(request: Request, limit: int = 100):
368
+ require_user(request)
369
+ scope = _gate_read(request)
370
+ return WORKSPACE_OS.timeline(get_audit_log(), limit=limit, workspace_id=scope)
371
+
372
+ @router.get("/workspace/time-machine/{snapshot_id}/{area}")
373
+ async def workspace_time_machine_view(snapshot_id: str, area: str, request: Request):
374
+ require_user(request)
375
+ try:
376
+ return WORKSPACE_OS.snapshot_view(snapshot_id, area)
377
+ except FileNotFoundError as exc:
378
+ raise HTTPException(status_code=404, detail=f"Snapshot not found: {exc}") from exc
379
+
380
+ # ── Personal memory ───────────────────────────────────────────────────
381
+
382
+ @router.get("/workspace/memories")
383
+ async def workspace_memories(request: Request, kind: Optional[str] = None):
384
+ current_user = require_user(request)
385
+ scope = _gate_read(request)
386
+ return WORKSPACE_OS.list_memories(user_email=current_user or None, kind=kind, workspace_id=scope)
387
+
388
+ @router.get("/workspace/memories/search")
389
+ async def workspace_memory_search(q: str, request: Request, limit: int = 20):
390
+ current_user = require_user(request)
391
+ scope = _gate_read(request)
392
+ return WORKSPACE_OS.search_memories(q, user_email=current_user or None, limit=limit, workspace_id=scope)
393
+
394
+ @router.post("/workspace/memories")
395
+ async def workspace_memory_upsert(req: WorkspaceMemoryRequest, request: Request):
396
+ current_user = require_user(request)
397
+ scope = _gate_write(request)
398
+ try:
399
+ record = WORKSPACE_OS.upsert_memory(
400
+ kind=req.kind,
401
+ content=req.content,
402
+ tags=req.tags,
403
+ memory_id=req.memory_id,
404
+ metadata=req.metadata,
405
+ user_email=current_user or None,
406
+ graph=_workspace_graph(),
407
+ workspace_id=scope,
408
+ )
409
+ except ValueError as exc:
410
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
411
+ return {"memory": record}
412
+
413
+ @router.delete("/workspace/memories/{memory_id}")
414
+ async def workspace_memory_delete(memory_id: str, request: Request):
415
+ require_user(request)
416
+ try:
417
+ return WORKSPACE_OS.delete_memory(memory_id)
418
+ except FileNotFoundError as exc:
419
+ raise HTTPException(status_code=404, detail=f"Memory not found: {exc}") from exc
420
+
421
+ # ── Agents & workflows ────────────────────────────────────────────────
422
+
423
+ @router.get("/workspace/agents")
424
+ async def workspace_agents(request: Request):
425
+ require_user(request)
426
+ scope = _gate_read(request)
427
+ return WORKSPACE_OS.list_agents(workspace_id=scope)
428
+
429
+ @router.post("/workspace/agents/runs")
430
+ async def workspace_agent_run(req: WorkspaceAgentRunRequest, request: Request):
431
+ current_user = require_user(request)
432
+ scope = _gate_write(request)
433
+ run = WORKSPACE_OS.record_agent_run(
434
+ agent_id=req.agent_id,
435
+ status=req.status,
436
+ input_text=req.input,
437
+ output_text=req.output,
438
+ timeline=req.timeline,
439
+ relationships=req.relationships,
440
+ user_email=current_user or None,
441
+ graph=_workspace_graph(),
442
+ workspace_id=scope,
443
+ )
444
+ return {"run": run}
445
+
446
+ @router.get("/workspace/relationships/{node_id:path}")
447
+ async def workspace_relationships(node_id: str, request: Request, target_id: Optional[str] = None):
448
+ require_user(request)
449
+ _require_graph()
450
+ return WORKSPACE_OS.relationship_explorer(KNOWLEDGE_GRAPH, node_id, target_id=target_id)
451
+
452
+ # ── Local computer memory ─────────────────────────────────────────────
453
+
454
+ @router.get("/workspace/computer-memory")
455
+ async def workspace_computer_memory(request: Request):
456
+ require_user(request)
457
+ return WORKSPACE_OS.load_state().get("computer_memory")
458
+
459
+ @router.post("/workspace/computer-memory")
460
+ async def workspace_computer_memory_config(req: WorkspaceComputerMemoryRequest, request: Request):
461
+ current_user = require_user(request)
462
+ try:
463
+ config = WORKSPACE_OS.configure_computer_memory(
464
+ enabled=req.enabled,
465
+ approved_by=current_user or None,
466
+ consent=req.consent,
467
+ scopes=req.scopes or None,
468
+ )
469
+ except PermissionError as exc:
470
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
471
+ append_audit_event("computer_memory_config", user_email=current_user, enabled=req.enabled)
472
+ return {"computer_memory": config}
473
+
474
+ @router.post("/workspace/computer-memory/activity")
475
+ async def workspace_computer_memory_activity(req: WorkspaceComputerActivityRequest, request: Request):
476
+ require_user(request)
477
+ return WORKSPACE_OS.record_computer_activity(req.activity, graph=_workspace_graph())
478
+
479
+ # ── Workflows ─────────────────────────────────────────────────────────
480
+
481
+ @router.get("/workspace/workflows")
482
+ async def workspace_workflows(request: Request, q: str = ""):
483
+ require_user(request)
484
+ scope = _gate_read(request)
485
+ return WORKSPACE_OS.list_workflows(query=q, workspace_id=scope)
486
+
487
+ @router.post("/workspace/workflows")
488
+ async def workspace_workflow_create(req: WorkspaceWorkflowRequest, request: Request):
489
+ current_user = require_user(request)
490
+ scope = _gate_write(request)
491
+ workflow = WORKSPACE_OS.create_workflow(
492
+ name=req.name,
493
+ steps=req.steps,
494
+ metadata=req.metadata,
495
+ user_email=current_user or None,
496
+ graph=_workspace_graph(),
497
+ workspace_id=scope,
498
+ )
499
+ return {"workflow": workflow}
500
+
501
+ @router.post("/workspace/workflows/{workflow_id}/events")
502
+ async def workspace_workflow_event(workflow_id: str, req: WorkspaceWorkflowEventRequest, request: Request):
503
+ require_user(request)
504
+ try:
505
+ return {"workflow": WORKSPACE_OS.record_workflow_event(workflow_id, req.event_type, req.payload)}
506
+ except FileNotFoundError as exc:
507
+ raise HTTPException(status_code=404, detail=f"Workflow not found: {exc}") from exc
508
+
509
+ # ── Skills (installed skills are machine-global shared state) ─────────
510
+
511
+ @router.get("/workspace/skills")
512
+ async def workspace_skills(request: Request):
513
+ require_user(request)
514
+ marketplace = []
515
+ try:
516
+ marketplace = await _fetch_skills_marketplace()
517
+ except Exception as exc:
518
+ logging.warning("workspace skills marketplace unavailable: %s", exc)
519
+ return WORKSPACE_OS.list_skill_registry(SKILLS_DIR, marketplace)
520
+
521
+ @router.post("/workspace/skills/install")
522
+ async def workspace_skill_install(req: WorkspaceSkillActionRequest, request: Request):
523
+ admin_email, _ = require_admin(request)
524
+ if req.plugin:
525
+ result = await install_skill(req.plugin, req.skill)
526
+ else:
527
+ result = {"status": "recorded", "skill": req.skill}
528
+ entry = WORKSPACE_OS.mark_skill_installed(req.skill, version=req.version or "local", metadata={"install_result": result, **req.metadata})
529
+ append_audit_event("skill_install", user_email=admin_email, plugin=req.plugin, skill=req.skill, workspace_os=True)
530
+ return {"skill": entry, "install": result}
531
+
532
+ @router.post("/workspace/skills/uninstall")
533
+ async def workspace_skill_uninstall(req: WorkspaceSkillActionRequest, request: Request):
534
+ admin_email, _ = require_admin(request)
535
+ removal = remove_skill_directory(SKILLS_DIR, req.skill)
536
+ entry = WORKSPACE_OS.mark_skill_uninstalled(req.skill)
537
+ append_audit_event("skill_uninstall", user_email=admin_email, skill=req.skill, workspace_os=True)
538
+ return {"skill": entry, "removal": removal}
539
+
540
+ @router.post("/workspace/skills/enable")
541
+ async def workspace_skill_enable(req: WorkspaceSkillActionRequest, request: Request):
542
+ require_user(request)
543
+ return {"skill": WORKSPACE_OS.set_skill_enabled(req.skill, True)}
544
+
545
+ @router.post("/workspace/skills/disable")
546
+ async def workspace_skill_disable(req: WorkspaceSkillActionRequest, request: Request):
547
+ require_user(request)
548
+ return {"skill": WORKSPACE_OS.set_skill_enabled(req.skill, False)}
549
+
550
+ @router.post("/workspace/skills/update")
551
+ async def workspace_skill_update(req: WorkspaceSkillActionRequest, request: Request):
552
+ admin_email, _ = require_admin(request)
553
+ if req.plugin:
554
+ result = await install_skill(req.plugin, req.skill)
555
+ else:
556
+ result = {"status": "version_recorded", "skill": req.skill}
557
+ entry = WORKSPACE_OS.mark_skill_installed(req.skill, version=req.version or "latest", metadata={"update_result": result, **req.metadata})
558
+ append_audit_event("skill_update", user_email=admin_email, plugin=req.plugin, skill=req.skill, workspace_os=True)
559
+ return {"skill": entry, "update": result}
560
+
561
+ # ── Audit timeline (admin only) ───────────────────────────────────────
562
+
563
+ @router.get("/workspace/audit-timeline")
564
+ async def workspace_audit_timeline(
565
+ request: Request,
566
+ user: Optional[str] = None,
567
+ event_type: Optional[str] = None,
568
+ model: Optional[str] = None,
569
+ since: Optional[str] = None,
570
+ until: Optional[str] = None,
571
+ limit: int = 100,
572
+ ):
573
+ require_admin(request)
574
+ return WORKSPACE_OS.filter_audit_timeline(
575
+ get_audit_log(),
576
+ user=user,
577
+ event_type=event_type,
578
+ model=model,
579
+ since=since,
580
+ until=until,
581
+ limit=limit,
582
+ )
583
+
584
+ # ── VS Code workflow bridge ───────────────────────────────────────────
585
+
586
+ @router.post("/workspace/vscode/send")
587
+ async def workspace_vscode_send(req: WorkspaceVSCodeRequest, request: Request):
588
+ current_user = require_user(request)
589
+ content = req.selection or req.content or req.prompt
590
+ workflow = WORKSPACE_OS.create_workflow(
591
+ name=f"VS Code: {req.action}",
592
+ steps=[
593
+ {"action": req.action, "file_path": req.file_path, "language": req.language},
594
+ {"action": "send_to_lattice", "chars": len(content or "")},
595
+ ],
596
+ metadata={
597
+ "file_path": req.file_path,
598
+ "language": req.language,
599
+ "content_preview": redact_secret_text(content or "")[:500],
600
+ },
601
+ user_email=current_user or None,
602
+ graph=_workspace_graph(),
603
+ )
604
+ if _workspace_graph() is not None and content:
605
+ try:
606
+ _workspace_graph().ingest_event(
607
+ "VSCodeWorkflow",
608
+ req.action,
609
+ user_email=current_user or None,
610
+ source="vscode",
611
+ metadata={
612
+ "file_path": req.file_path,
613
+ "language": req.language,
614
+ "chars": len(content),
615
+ "workflow_id": workflow["id"],
616
+ },
617
+ )
618
+ except Exception as exc:
619
+ logging.warning("vscode workflow graph ingest failed: %s", exc)
620
+ return {"status": "ok", "workflow": workflow}
621
+
622
+ # ── Organization Workspaces, membership, roles, and edition seam ──────
623
+
624
+ @router.get("/workspace/registry")
625
+ async def workspace_registry(request: Request):
626
+ user = require_user(request)
627
+ return svc.list_workspaces(user or None)
628
+
629
+ @router.get("/workspace/editions")
630
+ async def workspace_editions(request: Request):
631
+ require_user(request)
632
+ return capability_registry.describe()
633
+
634
+ @router.post("/workspace/activate")
635
+ async def workspace_activate(req: WorkspaceActivateRequest, request: Request):
636
+ user = require_user(request)
637
+ try:
638
+ return svc.set_active_workspace(req.workspace_id, user or None)
639
+ except FileNotFoundError as exc:
640
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
641
+ except PermissionError as exc:
642
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
643
+
644
+ @router.post("/workspace/orgs")
645
+ async def workspace_org_create(req: WorkspaceCreateRequest, request: Request):
646
+ user = require_user(request)
647
+ try:
648
+ workspace = svc.create_organization_workspace(
649
+ name=req.name,
650
+ owner_user_id=user or None,
651
+ settings=req.settings,
652
+ )
653
+ except ValueError as exc:
654
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
655
+ append_audit_event("workspace_created", user_email=user, workspace_id=workspace["workspace_id"])
656
+ return {"workspace": workspace}
657
+
658
+ @router.get("/workspace/orgs/{workspace_id}")
659
+ async def workspace_org_get(workspace_id: str, request: Request):
660
+ user = require_user(request)
661
+ try:
662
+ return {"workspace": svc.get_workspace(workspace_id, user or None)}
663
+ except FileNotFoundError as exc:
664
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
665
+ except PermissionError as exc:
666
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
667
+
668
+ @router.get("/workspace/orgs/{workspace_id}/summary")
669
+ async def workspace_org_summary(workspace_id: str, request: Request):
670
+ user = require_user(request)
671
+ try:
672
+ return svc.workspace_summary(workspace_id, user or None)
673
+ except FileNotFoundError as exc:
674
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
675
+ except PermissionError as exc:
676
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
677
+
678
+ @router.patch("/workspace/orgs/{workspace_id}")
679
+ async def workspace_org_update(workspace_id: str, req: WorkspaceUpdateRequest, request: Request):
680
+ user = require_user(request)
681
+ try:
682
+ workspace = svc.update_workspace(workspace_id, name=req.name, settings=req.settings, actor=user or None)
683
+ except FileNotFoundError as exc:
684
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
685
+ except PermissionError as exc:
686
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
687
+ except ValueError as exc:
688
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
689
+ append_audit_event("workspace_updated", user_email=user, workspace_id=workspace_id)
690
+ return {"workspace": workspace}
691
+
692
+ @router.post("/workspace/orgs/{workspace_id}/archive")
693
+ async def workspace_org_archive(workspace_id: str, request: Request):
694
+ user = require_user(request)
695
+ try:
696
+ workspace = svc.archive_workspace(workspace_id, actor=user or None)
697
+ except FileNotFoundError as exc:
698
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
699
+ except PermissionError as exc:
700
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
701
+ except ValueError as exc:
702
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
703
+ append_audit_event("workspace_archived", user_email=user, workspace_id=workspace_id)
704
+ return {"workspace": workspace}
705
+
706
+ @router.post("/workspace/orgs/{workspace_id}/members")
707
+ async def workspace_org_add_member(workspace_id: str, req: WorkspaceMemberRequest, request: Request):
708
+ user = require_user(request)
709
+ try:
710
+ workspace = svc.add_member(workspace_id, user_id=req.user_id, role=req.role, actor=user or None)
711
+ except FileNotFoundError as exc:
712
+ raise HTTPException(status_code=404, detail=f"Workspace not found: {exc}") from exc
713
+ except PermissionError as exc:
714
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
715
+ except ValueError as exc:
716
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
717
+ append_audit_event("workspace_member_added", user_email=user, workspace_id=workspace_id, member=req.user_id, role=req.role)
718
+ return {"workspace": workspace}
719
+
720
+ @router.patch("/workspace/orgs/{workspace_id}/members/{user_id}")
721
+ async def workspace_org_update_member(workspace_id: str, user_id: str, req: WorkspaceMemberRoleRequest, request: Request):
722
+ user = require_user(request)
723
+ try:
724
+ workspace = svc.update_member_role(workspace_id, user_id=user_id, role=req.role, actor=user or None)
725
+ except FileNotFoundError as exc:
726
+ raise HTTPException(status_code=404, detail=f"Not found: {exc}") from exc
727
+ except PermissionError as exc:
728
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
729
+ except ValueError as exc:
730
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
731
+ append_audit_event("workspace_member_role_updated", user_email=user, workspace_id=workspace_id, member=user_id, role=req.role)
732
+ return {"workspace": workspace}
733
+
734
+ @router.delete("/workspace/orgs/{workspace_id}/members/{user_id}")
735
+ async def workspace_org_remove_member(workspace_id: str, user_id: str, request: Request):
736
+ user = require_user(request)
737
+ try:
738
+ workspace = svc.remove_member(workspace_id, user_id=user_id, actor=user or None)
739
+ except FileNotFoundError as exc:
740
+ raise HTTPException(status_code=404, detail=f"Not found: {exc}") from exc
741
+ except PermissionError as exc:
742
+ raise HTTPException(status_code=403, detail=str(exc)) from exc
743
+ except ValueError as exc:
744
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
745
+ append_audit_event("workspace_member_removed", user_email=user, workspace_id=workspace_id, member=user_id)
746
+ return {"workspace": workspace}
747
+
748
+ return router