@soffits/dsh-plugin-sdk 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/LICENSE-lucide +45 -0
- package/NOTICE +9 -0
- package/README.md +42 -2
- package/actions/README.md +67 -0
- package/actions/client.js +606 -0
- package/actions/src/item.js +120 -0
- package/channel/README.md +24 -0
- package/channel/index.js +49 -0
- package/channel/src/answer.js +105 -0
- package/channel/src/config.js +18 -0
- package/channel/src/models.js +210 -0
- package/channel/src/principal.js +153 -0
- package/channel/src/sessions.js +372 -0
- package/channel/src/spool.js +175 -0
- package/channel/src/subagents.js +540 -0
- package/channel/src/voice.js +127 -0
- package/channel/testing.js +132 -0
- package/client.js +1762 -0
- package/cordis.patch.yml +4 -0
- package/host/README.md +44 -0
- package/host/build.js +1 -0
- package/host/index.js +4 -0
- package/host/src/build-client.js +104 -0
- package/host/src/event-streams.js +88 -0
- package/host/src/locales.js +90 -0
- package/host/src/notices.js +66 -0
- package/host/src/people.js +87 -0
- package/icon.svg +1 -0
- package/index.js +7 -0
- package/locale/en.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +80 -4
- package/ui/README.md +87 -0
- package/ui/client.js +1138 -0
- package/ui/icons.json +228 -0
package/cordis.patch.yml
ADDED
package/host/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Host helpers
|
|
2
|
+
|
|
3
|
+
Part of [@soffits/dsh-plugin-sdk](../README.md): `@soffits/dsh-plugin-sdk/host` (and `/host/build`). Its history before the merge is in the archived [soffits/dsh-host-kit](https://github.com/soffits/dsh-host-kit) repository.
|
|
4
|
+
|
|
5
|
+
Small helpers for the host half of [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) plugins, the parts every plugin with its own page ends up writing:
|
|
6
|
+
|
|
7
|
+
- **Who is asking** (`createPeople`, `viewerOf`, `personKey`, `expandHome`): maps the verified user behind an `/api` request (from any plugin that provides the `requestIdentity` service) to a person id, from inline config and an optional JSON file that is re-read when it changes. Without a mapping, everyone DSH lets in is its owner. A broken mapping file maps nobody, so it never widens access.
|
|
8
|
+
- **Words** (`readLocales`, `tables`, `tableFor`, `chain`, `BASE`, `TAG`): one `locale/<tag>.json` per language, `{ meta, ui }`. A missing word comes from the primary language, then from English, and a new language is a new file.
|
|
9
|
+
- **Change notices** (`createNotices`): tells open pages that something they show changed, merged into batches (`settleMs` after the first notice, at least `spacingMs` apart) and filtered per listener by scope, such as a person id.
|
|
10
|
+
- **Event streams** (`createEventStreams`): Server-Sent Events answers for Fetch routes, with a heartbeat, and ended when the page goes, when the route ends them, or when the plugin unloads (`close()`), so no stream outlives its plugin.
|
|
11
|
+
- **The client build** (`@soffits/dsh-plugin-sdk/host/build`: `buildClient`, `localeFiles`): bundles `src/client/` into `client.js` in DSH's module-loader format, registered under the package name, with `--check` failing on a stale build. Needs `esbuild` as a dev dependency of the plugin.
|
|
12
|
+
|
|
13
|
+
It is a plain library, not a DSH plugin: plugins list it as a dependency.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { createEventStreams, createNotices, createPeople, viewerOf } from '@soffits/dsh-plugin-sdk/host'
|
|
17
|
+
|
|
18
|
+
export function apply(ctx, config) {
|
|
19
|
+
const people = createPeople(config, { warn: message => ctx.logger.warn(message) })
|
|
20
|
+
const notices = createNotices({ settleMs: 50 })
|
|
21
|
+
const streams = createEventStreams()
|
|
22
|
+
ctx.effect(() => () => { streams.close(); notices.close() })
|
|
23
|
+
ctx.connection.fetch.register({
|
|
24
|
+
path: '/api/example/events',
|
|
25
|
+
methods: ['GET'],
|
|
26
|
+
requestBody: 'buffered',
|
|
27
|
+
fetch: async request => {
|
|
28
|
+
const viewer = await viewerOf(people, ctx.get('requestIdentity'))
|
|
29
|
+
if (viewer.unavailable) return Response.json({ error: 'No sign-in provider.' }, { status: 503 })
|
|
30
|
+
const mine = viewer.mode === 'owner' ? undefined : scope => scope === viewer.person
|
|
31
|
+
return streams.respond(request, ({ send }) => notices.listen(what => send({ type: 'change', what }), mine))
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
// scripts/build.mjs
|
|
39
|
+
import { dirname, resolve } from 'node:path'
|
|
40
|
+
import { fileURLToPath } from 'node:url'
|
|
41
|
+
import { buildClient } from '@soffits/dsh-plugin-sdk/host/build'
|
|
42
|
+
|
|
43
|
+
await buildClient({ root: resolve(dirname(fileURLToPath(import.meta.url)), '..') })
|
|
44
|
+
```
|
package/host/build.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { buildClient, localeFiles } from './src/build-client.js'
|
package/host/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createPeople, expandHome, personKey, viewerOf } from './src/people.js'
|
|
2
|
+
export { BASE, TAG, chain, readLocales, tableFor, tables } from './src/locales.js'
|
|
3
|
+
export { createNotices } from './src/notices.js'
|
|
4
|
+
export { createEventStreams } from './src/event-streams.js'
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build a plugin's `client.js`, its Web UI half, in DSH's module-loader format. DSH runs client
|
|
3
|
+
* scripts as classic scripts in one global scope, so the bundle stays inside a factory registered
|
|
4
|
+
* under the package's npm name (the id DSH looks the plugin up by). React and DSH's own components
|
|
5
|
+
* come from the page's shared module table, so they stay outside the bundle.
|
|
6
|
+
*
|
|
7
|
+
* The outputs are committed so a linked checkout runs as it is; with `check` nothing is written and
|
|
8
|
+
* the build fails when a committed output is stale.
|
|
9
|
+
*/
|
|
10
|
+
import { readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { join, resolve } from 'node:path'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `{ <tag>: <file contents> }` for every `<tag>.json` in a plugin's `locale/` folder, for a bundle
|
|
15
|
+
* that needs its words built in (a client bundle can't read package files at run time).
|
|
16
|
+
* @param {string} root
|
|
17
|
+
*/
|
|
18
|
+
export function localeFiles(root) {
|
|
19
|
+
const dir = join(root, 'locale')
|
|
20
|
+
return Object.fromEntries(readdirSync(dir).filter(name => name.endsWith('.json')).sort()
|
|
21
|
+
.map(name => [name.slice(0, -'.json'.length), JSON.parse(readFileSync(join(dir, name), 'utf8'))]))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {object} options
|
|
26
|
+
* @param {string} options.root - the plugin's folder (its package.json names the module)
|
|
27
|
+
* @param {string} [options.entry] - relative to `root`
|
|
28
|
+
* @param {string[]} [options.external] - modules the page's shared module table provides
|
|
29
|
+
* @param {Record<string, () => unknown>} [options.data] - `import x from 'virtual:<name>'` gives the
|
|
30
|
+
* value the function returns, built in as JSON
|
|
31
|
+
* @param {string} [options.from] - what the header says it was built from
|
|
32
|
+
* @param {Record<string, string>} [options.extra] - more committed outputs: file name → contents
|
|
33
|
+
* @param {boolean} [options.check] - compare instead of writing (default: `--check` on the command line)
|
|
34
|
+
* @returns {Promise<boolean>} whether every output was current (always true when writing)
|
|
35
|
+
*/
|
|
36
|
+
export async function buildClient({
|
|
37
|
+
root,
|
|
38
|
+
entry = 'src/client/index.js',
|
|
39
|
+
external = ['react', '@deepseek-ai/dsh-client-ui-primitives'],
|
|
40
|
+
data = {},
|
|
41
|
+
from = 'src/client/',
|
|
42
|
+
extra = {},
|
|
43
|
+
check = process.argv.includes('--check'),
|
|
44
|
+
}) {
|
|
45
|
+
const { build } = await import('esbuild')
|
|
46
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
|
|
47
|
+
const virtual = {
|
|
48
|
+
name: 'virtual',
|
|
49
|
+
setup(b) {
|
|
50
|
+
b.onResolve({ filter: /^virtual:/ }, args => {
|
|
51
|
+
const name = args.path.slice('virtual:'.length)
|
|
52
|
+
if (!Object.hasOwn(data, name)) return { errors: [{ text: `no data named ${name}` }] }
|
|
53
|
+
return { path: name, namespace: 'virtual' }
|
|
54
|
+
})
|
|
55
|
+
b.onLoad({ filter: /.*/, namespace: 'virtual' }, args => ({ contents: `export default ${JSON.stringify(data[args.path]())}`, loader: 'js' }))
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
const result = await build({
|
|
59
|
+
entryPoints: [resolve(root, entry)],
|
|
60
|
+
bundle: true,
|
|
61
|
+
write: false,
|
|
62
|
+
format: 'cjs',
|
|
63
|
+
platform: 'browser',
|
|
64
|
+
target: 'es2022',
|
|
65
|
+
external,
|
|
66
|
+
plugins: [virtual],
|
|
67
|
+
legalComments: 'none',
|
|
68
|
+
logLevel: 'warning',
|
|
69
|
+
})
|
|
70
|
+
const outputs = {
|
|
71
|
+
'client.js': `/* ${pkg.name} ${pkg.version}: built from ${from}. Do not edit. */
|
|
72
|
+
window.__ModuleLoader__.load({
|
|
73
|
+
id: ${JSON.stringify(pkg.name)},
|
|
74
|
+
factory: (require) => {
|
|
75
|
+
var module = { exports: {} }
|
|
76
|
+
var exports = module.exports
|
|
77
|
+
${result.outputFiles[0].text}
|
|
78
|
+
return module.exports
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
`,
|
|
82
|
+
...extra,
|
|
83
|
+
}
|
|
84
|
+
let current = true
|
|
85
|
+
for (const [file, text] of Object.entries(outputs)) {
|
|
86
|
+
const target = join(root, file)
|
|
87
|
+
if (!check) {
|
|
88
|
+
writeFileSync(target, text)
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
let committed = ''
|
|
92
|
+
try {
|
|
93
|
+
committed = readFileSync(target, 'utf8')
|
|
94
|
+
} catch {
|
|
95
|
+
// Not built yet: stale.
|
|
96
|
+
}
|
|
97
|
+
if (committed !== text) {
|
|
98
|
+
console.error(`${file} is stale: run npm run build`)
|
|
99
|
+
current = false
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!current) process.exitCode = 1
|
|
103
|
+
return current
|
|
104
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Sent Events answers for a plugin's `/api` Fetch routes. Each stream says `retry` once, sends
|
|
3
|
+
* a comment every `heartbeatMs` so proxies keep it open, and ends when the page goes (the request's
|
|
4
|
+
* abort or the reader's cancel), when the route ends it, or when the plugin unloads (`close()`): DSH
|
|
5
|
+
* drops a plugin's routes on unload but not the streams it already answered, which would otherwise
|
|
6
|
+
* live on, beating, with nobody left to tell them anything.
|
|
7
|
+
*
|
|
8
|
+
* @param {{ heartbeatMs?: number, headers?: Record<string, string> }} [options] - `headers` are added
|
|
9
|
+
* to every answer (a plugin's own security headers, say)
|
|
10
|
+
*/
|
|
11
|
+
export function createEventStreams({ heartbeatMs = 25_000, headers = {} } = {}) {
|
|
12
|
+
const open = new Set()
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Answer `request` with a stream. `start` runs once the stream is open, gets `send(value)` (one
|
|
16
|
+
* `data:` event with `value` as JSON) and `end()`, and returns (or resolves to) the function that
|
|
17
|
+
* stops whatever feeds the stream; it runs exactly once, however the stream ends.
|
|
18
|
+
*
|
|
19
|
+
* @param {{ signal?: AbortSignal }} request
|
|
20
|
+
* @param {(stream: { send: (value: unknown) => void, end: () => void }) => (void | (() => void) | Promise<void | (() => void)>)} start
|
|
21
|
+
* @returns {Response}
|
|
22
|
+
*/
|
|
23
|
+
function respond(request, start) {
|
|
24
|
+
const encoder = new TextEncoder()
|
|
25
|
+
let finish = () => {}
|
|
26
|
+
const body = new ReadableStream({
|
|
27
|
+
async start(controller) {
|
|
28
|
+
let live = true
|
|
29
|
+
let stop = () => {}
|
|
30
|
+
const write = text => {
|
|
31
|
+
if (!live) return
|
|
32
|
+
try {
|
|
33
|
+
controller.enqueue(encoder.encode(text))
|
|
34
|
+
} catch {
|
|
35
|
+
end()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const beat = setInterval(() => write(': beat\n\n'), heartbeatMs)
|
|
39
|
+
finish = () => {
|
|
40
|
+
if (!live) return
|
|
41
|
+
live = false
|
|
42
|
+
clearInterval(beat)
|
|
43
|
+
open.delete(end)
|
|
44
|
+
request.signal?.removeEventListener?.('abort', end)
|
|
45
|
+
stop()
|
|
46
|
+
}
|
|
47
|
+
const end = () => {
|
|
48
|
+
finish()
|
|
49
|
+
try {
|
|
50
|
+
controller.close()
|
|
51
|
+
} catch {
|
|
52
|
+
// Already closed.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
open.add(end)
|
|
56
|
+
request.signal?.addEventListener?.('abort', end, { once: true })
|
|
57
|
+
if (request.signal?.aborted) return end()
|
|
58
|
+
write('retry: 5000\n: open\n\n')
|
|
59
|
+
let stopping
|
|
60
|
+
try {
|
|
61
|
+
stopping = await start({ send: value => write(`data: ${JSON.stringify(value)}\n\n`), end })
|
|
62
|
+
} catch (error) {
|
|
63
|
+
finish()
|
|
64
|
+
controller.error(error)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
if (typeof stopping !== 'function') return
|
|
68
|
+
if (live) stop = stopping
|
|
69
|
+
else stopping()
|
|
70
|
+
},
|
|
71
|
+
cancel() {
|
|
72
|
+
finish()
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
return new Response(body, {
|
|
76
|
+
status: 200,
|
|
77
|
+
headers: { ...headers, 'content-type': 'text/event-stream; charset=utf-8', 'cache-control': 'private, no-cache, no-transform', 'x-accel-buffering': 'no', 'x-content-type-options': 'nosniff' },
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
respond,
|
|
83
|
+
/** End every open stream (the plugin is going away). */
|
|
84
|
+
close() {
|
|
85
|
+
for (const end of [...open]) end()
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plugin's words: one `locale/<tag>.json` per language, `{ meta, ui }` (`meta` is DSH's plugin title
|
|
3
|
+
* and description, `ui` the page's words). Which languages exist is whatever files are there, so a new
|
|
4
|
+
* language is a new file and no code names one. A missing word comes from the primary language
|
|
5
|
+
* (`zh` for `zh-TW`), then from the base language.
|
|
6
|
+
*
|
|
7
|
+
* Read the folder on every request rather than once at start: DSH swaps a plugin's browser half as soon
|
|
8
|
+
* as its files change, before the host half reloads, so words kept from start would hand a freshly
|
|
9
|
+
* updated page the old table.
|
|
10
|
+
*/
|
|
11
|
+
import { readdir, readFile } from 'node:fs/promises'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
|
|
14
|
+
/** The language every lookup ends at. */
|
|
15
|
+
export const BASE = 'en'
|
|
16
|
+
|
|
17
|
+
/** A language tag as DSH accepts it (`en`, `pt-BR`, `zh-Hant-TW`). */
|
|
18
|
+
export const TAG = /^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `{ <tag>: { <key>: text } }` from each `<tag>.json` in `dir` (its `ui` table, text values only).
|
|
22
|
+
* Files not ending in `.json` are ignored; a `.json` file that isn't named by a language tag, isn't
|
|
23
|
+
* JSON or has no `ui` table is skipped and reported, so one bad file doesn't take every language down.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} dir
|
|
26
|
+
* @param {{ warn?: (message: string) => void }} [options]
|
|
27
|
+
*/
|
|
28
|
+
export async function readLocales(dir, { warn = () => {} } = {}) {
|
|
29
|
+
const locales = {}
|
|
30
|
+
for (const name of (await readdir(dir)).filter(file => file.endsWith('.json')).sort()) {
|
|
31
|
+
const tag = name.slice(0, -'.json'.length)
|
|
32
|
+
if (!TAG.test(tag)) {
|
|
33
|
+
warn(`locale/${name}: the name is not a language tag; skipped`)
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const { ui } = JSON.parse(await readFile(join(dir, name), 'utf8'))
|
|
38
|
+
if (ui === null || typeof ui !== 'object' || Array.isArray(ui)) throw new Error('no "ui" table')
|
|
39
|
+
locales[tag] = Object.fromEntries(Object.entries(ui).filter(([, text]) => typeof text === 'string'))
|
|
40
|
+
} catch (error) {
|
|
41
|
+
warn(`locale/${name}: ${error.message}; skipped`)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (locales[BASE] === undefined) warn(`locale/${BASE}.json is missing or unreadable; every language falls back to it`)
|
|
45
|
+
return locales
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The tags to try for `tag`, most specific first, ending with the base: `fr-CA` → fr-ca, fr, en. */
|
|
49
|
+
export function chain(tag) {
|
|
50
|
+
const parts = String(tag ?? '').toLowerCase().split('-').filter(Boolean)
|
|
51
|
+
const tags = parts.map((_, i) => parts.slice(0, parts.length - i).join('-'))
|
|
52
|
+
return tags.includes(BASE) ? tags : [...tags, BASE]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const primary = tag => tag.split('-')[0]
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A complete table for every tag there is a file for: the base, overlaid by the primary language,
|
|
59
|
+
* overlaid by the tag's own file. A regional file without its primary one also answers for the
|
|
60
|
+
* primary tag, so `zh-CN.json` alone serves a page set to `zh`.
|
|
61
|
+
*
|
|
62
|
+
* @param {Record<string, Record<string, string>>} locales - from `readLocales`
|
|
63
|
+
*/
|
|
64
|
+
export function tables(locales) {
|
|
65
|
+
const base = locales[BASE] ?? {}
|
|
66
|
+
const out = {}
|
|
67
|
+
for (const tag of Object.keys(locales)) {
|
|
68
|
+
out[tag] = { ...base, ...(locales[primary(tag)] ?? {}), ...locales[tag] }
|
|
69
|
+
}
|
|
70
|
+
for (const tag of Object.keys(locales)) {
|
|
71
|
+
if (out[primary(tag)] === undefined) out[primary(tag)] = out[tag]
|
|
72
|
+
}
|
|
73
|
+
return out
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The complete table for one requested tag and the tag it was found under (lower case): the most
|
|
78
|
+
* specific tag in `chain(tag)` that `tables` has, else the base.
|
|
79
|
+
*
|
|
80
|
+
* @param {Record<string, Record<string, string>>} locales - from `readLocales`
|
|
81
|
+
* @param {string} tag
|
|
82
|
+
* @returns {{ tag: string, messages: Record<string, string> }}
|
|
83
|
+
*/
|
|
84
|
+
export function tableFor(locales, tag) {
|
|
85
|
+
const byTag = new Map(Object.entries(tables(locales)).map(([key, table]) => [key.toLowerCase(), table]))
|
|
86
|
+
for (const candidate of chain(tag)) {
|
|
87
|
+
if (byTag.has(candidate)) return { tag: candidate, messages: byTag.get(candidate) }
|
|
88
|
+
}
|
|
89
|
+
return { tag: BASE, messages: {} }
|
|
90
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Change notices for open pages: something a page shows has changed, and what kind. A notice carries
|
|
3
|
+
* no data; the page reads again. Where the changes come from (a file watch, a stat, the plugin's own
|
|
4
|
+
* writes) is the plugin's business; this is the part every plugin needs between that and its pages.
|
|
5
|
+
*
|
|
6
|
+
* Notices that come close together go out as one batch: a batch leaves `settleMs` after its first
|
|
7
|
+
* notice (a write and the rename that follows it are one change), and never sooner than `spacingMs`
|
|
8
|
+
* after the batch before (a download's progress doesn't flood a page).
|
|
9
|
+
*
|
|
10
|
+
* A notice may name its `scope` (whose data changed, say a person id); a listener hears only the
|
|
11
|
+
* scopes it accepts. A notice without a scope reaches everyone.
|
|
12
|
+
*
|
|
13
|
+
* @param {{ settleMs?: number, spacingMs?: number }} [options]
|
|
14
|
+
*/
|
|
15
|
+
export function createNotices({ settleMs = 0, spacingMs = 0 } = {}) {
|
|
16
|
+
const listeners = new Set()
|
|
17
|
+
let pending = []
|
|
18
|
+
let timer
|
|
19
|
+
let lastAt = -Infinity
|
|
20
|
+
|
|
21
|
+
function flush() {
|
|
22
|
+
timer = undefined
|
|
23
|
+
lastAt = Date.now()
|
|
24
|
+
const batch = pending
|
|
25
|
+
pending = []
|
|
26
|
+
for (const listener of [...listeners]) {
|
|
27
|
+
const what = new Set()
|
|
28
|
+
for (const notice of batch) {
|
|
29
|
+
if (notice.scope === undefined || listener.accepts === undefined || listener.accepts(notice.scope)) what.add(notice.what)
|
|
30
|
+
}
|
|
31
|
+
if (what.size > 0) listener.send([...what])
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
/**
|
|
37
|
+
* Something of kind `what` changed, for `scope` (omit for everyone).
|
|
38
|
+
* @param {string} what
|
|
39
|
+
* @param {string} [scope]
|
|
40
|
+
*/
|
|
41
|
+
notify(what, scope) {
|
|
42
|
+
pending.push({ what, scope })
|
|
43
|
+
if (timer !== undefined) return
|
|
44
|
+
timer = setTimeout(flush, Math.max(settleMs, lastAt + spacingMs - Date.now()))
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* Hear batches of kinds (`send(['library', 'rooms'])`), only for the scopes `accepts` takes
|
|
48
|
+
* (omit it to hear every scope).
|
|
49
|
+
* @param {(what: string[]) => void} send
|
|
50
|
+
* @param {(scope: string) => boolean} [accepts]
|
|
51
|
+
* @returns {() => void} stop listening
|
|
52
|
+
*/
|
|
53
|
+
listen(send, accepts) {
|
|
54
|
+
const listener = { send, accepts }
|
|
55
|
+
listeners.add(listener)
|
|
56
|
+
return () => listeners.delete(listener)
|
|
57
|
+
},
|
|
58
|
+
/** Drop every listener and anything pending (the plugin is going away). */
|
|
59
|
+
close() {
|
|
60
|
+
clearTimeout(timer)
|
|
61
|
+
timer = undefined
|
|
62
|
+
pending = []
|
|
63
|
+
listeners.clear()
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is asking. The caller comes from a `requestIdentity` service, if one is installed: any plugin
|
|
3
|
+
* that verifies the person behind an `/api` request can provide it (`current()` returns
|
|
4
|
+
* `{ id, name?, ... }` or null). The verified user id maps to a person id through the configured
|
|
5
|
+
* mapping: inline `people` and an optional `peopleFile`.
|
|
6
|
+
*
|
|
7
|
+
* Two modes:
|
|
8
|
+
* - people: a mapping is configured. A request needs a verified identity that maps to a person;
|
|
9
|
+
* without an identity provider nobody is anyone.
|
|
10
|
+
* - owner: no mapping. Everyone DSH lets in is its owner, which is what a single-person DSH wants.
|
|
11
|
+
*/
|
|
12
|
+
import { readFile, stat } from 'node:fs/promises'
|
|
13
|
+
|
|
14
|
+
/** Person ids compare without regard to case, width or surrounding space. */
|
|
15
|
+
export function personKey(value) {
|
|
16
|
+
return typeof value === 'string' ? value.trim().normalize('NFKC').toLowerCase() : ''
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Expand a leading `~/` to the home folder. */
|
|
20
|
+
export function expandHome(path, home) {
|
|
21
|
+
return typeof path === 'string' && (path === '~' || path.startsWith('~/')) ? home + path.slice(1) : path
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The configured user id → person id mapping, re-read from `peopleFile` whenever that file changes.
|
|
26
|
+
* `peopleKey` names the object inside the file that holds the mapping, when it isn't the whole file.
|
|
27
|
+
* A file that can't be read or parsed maps nobody (and is reported once per change), so a broken
|
|
28
|
+
* file never widens access. Inline entries win over the file.
|
|
29
|
+
*
|
|
30
|
+
* @param {{ people?: Record<string, string>, peopleFile?: string, peopleKey?: string }} config
|
|
31
|
+
* @param {{ warn?: (message: string) => void }} [options]
|
|
32
|
+
*/
|
|
33
|
+
export function createPeople({ people = {}, peopleFile = '', peopleKey = '' } = {}, { warn = () => {} } = {}) {
|
|
34
|
+
const inline = Object.entries(people ?? {})
|
|
35
|
+
const enabled = inline.length > 0 || peopleFile !== ''
|
|
36
|
+
let cached = { tag: undefined, entries: [] }
|
|
37
|
+
|
|
38
|
+
async function fromFile() {
|
|
39
|
+
if (peopleFile === '') return []
|
|
40
|
+
let tag
|
|
41
|
+
try {
|
|
42
|
+
const info = await stat(peopleFile)
|
|
43
|
+
tag = `${info.ino}:${info.size}:${info.mtimeMs}`
|
|
44
|
+
} catch (error) {
|
|
45
|
+
tag = `error:${error.code}`
|
|
46
|
+
}
|
|
47
|
+
if (tag === cached.tag) return cached.entries
|
|
48
|
+
let entries = []
|
|
49
|
+
try {
|
|
50
|
+
let value = JSON.parse(await readFile(peopleFile, 'utf8'))
|
|
51
|
+
if (peopleKey !== '') value = value?.[peopleKey]
|
|
52
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) throw new Error(peopleKey ? `no object at key ${peopleKey}` : 'not a JSON object')
|
|
53
|
+
entries = Object.entries(value).filter(([, person]) => typeof person === 'string')
|
|
54
|
+
} catch (error) {
|
|
55
|
+
warn(`people file ${peopleFile}: ${error.message}; nobody is mapped from it`)
|
|
56
|
+
}
|
|
57
|
+
cached = { tag, entries }
|
|
58
|
+
return entries
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
enabled,
|
|
63
|
+
/** The person id for a verified user id, or undefined. */
|
|
64
|
+
async personFor(userId) {
|
|
65
|
+
if (typeof userId !== 'string' || userId === '') return undefined
|
|
66
|
+
const person = new Map([...(await fromFile()), ...inline]).get(userId)
|
|
67
|
+
return person === undefined ? undefined : personKey(person)
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Who the current request is:
|
|
74
|
+
* - `{ mode: 'owner' }` without a mapping;
|
|
75
|
+
* - `{ mode: 'people', unavailable: true }` when a mapping is configured but no identity provider runs;
|
|
76
|
+
* - `{ mode: 'people', person, name }` otherwise, where `person` is undefined for someone the mapping
|
|
77
|
+
* doesn't know (they may see nothing) and `name` is the display name the provider gave, or null.
|
|
78
|
+
*
|
|
79
|
+
* @param {ReturnType<typeof createPeople>} people
|
|
80
|
+
* @param {{ current?: () => ({ id?: string, name?: string } | null) } | undefined} identityService
|
|
81
|
+
*/
|
|
82
|
+
export async function viewerOf(people, identityService) {
|
|
83
|
+
if (!people.enabled) return { mode: 'owner' }
|
|
84
|
+
if (typeof identityService?.current !== 'function') return { mode: 'people', unavailable: true }
|
|
85
|
+
const identity = identityService.current()
|
|
86
|
+
return { mode: 'people', person: await people.personFor(identity?.id), name: typeof identity?.name === 'string' ? identity.name : null }
|
|
87
|
+
}
|
package/icon.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="#4d6bfe" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"><path d="M3 4.5A1.5 1.5 0 0 1 4.5 3h7A1.5 1.5 0 0 1 13 4.5v5A1.5 1.5 0 0 1 11.5 11H7l-3 2.5V11h.5A1.5 1.5 0 0 1 3 9.5z"/><path d="M5.5 6.2l1.6 1.6 3.4-3.3"/></svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host row of @soffits/dsh-plugin-sdk. Its browser half (client.js) provides the page services
|
|
3
|
+
* `ctx.uiKit` and `ctx.uiActions`; the Host side has nothing to run. Host helpers are imported from
|
|
4
|
+
* the subpaths: `@soffits/dsh-plugin-sdk/host`, `/host/build`, `/channel`, `/channel/testing`.
|
|
5
|
+
*/
|
|
6
|
+
export const name = '@soffits/dsh-plugin-sdk'
|
|
7
|
+
export function apply() {}
|
package/locale/en.json
ADDED
package/locale/fr.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soffits/dsh-plugin-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared parts for DeepSeek Harness plugins: Host helpers (people, locales, change notices, event streams, the client build), chat bridge helpers, and Web page pieces (ctx.uiKit) and ask-the-assistant buttons (ctx.uiActions) as one browser service.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./host": "./host/index.js",
|
|
11
|
+
"./host/build": "./host/build.js",
|
|
12
|
+
"./channel": "./channel/index.js",
|
|
13
|
+
"./channel/testing": "./channel/testing.js",
|
|
14
|
+
"./actions/item": "./actions/src/item.js",
|
|
15
|
+
"./ui/icons.json": "./ui/icons.json",
|
|
16
|
+
"./locale/*.json": "./locale/*.json",
|
|
17
|
+
"./icon": "./icon.svg",
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"index.js",
|
|
22
|
+
"client.js",
|
|
23
|
+
"cordis.patch.yml",
|
|
24
|
+
"host/index.js",
|
|
25
|
+
"host/build.js",
|
|
26
|
+
"host/src/*.js",
|
|
27
|
+
"channel/index.js",
|
|
28
|
+
"channel/testing.js",
|
|
29
|
+
"channel/src/*.js",
|
|
30
|
+
"actions/src/item.js",
|
|
31
|
+
"ui/icons.json",
|
|
32
|
+
"locale/*.json",
|
|
33
|
+
"icon.svg",
|
|
34
|
+
"README.md",
|
|
35
|
+
"CHANGELOG.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"LICENSE-lucide",
|
|
38
|
+
"NOTICE"
|
|
39
|
+
],
|
|
40
|
+
"dsh": {
|
|
41
|
+
"bundle": {
|
|
42
|
+
"patch": "./cordis.patch.yml"
|
|
43
|
+
},
|
|
44
|
+
"client": {
|
|
45
|
+
"platform": "web",
|
|
46
|
+
"immediately": true,
|
|
47
|
+
"inject": [
|
|
48
|
+
"@deepseek-ai/dsh-client-locale",
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22",
|
|
55
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0-0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "node ui/scripts/build.mjs && node actions/scripts/build.mjs && node scripts/build-client.js",
|
|
59
|
+
"check": "node ui/scripts/build.mjs --check && node actions/scripts/build.mjs --check && node scripts/build-client.js --check && node --check index.js && node --check client.js",
|
|
60
|
+
"test": "node --test \"test/*.test.js\" \"host/test/*.test.js\" \"channel/test/*.test.js\" \"ui/test/*.test.js\" \"actions/test/*.test.js\""
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"esbuild": ">=0.25"
|
|
64
|
+
},
|
|
65
|
+
"peerDependenciesMeta": {
|
|
66
|
+
"esbuild": { "optional": true }
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@deepseek-ai/dsh-client-ui-primitives": "0.2.2-rc.1",
|
|
70
|
+
"esbuild": "^0.28.2",
|
|
71
|
+
"lucide-static": "1.74.0"
|
|
72
|
+
},
|
|
73
|
+
"keywords": ["dsh", "deepseek-harness", "dsh-plugin", "sdk"],
|
|
74
|
+
"license": "MIT",
|
|
75
|
+
"author": "Sakina",
|
|
76
|
+
"homepage": "https://github.com/soffits/dsh-plugin-sdk#readme",
|
|
77
|
+
"repository": {
|
|
78
|
+
"type": "git",
|
|
79
|
+
"url": "git+https://github.com/soffits/dsh-plugin-sdk.git"
|
|
80
|
+
},
|
|
81
|
+
"bugs": "https://github.com/soffits/dsh-plugin-sdk/issues"
|
|
82
|
+
}
|