@myrialabs/clopen 0.4.8 → 0.4.10
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 +10 -7
- package/backend/artifacts/detect.ts +129 -0
- package/backend/artifacts/engine-map.ts +53 -0
- package/backend/artifacts/frontmatter.ts +68 -0
- package/backend/artifacts/generate.ts +105 -0
- package/backend/artifacts/index.ts +34 -0
- package/backend/artifacts/markers.ts +76 -0
- package/backend/artifacts/matrix.ts +247 -0
- package/backend/artifacts/slug.ts +35 -0
- package/backend/artifacts/sync.ts +130 -0
- package/backend/artifacts/types.ts +152 -0
- package/backend/auth/permissions.ts +51 -1
- package/backend/chat/stream-manager.ts +14 -1
- package/backend/commands/detect.ts +25 -0
- package/backend/commands/index.ts +9 -0
- package/backend/commands/service.ts +178 -0
- package/backend/commands/store.ts +45 -0
- package/backend/commands/sync.ts +94 -0
- package/backend/database/migrations/045_create_commands_table.ts +28 -0
- package/backend/database/migrations/046_create_subagents_table.ts +29 -0
- package/backend/database/migrations/047_create_instructions_table.ts +29 -0
- package/backend/database/migrations/048_add_mcp_tool_overrides.ts +23 -0
- package/backend/database/migrations/049_create_permission_sets_table.ts +50 -0
- package/backend/database/migrations/050_create_profiles_tables.ts +57 -0
- package/backend/database/migrations/051_add_profile_to_sessions.ts +20 -0
- package/backend/database/migrations/052_add_default_profile_to_projects.ts +20 -0
- package/backend/database/migrations/053_add_profile_to_permission_sets.ts +37 -0
- package/backend/database/migrations/054_drop_subagent_agent_type.ts +20 -0
- package/backend/database/migrations/055_artifact_model_tools_per_engine.ts +43 -0
- package/backend/database/migrations/index.ts +77 -0
- package/backend/database/queries/command-queries.ts +82 -0
- package/backend/database/queries/index.ts +12 -2
- package/backend/database/queries/instruction-queries.ts +62 -0
- package/backend/database/queries/mcp-server-queries.ts +25 -0
- package/backend/database/queries/permission-set-queries.ts +155 -0
- package/backend/database/queries/profile-queries.ts +118 -0
- package/backend/database/queries/project-queries.ts +14 -0
- package/backend/database/queries/session-queries.ts +21 -0
- package/backend/database/queries/subagent-queries.ts +92 -0
- package/backend/engine/adapters/claude/stream.ts +27 -3
- package/backend/engine/adapters/codex/stream.ts +46 -14
- package/backend/engine/adapters/copilot/stream.ts +58 -4
- package/backend/engine/adapters/opencode/message-converter.ts +22 -2
- package/backend/engine/adapters/opencode/server.ts +166 -210
- package/backend/engine/adapters/opencode/stream.ts +150 -24
- package/backend/engine/adapters/qwen/stream.ts +23 -3
- package/backend/engine/artifact-sync.ts +51 -0
- package/backend/engine/types.ts +6 -0
- package/backend/instructions/index.ts +8 -0
- package/backend/instructions/service.ts +63 -0
- package/backend/instructions/sync.ts +56 -0
- package/backend/mcp/external/config.ts +42 -20
- package/backend/mcp/external/proxy.ts +89 -5
- package/backend/mcp/external/tools.ts +88 -0
- package/backend/mcp/index.ts +38 -15
- package/backend/mcp/internal/config.ts +48 -11
- package/backend/mcp/internal/remote-server.ts +9 -1
- package/backend/permissions/index.ts +27 -0
- package/backend/permissions/materialize.ts +96 -0
- package/backend/permissions/resolve.test.ts +162 -0
- package/backend/permissions/resolve.ts +106 -0
- package/backend/permissions/service.ts +167 -0
- package/backend/profiles/index.ts +10 -0
- package/backend/profiles/service.ts +169 -0
- package/backend/project/status-manager.ts +7 -25
- package/backend/skills/index.ts +1 -1
- package/backend/skills/sync.ts +88 -128
- package/backend/subagents/detect.ts +23 -0
- package/backend/subagents/index.ts +10 -0
- package/backend/subagents/service.ts +166 -0
- package/backend/subagents/store.ts +44 -0
- package/backend/subagents/sync.ts +144 -0
- package/backend/utils/static-curl.ts +12 -54
- package/backend/ws/artifacts/generate.ts +51 -0
- package/backend/ws/artifacts/index.ts +8 -0
- package/backend/ws/chat/stream.ts +33 -2
- package/backend/ws/commands/crud.ts +157 -0
- package/backend/ws/commands/index.ts +8 -0
- package/backend/ws/git/branch.ts +4 -2
- package/backend/ws/git/remote.ts +21 -11
- package/backend/ws/index.ts +17 -1
- package/backend/ws/instructions/crud.ts +53 -0
- package/backend/ws/instructions/index.ts +8 -0
- package/backend/ws/mcp/crud.ts +7 -1
- package/backend/ws/mcp/index.ts +4 -1
- package/backend/ws/mcp/tools.ts +112 -0
- package/backend/ws/permissions/crud.ts +72 -0
- package/backend/ws/permissions/index.ts +8 -0
- package/backend/ws/profiles/crud.ts +149 -0
- package/backend/ws/profiles/index.ts +9 -0
- package/backend/ws/sessions/crud.ts +12 -0
- package/backend/ws/subagents/crud.ts +136 -0
- package/backend/ws/subagents/index.ts +8 -0
- package/dist/assets/{index-jGo0X72U.js → index-C3kp8wpM.js} +1 -1
- package/dist/assets/{index-B_F_OgoC.js → index-cVUk7das.js} +1 -1
- package/dist/assets/main-CuCxUeqZ.js +533 -0
- package/dist/assets/main-D4pdkiQK.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +24 -24
- package/shared/constants/engine-tools.ts +56 -0
- package/shared/types/database/schema.ts +2 -0
- package/shared/types/stores/settings.ts +15 -0
- package/shared/types/unified/stream.ts +7 -0
- package/shared/utils/interactive-input.ts +71 -0
- package/shared/utils/logger.ts +6 -0
- package/dist/assets/main-BkCc3WEB.js +0 -481
- package/dist/assets/main-CWDwET4x.css +0 -1
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command CRUD Handlers (Settings → Commands)
|
|
3
|
+
*
|
|
4
|
+
* - commands:list / get / create / update / parse-import / import / toggle / delete
|
|
5
|
+
* - commands:detect — on-disk commands (managed + adoptable) per engine
|
|
6
|
+
*
|
|
7
|
+
* Mutations are admin-gated. Changes take effect on the next chat stream, when
|
|
8
|
+
* each engine re-syncs its commands.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { t } from 'elysia';
|
|
12
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
13
|
+
import { debug } from '$shared/utils/logger';
|
|
14
|
+
import { commandService, detectCommands, syncCommandsAllEngines } from '$backend/commands';
|
|
15
|
+
import { resolveActiveProfileId, artifactFilter } from '$backend/profiles';
|
|
16
|
+
|
|
17
|
+
/** EngineType → value map (model id / tool list). {} = inherit / all. */
|
|
18
|
+
const ENGINE_MAP_SCHEMA = t.Record(t.String(), t.String());
|
|
19
|
+
|
|
20
|
+
const COMMAND_SCHEMA = t.Object({
|
|
21
|
+
id: t.Number(),
|
|
22
|
+
slug: t.String(),
|
|
23
|
+
name: t.String(),
|
|
24
|
+
description: t.String(),
|
|
25
|
+
argumentHint: t.Union([t.String(), t.Null()]),
|
|
26
|
+
modelByEngine: ENGINE_MAP_SCHEMA,
|
|
27
|
+
source: t.Union([t.Literal('custom'), t.Literal('imported')]),
|
|
28
|
+
enabled: t.Boolean(),
|
|
29
|
+
present: t.Boolean(),
|
|
30
|
+
createdAt: t.String()
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const FIELDS_SCHEMA = {
|
|
34
|
+
name: t.String(),
|
|
35
|
+
description: t.String(),
|
|
36
|
+
argumentHint: t.Optional(t.Union([t.String(), t.Null()])),
|
|
37
|
+
modelByEngine: t.Optional(ENGINE_MAP_SCHEMA),
|
|
38
|
+
body: t.String()
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const DETECTED_SCHEMA = t.Object({
|
|
42
|
+
engine: t.String(),
|
|
43
|
+
detected: t.Array(t.Object({
|
|
44
|
+
slug: t.String(),
|
|
45
|
+
name: t.String(),
|
|
46
|
+
description: t.String(),
|
|
47
|
+
path: t.String(),
|
|
48
|
+
managed: t.Boolean(),
|
|
49
|
+
adoptable: t.Boolean()
|
|
50
|
+
}))
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const commandCrudHandler = createRouter()
|
|
54
|
+
.http('commands:list', {
|
|
55
|
+
data: t.Object({}),
|
|
56
|
+
response: t.Object({ commands: t.Array(COMMAND_SCHEMA) })
|
|
57
|
+
}, async () => {
|
|
58
|
+
debug.log('path', 'commands:list');
|
|
59
|
+
return { commands: await commandService.list() };
|
|
60
|
+
})
|
|
61
|
+
.http('commands:get', {
|
|
62
|
+
data: t.Object({ id: t.Number() }),
|
|
63
|
+
response: t.Object({ command: COMMAND_SCHEMA, body: t.String() })
|
|
64
|
+
}, async ({ data }) => {
|
|
65
|
+
debug.log('path', `commands:get ${data.id}`);
|
|
66
|
+
const result = await commandService.get(data.id);
|
|
67
|
+
if (!result) throw new Error('Command not found');
|
|
68
|
+
return result;
|
|
69
|
+
})
|
|
70
|
+
.http('commands:create', {
|
|
71
|
+
data: t.Object(FIELDS_SCHEMA),
|
|
72
|
+
response: t.Object({ command: COMMAND_SCHEMA })
|
|
73
|
+
}, async ({ data }) => {
|
|
74
|
+
debug.log('path', `commands:create ${data.name}`);
|
|
75
|
+
if (!data.name.trim()) throw new Error('A command name is required');
|
|
76
|
+
const command = await commandService.create(data);
|
|
77
|
+
await syncCommandsAllEngines();
|
|
78
|
+
return { command };
|
|
79
|
+
})
|
|
80
|
+
.http('commands:update', {
|
|
81
|
+
data: t.Object({ id: t.Number(), ...FIELDS_SCHEMA }),
|
|
82
|
+
response: t.Object({ command: COMMAND_SCHEMA })
|
|
83
|
+
}, async ({ data }) => {
|
|
84
|
+
debug.log('path', `commands:update ${data.id}`);
|
|
85
|
+
if (!data.name.trim()) throw new Error('A command name is required');
|
|
86
|
+
const command = await commandService.update(data.id, data);
|
|
87
|
+
await syncCommandsAllEngines();
|
|
88
|
+
return { command };
|
|
89
|
+
})
|
|
90
|
+
.http('commands:parse-import', {
|
|
91
|
+
data: t.Object({ text: t.String() }),
|
|
92
|
+
response: t.Object({
|
|
93
|
+
name: t.String(),
|
|
94
|
+
description: t.String(),
|
|
95
|
+
argumentHint: t.Union([t.String(), t.Null()]),
|
|
96
|
+
modelByEngine: ENGINE_MAP_SCHEMA,
|
|
97
|
+
body: t.String()
|
|
98
|
+
})
|
|
99
|
+
}, async ({ data }) => {
|
|
100
|
+
debug.log('path', 'commands:parse-import');
|
|
101
|
+
return commandService.parsePreview(data.text);
|
|
102
|
+
})
|
|
103
|
+
.http('commands:import', {
|
|
104
|
+
data: t.Object({ text: t.String(), name: t.Optional(t.String()) }),
|
|
105
|
+
response: t.Object({ command: COMMAND_SCHEMA })
|
|
106
|
+
}, async ({ data }) => {
|
|
107
|
+
debug.log('path', 'commands:import');
|
|
108
|
+
const command = await commandService.import(data.text, data.name);
|
|
109
|
+
await syncCommandsAllEngines();
|
|
110
|
+
return { command };
|
|
111
|
+
})
|
|
112
|
+
.http('commands:toggle', {
|
|
113
|
+
data: t.Object({ id: t.Number(), enabled: t.Boolean() }),
|
|
114
|
+
response: t.Object({ command: COMMAND_SCHEMA })
|
|
115
|
+
}, async ({ data }) => {
|
|
116
|
+
debug.log('path', `commands:toggle ${data.id} → ${data.enabled}`);
|
|
117
|
+
const command = commandService.toggle(data.id, data.enabled);
|
|
118
|
+
await syncCommandsAllEngines();
|
|
119
|
+
return { command };
|
|
120
|
+
})
|
|
121
|
+
.http('commands:delete', {
|
|
122
|
+
data: t.Object({ id: t.Number() }),
|
|
123
|
+
response: t.Object({ success: t.Boolean() })
|
|
124
|
+
}, async ({ data }) => {
|
|
125
|
+
debug.log('path', `commands:delete ${data.id}`);
|
|
126
|
+
await commandService.remove(data.id);
|
|
127
|
+
await syncCommandsAllEngines();
|
|
128
|
+
return { success: true };
|
|
129
|
+
})
|
|
130
|
+
.http('commands:available', {
|
|
131
|
+
// Non-admin: commands for the chat "/" picker (display fields only),
|
|
132
|
+
// narrowed by the session's active profile (if any) same as stream sync.
|
|
133
|
+
data: t.Object({
|
|
134
|
+
profileId: t.Optional(t.Union([t.Number(), t.Null()])),
|
|
135
|
+
projectId: t.Optional(t.String())
|
|
136
|
+
}),
|
|
137
|
+
response: t.Object({
|
|
138
|
+
commands: t.Array(t.Object({
|
|
139
|
+
slug: t.String(),
|
|
140
|
+
name: t.String(),
|
|
141
|
+
description: t.String(),
|
|
142
|
+
argumentHint: t.Union([t.String(), t.Null()])
|
|
143
|
+
}))
|
|
144
|
+
})
|
|
145
|
+
}, async ({ data }) => {
|
|
146
|
+
debug.log('path', 'commands:available');
|
|
147
|
+
const activeProfileId = resolveActiveProfileId(data.profileId ?? null, data.projectId);
|
|
148
|
+
const filter = artifactFilter(activeProfileId, 'command');
|
|
149
|
+
return { commands: commandService.available(filter) };
|
|
150
|
+
})
|
|
151
|
+
.http('commands:detect', {
|
|
152
|
+
data: t.Object({ projectPath: t.Optional(t.String()) }),
|
|
153
|
+
response: t.Object({ groups: t.Array(DETECTED_SCHEMA) })
|
|
154
|
+
}, async ({ data }) => {
|
|
155
|
+
debug.log('path', 'commands:detect');
|
|
156
|
+
return { groups: await detectCommands(data.projectPath) };
|
|
157
|
+
});
|
package/backend/ws/git/branch.ts
CHANGED
|
@@ -142,12 +142,14 @@ export const branchHandler = createRouter()
|
|
|
142
142
|
data: t.Object({
|
|
143
143
|
projectId: t.String(),
|
|
144
144
|
oldName: t.String(),
|
|
145
|
-
newName: t.String({ minLength: 1 })
|
|
145
|
+
newName: t.String({ minLength: 1 }),
|
|
146
|
+
repoPath: t.Optional(t.String())
|
|
146
147
|
}),
|
|
147
148
|
response: t.Object({ ok: t.Boolean() })
|
|
148
149
|
}, async ({ data, conn }) => {
|
|
149
150
|
const project = requireProjectAccess(conn, data.projectId);
|
|
150
|
-
|
|
151
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
152
|
+
await gitService.renameBranch(cwd, data.oldName, data.newName);
|
|
151
153
|
return { ok: true };
|
|
152
154
|
})
|
|
153
155
|
|
package/backend/ws/git/remote.ts
CHANGED
|
@@ -136,12 +136,14 @@ export const remoteHandler = createRouter()
|
|
|
136
136
|
data: t.Object({
|
|
137
137
|
projectId: t.String(),
|
|
138
138
|
name: t.String(),
|
|
139
|
-
url: t.String()
|
|
139
|
+
url: t.String(),
|
|
140
|
+
repoPath: t.Optional(t.String())
|
|
140
141
|
}),
|
|
141
142
|
response: t.Object({ ok: t.Boolean() })
|
|
142
143
|
}, async ({ data, conn }) => {
|
|
143
144
|
const project = requireProjectAccess(conn, data.projectId);
|
|
144
|
-
|
|
145
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
146
|
+
await gitService.addRemote(cwd, data.name, data.url);
|
|
145
147
|
return { ok: true };
|
|
146
148
|
})
|
|
147
149
|
|
|
@@ -149,12 +151,14 @@ export const remoteHandler = createRouter()
|
|
|
149
151
|
data: t.Object({
|
|
150
152
|
projectId: t.String(),
|
|
151
153
|
name: t.String(),
|
|
152
|
-
url: t.String()
|
|
154
|
+
url: t.String(),
|
|
155
|
+
repoPath: t.Optional(t.String())
|
|
153
156
|
}),
|
|
154
157
|
response: t.Object({ ok: t.Boolean() })
|
|
155
158
|
}, async ({ data, conn }) => {
|
|
156
159
|
const project = requireProjectAccess(conn, data.projectId);
|
|
157
|
-
|
|
160
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
161
|
+
await gitService.setRemoteUrl(cwd, data.name, data.url);
|
|
158
162
|
return { ok: true };
|
|
159
163
|
})
|
|
160
164
|
|
|
@@ -162,12 +166,14 @@ export const remoteHandler = createRouter()
|
|
|
162
166
|
data: t.Object({
|
|
163
167
|
projectId: t.String(),
|
|
164
168
|
oldName: t.String(),
|
|
165
|
-
newName: t.String()
|
|
169
|
+
newName: t.String(),
|
|
170
|
+
repoPath: t.Optional(t.String())
|
|
166
171
|
}),
|
|
167
172
|
response: t.Object({ ok: t.Boolean() })
|
|
168
173
|
}, async ({ data, conn }) => {
|
|
169
174
|
const project = requireProjectAccess(conn, data.projectId);
|
|
170
|
-
|
|
175
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
176
|
+
await gitService.renameRemote(cwd, data.oldName, data.newName);
|
|
171
177
|
return { ok: true };
|
|
172
178
|
})
|
|
173
179
|
|
|
@@ -176,27 +182,31 @@ export const remoteHandler = createRouter()
|
|
|
176
182
|
projectId: t.String(),
|
|
177
183
|
oldName: t.String(),
|
|
178
184
|
newName: t.String(),
|
|
179
|
-
newUrl: t.String()
|
|
185
|
+
newUrl: t.String(),
|
|
186
|
+
repoPath: t.Optional(t.String())
|
|
180
187
|
}),
|
|
181
188
|
response: t.Object({ ok: t.Boolean() })
|
|
182
189
|
}, async ({ data, conn }) => {
|
|
183
190
|
const project = requireProjectAccess(conn, data.projectId);
|
|
191
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
184
192
|
if (data.oldName !== data.newName) {
|
|
185
|
-
await gitService.renameRemote(
|
|
193
|
+
await gitService.renameRemote(cwd, data.oldName, data.newName);
|
|
186
194
|
}
|
|
187
|
-
await gitService.setRemoteUrl(
|
|
195
|
+
await gitService.setRemoteUrl(cwd, data.newName, data.newUrl);
|
|
188
196
|
return { ok: true };
|
|
189
197
|
})
|
|
190
198
|
|
|
191
199
|
.http('git:remove-remote', {
|
|
192
200
|
data: t.Object({
|
|
193
201
|
projectId: t.String(),
|
|
194
|
-
name: t.String()
|
|
202
|
+
name: t.String(),
|
|
203
|
+
repoPath: t.Optional(t.String())
|
|
195
204
|
}),
|
|
196
205
|
response: t.Object({ ok: t.Boolean() })
|
|
197
206
|
}, async ({ data, conn }) => {
|
|
198
207
|
const project = requireProjectAccess(conn, data.projectId);
|
|
199
|
-
|
|
208
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
209
|
+
await gitService.removeRemote(cwd, data.name);
|
|
200
210
|
return { ok: true };
|
|
201
211
|
})
|
|
202
212
|
|
package/backend/ws/index.ts
CHANGED
|
@@ -33,6 +33,12 @@ import { systemToolsRouter } from './system-tools';
|
|
|
33
33
|
import { dbClientRouter } from './db-client';
|
|
34
34
|
import { mcpRouter } from './mcp';
|
|
35
35
|
import { skillsRouter } from './skills';
|
|
36
|
+
import { commandsRouter } from './commands';
|
|
37
|
+
import { subagentsRouter } from './subagents';
|
|
38
|
+
import { instructionsRouter } from './instructions';
|
|
39
|
+
import { permissionsRouter } from './permissions';
|
|
40
|
+
import { profilesRouter } from './profiles';
|
|
41
|
+
import { artifactsRouter } from './artifacts';
|
|
36
42
|
|
|
37
43
|
// ============================================
|
|
38
44
|
// Main App Router - Merge All Module Routers
|
|
@@ -81,7 +87,17 @@ export const wsRouter = createRouter()
|
|
|
81
87
|
.merge(mcpRouter)
|
|
82
88
|
|
|
83
89
|
// Agent Skills management (create, import, install from a marketplace)
|
|
84
|
-
.merge(skillsRouter)
|
|
90
|
+
.merge(skillsRouter)
|
|
91
|
+
|
|
92
|
+
// Custom Commands, Subagents, and Project Instructions (artifact framework)
|
|
93
|
+
.merge(commandsRouter)
|
|
94
|
+
.merge(subagentsRouter)
|
|
95
|
+
.merge(instructionsRouter)
|
|
96
|
+
.merge(permissionsRouter)
|
|
97
|
+
|
|
98
|
+
// Profiles (reusable artifact bundles activated per-session)
|
|
99
|
+
.merge(profilesRouter)
|
|
100
|
+
.merge(artifactsRouter);
|
|
85
101
|
|
|
86
102
|
// Export API type for frontend type-safe access
|
|
87
103
|
export type WSAPI = typeof wsRouter['$api'];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instruction Handlers (Settings → Instructions)
|
|
3
|
+
*
|
|
4
|
+
* - instructions:get-global / save-global
|
|
5
|
+
* - instructions:get-project / save-project
|
|
6
|
+
*
|
|
7
|
+
* The managed block is injected into each engine's memory file as a
|
|
8
|
+
* marker-region; hand-written content is preserved. Mutations are admin-gated.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { t } from 'elysia';
|
|
12
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
13
|
+
import { debug } from '$shared/utils/logger';
|
|
14
|
+
import { instructionService } from '$backend/instructions';
|
|
15
|
+
|
|
16
|
+
const INSTRUCTION_SCHEMA = t.Object({
|
|
17
|
+
scope: t.Union([t.Literal('global'), t.Literal('project')]),
|
|
18
|
+
projectId: t.Union([t.String(), t.Null()]),
|
|
19
|
+
content: t.String(),
|
|
20
|
+
enabled: t.Boolean(),
|
|
21
|
+
updatedAt: t.Union([t.String(), t.Null()]),
|
|
22
|
+
engineFiles: t.Array(t.Object({ engine: t.String(), fileName: t.String() }))
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const instructionCrudHandler = createRouter()
|
|
26
|
+
.http('instructions:get-global', {
|
|
27
|
+
data: t.Object({}),
|
|
28
|
+
response: t.Object({ instruction: INSTRUCTION_SCHEMA })
|
|
29
|
+
}, async () => {
|
|
30
|
+
debug.log('path', 'instructions:get-global');
|
|
31
|
+
return { instruction: instructionService.getGlobal() };
|
|
32
|
+
})
|
|
33
|
+
.http('instructions:save-global', {
|
|
34
|
+
data: t.Object({ content: t.String(), enabled: t.Boolean() }),
|
|
35
|
+
response: t.Object({ instruction: INSTRUCTION_SCHEMA })
|
|
36
|
+
}, async ({ data }) => {
|
|
37
|
+
debug.log('path', 'instructions:save-global');
|
|
38
|
+
return { instruction: instructionService.saveGlobal(data.content, data.enabled) };
|
|
39
|
+
})
|
|
40
|
+
.http('instructions:get-project', {
|
|
41
|
+
data: t.Object({ projectId: t.String() }),
|
|
42
|
+
response: t.Object({ instruction: INSTRUCTION_SCHEMA })
|
|
43
|
+
}, async ({ data }) => {
|
|
44
|
+
debug.log('path', `instructions:get-project ${data.projectId}`);
|
|
45
|
+
return { instruction: instructionService.getForProject(data.projectId) };
|
|
46
|
+
})
|
|
47
|
+
.http('instructions:save-project', {
|
|
48
|
+
data: t.Object({ projectId: t.String(), content: t.String(), enabled: t.Boolean() }),
|
|
49
|
+
response: t.Object({ instruction: INSTRUCTION_SCHEMA })
|
|
50
|
+
}, async ({ data }) => {
|
|
51
|
+
debug.log('path', `instructions:save-project ${data.projectId}`);
|
|
52
|
+
return { instruction: instructionService.saveForProject(data.projectId, data.content, data.enabled) };
|
|
53
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instructions Router — Settings → Instructions (global + per-project blocks).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
6
|
+
import { instructionCrudHandler } from './crud';
|
|
7
|
+
|
|
8
|
+
export const instructionsRouter = createRouter().merge(instructionCrudHandler);
|
package/backend/ws/mcp/crud.ts
CHANGED
|
@@ -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
|
}
|
package/backend/ws/mcp/index.ts
CHANGED
|
@@ -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);
|