dsh-plugin-inspiration-workbench 0.9.0 → 0.13.2
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/README.md +27 -1
- package/lib/client.js +4191 -185
- package/lib/client.js.map +4 -4
- package/package.json +8 -2
- package/src/client/board.js +500 -30
- package/src/client/card-tools.js +1 -0
- package/src/client/docx-text.js +93 -0
- package/src/client/file-preview.js +103 -0
- package/src/client/file-store.js +74 -0
- package/src/client/flow-recorder.js +58 -0
- package/src/client/index.jsx +3 -4
- package/src/client/persistence.js +31 -0
- package/src/client/popovers.js +2 -2
- package/src/client/render.js +1 -0
- package/src/client/sessions.js +91 -11
- package/src/client/styles.css +116 -2
- package/src/client/task-ai.js +83 -0
- package/src/client/task-controller.js +620 -0
- package/src/client/task-intelligence.js +545 -0
- package/src/client/task-model.js +264 -0
- package/src/client/task-store.js +118 -0
- package/src/client/task-view.css +1263 -0
- package/src/client/task-view.js +1688 -0
- package/src/host/index.js +69 -7
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic task-draft and topology helpers.
|
|
3
|
+
*
|
|
4
|
+
* This module deliberately has no model, host, or DOM dependency. AI output is
|
|
5
|
+
* treated as untrusted data: parse it, normalize it, then validate it before
|
|
6
|
+
* using the plan or computing a layout.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MAX_NODES = 6;
|
|
10
|
+
const DEFAULT_TITLE = '任务计划';
|
|
11
|
+
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function cleanText(value, fallback = '') {
|
|
17
|
+
if (typeof value === 'string') return value.trim();
|
|
18
|
+
if (typeof value === 'number' || typeof value === 'bigint') return String(value);
|
|
19
|
+
return fallback;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function unique(values) {
|
|
23
|
+
return [...new Set(values)];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function shortTitle(value, fallback) {
|
|
27
|
+
const firstLine = cleanText(value).split(/\r?\n/, 1)[0];
|
|
28
|
+
if (!firstLine) return fallback;
|
|
29
|
+
return [...firstLine].slice(0, 60).join('');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Parse the first complete JSON value at the current cursor, if any. */
|
|
33
|
+
function scanJson(source) {
|
|
34
|
+
for (let start = 0; start < source.length; start += 1) {
|
|
35
|
+
if (source[start] !== '{' && source[start] !== '[') continue;
|
|
36
|
+
const stack = [];
|
|
37
|
+
let quoted = false;
|
|
38
|
+
let escaped = false;
|
|
39
|
+
|
|
40
|
+
for (let cursor = start; cursor < source.length; cursor += 1) {
|
|
41
|
+
const char = source[cursor];
|
|
42
|
+
if (quoted) {
|
|
43
|
+
if (escaped) escaped = false;
|
|
44
|
+
else if (char === '\\') escaped = true;
|
|
45
|
+
else if (char === '"') quoted = false;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (char === '"') {
|
|
49
|
+
quoted = true;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (char === '{' || char === '[') {
|
|
53
|
+
stack.push(char === '{' ? '}' : ']');
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (char !== '}' && char !== ']') continue;
|
|
57
|
+
if (stack.pop() !== char) break;
|
|
58
|
+
if (stack.length) continue;
|
|
59
|
+
|
|
60
|
+
const candidate = source.slice(start, cursor + 1);
|
|
61
|
+
try {
|
|
62
|
+
return { found: true, value: JSON.parse(candidate) };
|
|
63
|
+
} catch {
|
|
64
|
+
// A prose brace or a malformed outer object need not hide a later,
|
|
65
|
+
// well-formed JSON value. Continue at the next possible start.
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { found: false, value: undefined };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parse raw JSON, fenced Markdown JSON, or JSON embedded in explanatory prose.
|
|
75
|
+
* Returns the parsed value and throws a descriptive error when none is valid.
|
|
76
|
+
*/
|
|
77
|
+
export function parseAIResponse(response) {
|
|
78
|
+
if (typeof response !== 'string') {
|
|
79
|
+
throw new TypeError('AI response must be a string');
|
|
80
|
+
}
|
|
81
|
+
const source = response.replace(/^\uFEFF/, '').trim();
|
|
82
|
+
if (!source) throw new SyntaxError('AI response is empty');
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(source);
|
|
86
|
+
} catch {
|
|
87
|
+
// Try fenced blocks and embedded JSON below.
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Accept json, unlabelled, and same-line fences. For another info string,
|
|
91
|
+
// retain it as content so a same-line JSON object is not accidentally lost.
|
|
92
|
+
const ticks = String.fromCharCode(96).repeat(3);
|
|
93
|
+
const fence = new RegExp(ticks + '([^\r\n]*)\r?\n?([\s\S]*?)' + ticks, 'g');
|
|
94
|
+
for (let match = fence.exec(source); match; match = fence.exec(source)) {
|
|
95
|
+
const info = match[1].trim();
|
|
96
|
+
const block = /^(?:json)?$/i.test(info) ? match[2] : match[1] + '\n' + match[2];
|
|
97
|
+
try {
|
|
98
|
+
return JSON.parse(block.trim());
|
|
99
|
+
} catch {
|
|
100
|
+
const scanned = scanJson(block);
|
|
101
|
+
if (scanned.found) return scanned.value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const scanned = scanJson(source);
|
|
106
|
+
if (scanned.found) return scanned.value;
|
|
107
|
+
throw new SyntaxError('AI response does not contain valid JSON');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function rawDependencies(node) {
|
|
111
|
+
const value = node.dependsOn ?? node.dependencies ?? node.deps ?? [];
|
|
112
|
+
const list = Array.isArray(value) ? value : [value];
|
|
113
|
+
return unique(list.map((dependency) => {
|
|
114
|
+
if (isRecord(dependency)) return cleanText(dependency.id ?? dependency.nodeId);
|
|
115
|
+
return cleanText(dependency);
|
|
116
|
+
}).filter(Boolean));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function rawEdges(plan) {
|
|
120
|
+
if (!Array.isArray(plan.edges)) return [];
|
|
121
|
+
return plan.edges.map((edge) => {
|
|
122
|
+
const value = isRecord(edge) ? edge : {};
|
|
123
|
+
return {
|
|
124
|
+
from: cleanText(value.from ?? value.source ?? value.sourceId),
|
|
125
|
+
to: cleanText(value.to ?? value.target ?? value.targetId),
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convert AI-friendly shapes into one canonical plan without hiding semantic
|
|
132
|
+
* errors. Unknown dependencies, duplicate ids, cycles, and invalid node kinds
|
|
133
|
+
* are intentionally retained for validateTopologyPlan to report.
|
|
134
|
+
*
|
|
135
|
+
* Canonical node: { id, kind, title, dependsOn, prompt|note, ...binding }.
|
|
136
|
+
* Canonical edge: { from, to }, where from depends on to.
|
|
137
|
+
*/
|
|
138
|
+
export function normalizeTopologyPlan(value) {
|
|
139
|
+
let input = typeof value === 'string' ? parseAIResponse(value) : value;
|
|
140
|
+
if (isRecord(input) && !Array.isArray(input.nodes)) {
|
|
141
|
+
if (isRecord(input.plan)) input = input.plan;
|
|
142
|
+
else if (isRecord(input.topology)) input = input.topology;
|
|
143
|
+
}
|
|
144
|
+
if (!isRecord(input) || !Array.isArray(input.nodes)) {
|
|
145
|
+
throw new TypeError('Topology plan must be an object with a nodes array');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const nodes = input.nodes.map((value, index) => {
|
|
149
|
+
const raw = isRecord(value) ? value : {};
|
|
150
|
+
const id = cleanText(raw.id ?? raw.key);
|
|
151
|
+
const kind = cleanText(raw.kind ?? raw.type, 'session').toLowerCase();
|
|
152
|
+
const node = {
|
|
153
|
+
id,
|
|
154
|
+
kind,
|
|
155
|
+
title: cleanText(raw.title ?? raw.name) || (kind === 'note' ? '便签 ' : '会话 ') + (index + 1),
|
|
156
|
+
dependsOn: rawDependencies(raw),
|
|
157
|
+
};
|
|
158
|
+
if (kind === 'note') {
|
|
159
|
+
node.note = cleanText(raw.note ?? raw.content);
|
|
160
|
+
} else {
|
|
161
|
+
node.prompt = cleanText(raw.prompt ?? raw.instruction ?? raw.task);
|
|
162
|
+
}
|
|
163
|
+
const sessionId = cleanText(raw.sessionId);
|
|
164
|
+
const workspace = cleanText(raw.workspace);
|
|
165
|
+
if (sessionId) node.sessionId = sessionId;
|
|
166
|
+
if (workspace) node.workspace = workspace;
|
|
167
|
+
return node;
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Edges are accepted as a convenience for AI responses, but dependencies are
|
|
171
|
+
// the source of truth. Merge both forms and de-duplicate them deterministically.
|
|
172
|
+
const explicitEdges = rawEdges(input);
|
|
173
|
+
for (const edge of explicitEdges) {
|
|
174
|
+
const source = nodes.find((node) => node.id === edge.from);
|
|
175
|
+
if (source && edge.to) source.dependsOn = unique([...source.dependsOn, edge.to]);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const edgeMap = new Map();
|
|
179
|
+
const addEdge = (from, to) => {
|
|
180
|
+
const key = from + '\u0000' + to;
|
|
181
|
+
if (!edgeMap.has(key)) edgeMap.set(key, { from, to });
|
|
182
|
+
};
|
|
183
|
+
for (const node of nodes) {
|
|
184
|
+
for (const dependency of node.dependsOn) addEdge(node.id, dependency);
|
|
185
|
+
}
|
|
186
|
+
for (const edge of explicitEdges) addEdge(edge.from, edge.to);
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
title: cleanText(input.title ?? input.name) || DEFAULT_TITLE,
|
|
190
|
+
goal: cleanText(input.goal ?? input.objective ?? input.brief),
|
|
191
|
+
nodes,
|
|
192
|
+
edges: [...edgeMap.values()],
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Validate all topology invariants and return machine-readable errors. */
|
|
197
|
+
export function validateTopologyPlan(value) {
|
|
198
|
+
let plan;
|
|
199
|
+
try {
|
|
200
|
+
plan = normalizeTopologyPlan(value);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
return {
|
|
203
|
+
valid: false,
|
|
204
|
+
errors: [{ code: 'INVALID_PLAN', message: error.message || '拓扑计划格式无效' }],
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const errors = [];
|
|
209
|
+
const add = (code, message, details = {}) => errors.push({ code, message, ...details });
|
|
210
|
+
if (plan.nodes.length < 1 || plan.nodes.length > MAX_NODES) {
|
|
211
|
+
add('NODE_COUNT', '拓扑计划必须包含 1-6 个节点', { count: plan.nodes.length });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const counts = new Map();
|
|
215
|
+
for (const node of plan.nodes) counts.set(node.id, (counts.get(node.id) || 0) + 1);
|
|
216
|
+
const byId = new Map();
|
|
217
|
+
for (const node of plan.nodes) {
|
|
218
|
+
if (!node.id) add('INVALID_NODE_ID', '每个节点都必须有非空 id');
|
|
219
|
+
else if (counts.get(node.id) > 1) add('DUPLICATE_NODE_ID', '节点 id 不能重复:' + node.id, { nodeId: node.id });
|
|
220
|
+
if (!byId.has(node.id)) byId.set(node.id, node);
|
|
221
|
+
if (node.kind !== 'session' && node.kind !== 'note') {
|
|
222
|
+
add('INVALID_NODE_KIND', '节点类型必须是 session 或 note', { nodeId: node.id, kind: node.kind });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (!plan.nodes.some((node) => node.kind === 'session')) {
|
|
226
|
+
add('NO_SESSION', '拓扑计划至少需要一个会话节点');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
for (const node of plan.nodes) {
|
|
230
|
+
for (const dependency of node.dependsOn) {
|
|
231
|
+
if (dependency === node.id) {
|
|
232
|
+
add('SELF_DEPENDENCY', '节点不能依赖自身:' + node.id, { nodeId: node.id });
|
|
233
|
+
}
|
|
234
|
+
if (!byId.has(dependency)) {
|
|
235
|
+
add('UNKNOWN_DEPENDENCY', '依赖节点不存在:' + dependency, { nodeId: node.id, dependencyId: dependency });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
for (const edge of plan.edges) {
|
|
240
|
+
const source = byId.get(edge.from);
|
|
241
|
+
if (!source) {
|
|
242
|
+
add('UNKNOWN_EDGE_SOURCE', '连线上游节点不存在:' + edge.from, { nodeId: edge.from });
|
|
243
|
+
if (!byId.has(edge.to)) {
|
|
244
|
+
add('UNKNOWN_DEPENDENCY', '依赖节点不存在:' + edge.to, { nodeId: edge.from, dependencyId: edge.to });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const dependency = byId.get(edge.to);
|
|
248
|
+
if (dependency?.kind === 'note') {
|
|
249
|
+
add('NOTE_NOT_SINK', '便签只能作为汇点,不能有下游节点:' + dependency.id, { nodeId: dependency.id });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Kahn's algorithm over unique, known nodes. Other errors are still returned
|
|
254
|
+
// alongside a cycle so callers can show all actionable feedback at once.
|
|
255
|
+
const ids = plan.nodes.filter((node) => node.id && counts.get(node.id) === 1).map((node) => node.id);
|
|
256
|
+
const idSet = new Set(ids);
|
|
257
|
+
const indegree = new Map(ids.map((id) => [id, 0]));
|
|
258
|
+
const downstream = new Map(ids.map((id) => [id, []]));
|
|
259
|
+
for (const node of plan.nodes) {
|
|
260
|
+
if (!idSet.has(node.id)) continue;
|
|
261
|
+
for (const dependency of node.dependsOn) {
|
|
262
|
+
if (!idSet.has(dependency)) continue;
|
|
263
|
+
indegree.set(node.id, indegree.get(node.id) + 1);
|
|
264
|
+
downstream.get(dependency).push(node.id);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const queue = ids.filter((id) => indegree.get(id) === 0);
|
|
268
|
+
let visited = 0;
|
|
269
|
+
for (let cursor = 0; cursor < queue.length; cursor += 1) {
|
|
270
|
+
const id = queue[cursor];
|
|
271
|
+
visited += 1;
|
|
272
|
+
for (const target of downstream.get(id)) {
|
|
273
|
+
indegree.set(target, indegree.get(target) - 1);
|
|
274
|
+
if (indegree.get(target) === 0) queue.push(target);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (visited < ids.length) add('CYCLE', '拓扑依赖不能成环');
|
|
278
|
+
|
|
279
|
+
return { valid: errors.length === 0, errors };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Error raised by operations that require a valid topology. */
|
|
283
|
+
export class TopologyPlanError extends Error {
|
|
284
|
+
constructor(errors) {
|
|
285
|
+
super('Invalid topology plan: ' + errors.map((error) => error.code).join(', '));
|
|
286
|
+
this.name = 'TopologyPlanError';
|
|
287
|
+
this.errors = errors;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function validPlan(value) {
|
|
292
|
+
const plan = normalizeTopologyPlan(value);
|
|
293
|
+
const validation = validateTopologyPlan(plan);
|
|
294
|
+
if (!validation.valid) throw new TopologyPlanError(validation.errors);
|
|
295
|
+
return plan;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const LAYOUT_DEFAULTS = {
|
|
299
|
+
direction: 'horizontal',
|
|
300
|
+
originX: 40,
|
|
301
|
+
originY: 40,
|
|
302
|
+
nodeWidth: 320,
|
|
303
|
+
nodeHeight: 200,
|
|
304
|
+
layerGap: 80,
|
|
305
|
+
nodeGap: 24,
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
function layoutOptions(options) {
|
|
309
|
+
if (!isRecord(options)) throw new TypeError('Layout options must be an object');
|
|
310
|
+
const result = { ...LAYOUT_DEFAULTS, ...options };
|
|
311
|
+
if (result.direction !== 'horizontal' && result.direction !== 'vertical') {
|
|
312
|
+
throw new RangeError('Layout direction must be horizontal or vertical');
|
|
313
|
+
}
|
|
314
|
+
for (const key of ['originX', 'originY', 'nodeWidth', 'nodeHeight', 'layerGap', 'nodeGap']) {
|
|
315
|
+
if (typeof result[key] !== 'number' || !Number.isFinite(result[key]) || result[key] < 0) {
|
|
316
|
+
throw new RangeError(key + ' must be a finite non-negative number');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (!result.nodeWidth || !result.nodeHeight) {
|
|
320
|
+
throw new RangeError('Node width and height must be greater than zero');
|
|
321
|
+
}
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Compute longest-path dependency layers and deterministic canvas coordinates.
|
|
327
|
+
* The returned layout does not mutate or implicitly get attached to the plan.
|
|
328
|
+
*/
|
|
329
|
+
export function computeLayeredLayout(value, options = {}) {
|
|
330
|
+
const plan = validPlan(value);
|
|
331
|
+
const config = layoutOptions(options);
|
|
332
|
+
const nodes = plan.nodes;
|
|
333
|
+
const indexOf = new Map(nodes.map((node, index) => [node.id, index]));
|
|
334
|
+
const layerOf = new Map(nodes.map((node) => [node.id, 0]));
|
|
335
|
+
const indegree = new Map(nodes.map((node) => [node.id, node.dependsOn.length]));
|
|
336
|
+
const downstream = new Map(nodes.map((node) => [node.id, []]));
|
|
337
|
+
for (const node of nodes) {
|
|
338
|
+
for (const dependency of node.dependsOn) downstream.get(dependency).push(node.id);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const queue = nodes.filter((node) => indegree.get(node.id) === 0).map((node) => node.id);
|
|
342
|
+
for (let cursor = 0; cursor < queue.length; cursor += 1) {
|
|
343
|
+
const id = queue[cursor];
|
|
344
|
+
for (const target of downstream.get(id)) {
|
|
345
|
+
layerOf.set(target, Math.max(layerOf.get(target), layerOf.get(id) + 1));
|
|
346
|
+
indegree.set(target, indegree.get(target) - 1);
|
|
347
|
+
if (indegree.get(target) === 0) queue.push(target);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const layers = [];
|
|
352
|
+
for (const id of queue) {
|
|
353
|
+
const layer = layerOf.get(id);
|
|
354
|
+
if (!layers[layer]) layers[layer] = [];
|
|
355
|
+
layers[layer].push(id);
|
|
356
|
+
}
|
|
357
|
+
for (const layer of layers) {
|
|
358
|
+
layer.sort((left, right) => indexOf.get(left) - indexOf.get(right));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const positions = [];
|
|
362
|
+
layers.forEach((ids, layer) => {
|
|
363
|
+
ids.forEach((id, column) => {
|
|
364
|
+
const horizontal = config.direction === 'horizontal';
|
|
365
|
+
positions.push({
|
|
366
|
+
id,
|
|
367
|
+
layer,
|
|
368
|
+
column,
|
|
369
|
+
x: config.originX + (horizontal ? layer * (config.nodeWidth + config.layerGap) : column * (config.nodeWidth + config.nodeGap)),
|
|
370
|
+
y: config.originY + (horizontal ? column * (config.nodeHeight + config.nodeGap) : layer * (config.nodeHeight + config.layerGap)),
|
|
371
|
+
w: config.nodeWidth,
|
|
372
|
+
h: config.nodeHeight,
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const layerCount = layers.length;
|
|
378
|
+
const largestLayer = Math.max(...layers.map((layer) => layer.length));
|
|
379
|
+
const horizontal = config.direction === 'horizontal';
|
|
380
|
+
return {
|
|
381
|
+
direction: config.direction,
|
|
382
|
+
layers,
|
|
383
|
+
nodes: positions,
|
|
384
|
+
width: config.originX * 2 + (horizontal
|
|
385
|
+
? layerCount * config.nodeWidth + (layerCount - 1) * config.layerGap
|
|
386
|
+
: largestLayer * config.nodeWidth + (largestLayer - 1) * config.nodeGap),
|
|
387
|
+
height: config.originY * 2 + (horizontal
|
|
388
|
+
? largestLayer * config.nodeHeight + (largestLayer - 1) * config.nodeGap
|
|
389
|
+
: layerCount * config.nodeHeight + (layerCount - 1) * config.layerGap),
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function sessionCandidate(value) {
|
|
394
|
+
const raw = typeof value === 'string' ? { sessionId: value } : value;
|
|
395
|
+
if (!isRecord(raw) || raw.kind === 'note' || raw.type === 'note') return null;
|
|
396
|
+
const sessionId = cleanText(raw.sessionId ?? raw.id);
|
|
397
|
+
if (!sessionId) return null;
|
|
398
|
+
return {
|
|
399
|
+
key: sessionId,
|
|
400
|
+
sessionId,
|
|
401
|
+
title: cleanText(raw.title ?? raw.name),
|
|
402
|
+
workspace: cleanText(raw.workspace),
|
|
403
|
+
prompt: cleanText(raw.prompt ?? raw.instruction ?? raw.composerDraft ?? raw.draft),
|
|
404
|
+
pipeline: raw.pipeline,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function noteCandidate(value, index) {
|
|
409
|
+
if (!isRecord(value) || (value.kind !== 'note' && value.type !== 'note')) return null;
|
|
410
|
+
return {
|
|
411
|
+
key: cleanText(value.id ?? value.noteId, 'anonymous-note-' + (index + 1)),
|
|
412
|
+
title: cleanText(value.title ?? value.name),
|
|
413
|
+
note: cleanText(value.note ?? value.content),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function pipelineTargets(pipeline) {
|
|
418
|
+
if (!isRecord(pipeline)) return [];
|
|
419
|
+
if (Array.isArray(pipeline.targets)) return pipeline.targets.filter(isRecord);
|
|
420
|
+
if (pipeline.targetSessionId) {
|
|
421
|
+
return [{ kind: 'session', id: pipeline.targetSessionId, targetSessionId: pipeline.targetSessionId }];
|
|
422
|
+
}
|
|
423
|
+
return [];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Create a deterministic local draft from board cards and/or known sessions.
|
|
428
|
+
* Existing pipelines become dependencies. If they are malformed or cyclic, the
|
|
429
|
+
* same nodes fall back to a simple session chain with notes at the sink.
|
|
430
|
+
*/
|
|
431
|
+
export function createLocalTaskDraft(value = {}) {
|
|
432
|
+
const options = Array.isArray(value) ? { cards: value } : (isRecord(value) ? value : {});
|
|
433
|
+
const cards = Array.isArray(options.cards) ? options.cards : [];
|
|
434
|
+
const sessions = Array.isArray(options.sessions) ? options.sessions : [];
|
|
435
|
+
const goal = cleanText(options.goal ?? options.objective ?? options.brief);
|
|
436
|
+
const title = cleanText(options.title) || shortTitle(goal, '画布任务草案');
|
|
437
|
+
|
|
438
|
+
const sessionMap = new Map();
|
|
439
|
+
for (const raw of [...cards, ...sessions]) {
|
|
440
|
+
const candidate = sessionCandidate(raw);
|
|
441
|
+
if (candidate && !sessionMap.has(candidate.key)) sessionMap.set(candidate.key, candidate);
|
|
442
|
+
}
|
|
443
|
+
const noteMap = new Map();
|
|
444
|
+
cards.forEach((raw, index) => {
|
|
445
|
+
const candidate = noteCandidate(raw, index);
|
|
446
|
+
if (candidate && !noteMap.has(candidate.key)) noteMap.set(candidate.key, candidate);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
const sessionCandidates = [...sessionMap.values()];
|
|
450
|
+
if (!sessionCandidates.length) {
|
|
451
|
+
return createFallbackTopology({ title, goal, sessions });
|
|
452
|
+
}
|
|
453
|
+
const selectedSessions = sessionCandidates.slice(0, MAX_NODES);
|
|
454
|
+
const selectedNotes = [...noteMap.values()].slice(0, MAX_NODES - selectedSessions.length);
|
|
455
|
+
|
|
456
|
+
const nodes = [];
|
|
457
|
+
const sessionIds = new Map();
|
|
458
|
+
const noteIds = new Map();
|
|
459
|
+
selectedSessions.forEach((candidate, index) => {
|
|
460
|
+
const id = 'session-' + (index + 1);
|
|
461
|
+
sessionIds.set(candidate.key, id);
|
|
462
|
+
const nodeTitle = candidate.title || '会话 ' + (index + 1);
|
|
463
|
+
const node = {
|
|
464
|
+
id,
|
|
465
|
+
kind: 'session',
|
|
466
|
+
title: nodeTitle,
|
|
467
|
+
prompt: goal || candidate.prompt || '围绕「' + nodeTitle + '」继续工作,给出结论、依据和下一步。',
|
|
468
|
+
dependsOn: [],
|
|
469
|
+
sessionId: candidate.sessionId,
|
|
470
|
+
};
|
|
471
|
+
if (candidate.workspace) node.workspace = candidate.workspace;
|
|
472
|
+
nodes.push(node);
|
|
473
|
+
});
|
|
474
|
+
selectedNotes.forEach((candidate, index) => {
|
|
475
|
+
const id = 'note-' + (index + 1);
|
|
476
|
+
noteIds.set(candidate.key, id);
|
|
477
|
+
nodes.push({
|
|
478
|
+
id,
|
|
479
|
+
kind: 'note',
|
|
480
|
+
title: candidate.title || '便签 ' + (index + 1),
|
|
481
|
+
note: candidate.note,
|
|
482
|
+
dependsOn: [],
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
487
|
+
selectedSessions.forEach((candidate, index) => {
|
|
488
|
+
const from = 'session-' + (index + 1);
|
|
489
|
+
for (const target of pipelineTargets(candidate.pipeline)) {
|
|
490
|
+
const targetKey = cleanText(target.id ?? target.targetSessionId ?? target.targetNoteId);
|
|
491
|
+
const to = target.kind === 'note'
|
|
492
|
+
? noteIds.get(targetKey)
|
|
493
|
+
: (sessionIds.get(targetKey) || noteIds.get(targetKey));
|
|
494
|
+
if (to) nodeById.get(to).dependsOn.push(from);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const draft = normalizeTopologyPlan({ title, goal, nodes });
|
|
499
|
+
if (validateTopologyPlan(draft).valid) return draft;
|
|
500
|
+
|
|
501
|
+
// A local draft must always be usable. Preserve the selected cards but replace
|
|
502
|
+
// broken imported edges with a transparent, deterministic linear backbone.
|
|
503
|
+
nodes.forEach((node) => { node.dependsOn = []; });
|
|
504
|
+
const sessionNodes = nodes.filter((node) => node.kind === 'session');
|
|
505
|
+
const noteNodes = nodes.filter((node) => node.kind === 'note');
|
|
506
|
+
for (let index = 1; index < sessionNodes.length; index += 1) {
|
|
507
|
+
sessionNodes[index].dependsOn = [sessionNodes[index - 1].id];
|
|
508
|
+
}
|
|
509
|
+
for (const note of noteNodes) note.dependsOn = [sessionNodes[sessionNodes.length - 1].id];
|
|
510
|
+
return normalizeTopologyPlan({ title, goal, nodes });
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** A minimal, always-valid fallback: one session writes one result note. */
|
|
514
|
+
export function createFallbackTopology(value = {}) {
|
|
515
|
+
const options = typeof value === 'string' ? { goal: value } : (isRecord(value) ? value : {});
|
|
516
|
+
const goal = cleanText(options.goal ?? options.objective ?? options.brief);
|
|
517
|
+
const sessions = Array.isArray(options.sessions) ? options.sessions : [];
|
|
518
|
+
const cards = Array.isArray(options.cards) ? options.cards : [];
|
|
519
|
+
const existing = [...sessions, ...cards].map(sessionCandidate).find(Boolean);
|
|
520
|
+
const sessionTitle = cleanText(options.sessionTitle) || existing?.title || '执行任务';
|
|
521
|
+
const session = {
|
|
522
|
+
id: 'session-1',
|
|
523
|
+
kind: 'session',
|
|
524
|
+
title: sessionTitle,
|
|
525
|
+
prompt: goal || existing?.prompt || '明确目标与约束,执行任务,并汇报可验证的结果。',
|
|
526
|
+
dependsOn: [],
|
|
527
|
+
};
|
|
528
|
+
if (existing?.sessionId) session.sessionId = existing.sessionId;
|
|
529
|
+
if (existing?.workspace) session.workspace = existing.workspace;
|
|
530
|
+
|
|
531
|
+
return normalizeTopologyPlan({
|
|
532
|
+
title: cleanText(options.title) || shortTitle(goal, '基础任务'),
|
|
533
|
+
goal,
|
|
534
|
+
nodes: [
|
|
535
|
+
session,
|
|
536
|
+
{
|
|
537
|
+
id: 'note-1',
|
|
538
|
+
kind: 'note',
|
|
539
|
+
title: cleanText(options.noteTitle) || '结果汇总',
|
|
540
|
+
note: cleanText(options.note) || '# 结果汇总',
|
|
541
|
+
dependsOn: ['session-1'],
|
|
542
|
+
},
|
|
543
|
+
],
|
|
544
|
+
});
|
|
545
|
+
}
|