dsh-workflow-bar 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +256 -2
- package/README.zh.md +203 -0
- package/assets/icon-256.png +0 -0
- package/assets/panel-illustration.png +0 -0
- package/cordis.patch.yml +6 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +49 -0
- package/icon.svg +19 -0
- package/lib/client.js +847 -0
- package/lib/index.js +381 -0
- package/lib/prompts.js +112 -0
- package/lib/store.js +272 -0
- package/lib/web.js +198 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +64 -4
- package/screenshots.json +3 -0
package/lib/store.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable workflow library on disk.
|
|
3
|
+
*
|
|
4
|
+
* One JSON file per workflow under `$DSH_HOME/workflows` (default
|
|
5
|
+
* `~/.dsh/workflows`), so the library survives restarts, is readable in any
|
|
6
|
+
* Session, and can be hand-edited or version-controlled without this plugin.
|
|
7
|
+
*
|
|
8
|
+
* The record is deliberately plain data: the Host half, the agent tool, and the
|
|
9
|
+
* browser panel all go through this module, so the three callers cannot drift.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-workflow-bar/store
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs'
|
|
15
|
+
import os from 'node:os'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
|
|
18
|
+
/** Lowercase slug accepted as a workflow name and as its file stem. */
|
|
19
|
+
export const NAME_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/u
|
|
20
|
+
|
|
21
|
+
/** Record format version written by this release. */
|
|
22
|
+
export const RECORD_VERSION = 1
|
|
23
|
+
|
|
24
|
+
const MAX_DESCRIPTION_CHARS = 2000
|
|
25
|
+
const MAX_HINT_CHARS = 2000
|
|
26
|
+
const MAX_DEFAULT_INPUT_CHARS = 500
|
|
27
|
+
const MAX_SCRIPT_BYTES = 256 * 1024
|
|
28
|
+
|
|
29
|
+
/** Typed library failure the commands, the tool, and the panel can render. */
|
|
30
|
+
export class WorkflowStoreError extends Error {
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} code - stable machine code (`invalid-name`, `invalid-record`, `exists`, `not-found`, `io`).
|
|
33
|
+
* @param {string} message - user-facing explanation.
|
|
34
|
+
*/
|
|
35
|
+
constructor(code, message) {
|
|
36
|
+
super(message)
|
|
37
|
+
this.name = 'WorkflowStoreError'
|
|
38
|
+
this.code = code
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve the library directory.
|
|
44
|
+
*
|
|
45
|
+
* Precedence: an explicit configured directory, this plugin's own override
|
|
46
|
+
* variable, `$DSH_HOME`, then the operating-system home joined with `.dsh`.
|
|
47
|
+
* @param {string | undefined} configured - the plugin row's `directory` config value.
|
|
48
|
+
* @returns {string} absolute library directory (not created here).
|
|
49
|
+
*/
|
|
50
|
+
export function resolveLibraryDir(configured) {
|
|
51
|
+
const explicit = typeof configured === 'string' ? configured.trim() : ''
|
|
52
|
+
if (explicit.length > 0) return path.resolve(explicit)
|
|
53
|
+
const override = process.env.DSH_WORKFLOW_BAR_DIR?.trim()
|
|
54
|
+
if (override) return path.resolve(override)
|
|
55
|
+
const home = process.env.DSH_HOME?.trim()
|
|
56
|
+
const root = home && home.length > 0 ? path.resolve(home) : path.join(os.homedir(), '.dsh')
|
|
57
|
+
return path.join(root, 'workflows')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Reject a name that cannot be a workflow slug.
|
|
62
|
+
* @param {unknown} value - candidate name.
|
|
63
|
+
* @returns {string} the trimmed name.
|
|
64
|
+
*/
|
|
65
|
+
function assertName(value) {
|
|
66
|
+
const name = typeof value === 'string' ? value.trim() : ''
|
|
67
|
+
if (!NAME_PATTERN.test(name)) {
|
|
68
|
+
throw new WorkflowStoreError(
|
|
69
|
+
'invalid-name',
|
|
70
|
+
'Workflow name must be a lowercase slug of letters, digits, "-", or "_" (max 64 characters, starting with a letter).',
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
return name
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Trim a possibly-absent optional text field into a bounded string. */
|
|
77
|
+
function text(value, limit) {
|
|
78
|
+
if (typeof value !== 'string') return ''
|
|
79
|
+
return value.trim().slice(0, limit)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Validate and canonicalize one workflow record.
|
|
84
|
+
* @param {unknown} input - untrusted record fields.
|
|
85
|
+
* @param {{ previous?: Record<string, unknown>, stampUpdatedAt?: boolean }} options - the stored
|
|
86
|
+
* record this one replaces, and whether this call is a write (only a write re-stamps `updatedAt`).
|
|
87
|
+
* @returns {object} canonical record ready to persist.
|
|
88
|
+
*/
|
|
89
|
+
export function normalizeRecord(input, options = {}) {
|
|
90
|
+
if (typeof input !== 'object' || input === null) {
|
|
91
|
+
throw new WorkflowStoreError('invalid-record', 'Workflow record must be an object.')
|
|
92
|
+
}
|
|
93
|
+
const raw = /** @type {Record<string, unknown>} */ (input)
|
|
94
|
+
const name = assertName(raw.name)
|
|
95
|
+
const script = typeof raw.script === 'string' ? raw.script.trim() : ''
|
|
96
|
+
if (script.length === 0) {
|
|
97
|
+
throw new WorkflowStoreError('invalid-record', 'Workflow script must not be empty.')
|
|
98
|
+
}
|
|
99
|
+
if (Buffer.byteLength(script, 'utf8') > MAX_SCRIPT_BYTES) {
|
|
100
|
+
throw new WorkflowStoreError('invalid-record', `Workflow script exceeds ${MAX_SCRIPT_BYTES} bytes.`)
|
|
101
|
+
}
|
|
102
|
+
const description = text(raw.description, MAX_DESCRIPTION_CHARS)
|
|
103
|
+
if (description.length === 0) {
|
|
104
|
+
throw new WorkflowStoreError('invalid-record', 'Workflow description must not be empty.')
|
|
105
|
+
}
|
|
106
|
+
const now = new Date().toISOString()
|
|
107
|
+
const previousCreatedAt = typeof options.previous?.createdAt === 'string' ? options.previous.createdAt : undefined
|
|
108
|
+
const previousUpdatedAt = typeof options.previous?.updatedAt === 'string' ? options.previous.updatedAt : undefined
|
|
109
|
+
const createdAt = previousCreatedAt ?? now
|
|
110
|
+
return {
|
|
111
|
+
version: RECORD_VERSION,
|
|
112
|
+
name,
|
|
113
|
+
description,
|
|
114
|
+
whenToUse: text(raw.whenToUse, MAX_HINT_CHARS),
|
|
115
|
+
argsHint: text(raw.argsHint, MAX_HINT_CHARS),
|
|
116
|
+
// Used only when an invocation carries no input, so a picker choice can run
|
|
117
|
+
// without a second typing step.
|
|
118
|
+
defaultInput: text(raw.defaultInput, MAX_DEFAULT_INPUT_CHARS),
|
|
119
|
+
script,
|
|
120
|
+
createdAt,
|
|
121
|
+
// Only a write stamps a new time. Reading must not, or every listing would
|
|
122
|
+
// report "updated just now" and the panel's 更新时间 would be meaningless.
|
|
123
|
+
updatedAt: options.stampUpdatedAt === true ? now : previousUpdatedAt ?? createdAt,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** File stem for one validated workflow name. */
|
|
128
|
+
function fileName(name) {
|
|
129
|
+
return `${name}.json`
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The workflow library directory and its operations. */
|
|
133
|
+
export class WorkflowStore {
|
|
134
|
+
/** @param {{ directory?: string }} [options] - configured directory override. */
|
|
135
|
+
constructor(options = {}) {
|
|
136
|
+
this.directory = resolveLibraryDir(options.directory)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Create the directory on first write. */
|
|
140
|
+
#ensureDir() {
|
|
141
|
+
try {
|
|
142
|
+
fs.mkdirSync(this.directory, { recursive: true })
|
|
143
|
+
} catch (error) {
|
|
144
|
+
throw new WorkflowStoreError('io', `Cannot create ${this.directory}: ${message(error)}`)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** @param {string} name - candidate name. @returns {string} absolute file path. */
|
|
149
|
+
#filePath(name) {
|
|
150
|
+
if (!NAME_PATTERN.test(name)) {
|
|
151
|
+
throw new WorkflowStoreError('invalid-name', `Unknown workflow "${name}".`)
|
|
152
|
+
}
|
|
153
|
+
return path.join(this.directory, fileName(name))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Summaries of every readable workflow, sorted by name.
|
|
158
|
+
* Broken files are reported instead of failing the whole library.
|
|
159
|
+
* @returns {{ workflows: object[], broken: { file: string, message: string }[] }}
|
|
160
|
+
*/
|
|
161
|
+
list() {
|
|
162
|
+
/** @type {object[]} */
|
|
163
|
+
const workflows = []
|
|
164
|
+
/** @type {{ file: string, message: string }[]} */
|
|
165
|
+
const broken = []
|
|
166
|
+
let entries = []
|
|
167
|
+
try {
|
|
168
|
+
entries = fs.readdirSync(this.directory, { withFileTypes: true })
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (error && error.code === 'ENOENT') return { workflows, broken }
|
|
171
|
+
throw new WorkflowStoreError('io', `Cannot read ${this.directory}: ${message(error)}`)
|
|
172
|
+
}
|
|
173
|
+
for (const entry of entries) {
|
|
174
|
+
if (!entry.isFile() || !entry.name.endsWith('.json')) continue
|
|
175
|
+
try {
|
|
176
|
+
const record = JSON.parse(fs.readFileSync(path.join(this.directory, entry.name), 'utf8'))
|
|
177
|
+
const normalized = normalizeRecord(record, { previous: record })
|
|
178
|
+
workflows.push({
|
|
179
|
+
name: normalized.name,
|
|
180
|
+
description: normalized.description,
|
|
181
|
+
whenToUse: normalized.whenToUse,
|
|
182
|
+
argsHint: normalized.argsHint,
|
|
183
|
+
defaultInput: normalized.defaultInput,
|
|
184
|
+
bytes: Buffer.byteLength(normalized.script, 'utf8'),
|
|
185
|
+
updatedAt: normalized.updatedAt,
|
|
186
|
+
})
|
|
187
|
+
} catch (error) {
|
|
188
|
+
broken.push({ file: entry.name, message: message(error) })
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
workflows.sort((left, right) => left.name.localeCompare(right.name))
|
|
192
|
+
return { workflows, broken }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Read one complete record.
|
|
197
|
+
* @param {string} name - workflow name.
|
|
198
|
+
* @returns {object} canonical record.
|
|
199
|
+
*/
|
|
200
|
+
read(name) {
|
|
201
|
+
const file = this.#filePath(name)
|
|
202
|
+
let raw
|
|
203
|
+
try {
|
|
204
|
+
raw = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (error && error.code === 'ENOENT') {
|
|
207
|
+
throw new WorkflowStoreError('not-found', `No workflow named "${name}".`)
|
|
208
|
+
}
|
|
209
|
+
throw new WorkflowStoreError('io', `Cannot read ${file}: ${message(error)}`)
|
|
210
|
+
}
|
|
211
|
+
return normalizeRecord(raw, { previous: raw })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Persist one workflow.
|
|
216
|
+
* @param {unknown} input - untrusted record fields.
|
|
217
|
+
* @param {{ overwrite?: boolean }} [options] - whether an existing record may be replaced.
|
|
218
|
+
* @returns {object} the stored record.
|
|
219
|
+
*/
|
|
220
|
+
save(input, options = {}) {
|
|
221
|
+
const name = assertName(/** @type {any} */ (input)?.name)
|
|
222
|
+
const file = path.join(this.directory, fileName(name))
|
|
223
|
+
let previous
|
|
224
|
+
try {
|
|
225
|
+
previous = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (!error || error.code !== 'ENOENT') previous = undefined
|
|
228
|
+
}
|
|
229
|
+
if (previous !== undefined && options.overwrite !== true) {
|
|
230
|
+
throw new WorkflowStoreError('exists', `Workflow "${name}" already exists. Pass overwrite to replace it.`)
|
|
231
|
+
}
|
|
232
|
+
const record = normalizeRecord(input, { previous, stampUpdatedAt: true })
|
|
233
|
+
this.#ensureDir()
|
|
234
|
+
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`
|
|
235
|
+
try {
|
|
236
|
+
fs.writeFileSync(temporary, `${JSON.stringify(record, null, 2)}\n`, 'utf8')
|
|
237
|
+
fs.rmSync(file, { force: true })
|
|
238
|
+
fs.renameSync(temporary, file)
|
|
239
|
+
} catch (error) {
|
|
240
|
+
try {
|
|
241
|
+
fs.rmSync(temporary, { force: true })
|
|
242
|
+
} catch {
|
|
243
|
+
// The temporary file is best-effort cleanup; the write error is the report.
|
|
244
|
+
}
|
|
245
|
+
throw new WorkflowStoreError('io', `Cannot write ${file}: ${message(error)}`)
|
|
246
|
+
}
|
|
247
|
+
return record
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Remove one workflow.
|
|
252
|
+
* @param {string} name - workflow name.
|
|
253
|
+
* @returns {{ name: string }} the removed name.
|
|
254
|
+
*/
|
|
255
|
+
remove(name) {
|
|
256
|
+
const file = this.#filePath(name)
|
|
257
|
+
try {
|
|
258
|
+
fs.rmSync(file)
|
|
259
|
+
} catch (error) {
|
|
260
|
+
if (error && error.code === 'ENOENT') {
|
|
261
|
+
throw new WorkflowStoreError('not-found', `No workflow named "${name}".`)
|
|
262
|
+
}
|
|
263
|
+
throw new WorkflowStoreError('io', `Cannot remove ${file}: ${message(error)}`)
|
|
264
|
+
}
|
|
265
|
+
return { name }
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Human-readable error text for a caught value. */
|
|
270
|
+
function message(error) {
|
|
271
|
+
return error instanceof Error ? error.message : String(error)
|
|
272
|
+
}
|
package/lib/web.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Same-origin JSON API between the browser half and the Host half.
|
|
3
|
+
*
|
|
4
|
+
* One POST route, `/workflow-bar/api`, with a method whitelist so the browser
|
|
5
|
+
* can never reflect an arbitrary call into the Host. Every request passes a
|
|
6
|
+
* trust fence: loopback or trusted host, not cross-site, and an Origin that
|
|
7
|
+
* matches the Host header.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-workflow-bar/web
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Browser-callable operations: method name to implementation. */
|
|
13
|
+
const METHODS = new Map([
|
|
14
|
+
['list', (api, params) => api.list(params)],
|
|
15
|
+
['detail', (api, params) => api.detail(params)],
|
|
16
|
+
['save', (api, params) => api.save(params)],
|
|
17
|
+
['remove', (api, params) => api.remove(params)],
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
/** Read one header as a single string. */
|
|
21
|
+
function header(headers, name) {
|
|
22
|
+
const value = headers?.[name]
|
|
23
|
+
return Array.isArray(value) ? value[0] : value
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Parse an `authority[:port]` value into its parts. */
|
|
27
|
+
function parseAuthority(host) {
|
|
28
|
+
if (typeof host !== 'string' || host === '') return undefined
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(`http://${host}`)
|
|
31
|
+
return url.hostname ? { hostname: url.hostname, host: url.host } : undefined
|
|
32
|
+
} catch {
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Whether a hostname names this machine's loopback interface. */
|
|
38
|
+
function isLoopbackHostname(hostname) {
|
|
39
|
+
const name = hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname
|
|
40
|
+
return name === 'localhost'
|
|
41
|
+
|| name === '127.0.0.1'
|
|
42
|
+
|| name === '::1'
|
|
43
|
+
|| name === '0:0:0:0:0:0:0:1'
|
|
44
|
+
|| name.endsWith('.localhost')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether the authority matches one of the deployment's trusted hosts. */
|
|
48
|
+
function isTrustedAuthority(url, trustedHosts) {
|
|
49
|
+
for (const raw of trustedHosts ?? []) {
|
|
50
|
+
if (typeof raw !== 'string') continue
|
|
51
|
+
const authority = parseAuthority(raw)
|
|
52
|
+
if (authority && authority.host === url.host) return true
|
|
53
|
+
}
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Whether a request may reach the API: loopback or trusted Host, not
|
|
59
|
+
* cross-site, and same-origin when the browser sends an Origin.
|
|
60
|
+
* @param {import('node:http').IncomingMessage} request - incoming request.
|
|
61
|
+
* @param {readonly string[]} trustedHosts - deployment-accepted authorities.
|
|
62
|
+
* @returns {boolean} whether the request is trusted.
|
|
63
|
+
*/
|
|
64
|
+
export function isTrustedWebRequest(request, trustedHosts = []) {
|
|
65
|
+
const host = header(request.headers, 'host')
|
|
66
|
+
if (!host) return false
|
|
67
|
+
const hostUrl = parseAuthority(host)
|
|
68
|
+
if (!hostUrl) return false
|
|
69
|
+
if (!isLoopbackHostname(hostUrl.hostname) && !isTrustedAuthority(hostUrl, trustedHosts)) return false
|
|
70
|
+
if (header(request.headers, 'sec-fetch-site') === 'cross-site') return false
|
|
71
|
+
const origin = header(request.headers, 'origin')
|
|
72
|
+
if (origin === undefined) return true
|
|
73
|
+
try {
|
|
74
|
+
return new URL(origin).host === hostUrl.host
|
|
75
|
+
} catch {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Write one JSON response with no-store and nosniff. */
|
|
81
|
+
function writeJson(res, status, value) {
|
|
82
|
+
const body = JSON.stringify(value)
|
|
83
|
+
res.writeHead(status, {
|
|
84
|
+
'content-type': 'application/json; charset=utf-8',
|
|
85
|
+
'cache-control': 'no-store',
|
|
86
|
+
'x-content-type-options': 'nosniff',
|
|
87
|
+
'referrer-policy': 'no-referrer',
|
|
88
|
+
'content-length': Buffer.byteLength(body),
|
|
89
|
+
})
|
|
90
|
+
res.end(body)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Read and parse a bounded JSON body. */
|
|
94
|
+
async function readJsonBody(req) {
|
|
95
|
+
const chunks = []
|
|
96
|
+
let length = 0
|
|
97
|
+
for await (const chunk of req) {
|
|
98
|
+
length += chunk.length
|
|
99
|
+
if (length > 1024 * 1024) throw new Error('Request body exceeds 1 MiB.')
|
|
100
|
+
chunks.push(chunk)
|
|
101
|
+
}
|
|
102
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
103
|
+
if (!text.trim()) return {}
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(text)
|
|
106
|
+
} catch {
|
|
107
|
+
throw new Error('Request body is not valid JSON.')
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** One refused or failed request as a JSON body. */
|
|
112
|
+
function errorPayload(error) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
code: typeof error?.code === 'string' ? error.code : 'error',
|
|
116
|
+
message: error instanceof Error ? error.message : String(error),
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Mount the route on one web server context. */
|
|
121
|
+
function mount(wctx, api, { logger, trustedHosts = [] } = {}) {
|
|
122
|
+
return wctx.webServer.register({
|
|
123
|
+
kind: 'prefix',
|
|
124
|
+
path: '/workflow-bar/api',
|
|
125
|
+
handler: async (req, res) => {
|
|
126
|
+
if (!isTrustedWebRequest(req, trustedHosts)) {
|
|
127
|
+
writeJson(res, 403, { ok: false, code: 'forbidden', message: 'forbidden' })
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
if (req.method !== 'POST') {
|
|
131
|
+
res.writeHead(405, { allow: 'POST' })
|
|
132
|
+
res.end()
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
let method = ''
|
|
136
|
+
try {
|
|
137
|
+
const body = await readJsonBody(req)
|
|
138
|
+
method = typeof body?.method === 'string' ? body.method : ''
|
|
139
|
+
const dispatch = METHODS.get(method)
|
|
140
|
+
if (!dispatch) {
|
|
141
|
+
writeJson(res, 400, { ok: false, code: 'unknown-method', message: `Unknown method "${method}".` })
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
writeJson(res, 200, { ok: true, ...(await dispatch(api, body?.params ?? {})) })
|
|
145
|
+
} catch (error) {
|
|
146
|
+
logger?.warn?.(`workflow-bar api ${method} failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
147
|
+
const status = error?.code === 'not-found' ? 404 : 400
|
|
148
|
+
writeJson(res, status, errorPayload(error))
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Mount the API once a web server exists in the composition.
|
|
156
|
+
*
|
|
157
|
+
* Accepts either service name a deployment may supply (`webStartup` on the
|
|
158
|
+
* Web/Desktop bundles, `webRuntime` on older ones); whichever resolves first
|
|
159
|
+
* owns the route until it goes away.
|
|
160
|
+
* @param {any} ctx - registrant context.
|
|
161
|
+
* @param {object} api - operation implementations.
|
|
162
|
+
* @param {{ logger?: any }} [options] - diagnostics.
|
|
163
|
+
* @returns {void}
|
|
164
|
+
*/
|
|
165
|
+
export function injectWebRoutes(ctx, api, options = {}) {
|
|
166
|
+
const candidates = new Map()
|
|
167
|
+
let active
|
|
168
|
+
let dispose
|
|
169
|
+
const reconcile = () => {
|
|
170
|
+
const next = candidates.get('webStartup') ?? candidates.get('webRuntime')
|
|
171
|
+
if (next === active) return
|
|
172
|
+
dispose?.()
|
|
173
|
+
dispose = undefined
|
|
174
|
+
active = next
|
|
175
|
+
if (next) {
|
|
176
|
+
dispose = mount(next.wctx, api, { ...options, trustedHosts: next.trustedHosts })
|
|
177
|
+
options.logger?.info?.('workflow-bar API mounted at /workflow-bar/api')
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
for (const name of ['webStartup', 'webRuntime']) {
|
|
182
|
+
ctx.inject(['webServer', name], (wctx) => {
|
|
183
|
+
if (typeof wctx?.webServer?.register !== 'function') {
|
|
184
|
+
options.logger?.warn?.(`workflow-bar: "${name}" resolved without a web server; API not mounted`)
|
|
185
|
+
return undefined
|
|
186
|
+
}
|
|
187
|
+
return wctx.effect(() => {
|
|
188
|
+
const candidate = { wctx, trustedHosts: wctx[name]?.trustedHosts ?? [] }
|
|
189
|
+
candidates.set(name, candidate)
|
|
190
|
+
reconcile()
|
|
191
|
+
return () => {
|
|
192
|
+
if (candidates.get(name) === candidate) candidates.delete(name)
|
|
193
|
+
reconcile()
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Workflow bar",
|
|
4
|
+
"description": "Save workflow scripts once and run them by name. The left sidebar row \"Workflow 库\" opens a library page that lists, creates, edits, and deletes saved workflows; \"/workflow-add <requirement>\" starts a creation flow where the agent writes the script and saves it; \"/workflow-list\" lists the library; \"/workflow <name> <input>\" hands a saved script to the agent's workflow tool. Workflows are JSON files under ~/.dsh/workflows (or the directory this row configures), so they survive restarts and can be edited or version-controlled outside DSH. Running a workflow costs one agent turn plus its subagents; the panel itself costs no model tokens. The run path needs an agent preset that mounts the workflow tool: standard and cordis do, ptc and minimal do not.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Workflow 库",
|
|
4
|
+
"description": "把 workflow 脚本存一次,之后按名字直接跑。左侧边栏的「Workflow 库」打开管理页,可以列出、新建、编辑、删除已注册的 workflow;\"/workflow-add <需求描述>\" 触发创建流程,由 agent 写脚本并保存;\"/workflow-list\" 列出整个库;\"/workflow <名称> <输入>\" 把已保存的脚本交给 agent 的 workflow 工具执行。workflow 以 JSON 文件保存在 ~/.dsh/workflows(或本行配置的目录),重启后仍在,也可以脱离 DSH 直接编辑或纳入版本管理。运行一次会消耗一轮 agent 对话及其子代理的 token,面板本身不消耗模型 token。运行路径需要挂载 workflow 工具的 agent preset:standard 和 cordis 可以,ptc 和 minimal 不行。",
|
|
5
|
+
"detail": "docs/DETAIL.zh.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workflow-bar",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Saved-workflow library for DeepSeek Harness: create workflows once, run them with /workflow, manage them from a left-sidebar bar.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./lib/client.js",
|
|
10
|
+
"./locale/*.json": "./locale/*.json",
|
|
11
|
+
"./icon": "./icon.svg"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib",
|
|
15
|
+
"cordis.patch.yml",
|
|
16
|
+
"locale/*.json",
|
|
17
|
+
"docs",
|
|
18
|
+
"assets",
|
|
19
|
+
"icon.svg",
|
|
20
|
+
"screenshots.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"README.zh.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"dsh": {
|
|
26
|
+
"bundle": { "patch": "./cordis.patch.yml" },
|
|
27
|
+
"client": {
|
|
28
|
+
"platform": "web",
|
|
29
|
+
"immediately": true,
|
|
30
|
+
"inject": [
|
|
31
|
+
"@deepseek-ai/dsh-client-ui-layout",
|
|
32
|
+
"@deepseek-ai/dsh-client-ui-sidebar"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"dsh": ">=0.2.2-rc.1"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"deepseek",
|
|
41
|
+
"deepseek-harness",
|
|
42
|
+
"dsh",
|
|
43
|
+
"dsh-plugin",
|
|
44
|
+
"workflow",
|
|
45
|
+
"workflows",
|
|
46
|
+
"slash-command",
|
|
47
|
+
"sidebar",
|
|
48
|
+
"automation",
|
|
49
|
+
"orchestration"
|
|
50
|
+
],
|
|
51
|
+
"author": "Jaycebox",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"homepage": "https://github.com/Jaycebox/dsh-workflow-bar#readme",
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/Jaycebox/dsh-workflow-bar.git"
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/Jaycebox/dsh-workflow-bar/issues"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"test": "node test/client-smoke.mjs",
|
|
63
|
+
"check": "node --check lib/index.js && node --check lib/client.js && node --check lib/store.js && node --check lib/web.js && node --check lib/prompts.js && node test/client-smoke.mjs",
|
|
64
|
+
"index": "node scripts/make-index.mjs"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/screenshots.json
ADDED