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/index.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-workflow-bar — Host half.
|
|
3
|
+
*
|
|
4
|
+
* Owns the saved-workflow library and exposes it through three surfaces that
|
|
5
|
+
* all call the same store:
|
|
6
|
+
* - the human command plane (`/workflow`, `/workflow-list`, `/workflow-add`);
|
|
7
|
+
* - one model-facing tool (`workflow_library`) so the agent can list and save
|
|
8
|
+
* workflows during a creation flow;
|
|
9
|
+
* - a same-origin JSON API the browser panel reads and writes through.
|
|
10
|
+
*
|
|
11
|
+
* Running a saved workflow is delegated to the agent: the workflow engine is
|
|
12
|
+
* composed inside each Agent preset, so this Host row hands the stored script
|
|
13
|
+
* to the agent's `workflow` tool instead of reaching for an isolated service.
|
|
14
|
+
*
|
|
15
|
+
* This module deliberately imports nothing from the Harness: a bundle linked
|
|
16
|
+
* from a workspace directory resolves its imports from that directory, where
|
|
17
|
+
* no `@deepseek-ai/*` packages exist. The two contracts it uses are narrow and
|
|
18
|
+
* stable — the tool-definition object shape, and an identified user message —
|
|
19
|
+
* so both are built locally.
|
|
20
|
+
*
|
|
21
|
+
* @module dsh-workflow-bar
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { randomUUID } from 'node:crypto'
|
|
25
|
+
import os from 'node:os'
|
|
26
|
+
|
|
27
|
+
import { buildCreateFollowup, buildRunFollowup, renderList } from './prompts.js'
|
|
28
|
+
import { WorkflowStore, WorkflowStoreError } from './store.js'
|
|
29
|
+
import { injectWebRoutes } from './web.js'
|
|
30
|
+
|
|
31
|
+
export const name = 'workflow-bar'
|
|
32
|
+
|
|
33
|
+
/** The human command plane and the tool registry this plugin contributes to. */
|
|
34
|
+
export const inject = ['commands', 'tools']
|
|
35
|
+
|
|
36
|
+
/** @typedef {{ directory?: string, summarySection?: boolean }} WorkflowBarConfig */
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Standing instruction appended after the workflow tool's own guidance.
|
|
40
|
+
*
|
|
41
|
+
* A workflow returns one big JSON value; without a rule the reply either
|
|
42
|
+
* repeats it or says nothing. This makes the closing summary part of every
|
|
43
|
+
* request instead of something each follow-up has to ask for.
|
|
44
|
+
*/
|
|
45
|
+
const SUMMARY_SECTION = {
|
|
46
|
+
name: 'workflow-bar: run summary',
|
|
47
|
+
// `TOOL_WORKFLOW` is 2600; this sits immediately after it.
|
|
48
|
+
order: 2610,
|
|
49
|
+
text: 'After a workflow tool call returns, end your reply with a short summary of that run: '
|
|
50
|
+
+ 'one sentence stating the outcome, then up to three key facts or numbers taken from the returned value. '
|
|
51
|
+
+ 'Mention any file or artifact the run produced, or the next step if it failed. '
|
|
52
|
+
+ 'Never paste the raw JSON result.',
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Recursively freeze a plain JSON value, matching the messages Harness builds. */
|
|
56
|
+
function deepFreeze(value) {
|
|
57
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
58
|
+
Object.freeze(value)
|
|
59
|
+
for (const key of Object.keys(value)) deepFreeze(value[key])
|
|
60
|
+
}
|
|
61
|
+
return value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build the identified user message `agent.followup()` receives.
|
|
66
|
+
*
|
|
67
|
+
* Mirrors `createUserMessage` from the Harness message module: a fresh UUID
|
|
68
|
+
* identity, the user role, the content blocks, and a user source, deep-frozen.
|
|
69
|
+
* @param {string} text - the follow-up prompt.
|
|
70
|
+
* @returns {object} an identified user message.
|
|
71
|
+
*/
|
|
72
|
+
function userMessage(text) {
|
|
73
|
+
return deepFreeze({
|
|
74
|
+
id: randomUUID(),
|
|
75
|
+
role: 'user',
|
|
76
|
+
content: [{ type: 'text', text }],
|
|
77
|
+
source: { kind: 'user' },
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Render the library directory symbolically for user-facing text. */
|
|
82
|
+
function displayDirectory(directory) {
|
|
83
|
+
const home = os.homedir()
|
|
84
|
+
if (directory === home) return '~'
|
|
85
|
+
const separator = os.platform() === 'win32' ? '\\' : '/'
|
|
86
|
+
if (directory.startsWith(`${home}${separator}`)) return `~${directory.slice(home.length)}`
|
|
87
|
+
return directory
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One command result with a success outcome. */
|
|
91
|
+
function ok(text) {
|
|
92
|
+
return { kind: 'success', text }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** One command result with an error outcome. */
|
|
96
|
+
function fail(text) {
|
|
97
|
+
return { kind: 'error', text }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Message text for any caught value. */
|
|
101
|
+
function message(error) {
|
|
102
|
+
return error instanceof Error ? error.message : String(error)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A workflow summary as the tool reports it. */
|
|
106
|
+
function summaryOf(workflow) {
|
|
107
|
+
return {
|
|
108
|
+
name: workflow.name,
|
|
109
|
+
description: workflow.description,
|
|
110
|
+
whenToUse: workflow.whenToUse,
|
|
111
|
+
argsHint: workflow.argsHint,
|
|
112
|
+
defaultInput: workflow.defaultInput,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Parse `/workflow <name> [input]`.
|
|
118
|
+
* @param {string} rawInput - text after the command name.
|
|
119
|
+
* @returns {{ name: string, input: string } | undefined} the parsed call.
|
|
120
|
+
*/
|
|
121
|
+
function parseRun(rawInput) {
|
|
122
|
+
const trimmed = rawInput.trim()
|
|
123
|
+
if (trimmed.length === 0) return undefined
|
|
124
|
+
const match = /^([^\s]+)(?:[\s]+([\s\S]*))?$/u.exec(trimmed)
|
|
125
|
+
if (match === null) return undefined
|
|
126
|
+
return { name: match[1], input: (match[2] ?? '').trim() }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Register the model-facing library tool.
|
|
131
|
+
* @param {any} ctx - registrant context.
|
|
132
|
+
* @param {WorkflowStore} store - the library.
|
|
133
|
+
* @returns {void}
|
|
134
|
+
*/
|
|
135
|
+
function registerTool(ctx, store) {
|
|
136
|
+
const tool = {
|
|
137
|
+
name: 'workflow_library',
|
|
138
|
+
description:
|
|
139
|
+
'Read and write the user\'s saved workflow library (the Workflow Bar). '
|
|
140
|
+
+ 'action="list" returns every saved workflow with its description and input hint; '
|
|
141
|
+
+ 'call it before saving so names do not collide. '
|
|
142
|
+
+ 'action="save" persists one workflow script the user asked for: supply name (lowercase slug), '
|
|
143
|
+
+ 'description (one line), script (a workflow-tool script body: plain JavaScript with top-level await, '
|
|
144
|
+
+ 'the agent()/parallel()/pipeline()/phase()/log() hooks, and a final JSON return value), plus optional '
|
|
145
|
+
+ 'whenToUse and argsHint. The user\'s run input reaches the script as args.input. '
|
|
146
|
+
+ 'Saving an existing name fails unless overwrite is true. Deleting a workflow is user-only.',
|
|
147
|
+
// JSON Schema for the arguments. The model sees exactly this.
|
|
148
|
+
parameters: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
additionalProperties: false,
|
|
151
|
+
required: ['action'],
|
|
152
|
+
properties: {
|
|
153
|
+
action: {
|
|
154
|
+
type: 'string',
|
|
155
|
+
enum: ['list', 'save'],
|
|
156
|
+
description: 'list (read the library) | save (persist one workflow).',
|
|
157
|
+
},
|
|
158
|
+
name: { type: 'string', description: 'save: lowercase slug of letters, digits, "-", or "_".' },
|
|
159
|
+
description: { type: 'string', description: 'save: one sentence describing what the workflow does.' },
|
|
160
|
+
whenToUse: { type: 'string', description: 'save: when a user should reach for this workflow.' },
|
|
161
|
+
argsHint: {
|
|
162
|
+
type: 'string',
|
|
163
|
+
description: 'save: what the user types as <输入>; the script reads it as args.input.',
|
|
164
|
+
},
|
|
165
|
+
defaultInput: {
|
|
166
|
+
type: 'string',
|
|
167
|
+
description: 'save: input used when an invocation supplies none, so picking this workflow can run it immediately.',
|
|
168
|
+
},
|
|
169
|
+
script: { type: 'string', description: 'save: the workflow script body, exactly as it should run.' },
|
|
170
|
+
overwrite: { type: 'boolean', description: 'save: replace an existing workflow with this name.' },
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
output: {
|
|
174
|
+
schema: {
|
|
175
|
+
type: 'object',
|
|
176
|
+
additionalProperties: false,
|
|
177
|
+
properties: {
|
|
178
|
+
workflows: {
|
|
179
|
+
type: 'array',
|
|
180
|
+
items: {
|
|
181
|
+
type: 'object',
|
|
182
|
+
additionalProperties: false,
|
|
183
|
+
properties: {
|
|
184
|
+
name: { type: 'string' },
|
|
185
|
+
description: { type: 'string' },
|
|
186
|
+
whenToUse: { type: 'string' },
|
|
187
|
+
argsHint: { type: 'string' },
|
|
188
|
+
defaultInput: { type: 'string' },
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
saved: {
|
|
193
|
+
type: 'object',
|
|
194
|
+
additionalProperties: false,
|
|
195
|
+
properties: {
|
|
196
|
+
name: { type: 'string' },
|
|
197
|
+
directory: { type: 'string' },
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
text: { type: 'string' },
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
204
|
+
},
|
|
205
|
+
execute(args) {
|
|
206
|
+
if (typeof args !== 'object' || args === null) {
|
|
207
|
+
throw new Error('workflow_library requires an arguments object with an "action".')
|
|
208
|
+
}
|
|
209
|
+
const directory = displayDirectory(store.directory)
|
|
210
|
+
if (args.action === 'list') {
|
|
211
|
+
const library = store.list()
|
|
212
|
+
const lines = library.workflows.length === 0
|
|
213
|
+
? [`No saved workflows yet (${directory}).`]
|
|
214
|
+
: [
|
|
215
|
+
`Saved workflows (${library.workflows.length}) in ${directory}:`,
|
|
216
|
+
...library.workflows.map(workflow => workflow.argsHint
|
|
217
|
+
? `- ${workflow.name} — ${workflow.description} · input: ${workflow.argsHint}`
|
|
218
|
+
: `- ${workflow.name} — ${workflow.description}`),
|
|
219
|
+
]
|
|
220
|
+
if (library.broken.length > 0) {
|
|
221
|
+
lines.push(`Unreadable files: ${library.broken.map(entry => entry.file).join(', ')}`)
|
|
222
|
+
}
|
|
223
|
+
return { workflows: library.workflows.map(summaryOf), text: lines.join('\n') }
|
|
224
|
+
}
|
|
225
|
+
if (args.action === 'save') {
|
|
226
|
+
if (typeof args.script !== 'string' || args.script.trim().length === 0) {
|
|
227
|
+
throw new Error('save requires a non-empty "script" string.')
|
|
228
|
+
}
|
|
229
|
+
const record = store.save({
|
|
230
|
+
name: args.name,
|
|
231
|
+
description: args.description,
|
|
232
|
+
whenToUse: args.whenToUse,
|
|
233
|
+
argsHint: args.argsHint,
|
|
234
|
+
defaultInput: args.defaultInput,
|
|
235
|
+
script: args.script,
|
|
236
|
+
}, { overwrite: args.overwrite === true })
|
|
237
|
+
return {
|
|
238
|
+
workflows: [],
|
|
239
|
+
saved: { name: record.name, directory },
|
|
240
|
+
text: `Saved workflow "${record.name}" in ${directory}. Run it with /workflow ${record.name} <input>.`,
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
throw new Error(`unknown workflow_library action: ${String(args.action)} (expected "list" or "save")`)
|
|
244
|
+
},
|
|
245
|
+
}
|
|
246
|
+
ctx.effect(() => ctx.tools.register(tool), 'workflow-bar: workflow_library tool')
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Register the human slash commands.
|
|
251
|
+
* @param {any} ctx - registrant context.
|
|
252
|
+
* @param {WorkflowStore} store - the library.
|
|
253
|
+
* @returns {void}
|
|
254
|
+
*/
|
|
255
|
+
function registerCommands(ctx, store) {
|
|
256
|
+
ctx.effect(() => ctx.commands.register({
|
|
257
|
+
name: 'workflow-list',
|
|
258
|
+
description: '列出已保存的 workflow,可带查询词过滤。',
|
|
259
|
+
input: { hint: '[查询词]' },
|
|
260
|
+
handler: ({ rawInput }) => {
|
|
261
|
+
const query = rawInput.trim()
|
|
262
|
+
const library = store.list()
|
|
263
|
+
const filtered = query.length === 0
|
|
264
|
+
? library
|
|
265
|
+
: {
|
|
266
|
+
workflows: library.workflows.filter(workflow =>
|
|
267
|
+
workflow.name.includes(query.toLowerCase())
|
|
268
|
+
|| workflow.description.toLowerCase().includes(query.toLowerCase())),
|
|
269
|
+
broken: library.broken,
|
|
270
|
+
}
|
|
271
|
+
return ok(renderList(filtered, query))
|
|
272
|
+
},
|
|
273
|
+
}), 'workflow-bar: /workflow-list')
|
|
274
|
+
|
|
275
|
+
ctx.effect(() => ctx.commands.register({
|
|
276
|
+
name: 'workflow',
|
|
277
|
+
description: '按名称运行已保存的 workflow,并传入这次输入。',
|
|
278
|
+
input: { hint: '<名称> <输入>' },
|
|
279
|
+
handler: ({ agent, rawInput }) => {
|
|
280
|
+
const call = parseRun(rawInput)
|
|
281
|
+
if (call === undefined) {
|
|
282
|
+
const library = store.list()
|
|
283
|
+
const names = library.workflows.map(workflow => workflow.name)
|
|
284
|
+
const body = names.length === 0
|
|
285
|
+
? 'workflow 库还是空的。用 /workflow-add <需求描述> 让 agent 帮你写一个。'
|
|
286
|
+
: `已注册:${names.join('、')}\n\n运行方式:/workflow <名称> <输入>`
|
|
287
|
+
return fail(`用法:/workflow <名称> <输入>\n\n${body}`)
|
|
288
|
+
}
|
|
289
|
+
let record
|
|
290
|
+
try {
|
|
291
|
+
record = store.read(call.name)
|
|
292
|
+
} catch (error) {
|
|
293
|
+
if (error instanceof WorkflowStoreError && error.code === 'not-found') {
|
|
294
|
+
const library = store.list()
|
|
295
|
+
const names = library.workflows.map(workflow => workflow.name)
|
|
296
|
+
return fail(names.length === 0
|
|
297
|
+
? `没有名为“${call.name}”的 workflow,库还是空的。`
|
|
298
|
+
: `没有名为“${call.name}”的 workflow。已有:${names.join('、')}`)
|
|
299
|
+
}
|
|
300
|
+
return fail(message(error))
|
|
301
|
+
}
|
|
302
|
+
// An invocation without input falls back to the record's default, which is
|
|
303
|
+
// what makes a picker choice runnable in one step.
|
|
304
|
+
const input = call.input.length > 0 ? call.input : (record.defaultInput ?? '')
|
|
305
|
+
agent.followup(userMessage(buildRunFollowup(record, input)))
|
|
306
|
+
const usedDefault = call.input.length === 0 && input.length > 0
|
|
307
|
+
const described = input.length === 0
|
|
308
|
+
? '(无输入)'
|
|
309
|
+
: `,输入:${input}${usedDefault ? '(默认)' : ''}`
|
|
310
|
+
return ok(`已把 workflow「${record.name}」交给 agent 运行${described}。`)
|
|
311
|
+
},
|
|
312
|
+
}), 'workflow-bar: /workflow')
|
|
313
|
+
|
|
314
|
+
ctx.effect(() => ctx.commands.register({
|
|
315
|
+
name: 'workflow-add',
|
|
316
|
+
description: '进入 workflow 创建流程:描述你想让 workflow 做什么。',
|
|
317
|
+
input: { hint: '<需求描述>' },
|
|
318
|
+
handler: ({ agent, rawInput }) => {
|
|
319
|
+
const requirements = rawInput.trim()
|
|
320
|
+
if (requirements.length === 0) {
|
|
321
|
+
return fail([
|
|
322
|
+
'用法:/workflow-add <需求描述>',
|
|
323
|
+
'',
|
|
324
|
+
'例:/workflow-add 并行检索百度与谷歌,去重后按重合度输出',
|
|
325
|
+
'',
|
|
326
|
+
'也可以点左侧边栏的 Workflows,在面板里手写脚本。',
|
|
327
|
+
].join('\n'))
|
|
328
|
+
}
|
|
329
|
+
agent.followup(userMessage(buildCreateFollowup(requirements)))
|
|
330
|
+
return ok(`已把创建需求交给 agent:「${requirements}」。它会先看已有 workflow,再调用 workflow_library 保存。`)
|
|
331
|
+
},
|
|
332
|
+
}), 'workflow-bar: /workflow-add')
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Build the browser API over the same store.
|
|
337
|
+
* @param {WorkflowStore} store - the library.
|
|
338
|
+
* @returns {object} operation table consumed by `injectWebRoutes`.
|
|
339
|
+
*/
|
|
340
|
+
function createApi(store) {
|
|
341
|
+
const requireName = (params) => {
|
|
342
|
+
const name = typeof params?.name === 'string' ? params.name.trim() : ''
|
|
343
|
+
if (name.length === 0) throw new WorkflowStoreError('invalid-name', 'A workflow name is required.')
|
|
344
|
+
return name
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
list: () => {
|
|
348
|
+
const { workflows, broken } = store.list()
|
|
349
|
+
return { directory: displayDirectory(store.directory), workflows, broken }
|
|
350
|
+
},
|
|
351
|
+
detail: (params) => ({ workflow: store.read(requireName(params)) }),
|
|
352
|
+
save: (params) => ({ workflow: store.save(params, { overwrite: params?.overwrite === true }) }),
|
|
353
|
+
remove: (params) => ({ removed: store.remove(requireName(params)) }),
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Activate the Host half.
|
|
359
|
+
* @param {any} ctx - registrant context.
|
|
360
|
+
* @param {WorkflowBarConfig} [config] - the plugin row's configuration.
|
|
361
|
+
* @returns {void}
|
|
362
|
+
*/
|
|
363
|
+
export function apply(ctx, config = {}) {
|
|
364
|
+
const store = new WorkflowStore({ directory: config?.directory })
|
|
365
|
+
registerCommands(ctx, store)
|
|
366
|
+
registerTool(ctx, store)
|
|
367
|
+
// The route lives on the web-server context this injection resolves; that
|
|
368
|
+
// child context owns the disposer, so no extra effect wraps it.
|
|
369
|
+
injectWebRoutes(ctx, createApi(store), { logger: ctx.logger })
|
|
370
|
+
|
|
371
|
+
// Optional: profiles without a system-prompt registry simply skip the rule.
|
|
372
|
+
// A composition can turn it off with `config.summarySection: false`.
|
|
373
|
+
if (config?.summarySection !== false) {
|
|
374
|
+
ctx.inject(['systemPrompt'], (scope) => {
|
|
375
|
+
scope.effect(
|
|
376
|
+
() => scope.systemPrompt.section(SUMMARY_SECTION),
|
|
377
|
+
'workflow-bar: run summary section',
|
|
378
|
+
)
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
}
|
package/lib/prompts.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User-facing text assembled by the Host half: command output and the
|
|
3
|
+
* follow-up messages that hand work to the agent.
|
|
4
|
+
*
|
|
5
|
+
* Kept in one module so `/workflow`, `/workflow-list`, and `/workflow-add`
|
|
6
|
+
* cannot describe the same library differently.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-workflow-bar/prompts
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Workflow metadata block handed to the `workflow` tool. */
|
|
12
|
+
export function metaFor(record) {
|
|
13
|
+
return {
|
|
14
|
+
name: record.name,
|
|
15
|
+
description: record.description,
|
|
16
|
+
...(record.whenToUse ? { whenToUse: record.whenToUse } : {}),
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The `args` object exposed to a saved script as its `args` global. */
|
|
21
|
+
export function argsFor(input) {
|
|
22
|
+
return { input: typeof input === 'string' ? input : '' }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A fence longer than any backtick run inside the script. */
|
|
26
|
+
function fenced(script) {
|
|
27
|
+
let longest = 0
|
|
28
|
+
for (const match of script.matchAll(/`+/gu)) longest = Math.max(longest, match[0].length)
|
|
29
|
+
const fence = '`'.repeat(Math.max(3, longest + 1))
|
|
30
|
+
return `${fence}js\n${script}\n${fence}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One workflow row in command output. */
|
|
34
|
+
function listLine(workflow) {
|
|
35
|
+
const hint = workflow.argsHint ? ` · 输入:${workflow.argsHint}` : ''
|
|
36
|
+
const fallback = workflow.defaultInput ? `(默认:${workflow.defaultInput})` : ''
|
|
37
|
+
return `- ${workflow.name} — ${workflow.description}${hint}${fallback}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Render `/workflow-list` output.
|
|
42
|
+
* @param {{ workflows: object[], broken: { file: string, message: string }[] }} library - library listing.
|
|
43
|
+
* @param {string} query - optional filter already applied by the caller.
|
|
44
|
+
* @returns {string} text for the command result.
|
|
45
|
+
*/
|
|
46
|
+
export function renderList(library, query) {
|
|
47
|
+
const { workflows, broken } = library
|
|
48
|
+
const lines = []
|
|
49
|
+
if (workflows.length === 0) {
|
|
50
|
+
lines.push(query
|
|
51
|
+
? `没有名称或描述匹配“${query}”的 workflow。`
|
|
52
|
+
: 'workflow 库还是空的。用 /workflow-add <需求描述> 让 agent 帮你写一个,或在侧边栏 Workflows 面板里手动新建。')
|
|
53
|
+
} else {
|
|
54
|
+
lines.push(query ? `匹配“${query}”的 workflow(${workflows.length} 个):` : `已注册的 workflow(${workflows.length} 个):`)
|
|
55
|
+
lines.push(...workflows.map(listLine))
|
|
56
|
+
lines.push('运行方式:/workflow <名称> <输入>')
|
|
57
|
+
}
|
|
58
|
+
if (broken.length > 0) {
|
|
59
|
+
lines.push('', `有 ${broken.length} 个文件无法解析:`)
|
|
60
|
+
lines.push(...broken.map(entry => `- ${entry.file}: ${entry.message}`))
|
|
61
|
+
}
|
|
62
|
+
return lines.join('\n')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Follow-up that asks the agent to run one saved workflow through the
|
|
67
|
+
* `workflow` tool, using the stored script verbatim.
|
|
68
|
+
* @param {object} record - complete workflow record.
|
|
69
|
+
* @param {string} input - the user's raw input for this run.
|
|
70
|
+
* @returns {string} follow-up message text.
|
|
71
|
+
*/
|
|
72
|
+
export function buildRunFollowup(record, input) {
|
|
73
|
+
const meta = JSON.stringify(metaFor(record), null, 2)
|
|
74
|
+
const args = JSON.stringify(argsFor(input), null, 2)
|
|
75
|
+
const rules = [
|
|
76
|
+
'要求:',
|
|
77
|
+
'1. 只调用一次 workflow 工具,参数照抄下面的 meta、args 和 script,不要改写脚本,也不要增删 meta 字段。',
|
|
78
|
+
'2. 脚本以 args.input 接收这次的输入,脚本自己负责校验;输入为空时按脚本的报错处理,不要替它编造内容。',
|
|
79
|
+
'3. 工具返回后先给一句结论,再列最多 3 条关键数字或事实(优先取返回值里的 stats 这类字段),最后一句说明产物或下一步;不要原样粘贴 JSON。',
|
|
80
|
+
]
|
|
81
|
+
if (record.argsHint) rules.push(`4. 这次 <输入> 的含义:${record.argsHint}`)
|
|
82
|
+
return [
|
|
83
|
+
`运行已保存的 workflow「${record.name}」。`,
|
|
84
|
+
rules.join('\n'),
|
|
85
|
+
`meta:\n${meta}`,
|
|
86
|
+
`args:\n${args}`,
|
|
87
|
+
`script:\n${fenced(record.script)}`,
|
|
88
|
+
].join('\n\n')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Follow-up that starts the workflow creation flow: the agent designs the
|
|
93
|
+
* script and persists it through the `workflow_library` tool.
|
|
94
|
+
* @param {string} requirements - the user's description of what the workflow should do.
|
|
95
|
+
* @returns {string} follow-up message text.
|
|
96
|
+
*/
|
|
97
|
+
export function buildCreateFollowup(requirements) {
|
|
98
|
+
return [
|
|
99
|
+
'请为 dsh-workflow-bar 的 workflow 库创建一个新的 workflow。',
|
|
100
|
+
'',
|
|
101
|
+
'【用户需求】',
|
|
102
|
+
requirements,
|
|
103
|
+
'',
|
|
104
|
+
'【步骤】',
|
|
105
|
+
'1. 先调用 workflow_library(action="list")查看已有名称,避免重名;必要时先问用户 1~2 个真正影响脚本设计的问题。',
|
|
106
|
+
'2. 设计脚本:普通 JavaScript 正文(不是 TypeScript),允许顶层 await,可用 agent()、parallel()、pipeline()、phase()、log(),最后 return 一个 JSON 值。用户输入从 args.input 读取,脚本开头先做 trim 和校验,缺失就抛错。',
|
|
107
|
+
'3. 用 workflow_library(action="save")保存:name 用小写字母、数字、连字符;description 一句话说明它做什么;whenToUse 说明什么时候该用它;argsHint 说明 <输入> 该填什么;script 是脚本正文。',
|
|
108
|
+
'4. 保存成功后,用一句话告诉用户名称和运行方式:/workflow <名称> <输入>。',
|
|
109
|
+
'',
|
|
110
|
+
'只创建用户要求的这一个 workflow,不要顺手创建别的。',
|
|
111
|
+
].join('\n')
|
|
112
|
+
}
|