dsh-remote-plugin 0.7.4 → 0.7.6

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,34 @@
1
+ import { once } from 'node:events'
2
+ export async function serveSessionFile(ctx, req, res, url) {
3
+ const send = (status, code) => { res.writeHead(status, { 'content-type':'application/json', 'cache-control':'no-store' }); res.end(JSON.stringify({ok:false,code})) }
4
+ if (!['GET','HEAD'].includes(req.method)) return send(405,'read-only')
5
+ const sessionId=url.searchParams.get('sessionId'),id=url.searchParams.get('attachmentId')
6
+ if (!sessionId || sessionId.length>200 || !/^[a-f0-9]{64}$/.test(id||'') || [...url.searchParams.keys()].some(k=>!['sessionId','attachmentId','token'].includes(k)) || url.searchParams.getAll('attachmentId').length!==1 || url.searchParams.getAll('sessionId').length!==1) return send(400,'invalid-reference')
7
+ let controller,store
8
+ try {controller=ctx.get?.('sessionController');store=ctx.get?.('attachments')}catch{return send(503,'files-unsupported')}
9
+ const abort=new AbortController()
10
+ if (!controller?.inspect || !store?.readFileStream) return send(503,'files-unsupported')
11
+ const cancel=()=>abort.abort();res.once('close',cancel)
12
+ try {
13
+ const inspection=await controller.inspect(sessionId,abort.signal)
14
+ let ref
15
+ for (const event of inspection?.events||[]) {
16
+ if (!['user/message','assistant/message','tool/result'].includes(event.type)) continue
17
+ const blocks=event.data?.message?.content||event.data?.content||[]
18
+ if (!Array.isArray(blocks)) continue
19
+ ref=blocks.find(b=>b?.type==='file'&&b.attachment?.attachmentId===id)?.attachment
20
+ if(ref)break
21
+ }
22
+ if (!ref) return send(404,'file-not-in-session')
23
+ if (!Number.isSafeInteger(ref.bytes)||ref.bytes<0||typeof ref.name!=='string') return send(502,'invalid-host-reference')
24
+ const name=ref.name.replace(/[\\/\r\n"\x00-\x1f]/g,'_').slice(0,200)||'download'
25
+ const headers={'content-type':'application/octet-stream','content-length':String(ref.bytes),'content-disposition':"attachment; filename*=UTF-8''"+encodeURIComponent(name),'cache-control':'private, no-store','x-content-type-options':'nosniff'}
26
+ if(req.method==='HEAD'){res.writeHead(200,headers);return res.end()}
27
+ const stream=store.readFileStream(ref,abort.signal)[Symbol.asyncIterator](),first=await stream.next()
28
+ res.writeHead(200,headers)
29
+ let bytes=0,next=first
30
+ try { while(!next.done){abort.signal.throwIfAborted();const chunk=Buffer.from(next.value);bytes+=chunk.length;if(bytes>ref.bytes)throw Error('size');if(!res.write(chunk))await once(res,'drain',{signal:abort.signal});next=await stream.next()}if(bytes!==ref.bytes)throw Error('size');res.end() }
31
+ finally{await stream.return?.()}
32
+ } catch { if(res.headersSent)res.destroy();else if(!res.destroyed)send(502,'file-unavailable') }
33
+ finally {res.off('close',cancel)}
34
+ }
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
+ }