dsh-taskflow 0.1.2 → 0.1.3
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 +36 -0
- package/LICENSE +201 -201
- package/NOTICE +6 -1
- package/README.md +238 -42
- package/README_en.md +23 -60
- package/client.js +804 -59
- package/docs/DETAIL.md +32 -5
- package/docs/DETAIL.zh-CN.md +13 -3
- package/licenses/Marked-MIT.txt +44 -0
- package/package.json +137 -134
- package/src/application/task-runtime.mjs +123 -114
- package/src/domain/task-service.mjs +247 -178
- package/src/domain/tool-view.mjs +11 -0
- package/src/server/config.mjs +1 -1
- package/src/server/index.mjs +18 -8
- package/src/server/inline-sessions.mjs +154 -0
- package/src/server/planning-check.mjs +105 -0
- package/src/server/prompts.mjs +12 -8
- package/src/server/quick-replies.mjs +88 -0
- package/src/server/rpc.mjs +43 -22
- package/src/server/session-context.mjs +11 -5
- package/src/server/task-actions.mjs +63 -0
- package/src/server/tool-schemas.mjs +33 -0
- package/src/server/tool-views.mjs +36 -8
- package/src/server/tools.mjs +20 -14
|
@@ -1,14 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { documentPath } from '../domain/tool-view.mjs';
|
|
3
|
+
|
|
4
|
+
const keys = new Set(['path', 'file', 'file_path', 'filePath', 'filename', 'fileName', 'output_path', 'outputPath', 'absolutePath', 'destination', 'files', 'paths', 'artifacts', 'outputs', 'documents', 'result', 'data']);
|
|
5
|
+
export function outputFiles(...values) {
|
|
6
|
+
const found = new Set();
|
|
7
|
+
const visit = (value, depth = 0) => {
|
|
8
|
+
if (depth > 6 || found.size >= 20) return;
|
|
9
|
+
if (typeof value === 'string') { if (documentPath(value)) found.add(value); return; }
|
|
10
|
+
if (Array.isArray(value)) { for (const item of value.slice(0, 50)) visit(item, depth + 1); }
|
|
11
|
+
else if (value && typeof value === 'object') for (const [key, item] of Object.entries(value)) if (keys.has(key)) visit(item, depth + 1);
|
|
12
|
+
};
|
|
13
|
+
for (const value of values) if (value && typeof value === 'object') visit(value);
|
|
14
|
+
return [...found];
|
|
15
|
+
}
|
|
16
|
+
const urlOf = value => { try { const u = new URL(value); return ['http:', 'https:'].includes(u.protocol) && !u.username && !u.password ? u.href : null; } catch { return null; } };
|
|
17
|
+
const kindOf = name => /browser|navigate|web_page/.test(name) ? 'browser' : /bash|pwsh|terminal|command|run_code/.test(name) ? 'terminal' : /^(read|write|edit|str_replace)/.test(name) ? 'editor' : 'plugin';
|
|
18
|
+
|
|
19
|
+
// Observe admitted results without invoking tools again or reading arbitrary files.
|
|
2
20
|
export function registerToolViews(ctx, service, log) {
|
|
21
|
+
const browserUrls = new Map();
|
|
22
|
+
ctx.effect(() => () => browserUrls.clear());
|
|
3
23
|
ctx.on('tools/execute', async (exec, next) => {
|
|
4
24
|
const sid = exec.agent?.session.id;
|
|
5
|
-
if (sid
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (kind) { const output = (result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n'); if (output) await service.openView(sid, { ...service.view(sid).tool, content: output }).catch(log); }
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
25
|
+
if (!sid || !service.view(sid).plan || exec.name.startsWith('taskflow_') || ['structured_output', 'todo_write', 'ask_user'].includes(exec.name)) return next();
|
|
26
|
+
const args = exec.arguments ?? {}, kind = kindOf(exec.name), id = exec.callId || randomUUID();
|
|
27
|
+
const url = kind === 'browser' ? urlOf(args.url) || browserUrls.get(sid) || null : null;
|
|
28
|
+
await service.openView(sid, { kind, title: exec.name, id, url, phase: 'running', sourceTool: exec.name, content: '工具正在执行…' }).catch(log);
|
|
12
29
|
return next();
|
|
13
30
|
}, { global: true });
|
|
31
|
+
ctx.on('tools/result', (exec, result) => {
|
|
32
|
+
const sid = exec.agent?.session.id, current = sid && service.view(sid).tool;
|
|
33
|
+
if (!current || current.id !== exec.callId) return;
|
|
34
|
+
const args = exec.arguments ?? {}, output = (result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
|
|
35
|
+
let parsed; try { parsed = JSON.parse(output); } catch {}
|
|
36
|
+
const files = result.isError ? [] : outputFiles(result.value, result.meta, parsed, args);
|
|
37
|
+
const url = current.kind === 'browser' ? urlOf(result.value?.url) || urlOf(parsed?.url) || current.url : null;
|
|
38
|
+
if (url && !result.isError) browserUrls.set(sid, url);
|
|
39
|
+
const autoOpen = !result.isError && !/browser_(close|install|wait)$/.test(exec.name);
|
|
40
|
+
void service.openView(sid, { ...current, expectedId: current.id, requireOpen: true, kind: files.length ? 'editor' : current.kind, files, filePath: files[0], url, phase: result.isError ? 'failed' : 'ready', autoOpen, content: output }).catch(log);
|
|
41
|
+
}, { global: true });
|
|
14
42
|
}
|
package/src/server/tools.mjs
CHANGED
|
@@ -1,28 +1,34 @@
|
|
|
1
1
|
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { tasksSchema, patchSchema, normalizeToolArguments } from './tool-schemas.mjs';
|
|
2
3
|
|
|
3
4
|
const string = description => ({ type: 'string', description });
|
|
4
|
-
const json = description => ({ type: 'json', description });
|
|
5
5
|
const number = description => ({ type: 'number', description });
|
|
6
6
|
const required = schema => ({ ...schema, required: true });
|
|
7
7
|
const jsonOutput = { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] };
|
|
8
|
-
const taskHelp = '任务数组。每项 title/instructions/criteria、complexity:low|medium|high、independent:boolean、mode:inline|delegate;可选 ref 为本批次临时引用,parentId 和 dependsOn 可引用该 ref 或现有任务 ID。';
|
|
9
8
|
|
|
10
9
|
|
|
11
10
|
/** 把模型工具映射到领域服务;不在此保存任务状态。 */
|
|
12
|
-
export function registerTools(ctx, service, runtime) {
|
|
13
|
-
const register = (toolName, description, parameters, execute) =>
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
export function registerTools(ctx, service, runtime, planning, records) {
|
|
12
|
+
const register = (toolName, description, parameters, execute) => {
|
|
13
|
+
const tool = defineTool({
|
|
14
|
+
name: toolName, description, parameters, output: jsonOutput,
|
|
15
|
+
async execute(args, exec) { if (!exec.agent) throw new Error('Taskflow 需要会话上下文'); if (exec.signal.aborted) throw exec.signal.reason; return (await execute(args, exec.agent.session.id, exec)) ?? { ok: true }; },
|
|
16
|
+
presentCall: () => ({ card: 'generic', title: description.split('。')[0] }), presentResult: () => ({ card: 'generic' }),
|
|
17
|
+
});
|
|
18
|
+
const executeValidated = tool.execute;
|
|
19
|
+
tool.execute = async (args, exec) => executeValidated(normalizeToolArguments(toolName, args), exec);
|
|
20
|
+
return ctx.tools.register(tool);
|
|
21
|
+
};
|
|
22
|
+
register('taskflow_plan', '规划并保存当前会话的持久层级待办。已有计划不能覆盖;子会话请用 taskflow_add。', { title: required(string('计划标题')), goal: required(string('用户目标与约束')), tasks: required(tasksSchema) }, (a, sid) => service.create(sid, a));
|
|
23
|
+
register('taskflow_assess', '判断任务复杂度。无计划时 needsPlan 表示是否建立主清单;已有计划且正在执行事项时,表示此事项是否需要继续拆解下级。每层中高复杂度事项执行前均需判断;true 后用 taskflow_add,false 说明直接执行依据。不创建或清空任务。', { needsPlan: required({ type: 'boolean', description: '无计划时是否需要清单;已有计划时当前事项是否需要拆解' }), reason: required(string('判断依据')), taskId: string('已有计划时正在执行的任务 ID;省略取当前任务') }, (a, _sid, exec) => planning?.assess(exec.agent, a.needsPlan, a.reason, a.taskId) ?? { needsPlan: a.needsPlan, reason: a.reason });
|
|
24
|
+
register('taskflow_status', '读取整个持久计划、当前任务、版本和工具视图。不会重置计划。', {}, (_a, sid) => ({ ...service.view(sid), planning: planning?.view(sid) ?? null }));
|
|
25
|
+
register('taskflow_add', '在当前任务或明确指定父任务下增补待办。目标歧义会拒绝,不改变其他事项。', { tasks: required(tasksSchema), parentId: string('可选父任务 ID 或完整名称;不填则为当前任务'), expectedVersion: number('可选计划版本,防止并发增补冲突') }, (a, sid) => service.add(sid, a.tasks, a.parentId, a.expectedVersion));
|
|
26
|
+
register('taskflow_update', '增量修改任务要求。递增版本,中止旧执行;不会重写整棵树。', { taskId: required(string('任务 ID')), version: required(number('当前任务版本')), patch: required(patchSchema) }, (a, sid) => service.update(sid, a.taskId, a.patch, a.version));
|
|
22
27
|
register('taskflow_reorder', '调整同级事项顺序,保留任务 ID、执行状态和历史。', { taskId: required(string('任务 ID')), direction: required(number('-1 上移,1 下移')), expectedVersion: required(number('当前计划版本')) }, (a, sid) => service.reorder(sid, a.taskId, a.direction, a.expectedVersion));
|
|
23
28
|
register('taskflow_cancel', '取消任务及其下级,保留历史会话与已交付记录。取消不等于完成。', { taskId: required(string('任务 ID')), version: required(number('当前任务版本')) }, (a, sid) => service.cancel(sid, a.taskId, a.version));
|
|
24
|
-
register('taskflow_start', '启动事项。inline
|
|
29
|
+
register('taskflow_start', '启动事项。inline 在父会话执行并自动保存到事项子会话,delegate 排队并在子会话独立执行;两种方式均在左侧展示子会话。', { taskId: required(string('待启动的任务 ID')) }, (a, sid) => service.start(sid, a.taskId));
|
|
25
30
|
register('taskflow_wait', '等待当前任务下级的执行结果,释放本层并发名额;返回后继续本层工作。', { taskId: required(string('本会话正在执行的任务 ID')) }, (a, sid, exec) => runtime.wait(sid, a.taskId, exec.signal));
|
|
31
|
+
register('taskflow_progress', '将当前任务的执行过程、阶段进展或关键结果保存到当前事项子会话,不向父任务发送过程通知。最终结果通过完成工具或结构化输出报告。', { taskId: string('当前正在执行的任务 ID;省略则取当前任务'), content: required(string('实际执行进展、关键发现或阶段结果,最多16000字符')) }, (a, sid, exec) => { if (!records) throw new Error('事项记录服务尚未就绪'); return records.progress(sid, a.taskId, a.content, exec.callId); });
|
|
26
32
|
register('taskflow_complete', '保存行内事项的真实执行结果并立即勾选、通知直接父任务。委派任务通过最终结构化输出完成。', { taskId: required(string('任务 ID')), version: required(number('执行要求版本')), attemptId: required(string('taskflow_start 返回的 attempt.id')), summary: required(string('实际完成结果、证据或阻塞原因')), status: string('completed(默认)/blocked/failed') }, (a, sid) => service.settle(sid, a.taskId, a.attemptId, a.version, { status: a.status ?? 'completed', summary: a.summary }));
|
|
27
|
-
register('taskflow_open_view', '
|
|
33
|
+
register('taskflow_open_view', '展示实际工具产物。Markdown 支持预览,Office/PDF 使用宿主文档插件,网页使用浏览器插件;不执行或重跑原操作。', { kind: required(string('browser/editor/terminal/plugin/orchestration')), title: string('当前工具标题'), url: string('实际 HTTP(S) 页面地址,可选'), filePath: string('已生成文件的实际本地或工作区路径,可选;支持 md/markdown/xls/xlsx/csv/tsv/doc/docx/ppt/pptx/pdf'), files: { type: 'array', description: '多个已生成文档的实际路径,最多展示20项', items: string('文件路径') }, content: string('实际工具输出,可选;不是完整文档正文时不要当作文件内容') }, (a, sid) => service.openView(sid, a));
|
|
28
34
|
}
|