dsh-remote-plugin 0.7.4 → 0.7.5

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/skills.mjs ADDED
@@ -0,0 +1,53 @@
1
+ // Read-only adapter for dsh-skill-explorer 0.4.x. Paths never come from clients.
2
+ import { createHash } from 'node:crypto'
3
+ const text = (value, max) => typeof value === 'string' ? value.slice(0, max) : ''
4
+ const idOf = (name, path, source) => createHash('sha256').update(JSON.stringify([name, path, source])).digest('hex')
5
+ const unavailable = code => ({ status: 200, body: { ok: true, available: false, code } })
6
+ export function createSkillsAdapter(requestNative) {
7
+ async function list() {
8
+ const raw = await requestNative('/api/dsh-skill-explorer/list')
9
+ if (!raw || !Array.isArray(raw.groups) || typeof raw.complete !== 'boolean') throw new Error('unsupported')
10
+ const items = [], identities = new Map()
11
+ let truncated = raw.groups.length > 50
12
+ for (const group of raw.groups.slice(0, 50)) {
13
+ if (!Array.isArray(group?.skills)) throw new Error('unsupported')
14
+ for (const row of group.skills) {
15
+ if (items.length >= 500) { truncated = true; break }
16
+ if (!row || typeof row.name !== 'string' || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(row.name) || row.name.length > 200) continue
17
+ const source = text(group.key, 100), path = text(row.path, 4096), id = idOf(row.name, path, source)
18
+ const item = { id, name: row.name, description: text(row.description, 3000), whenToUse: text(row.whenToUse, 3000), source,
19
+ modelInvocable: typeof row.modelInvocable === 'boolean' ? row.modelInvocable : null,
20
+ userInvocable: typeof row.userInvocable === 'boolean' ? row.userInvocable : null,
21
+ readable: !!path, workspace: text(row.workspaceName, 200) }
22
+ identities.set(id, { name: row.name, path, item }); items.push(item)
23
+ }
24
+ }
25
+ return { body: { ok: true, schema: 1, available: true, source: 'dsh-skill-explorer', complete: raw.complete, truncated,
26
+ generatedAt: Date.now(), items }, identities }
27
+ }
28
+ return { async handle(method, endpoint, query = new URLSearchParams()) {
29
+ if (method !== 'GET') return { status: 405, body: { ok: false, code: 'read-only' } }
30
+ if (!['/list', '/read'].includes(endpoint)) return { status: 404, body: { ok: false, code: 'unknown-route' } }
31
+ const keys = [...query.keys()]
32
+ if (endpoint === '/list' ? keys.length : keys.length !== 1 || keys[0] !== 'id' || !/^[a-f0-9]{64}$/.test(query.get('id') || '')) {
33
+ return { status: 400, body: { ok: false, code: 'invalid-query' } }
34
+ }
35
+ try {
36
+ const state = await list()
37
+ if (endpoint === '/list') return { status: 200, body: state.body }
38
+ const found = state.identities.get(query.get('id'))
39
+ if (!found) return { status: 409, body: { ok: false, code: 'skill-changed' } }
40
+ if (!found.path) return { status: 200, body: { ok: true, available: true, item: found.item, content: null } }
41
+ const params = new URLSearchParams({ name: found.name, path: found.path })
42
+ const detail = await requestNative('/api/dsh-skill-explorer/read?' + params)
43
+ if (!detail || detail.name !== found.name || detail.path !== found.path || typeof detail.content !== 'string') throw new Error('unsupported')
44
+ return { status: 200, body: { ok: true, available: true, item: found.item,
45
+ content: detail.content.slice(0, 100000), truncated: detail.content.length > 100000 } }
46
+ } catch (error) {
47
+ if (error.status === 404) return unavailable('not-installed')
48
+ if (error.status === 401 || error.status === 403) return { status: 502, body: { ok: false, code: 'host-denied' } }
49
+ if (error.status === 409) return { status: 409, body: { ok: false, code: 'skill-changed' } }
50
+ return { status: 502, body: { ok: false, code: error.message === 'unsupported' ? 'unsupported' : 'skills-unavailable' } }
51
+ }
52
+ } }
53
+ }