@myrialabs/clopen 0.4.8 → 0.4.9

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.
Files changed (101) hide show
  1. package/backend/artifacts/detect.ts +129 -0
  2. package/backend/artifacts/engine-map.ts +53 -0
  3. package/backend/artifacts/frontmatter.ts +68 -0
  4. package/backend/artifacts/generate.ts +105 -0
  5. package/backend/artifacts/index.ts +34 -0
  6. package/backend/artifacts/markers.ts +76 -0
  7. package/backend/artifacts/matrix.ts +247 -0
  8. package/backend/artifacts/slug.ts +35 -0
  9. package/backend/artifacts/sync.ts +130 -0
  10. package/backend/artifacts/types.ts +152 -0
  11. package/backend/auth/permissions.ts +51 -1
  12. package/backend/chat/stream-manager.ts +14 -1
  13. package/backend/commands/detect.ts +25 -0
  14. package/backend/commands/index.ts +9 -0
  15. package/backend/commands/service.ts +178 -0
  16. package/backend/commands/store.ts +45 -0
  17. package/backend/commands/sync.ts +94 -0
  18. package/backend/database/migrations/045_create_commands_table.ts +28 -0
  19. package/backend/database/migrations/046_create_subagents_table.ts +29 -0
  20. package/backend/database/migrations/047_create_instructions_table.ts +29 -0
  21. package/backend/database/migrations/048_add_mcp_tool_overrides.ts +23 -0
  22. package/backend/database/migrations/049_create_permission_sets_table.ts +50 -0
  23. package/backend/database/migrations/050_create_profiles_tables.ts +57 -0
  24. package/backend/database/migrations/051_add_profile_to_sessions.ts +20 -0
  25. package/backend/database/migrations/052_add_default_profile_to_projects.ts +20 -0
  26. package/backend/database/migrations/053_add_profile_to_permission_sets.ts +37 -0
  27. package/backend/database/migrations/054_drop_subagent_agent_type.ts +20 -0
  28. package/backend/database/migrations/055_artifact_model_tools_per_engine.ts +43 -0
  29. package/backend/database/migrations/index.ts +77 -0
  30. package/backend/database/queries/command-queries.ts +82 -0
  31. package/backend/database/queries/index.ts +12 -2
  32. package/backend/database/queries/instruction-queries.ts +62 -0
  33. package/backend/database/queries/mcp-server-queries.ts +25 -0
  34. package/backend/database/queries/permission-set-queries.ts +155 -0
  35. package/backend/database/queries/profile-queries.ts +118 -0
  36. package/backend/database/queries/project-queries.ts +14 -0
  37. package/backend/database/queries/session-queries.ts +10 -0
  38. package/backend/database/queries/subagent-queries.ts +92 -0
  39. package/backend/engine/adapters/claude/stream.ts +27 -3
  40. package/backend/engine/adapters/codex/stream.ts +46 -14
  41. package/backend/engine/adapters/copilot/stream.ts +58 -4
  42. package/backend/engine/adapters/opencode/message-converter.ts +22 -2
  43. package/backend/engine/adapters/opencode/server.ts +166 -210
  44. package/backend/engine/adapters/opencode/stream.ts +150 -24
  45. package/backend/engine/adapters/qwen/stream.ts +23 -3
  46. package/backend/engine/artifact-sync.ts +51 -0
  47. package/backend/engine/types.ts +6 -0
  48. package/backend/instructions/index.ts +8 -0
  49. package/backend/instructions/service.ts +63 -0
  50. package/backend/instructions/sync.ts +56 -0
  51. package/backend/mcp/external/config.ts +42 -20
  52. package/backend/mcp/external/proxy.ts +89 -5
  53. package/backend/mcp/external/tools.ts +88 -0
  54. package/backend/mcp/index.ts +38 -15
  55. package/backend/mcp/internal/config.ts +48 -11
  56. package/backend/mcp/internal/remote-server.ts +9 -1
  57. package/backend/permissions/index.ts +27 -0
  58. package/backend/permissions/materialize.ts +96 -0
  59. package/backend/permissions/resolve.test.ts +162 -0
  60. package/backend/permissions/resolve.ts +106 -0
  61. package/backend/permissions/service.ts +167 -0
  62. package/backend/profiles/index.ts +10 -0
  63. package/backend/profiles/service.ts +169 -0
  64. package/backend/skills/index.ts +1 -1
  65. package/backend/skills/sync.ts +88 -128
  66. package/backend/subagents/detect.ts +23 -0
  67. package/backend/subagents/index.ts +10 -0
  68. package/backend/subagents/service.ts +166 -0
  69. package/backend/subagents/store.ts +44 -0
  70. package/backend/subagents/sync.ts +144 -0
  71. package/backend/ws/artifacts/generate.ts +51 -0
  72. package/backend/ws/artifacts/index.ts +8 -0
  73. package/backend/ws/chat/stream.ts +33 -2
  74. package/backend/ws/commands/crud.ts +157 -0
  75. package/backend/ws/commands/index.ts +8 -0
  76. package/backend/ws/git/branch.ts +4 -2
  77. package/backend/ws/git/remote.ts +21 -11
  78. package/backend/ws/index.ts +17 -1
  79. package/backend/ws/instructions/crud.ts +53 -0
  80. package/backend/ws/instructions/index.ts +8 -0
  81. package/backend/ws/mcp/crud.ts +7 -1
  82. package/backend/ws/mcp/index.ts +4 -1
  83. package/backend/ws/mcp/tools.ts +112 -0
  84. package/backend/ws/permissions/crud.ts +72 -0
  85. package/backend/ws/permissions/index.ts +8 -0
  86. package/backend/ws/profiles/crud.ts +149 -0
  87. package/backend/ws/profiles/index.ts +9 -0
  88. package/backend/ws/subagents/crud.ts +136 -0
  89. package/backend/ws/subagents/index.ts +8 -0
  90. package/dist/assets/{index-jGo0X72U.js → index-BdRui-1z.js} +1 -1
  91. package/dist/assets/{index-B_F_OgoC.js → index-BjaIjmd4.js} +1 -1
  92. package/dist/assets/{main-BkCc3WEB.js → main-Cf8SUfQ1.js} +367 -315
  93. package/dist/assets/main-D4pdkiQK.css +1 -0
  94. package/dist/index.html +2 -2
  95. package/package.json +1 -1
  96. package/shared/constants/engine-tools.ts +56 -0
  97. package/shared/types/database/schema.ts +2 -0
  98. package/shared/types/stores/settings.ts +15 -0
  99. package/shared/types/unified/stream.ts +7 -0
  100. package/shared/utils/logger.ts +6 -0
  101. package/dist/assets/main-CWDwET4x.css +0 -1
@@ -24,7 +24,8 @@ import {
24
24
  startAuthorization,
25
25
  completeAuthorization,
26
26
  getValidAccessToken,
27
- parseMcpConfig
27
+ parseMcpConfig,
28
+ parseToolOverrides
28
29
  } from '$backend/mcp';
29
30
 
30
31
  const TRANSPORT_SCHEMA = t.Union([t.Literal('stdio'), t.Literal('http'), t.Literal('sse')]);
@@ -59,6 +60,9 @@ const INSTALLED_SERVER_SCHEMA = t.Object({
59
60
  url: t.Union([t.String(), t.Null()]),
60
61
  source: t.String(),
61
62
  enabled: t.Boolean(),
63
+ // Number of tools with a stored exposure restriction — drives a "N restricted"
64
+ // badge without a per-server upstream round-trip. The details live in mcp:tools.
65
+ restrictedToolCount: t.Number(),
62
66
  createdAt: t.String()
63
67
  });
64
68
 
@@ -71,6 +75,7 @@ function toDTO(row: McpServerRow) {
71
75
  try { env = JSON.parse(row.env); } catch { /* ignore */ }
72
76
  try { headers = JSON.parse(row.headers); } catch { /* ignore */ }
73
77
  try { configSchema = JSON.parse(row.config_schema); } catch { /* ignore */ }
78
+ const restrictedToolCount = Object.keys(parseToolOverrides(row.tool_overrides)).length;
74
79
  return {
75
80
  id: row.id,
76
81
  slug: row.slug,
@@ -88,6 +93,7 @@ function toDTO(row: McpServerRow) {
88
93
  url: row.url,
89
94
  source: row.source,
90
95
  enabled: row.is_enabled === 1,
96
+ restrictedToolCount,
91
97
  createdAt: row.created_at
92
98
  };
93
99
  }
@@ -4,6 +4,7 @@
4
4
  * Entry point for external (user-installed) MCP server management:
5
5
  * - mcp:catalog — browse the official MCP registry
6
6
  * - mcp:list / install / toggle / uninstall — manage installed servers
7
+ * - mcp:tools / set-tool-overrides / call-tool — per-tool control + inspector
7
8
  *
8
9
  * Internal custom-tool servers (backend/mcp/internal) are NOT managed here —
9
10
  * they are defined in code.
@@ -12,7 +13,9 @@
12
13
  import { createRouter } from '$shared/utils/ws-server';
13
14
  import { mcpCatalogHandler } from './catalog';
14
15
  import { mcpCrudHandler } from './crud';
16
+ import { mcpToolsHandler } from './tools';
15
17
 
16
18
  export const mcpRouter = createRouter()
17
19
  .merge(mcpCatalogHandler)
18
- .merge(mcpCrudHandler);
20
+ .merge(mcpCrudHandler)
21
+ .merge(mcpToolsHandler);
@@ -0,0 +1,112 @@
1
+ /**
2
+ * MCP Tool-level Control + Inspector Handlers
3
+ *
4
+ * Deepens the installed-server surface with per-tool control and a playground:
5
+ * - mcp:tools — list a server's live tools + their exposure state
6
+ * - mcp:set-tool-overrides — persist per-tool + per-engine enable state
7
+ * - mcp:call-tool — inspector: invoke one tool and return its result
8
+ *
9
+ * All three go through the SAME upstream introspection path the bridge uses
10
+ * (`backend/mcp/external/proxy.ts`), so what Settings shows matches what the
11
+ * engines receive. Mutations are admin-gated in `backend/auth/permissions.ts`.
12
+ */
13
+
14
+ import { t } from 'elysia';
15
+ import { createRouter } from '$shared/utils/ws-server';
16
+ import { debug } from '$shared/utils/logger';
17
+ import { mcpServerQueries, type McpToolOverrides } from '$backend/database/queries';
18
+ import {
19
+ listExternalServerTools,
20
+ callExternalServerTool,
21
+ parseToolOverrides,
22
+ resolveToolExposure,
23
+ pruneToolOverrides,
24
+ getValidAccessToken
25
+ } from '$backend/mcp';
26
+
27
+ /** A single per-tool override the client sends back (only restrictions matter). */
28
+ const TOOL_OVERRIDE_SCHEMA = t.Object({
29
+ enabled: t.Optional(t.Boolean()),
30
+ engines: t.Optional(t.Record(t.String(), t.Boolean()))
31
+ });
32
+
33
+ /**
34
+ * Load a non-internal server by id, or throw. External MCP introspection makes
35
+ * no sense for the in-process internal servers (their tools are code-defined).
36
+ */
37
+ function requireExternal(id: number) {
38
+ const row = mcpServerQueries.getById(id);
39
+ if (!row) throw new Error('MCP server not found');
40
+ if (row.source === 'internal') throw new Error('Built-in MCP servers cannot be inspected');
41
+ return row;
42
+ }
43
+
44
+ /** Refresh a near-expiry OAuth token so the live upstream connection authenticates. */
45
+ async function ensureFreshToken(id: number, hasOAuth: boolean): Promise<void> {
46
+ if (hasOAuth) await getValidAccessToken(id);
47
+ }
48
+
49
+ export const mcpToolsHandler = createRouter()
50
+ .http('mcp:tools', {
51
+ data: t.Object({ id: t.Number() }),
52
+ response: t.Object({
53
+ tools: t.Array(t.Object({
54
+ name: t.String(),
55
+ description: t.Optional(t.String()),
56
+ inputSchema: t.Any(),
57
+ // Effective exposure resolved from stored overrides (default: all on).
58
+ enabled: t.Boolean(),
59
+ engines: t.Record(t.String(), t.Boolean())
60
+ }))
61
+ })
62
+ }, async ({ data }) => {
63
+ debug.log('path', `mcp:tools ${data.id}`);
64
+ const row = requireExternal(data.id);
65
+ await ensureFreshToken(row.id, !!row.oauth);
66
+ const live = await listExternalServerTools(row.slug);
67
+ const overrides = parseToolOverrides(row.tool_overrides);
68
+ return {
69
+ tools: live.map(tool => {
70
+ const exposure = resolveToolExposure(overrides[tool.name]);
71
+ return {
72
+ name: tool.name,
73
+ description: tool.description,
74
+ inputSchema: tool.inputSchema,
75
+ enabled: exposure.enabled,
76
+ engines: exposure.engines
77
+ };
78
+ })
79
+ };
80
+ })
81
+ .http('mcp:set-tool-overrides', {
82
+ data: t.Object({
83
+ id: t.Number(),
84
+ overrides: t.Record(t.String(), TOOL_OVERRIDE_SCHEMA)
85
+ }),
86
+ response: t.Object({ success: t.Boolean() })
87
+ }, async ({ data }) => {
88
+ debug.log('path', `mcp:set-tool-overrides ${data.id}`);
89
+ const row = requireExternal(data.id);
90
+ // Prune no-op entries so the column only ever stores real restrictions.
91
+ const overrides = pruneToolOverrides(data.overrides as McpToolOverrides);
92
+ mcpServerQueries.updateToolOverrides(row.id, overrides);
93
+ debug.log('mcp', `🎛️ Updated tool exposure for external MCP server: ${row.slug} (${Object.keys(overrides).length} restricted)`);
94
+ return { success: true };
95
+ })
96
+ .http('mcp:call-tool', {
97
+ // Inspector / playground: invoke one tool with the given arguments and
98
+ // return its raw MCP result (including `isError` results, so the UI can
99
+ // render tool-reported failures). Connection failures throw.
100
+ data: t.Object({
101
+ id: t.Number(),
102
+ tool: t.String(),
103
+ args: t.Optional(t.Any())
104
+ }),
105
+ response: t.Object({ result: t.Any() })
106
+ }, async ({ data }) => {
107
+ debug.log('path', `mcp:call-tool ${data.id} → ${data.tool}`);
108
+ const row = requireExternal(data.id);
109
+ await ensureFreshToken(row.id, !!row.oauth);
110
+ const result = await callExternalServerTool(row.slug, data.tool, data.args ?? {});
111
+ return { result };
112
+ });
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Permissions Handlers (Settings → Permissions)
3
+ *
4
+ * - permissions:list — stored allow/deny sets (global + optional project)
5
+ * - permissions:inventory — tool inventory offered as allow/deny targets
6
+ * - permissions:save — upsert one (scope, project, engine) rule set
7
+ *
8
+ * Rules are enforced at each engine's runtime auto-approve hook (real on
9
+ * Claude/Qwen/Copilot; best-effort/unverified on OpenCode/Codex). Mutations are
10
+ * admin-gated in `backend/auth/permissions.ts`. Changes take effect on the next
11
+ * chat stream.
12
+ */
13
+
14
+ import { t } from 'elysia';
15
+ import { createRouter } from '$shared/utils/ws-server';
16
+ import { debug } from '$shared/utils/logger';
17
+ import { permissionService } from '$backend/permissions';
18
+ import type { PermissionScope } from '$backend/database/queries';
19
+ import type { EngineType } from '$shared/types/unified';
20
+
21
+ const PERMISSION_SET_SCHEMA = t.Object({
22
+ scope: t.Union([t.Literal('global'), t.Literal('project'), t.Literal('profile')]),
23
+ projectId: t.Union([t.String(), t.Null()]),
24
+ profileId: t.Union([t.Number(), t.Null()]),
25
+ engine: t.String(),
26
+ allow: t.Array(t.String()),
27
+ deny: t.Array(t.String())
28
+ });
29
+
30
+ const INVENTORY_SCHEMA = t.Object({
31
+ engines: t.Array(t.Object({
32
+ engine: t.String(),
33
+ builtin: t.Array(t.String()),
34
+ bestEffort: t.Boolean()
35
+ })),
36
+ mcp: t.Array(t.String()),
37
+ subagent: t.Array(t.String())
38
+ });
39
+
40
+ export const permissionsCrudHandler = createRouter()
41
+ .http('permissions:list', {
42
+ data: t.Object({ projectId: t.Optional(t.String()) }),
43
+ response: t.Object({ sets: t.Array(PERMISSION_SET_SCHEMA) })
44
+ }, async ({ data }) => {
45
+ debug.log('path', 'permissions:list');
46
+ return { sets: permissionService.list(data.projectId) };
47
+ })
48
+ .http('permissions:inventory', {
49
+ data: t.Object({}),
50
+ response: INVENTORY_SCHEMA
51
+ }, async () => {
52
+ debug.log('path', 'permissions:inventory');
53
+ return permissionService.inventory();
54
+ })
55
+ .http('permissions:save', {
56
+ data: t.Object({
57
+ scope: t.Union([t.Literal('global'), t.Literal('project')]),
58
+ projectId: t.Optional(t.Union([t.String(), t.Null()])),
59
+ engine: t.String(),
60
+ allow: t.Array(t.String()),
61
+ deny: t.Array(t.String())
62
+ }),
63
+ response: t.Object({ success: t.Boolean() })
64
+ }, async ({ data }) => {
65
+ debug.log('path', `permissions:save ${data.engine}/${data.scope}`);
66
+ const projectId = data.scope === 'project' ? (data.projectId ?? null) : null;
67
+ if (data.scope === 'project' && !projectId) throw new Error('A project is required for project-scoped permissions');
68
+ // This admin endpoint only manages global/project scope. Profile-scoped
69
+ // permission overlays are edited through the profiles editor (profiles:save-permissions).
70
+ permissionService.save(data.scope as PermissionScope, projectId, null, data.engine as EngineType, data.allow, data.deny);
71
+ return { success: true };
72
+ });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Permissions Router — Settings → Permissions (per-engine tool allow/deny).
3
+ */
4
+
5
+ import { createRouter } from '$shared/utils/ws-server';
6
+ import { permissionsCrudHandler } from './crud';
7
+
8
+ export const permissionsRouter = createRouter().merge(permissionsCrudHandler);
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Profiles Handlers (Settings → Profiles) + per-session picker + project default.
3
+ *
4
+ * - profiles:list / get / inventory / create / update / delete (admin)
5
+ * - profiles:get-permissions / save-permissions (admin, Fase 3)
6
+ * - profiles:set-project-default (admin)
7
+ * - profiles:available / project-default (non-admin)
8
+ *
9
+ * A Profile bundles references to existing artifacts (Skills, Commands,
10
+ * Subagents, MCP Connectors) by slug plus an optional per-engine allow/deny
11
+ * overlay. Nothing is duplicated — see `backend/profiles`. Admin gating lives in
12
+ * `backend/auth/permissions.ts`; the picker/default-read routes stay non-admin
13
+ * (choosing a profile for a session is a run choice like the model).
14
+ */
15
+
16
+ import { t } from 'elysia';
17
+ import { createRouter } from '$shared/utils/ws-server';
18
+ import { debug } from '$shared/utils/logger';
19
+ import { profileService } from '$backend/profiles';
20
+ import { permissionService } from '$backend/permissions';
21
+ import { projectQueries } from '$backend/database/queries';
22
+ import type { PermissionScope } from '$backend/database/queries';
23
+ import type { EngineType } from '$shared/types/unified';
24
+
25
+ const ITEMS_SCHEMA = t.Object({
26
+ skill: t.Array(t.String()),
27
+ command: t.Array(t.String()),
28
+ subagent: t.Array(t.String()),
29
+ mcp: t.Array(t.String())
30
+ });
31
+
32
+ const PROFILE_SCHEMA = t.Object({
33
+ id: t.Number(),
34
+ slug: t.String(),
35
+ name: t.String(),
36
+ description: t.String(),
37
+ items: ITEMS_SCHEMA,
38
+ itemCount: t.Number(),
39
+ createdAt: t.String(),
40
+ updatedAt: t.String()
41
+ });
42
+
43
+ const ITEM_INPUT_SCHEMA = t.Array(t.Object({
44
+ artifactType: t.Union([t.Literal('skill'), t.Literal('command'), t.Literal('subagent'), t.Literal('mcp')]),
45
+ ref: t.String()
46
+ }));
47
+
48
+ const INVENTORY_ENTRY = t.Array(t.Object({ slug: t.String(), name: t.String(), enabled: t.Boolean() }));
49
+
50
+ const PERMISSION_OVERLAY_SCHEMA = t.Object({
51
+ engine: t.String(),
52
+ allow: t.Array(t.String()),
53
+ deny: t.Array(t.String())
54
+ });
55
+
56
+ export const profilesCrudHandler = createRouter()
57
+ .http('profiles:list', {
58
+ data: t.Object({}),
59
+ response: t.Object({ profiles: t.Array(PROFILE_SCHEMA) })
60
+ }, () => {
61
+ debug.log('path', 'profiles:list');
62
+ return { profiles: profileService.list() };
63
+ })
64
+ .http('profiles:get', {
65
+ data: t.Object({ id: t.Number() }),
66
+ response: t.Object({ profile: PROFILE_SCHEMA })
67
+ }, ({ data }) => {
68
+ debug.log('path', `profiles:get ${data.id}`);
69
+ const profile = profileService.get(data.id);
70
+ if (!profile) throw new Error('Profile not found');
71
+ return { profile };
72
+ })
73
+ .http('profiles:inventory', {
74
+ data: t.Object({}),
75
+ response: t.Object({ skill: INVENTORY_ENTRY, command: INVENTORY_ENTRY, subagent: INVENTORY_ENTRY, mcp: INVENTORY_ENTRY })
76
+ }, () => {
77
+ debug.log('path', 'profiles:inventory');
78
+ return profileService.inventory();
79
+ })
80
+ .http('profiles:create', {
81
+ data: t.Object({ name: t.String(), description: t.String(), items: ITEM_INPUT_SCHEMA }),
82
+ response: t.Object({ profile: PROFILE_SCHEMA })
83
+ }, ({ data }) => {
84
+ debug.log('path', `profiles:create ${data.name}`);
85
+ if (!data.name.trim()) throw new Error('A profile name is required');
86
+ return { profile: profileService.create(data.name.trim(), data.description, data.items) };
87
+ })
88
+ .http('profiles:update', {
89
+ data: t.Object({ id: t.Number(), name: t.String(), description: t.String(), items: ITEM_INPUT_SCHEMA }),
90
+ response: t.Object({ profile: PROFILE_SCHEMA })
91
+ }, ({ data }) => {
92
+ debug.log('path', `profiles:update ${data.id}`);
93
+ if (!data.name.trim()) throw new Error('A profile name is required');
94
+ return { profile: profileService.update(data.id, data.name.trim(), data.description, data.items) };
95
+ })
96
+ .http('profiles:delete', {
97
+ data: t.Object({ id: t.Number() }),
98
+ response: t.Object({ success: t.Boolean() })
99
+ }, ({ data }) => {
100
+ debug.log('path', `profiles:delete ${data.id}`);
101
+ profileService.remove(data.id);
102
+ return { success: true };
103
+ })
104
+
105
+ // ── Per-profile permission overlay (Fase 3) ────────────────────────────────
106
+ .http('profiles:get-permissions', {
107
+ data: t.Object({ id: t.Number() }),
108
+ response: t.Object({ sets: t.Array(PERMISSION_OVERLAY_SCHEMA) })
109
+ }, ({ data }) => {
110
+ debug.log('path', `profiles:get-permissions ${data.id}`);
111
+ const sets = permissionService.listForProfile(data.id).map(s => ({ engine: s.engine, allow: s.allow, deny: s.deny }));
112
+ return { sets };
113
+ })
114
+ .http('profiles:save-permissions', {
115
+ data: t.Object({ id: t.Number(), engine: t.String(), allow: t.Array(t.String()), deny: t.Array(t.String()) }),
116
+ response: t.Object({ success: t.Boolean() })
117
+ }, ({ data }) => {
118
+ debug.log('path', `profiles:save-permissions ${data.id}/${data.engine}`);
119
+ permissionService.save('profile' as PermissionScope, null, data.id, data.engine as EngineType, data.allow, data.deny);
120
+ return { success: true };
121
+ })
122
+
123
+ // ── Project default (shared) ───────────────────────────────────────────────
124
+ .http('profiles:set-project-default', {
125
+ data: t.Object({ projectId: t.String(), profileId: t.Union([t.Number(), t.Null()]) }),
126
+ response: t.Object({ success: t.Boolean() })
127
+ }, ({ data }) => {
128
+ debug.log('path', `profiles:set-project-default ${data.projectId}`);
129
+ projectQueries.setDefaultProfileId(data.projectId, data.profileId);
130
+ return { success: true };
131
+ })
132
+
133
+ // ── Non-admin: per-session picker + default read ───────────────────────────
134
+ .http('profiles:available', {
135
+ data: t.Object({}),
136
+ response: t.Object({
137
+ profiles: t.Array(t.Object({ id: t.Number(), slug: t.String(), name: t.String(), description: t.String() }))
138
+ })
139
+ }, () => {
140
+ debug.log('path', 'profiles:available');
141
+ return { profiles: profileService.available() };
142
+ })
143
+ .http('profiles:project-default', {
144
+ data: t.Object({ projectId: t.String() }),
145
+ response: t.Object({ profileId: t.Union([t.Number(), t.Null()]) })
146
+ }, ({ data }) => {
147
+ debug.log('path', `profiles:project-default ${data.projectId}`);
148
+ return { profileId: projectQueries.getDefaultProfileId(data.projectId) };
149
+ });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Profiles Router — Settings → Profiles CRUD + per-session picker + project
3
+ * default + per-profile permission overlay.
4
+ */
5
+
6
+ import { createRouter } from '$shared/utils/ws-server';
7
+ import { profilesCrudHandler } from './crud';
8
+
9
+ export const profilesRouter = createRouter().merge(profilesCrudHandler);
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Subagent CRUD Handlers (Settings → Subagents)
3
+ *
4
+ * - subagents:list / get / create / update / parse-import / import / toggle / delete
5
+ * - subagents:detect — on-disk subagents (managed + adoptable) per engine
6
+ *
7
+ * Mutations are admin-gated. Changes take effect on the next chat stream.
8
+ */
9
+
10
+ import { t } from 'elysia';
11
+ import { createRouter } from '$shared/utils/ws-server';
12
+ import { debug } from '$shared/utils/logger';
13
+ import { subagentService, detectSubagents, syncSubagentsAllEngines } from '$backend/subagents';
14
+
15
+ /** EngineType → value map (model id / tool list). {} = inherit / all. */
16
+ const ENGINE_MAP_SCHEMA = t.Record(t.String(), t.String());
17
+
18
+ const SUBAGENT_SCHEMA = t.Object({
19
+ id: t.Number(),
20
+ slug: t.String(),
21
+ name: t.String(),
22
+ description: t.String(),
23
+ toolsByEngine: ENGINE_MAP_SCHEMA,
24
+ modelByEngine: ENGINE_MAP_SCHEMA,
25
+ source: t.Union([t.Literal('custom'), t.Literal('imported')]),
26
+ enabled: t.Boolean(),
27
+ present: t.Boolean(),
28
+ createdAt: t.String()
29
+ });
30
+
31
+ const FIELDS_SCHEMA = {
32
+ name: t.String(),
33
+ description: t.String(),
34
+ toolsByEngine: t.Optional(ENGINE_MAP_SCHEMA),
35
+ modelByEngine: t.Optional(ENGINE_MAP_SCHEMA),
36
+ body: t.String()
37
+ };
38
+
39
+ const DETECTED_SCHEMA = t.Object({
40
+ engine: t.String(),
41
+ detected: t.Array(t.Object({
42
+ slug: t.String(),
43
+ name: t.String(),
44
+ description: t.String(),
45
+ path: t.String(),
46
+ managed: t.Boolean(),
47
+ adoptable: t.Boolean()
48
+ }))
49
+ });
50
+
51
+ export const subagentCrudHandler = createRouter()
52
+ .http('subagents:list', {
53
+ data: t.Object({}),
54
+ response: t.Object({ subagents: t.Array(SUBAGENT_SCHEMA) })
55
+ }, async () => {
56
+ debug.log('path', 'subagents:list');
57
+ return { subagents: await subagentService.list() };
58
+ })
59
+ .http('subagents:get', {
60
+ data: t.Object({ id: t.Number() }),
61
+ response: t.Object({ subagent: SUBAGENT_SCHEMA, body: t.String() })
62
+ }, async ({ data }) => {
63
+ debug.log('path', `subagents:get ${data.id}`);
64
+ const result = await subagentService.get(data.id);
65
+ if (!result) throw new Error('Subagent not found');
66
+ return result;
67
+ })
68
+ .http('subagents:create', {
69
+ data: t.Object(FIELDS_SCHEMA),
70
+ response: t.Object({ subagent: SUBAGENT_SCHEMA })
71
+ }, async ({ data }) => {
72
+ debug.log('path', `subagents:create ${data.name}`);
73
+ if (!data.name.trim()) throw new Error('A subagent name is required');
74
+ if (!data.description.trim()) throw new Error('A subagent description is required');
75
+ const subagent = await subagentService.create(data);
76
+ await syncSubagentsAllEngines();
77
+ return { subagent };
78
+ })
79
+ .http('subagents:update', {
80
+ data: t.Object({ id: t.Number(), ...FIELDS_SCHEMA }),
81
+ response: t.Object({ subagent: SUBAGENT_SCHEMA })
82
+ }, async ({ data }) => {
83
+ debug.log('path', `subagents:update ${data.id}`);
84
+ if (!data.name.trim()) throw new Error('A subagent name is required');
85
+ if (!data.description.trim()) throw new Error('A subagent description is required');
86
+ const subagent = await subagentService.update(data.id, data);
87
+ await syncSubagentsAllEngines();
88
+ return { subagent };
89
+ })
90
+ .http('subagents:parse-import', {
91
+ data: t.Object({ text: t.String() }),
92
+ response: t.Object({
93
+ name: t.String(),
94
+ description: t.String(),
95
+ toolsByEngine: ENGINE_MAP_SCHEMA,
96
+ modelByEngine: ENGINE_MAP_SCHEMA,
97
+ body: t.String()
98
+ })
99
+ }, async ({ data }) => {
100
+ debug.log('path', 'subagents:parse-import');
101
+ return subagentService.parsePreview(data.text);
102
+ })
103
+ .http('subagents:import', {
104
+ data: t.Object({ text: t.String(), name: t.Optional(t.String()) }),
105
+ response: t.Object({ subagent: SUBAGENT_SCHEMA })
106
+ }, async ({ data }) => {
107
+ debug.log('path', 'subagents:import');
108
+ const subagent = await subagentService.import(data.text, data.name);
109
+ await syncSubagentsAllEngines();
110
+ return { subagent };
111
+ })
112
+ .http('subagents:toggle', {
113
+ data: t.Object({ id: t.Number(), enabled: t.Boolean() }),
114
+ response: t.Object({ subagent: SUBAGENT_SCHEMA })
115
+ }, async ({ data }) => {
116
+ debug.log('path', `subagents:toggle ${data.id} → ${data.enabled}`);
117
+ const subagent = subagentService.toggle(data.id, data.enabled);
118
+ await syncSubagentsAllEngines();
119
+ return { subagent };
120
+ })
121
+ .http('subagents:delete', {
122
+ data: t.Object({ id: t.Number() }),
123
+ response: t.Object({ success: t.Boolean() })
124
+ }, async ({ data }) => {
125
+ debug.log('path', `subagents:delete ${data.id}`);
126
+ await subagentService.remove(data.id);
127
+ await syncSubagentsAllEngines();
128
+ return { success: true };
129
+ })
130
+ .http('subagents:detect', {
131
+ data: t.Object({ projectPath: t.Optional(t.String()) }),
132
+ response: t.Object({ groups: t.Array(DETECTED_SCHEMA) })
133
+ }, async ({ data }) => {
134
+ debug.log('path', 'subagents:detect');
135
+ return { groups: await detectSubagents(data.projectPath) };
136
+ });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Subagents Router — Settings → Subagents CRUD + on-disk detection.
3
+ */
4
+
5
+ import { createRouter } from '$shared/utils/ws-server';
6
+ import { subagentCrudHandler } from './crud';
7
+
8
+ export const subagentsRouter = createRouter().merge(subagentCrudHandler);