self-evolving-workflow 0.0.0-stage → 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/LICENSE +27 -0
- package/README.md +90 -2
- package/dist/agent-memory-prompts.d.ts +13 -0
- package/dist/agent-memory-prompts.js +55 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +6 -0
- package/dist/closeout.d.ts +105 -0
- package/dist/closeout.js +479 -0
- package/dist/code.d.ts +39 -0
- package/dist/code.js +124 -0
- package/dist/common.d.ts +9 -0
- package/dist/common.js +42 -0
- package/dist/context-envelope.d.ts +59 -0
- package/dist/context-envelope.js +23 -0
- package/dist/host-adapters.d.ts +34 -0
- package/dist/host-adapters.js +151 -0
- package/dist/http.d.ts +3 -0
- package/dist/http.js +90 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +13 -0
- package/dist/knowledge-cli.d.ts +183 -0
- package/dist/knowledge-cli.js +654 -0
- package/dist/knowledge-reads.d.ts +71 -0
- package/dist/knowledge-reads.js +193 -0
- package/dist/memory.d.ts +97 -0
- package/dist/memory.js +250 -0
- package/dist/retrospective.d.ts +57 -0
- package/dist/retrospective.js +248 -0
- package/dist/search.d.ts +36 -0
- package/dist/search.js +112 -0
- package/dist/skill-addition.d.ts +25 -0
- package/dist/skill-addition.js +89 -0
- package/dist/skill-design.d.ts +96 -0
- package/dist/skill-design.js +226 -0
- package/dist/skill-replay.d.ts +55 -0
- package/dist/skill-replay.js +451 -0
- package/dist/skill-route.d.ts +30 -0
- package/dist/skill-route.js +75 -0
- package/dist/transcript-evidence.d.ts +3 -0
- package/dist/transcript-evidence.js +58 -0
- package/dist/upstream/chunker.d.ts +24 -0
- package/dist/upstream/chunker.js +96 -0
- package/dist/upstream/file-protocol.d.ts +45 -0
- package/dist/upstream/file-protocol.js +117 -0
- package/dist/upstream/frontmatter.d.ts +47 -0
- package/dist/upstream/frontmatter.js +86 -0
- package/dist/upstream/graph-search.d.ts +33 -0
- package/dist/upstream/graph-search.js +67 -0
- package/dist/upstream/l1-extraction.d.ts +26 -0
- package/dist/upstream/l1-extraction.js +405 -0
- package/dist/upstream/llm.d.ts +8 -0
- package/dist/upstream/llm.js +1 -0
- package/dist/upstream/merge.d.ts +50 -0
- package/dist/upstream/merge.js +153 -0
- package/dist/upstream/prompts.d.ts +53 -0
- package/dist/upstream/prompts.js +185 -0
- package/dist/upstream/search-utils.d.ts +34 -0
- package/dist/upstream/search-utils.js +55 -0
- package/dist/upstream/slug.d.ts +26 -0
- package/dist/upstream/slug.js +103 -0
- package/dist/upstream/template.d.ts +29 -0
- package/dist/upstream/template.js +80 -0
- package/dist/upstream/tokenize.d.ts +4 -0
- package/dist/upstream/tokenize.js +29 -0
- package/dist/wiki-change.d.ts +76 -0
- package/dist/wiki-change.js +299 -0
- package/dist/wiki-entry.d.ts +26 -0
- package/dist/wiki-entry.js +93 -0
- package/dist/wiki-ingest.d.ts +12 -0
- package/dist/wiki-ingest.js +126 -0
- package/dist/wiki-migration.d.ts +16 -0
- package/dist/wiki-migration.js +268 -0
- package/dist/wiki-tokens.d.ts +1 -0
- package/dist/wiki-tokens.js +46 -0
- package/dist/wiki.d.ts +103 -0
- package/dist/wiki.js +330 -0
- package/dist/workflow-cli.d.ts +2 -0
- package/dist/workflow-cli.js +258 -0
- package/dist/workflow-install.d.ts +36 -0
- package/dist/workflow-install.js +148 -0
- package/dist/workflow-knowledge.d.ts +16 -0
- package/dist/workflow-knowledge.js +24 -0
- package/dist/workflow-mcp.d.ts +2 -0
- package/dist/workflow-mcp.js +144 -0
- package/dist/workflow-names.d.ts +4 -0
- package/dist/workflow-names.js +18 -0
- package/dist/workflow-runtime.d.ts +622 -0
- package/dist/workflow-runtime.js +627 -0
- package/dist/workflow-skills.d.ts +48 -0
- package/dist/workflow-skills.js +141 -0
- package/package.json +76 -4
- package/skills/ew-odyssey/SKILL.md +237 -0
- package/skills/ew-odyssey/references/audit-artifacts.md +7 -0
- package/skills/ew-odyssey/references/odyssey-base-codex.md +3 -0
- package/skills/ew-odyssey/references/odyssey-base.md +278 -0
- package/skills/ew-odyssey/references/odyssey-debug.codex.md +305 -0
- package/skills/ew-odyssey/references/odyssey-debug.md +283 -0
- package/skills/ew-odyssey/references/odyssey-defensive.codex.md +395 -0
- package/skills/ew-odyssey/references/odyssey-defensive.md +369 -0
- package/skills/ew-odyssey/references/odyssey-improve.codex.md +270 -0
- package/skills/ew-odyssey/references/odyssey-improve.md +248 -0
- package/skills/ew-odyssey/references/odyssey-planex.codex.md +352 -0
- package/skills/ew-odyssey/references/odyssey-planex.md +312 -0
- package/skills/ew-odyssey/references/odyssey-review.codex.md +283 -0
- package/skills/ew-odyssey/references/odyssey-review.md +251 -0
- package/skills/ew-odyssey/references/odyssey-security.md +249 -0
- package/skills/ew-odyssey/references/odyssey-ui.codex.md +294 -0
- package/skills/ew-odyssey/references/odyssey-ui.md +250 -0
- package/skills/ew-odyssey/references/prepare/odyssey-debug.md +105 -0
- package/skills/ew-odyssey/references/prepare/odyssey-defensive.md +121 -0
- package/skills/ew-odyssey/references/prepare/odyssey-improve.md +93 -0
- package/skills/ew-odyssey/references/prepare/odyssey-planex.md +104 -0
- package/skills/ew-odyssey/references/prepare/odyssey-review.md +98 -0
- package/skills/ew-odyssey/references/prepare/odyssey-security.md +94 -0
- package/skills/ew-odyssey/references/prepare/odyssey-ui.md +87 -0
- package/skills/ew-orchestrate/SKILL.md +236 -0
- package/skills/ew-orchestrate/references/auto-policy.md +165 -0
- package/skills/ew-orchestrate/references/companion-route.md +52 -0
- package/skills/ew-orchestrate/references/ew-orchestrate.md +27 -0
- package/skills/ew-orchestrate/references/goal.md +53 -0
- package/skills/ew-orchestrate/references/knowledge-route.md +163 -0
- package/skills/ew-orchestrate/references/orchestrator-run-loop.md +36 -0
- package/skills/ew-orchestrate/references/planning.md +137 -0
- package/skills/ew-orchestrate/steps/ew-analyze/SKILL.md +62 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/boundary-grill.md +50 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/issue-gaps-analyze.md +59 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/prepare.md +42 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/workflow.md +401 -0
- package/skills/ew-orchestrate/steps/ew-brainstorm/SKILL.md +32 -0
- package/skills/ew-orchestrate/steps/ew-companion/SKILL.md +9 -0
- package/skills/ew-orchestrate/steps/ew-execute/SKILL.md +71 -0
- package/skills/ew-orchestrate/steps/ew-execute/references/prepare.md +69 -0
- package/skills/ew-orchestrate/steps/ew-execute/references/workflow.md +373 -0
- package/skills/ew-orchestrate/steps/ew-plan/SKILL.md +102 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/boundary-grill.md +50 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/prepare.md +51 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/tdd.md +53 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/workflow.md +278 -0
- package/skills/ew-orchestrate/steps/ew-review/SKILL.md +44 -0
- package/skills/ew-orchestrate/steps/ew-review/references/odyssey-base-codex.md +3 -0
- package/skills/ew-orchestrate/steps/ew-review/references/prepare.md +76 -0
- package/skills/ew-orchestrate/steps/ew-review/references/workflow.md +283 -0
- package/skills/ew-skill-design/SKILL.md +28 -0
- package/skills/ew-skill-design/references/package-contract.md +28 -0
- package/skills/ew-skill-design/references/validation.md +7 -0
- package/skills/team-swarm/SKILL.md +231 -0
- package/skills/team-swarm/references/mcp-coordination.md +10 -0
- package/skills/team-swarm/roles/analyst/role.md +181 -0
- package/skills/team-swarm/roles/ant/role.md +149 -0
- package/skills/team-swarm/roles/coordinator/commands/converge.md +154 -0
- package/skills/team-swarm/roles/coordinator/commands/init-swarm.md +134 -0
- package/skills/team-swarm/roles/coordinator/commands/iterate.md +202 -0
- package/skills/team-swarm/roles/coordinator/role.md +228 -0
- package/skills/team-swarm/roles/scorer/role.md +150 -0
- package/skills/team-swarm/scripts/aco.py +646 -0
- package/skills/team-swarm/scripts/pheromone.py +144 -0
- package/skills/team-swarm/scripts/scoring.py +111 -0
- package/skills/team-swarm/scripts/test_aco.py +606 -0
- package/skills/team-swarm/specs/ant-output-schema.md +127 -0
- package/skills/team-swarm/specs/convergence-criteria.md +108 -0
- package/skills/team-swarm/specs/pheromone-schema.md +125 -0
- package/skills/team-swarm/specs/swarm-config-template.json +71 -0
- package/skills/team-swarm/specs/swarm-protocol.md +123 -0
- package/skills/workflow-closeout/SKILL.md +10 -0
- package/skills/workflow-evolve/SKILL.md +79 -0
- package/skills/workflow-evolve/references/agy-gate.md +27 -0
- package/skills/workflow-evolve/references/auto-loop.md +20 -0
- package/skills/workflow-evolve/references/history-review.md +14 -0
- package/skills/workflow-evolve/scripts/codex-memory-replay.mjs +76 -0
- package/skills/workflow-evolve/scripts/codex-replay.mjs +1 -0
- package/skills/workflow-evolve/scripts/evolve-loop.mjs +259 -0
- package/skills/workflow-evolve/scripts/memory-codex-mcp.mjs +124 -0
- package/workflows/index.json +23 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { lstatSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { basename, join, posix } from 'node:path';
|
|
4
|
+
import { stringify } from 'yaml';
|
|
5
|
+
import { InputError, requireText } from './common.js';
|
|
6
|
+
import { parseFrontmatter } from './upstream/frontmatter.js';
|
|
7
|
+
import { isRepositoryId, isWikiCategory, readWikiEntryMetadata, splitLegacyKeywords } from './wiki-entry.js';
|
|
8
|
+
export function validateWikiMemberUid(uid) {
|
|
9
|
+
if (!uid || !/^[a-z0-9][a-z0-9_.+@-]*$/i.test(uid) || uid.endsWith('.')) {
|
|
10
|
+
throw new InputError('Personal Wiki migration requires a valid member UID');
|
|
11
|
+
}
|
|
12
|
+
return uid;
|
|
13
|
+
}
|
|
14
|
+
const legacySpecFiles = {
|
|
15
|
+
'coding-conventions.md': 'coding', 'architecture-constraints.md': 'arch', 'debug-notes.md': 'debug',
|
|
16
|
+
'test-conventions.md': 'test', 'review-standards.md': 'review', 'quality-rules.md': 'review',
|
|
17
|
+
'learnings.md': 'learning', 'ui-conventions.md': 'ui',
|
|
18
|
+
};
|
|
19
|
+
function legacyRepositories(raw) {
|
|
20
|
+
if (raw === undefined)
|
|
21
|
+
return undefined;
|
|
22
|
+
const list = typeof raw === 'string' ? raw.split(',').map(id => id.trim()).filter(Boolean) : raw;
|
|
23
|
+
if (!Array.isArray(list) || !list.length || !list.every(isRepositoryId))
|
|
24
|
+
return null;
|
|
25
|
+
return [...new Set(list)].sort();
|
|
26
|
+
}
|
|
27
|
+
function parseSpecs(source, origin) {
|
|
28
|
+
const document = parseFrontmatter(source);
|
|
29
|
+
if (source.startsWith('---\n') || source.startsWith('---\r\n')) {
|
|
30
|
+
if (!document.hasFrontmatter)
|
|
31
|
+
throw new InputError(`Invalid Spec frontmatter: ${origin}`);
|
|
32
|
+
}
|
|
33
|
+
const firstLine = source.slice(0, source.length - document.body.length).split('\n').length;
|
|
34
|
+
const matches = [...document.body.matchAll(/<spec-entry\b\s*([^>]*)>([\s\S]*?)<\/spec-entry>/g)];
|
|
35
|
+
const specs = new Map();
|
|
36
|
+
const fileMetadata = document.hasFrontmatter ? document.frontmatter : {};
|
|
37
|
+
const category = legacySpecFiles[basename(origin)] ??
|
|
38
|
+
[fileMetadata.category, basename(origin, '.md')].find(isWikiCategory);
|
|
39
|
+
const fileRepositories = fileMetadata.appliesToRepoIds ?? fileMetadata['applies-to-repo-ids'];
|
|
40
|
+
for (const match of matches) {
|
|
41
|
+
const body = match[2].trim();
|
|
42
|
+
const attributes = Object.fromEntries([...match[1].matchAll(/([\w-]+)="([^"]*)"/g)]
|
|
43
|
+
.map(([, key, value]) => [key, value.replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>').replaceAll('&', '&')]));
|
|
44
|
+
const title = attributes.title || body.match(/^#{2,6}\s+(.+)$/m)?.[1] ||
|
|
45
|
+
[...(body.split(/\r?\n/, 1)[0] ?? '')].slice(0, 100).join('');
|
|
46
|
+
if (!title || !body)
|
|
47
|
+
throw new InputError(`Unrecognized Spec entry in ${origin}`);
|
|
48
|
+
const line = firstLine + document.body.slice(0, match.index).split('\n').length - 1;
|
|
49
|
+
const entryRepositories = attributes.appliesToRepoIds ?? attributes['applies-to-repo-ids'];
|
|
50
|
+
specs.set(line, {
|
|
51
|
+
origin: `${origin}#L${line}`, oldId: attributes.sid || '', title, body, mode: 'constraint',
|
|
52
|
+
metadata: { ...fileMetadata, ...attributes },
|
|
53
|
+
category, repositories: entryRepositories?.trim() ? entryRepositories : fileRepositories,
|
|
54
|
+
keywords: splitLegacyKeywords(attributes.keywords),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const remaining = document.body.replace(/<spec-entry\b\s*[^>]*>[\s\S]*?<\/spec-entry>/g, match => match.replace(/[^\r\n]/g, ' '));
|
|
58
|
+
if (/<\/?spec-entry\b/.test(remaining))
|
|
59
|
+
throw new InputError(`Unclosed Spec entry in ${origin}`);
|
|
60
|
+
const headings = [...remaining.matchAll(/^###\s+(?:\[[\w-]+\]\s+)?\[\d{4}-\d{2}-\d{2}(?:[ \t][\d:]+)?\][ \t]*([^\r\n]+)\r?$/gm)];
|
|
61
|
+
for (const [index, heading] of headings.entries()) {
|
|
62
|
+
const line = firstLine + remaining.slice(0, heading.index).split('\n').length - 1;
|
|
63
|
+
const next = headings[index + 1]?.index ?? remaining.length;
|
|
64
|
+
const body = remaining.slice(heading.index, next).trim();
|
|
65
|
+
if (body === heading[0].trim())
|
|
66
|
+
continue;
|
|
67
|
+
specs.set(line, {
|
|
68
|
+
origin: `${origin}#L${line}`, oldId: '', title: heading[1], body, mode: 'constraint', metadata: fileMetadata,
|
|
69
|
+
category, repositories: fileRepositories,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const plainBody = document.body.replace(/^#+\s+.*$/gm, '').replace(/^---+$/gm, '').trim();
|
|
73
|
+
const emptyLearningsSeed = basename(origin) === 'learnings.md' && plainBody === 'Add entries with: `/maestro-spec learning <description>`';
|
|
74
|
+
if (!specs.size && plainBody && !emptyLearningsSeed) {
|
|
75
|
+
const title = typeof document.frontmatter.title === 'string' && document.frontmatter.title.trim()
|
|
76
|
+
? document.frontmatter.title.trim() : document.body.match(/^#\s+(.+)$/m)?.[1]?.trim() || basename(origin, '.md');
|
|
77
|
+
specs.set(firstLine, {
|
|
78
|
+
origin, oldId: '', title, body: document.body, mode: 'constraint', metadata: fileMetadata,
|
|
79
|
+
category, repositories: fileRepositories,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return [...specs.entries()].sort(([left], [right]) => left - right).map(([, entry]) => entry);
|
|
83
|
+
}
|
|
84
|
+
function readLegacySpecFolder(sourceRoot, relative) {
|
|
85
|
+
const directory = join(sourceRoot, relative);
|
|
86
|
+
const info = lstatSync(directory, { throwIfNoEntry: false });
|
|
87
|
+
if (!info)
|
|
88
|
+
return [];
|
|
89
|
+
if (!info.isDirectory())
|
|
90
|
+
throw new InputError(`Invalid legacy Spec directory: ${directory}`);
|
|
91
|
+
return readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap(file => file.isFile() && file.name.endsWith('.md')
|
|
92
|
+
? parseSpecs(readFileSync(join(directory, file.name), 'utf8').replace(/^\uFEFF/, ''), `${relative}/${file.name}`)
|
|
93
|
+
: []);
|
|
94
|
+
}
|
|
95
|
+
function readLegacyKnowhowFolder(sourceRoot, relative) {
|
|
96
|
+
const entries = [];
|
|
97
|
+
const directory = join(sourceRoot, relative);
|
|
98
|
+
const info = lstatSync(directory, { throwIfNoEntry: false });
|
|
99
|
+
if (!info)
|
|
100
|
+
return entries;
|
|
101
|
+
if (!info.isDirectory())
|
|
102
|
+
throw new InputError(`Invalid legacy knowledge directory: ${directory}`);
|
|
103
|
+
for (const file of readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
104
|
+
if (!file.isFile() || !file.name.endsWith('.md'))
|
|
105
|
+
continue;
|
|
106
|
+
const source = readFileSync(join(directory, file.name), 'utf8').replace(/^\uFEFF/, '');
|
|
107
|
+
const origin = `${relative}/${file.name}`;
|
|
108
|
+
const parsed = parseFrontmatter(source);
|
|
109
|
+
if (source.startsWith('---') && !parsed.hasFrontmatter)
|
|
110
|
+
throw new InputError(`Invalid Knowhow frontmatter: ${origin}`);
|
|
111
|
+
const oldId = typeof parsed.frontmatter.id === 'string' ? parsed.frontmatter.id : basename(file.name, '.md');
|
|
112
|
+
const title = parsed.frontmatter.title || parsed.body.match(/^#\s+(.+)$/m)?.[1] || oldId;
|
|
113
|
+
entries.push({ origin, oldId, title, body: parsed.body, mode: 'reference', metadata: parsed.frontmatter,
|
|
114
|
+
repositories: parsed.frontmatter.appliesToRepoIds ?? parsed.frontmatter['applies-to-repo-ids'] });
|
|
115
|
+
}
|
|
116
|
+
return entries;
|
|
117
|
+
}
|
|
118
|
+
function collectLegacy(sourceRoot, layer) {
|
|
119
|
+
const workflow = join(sourceRoot, '.workflow');
|
|
120
|
+
if (layer.scope !== 'global' && !lstatSync(workflow, { throwIfNoEntry: false })?.isDirectory()) {
|
|
121
|
+
throw new InputError(`Legacy project has no regular .workflow directory: ${workflow}`);
|
|
122
|
+
}
|
|
123
|
+
if (layer.scope === 'project' || layer.scope === 'global') {
|
|
124
|
+
const prefix = layer.scope === 'project' ? '.workflow/' : '';
|
|
125
|
+
const specs = layer.knowhowOnly ? [] : readLegacySpecFolder(sourceRoot, `${prefix}specs`);
|
|
126
|
+
return [...specs, ...readLegacyKnowhowFolder(sourceRoot, `${prefix}knowhow`)];
|
|
127
|
+
}
|
|
128
|
+
const collab = lstatSync(join(workflow, 'collab'), { throwIfNoEntry: false });
|
|
129
|
+
if (!collab)
|
|
130
|
+
return [];
|
|
131
|
+
if (!collab.isDirectory())
|
|
132
|
+
throw new InputError('Invalid legacy collaboration directory');
|
|
133
|
+
const shared = '.workflow/collab/specs';
|
|
134
|
+
if (layer.scope === 'team')
|
|
135
|
+
return readLegacySpecFolder(sourceRoot, shared);
|
|
136
|
+
const specs = lstatSync(join(sourceRoot, shared), { throwIfNoEntry: false });
|
|
137
|
+
if (!specs)
|
|
138
|
+
return [];
|
|
139
|
+
if (!specs.isDirectory())
|
|
140
|
+
throw new InputError('Invalid legacy collaboration Spec directory');
|
|
141
|
+
return readLegacySpecFolder(sourceRoot, `${shared}/${layer.uid}`);
|
|
142
|
+
}
|
|
143
|
+
function statusFor(entry) {
|
|
144
|
+
const status = entry.metadata.lifecycleStatus ?? entry.metadata['lifecycle-status'] ?? entry.metadata.status;
|
|
145
|
+
if (status === 'deprecated' || status === 'superseded')
|
|
146
|
+
return 'deprecated';
|
|
147
|
+
if (entry.metadata['conflict-marker'] || entry.metadata.confidence === 'contested' ||
|
|
148
|
+
legacyRepositories(entry.repositories) === null ||
|
|
149
|
+
(status && !['active', 'accepted'].includes(String(status))))
|
|
150
|
+
return 'needs_review';
|
|
151
|
+
return 'active';
|
|
152
|
+
}
|
|
153
|
+
export function migrateLegacyWiki(wiki, sourceRoot, project, apply = false, layer = { scope: 'project' }) {
|
|
154
|
+
project = requireText(project, 'legacy project ID');
|
|
155
|
+
if (!/^[a-z0-9][a-z0-9_.-]*$/i.test(project))
|
|
156
|
+
throw new InputError('Legacy project ID must be a stable slug');
|
|
157
|
+
if (layer.knowhowOnly && (layer.scope === 'team' || layer.scope === 'personal')) {
|
|
158
|
+
throw new InputError('--knowhow-only requires project or global scope');
|
|
159
|
+
}
|
|
160
|
+
if (layer.scope === 'personal')
|
|
161
|
+
validateWikiMemberUid(layer.uid);
|
|
162
|
+
const sources = collectLegacy(sourceRoot, layer);
|
|
163
|
+
const destination = join(wiki.root, 'wiki', 'entries');
|
|
164
|
+
for (const path of [join(wiki.root, 'wiki'), destination]) {
|
|
165
|
+
const info = lstatSync(path, { throwIfNoEntry: false });
|
|
166
|
+
if (info && !info.isDirectory())
|
|
167
|
+
throw new InputError(`Invalid Wiki migration directory: ${path}`);
|
|
168
|
+
}
|
|
169
|
+
const oldIds = new Map();
|
|
170
|
+
const knowhowPaths = new Map();
|
|
171
|
+
const namespace = layer.scope === 'personal' ? `personal:${layer.uid}:` : layer.scope === 'project' ? '' : `${layer.scope}:`;
|
|
172
|
+
const writes = sources.map(entry => {
|
|
173
|
+
const kind = entry.mode === 'constraint' ? 'spec' : 'knowhow';
|
|
174
|
+
const key = `${project}:${kind}:${namespace}${entry.oldId || entry.origin}`;
|
|
175
|
+
const id = `legacy-${createHash('sha256').update(key).digest('hex').slice(0, 24)}`;
|
|
176
|
+
if (entry.oldId) {
|
|
177
|
+
const oldKey = `${kind}:${entry.oldId.toLowerCase()}`;
|
|
178
|
+
if (oldIds.has(oldKey))
|
|
179
|
+
throw new InputError(`Duplicate legacy Wiki ID: ${entry.oldId}`);
|
|
180
|
+
oldIds.set(oldKey, id);
|
|
181
|
+
}
|
|
182
|
+
if (kind === 'knowhow')
|
|
183
|
+
knowhowPaths.set(entry.origin.replace(/^\.workflow\//, '').toLowerCase(), id);
|
|
184
|
+
return { entry, id, key, kind, status: statusFor(entry) };
|
|
185
|
+
});
|
|
186
|
+
const planned = writes.map(({ entry, id, key, kind, status }) => {
|
|
187
|
+
const relations = [];
|
|
188
|
+
const issues = [];
|
|
189
|
+
for (const [field, label] of [
|
|
190
|
+
[entry.metadata.supersededBy ?? entry.metadata['superseded-by'], 'Superseding entry'],
|
|
191
|
+
[entry.metadata.supersedes, 'Earlier entry'],
|
|
192
|
+
]) {
|
|
193
|
+
if (field === undefined)
|
|
194
|
+
continue;
|
|
195
|
+
const targets = new Set();
|
|
196
|
+
let unresolved = false;
|
|
197
|
+
for (const reference of Array.isArray(field) ? field : [field]) {
|
|
198
|
+
const key = typeof reference === 'string' ? reference.trim().toLowerCase() : '';
|
|
199
|
+
const target = oldIds.get(`${kind}:${key}`) ??
|
|
200
|
+
(kind === 'knowhow' ? oldIds.get(`${kind}:${key.replace(/^knowhow-/, '')}`) : undefined);
|
|
201
|
+
if (target && target !== id)
|
|
202
|
+
targets.add(target);
|
|
203
|
+
else
|
|
204
|
+
unresolved = true;
|
|
205
|
+
}
|
|
206
|
+
for (const target of targets)
|
|
207
|
+
relations.push(`[[entries/${target}|${label}]]`);
|
|
208
|
+
if (unresolved)
|
|
209
|
+
issues.push(`Unresolved ${label.toLowerCase()}`);
|
|
210
|
+
}
|
|
211
|
+
const ref = entry.metadata.ref;
|
|
212
|
+
if (kind === 'spec' && ref !== undefined) {
|
|
213
|
+
const refPath = typeof ref === 'string' ? posix.normalize(ref.replaceAll('\\', '/')).toLowerCase() : '';
|
|
214
|
+
const target = knowhowPaths.get(refPath);
|
|
215
|
+
if (target)
|
|
216
|
+
relations.push(`[[entries/${target}|Legacy reference]]`);
|
|
217
|
+
else
|
|
218
|
+
issues.push('Unresolved legacy reference');
|
|
219
|
+
}
|
|
220
|
+
const effectiveStatus = issues.length && status === 'active' ? 'needs_review' : status;
|
|
221
|
+
if (status === 'needs_review')
|
|
222
|
+
issues.push('Review legacy lifecycle, conflict, or repository scope');
|
|
223
|
+
const repositories = legacyRepositories(entry.repositories);
|
|
224
|
+
const sources = [`legacy:${project}:${entry.origin}`];
|
|
225
|
+
for (const source of [
|
|
226
|
+
entry.metadata.sourceRef, entry.metadata['source-ref'], entry.metadata.source,
|
|
227
|
+
...(Array.isArray(entry.metadata.sources) ? entry.metadata.sources : []),
|
|
228
|
+
]) {
|
|
229
|
+
if (typeof source === 'string' && source.trim() && !sources.includes(source))
|
|
230
|
+
sources.push(source);
|
|
231
|
+
}
|
|
232
|
+
const data = {
|
|
233
|
+
title: entry.title,
|
|
234
|
+
mode: kind === 'spec' && typeof ref === 'string' && ref.trim() ? 'reference' : entry.mode,
|
|
235
|
+
status: effectiveStatus,
|
|
236
|
+
scope: ['project'],
|
|
237
|
+
...(kind === 'spec' ? { categories: entry.category ? [entry.category] : [] } : {}),
|
|
238
|
+
...(repositories ? { repositories } : {}),
|
|
239
|
+
sources,
|
|
240
|
+
aliases: [key],
|
|
241
|
+
legacy: entry.metadata,
|
|
242
|
+
...(issues.length ? { migration_issues: issues } : {}),
|
|
243
|
+
};
|
|
244
|
+
const body = entry.body.trim() + (relations.length ? `\n\n${relations.join('\n')}` : '');
|
|
245
|
+
const metadata = entry.keywords === undefined ? data : { ...data, keywords: entry.keywords };
|
|
246
|
+
const content = `---\n${stringify(metadata)}---\n\n${body}\n`;
|
|
247
|
+
readWikiEntryMetadata(`wiki/entries/${id}.md`, { type: 'other', ...metadata }, body);
|
|
248
|
+
const path = join(destination, `${id}.md`);
|
|
249
|
+
const existing = lstatSync(path, { throwIfNoEntry: false });
|
|
250
|
+
const previous = existing?.isFile() ? readFileSync(path, 'utf8') : undefined;
|
|
251
|
+
const unchangedLegacy = entry.keywords !== undefined && previous === `---\n${stringify(data)}---\n\n${body}\n`;
|
|
252
|
+
if (existing && previous !== content && !unchangedLegacy) {
|
|
253
|
+
throw new InputError(`Migration would overwrite an existing entry: ${id}`);
|
|
254
|
+
}
|
|
255
|
+
return { id, origin: entry.origin, alias: key, status: effectiveStatus, path, content, existing: Boolean(existing), issues };
|
|
256
|
+
});
|
|
257
|
+
if (apply && planned.length) {
|
|
258
|
+
mkdirSync(destination, { recursive: true });
|
|
259
|
+
for (const item of planned) {
|
|
260
|
+
if (!item.existing)
|
|
261
|
+
writeFileSync(item.path, item.content, { encoding: 'utf8', flag: 'wx' });
|
|
262
|
+
}
|
|
263
|
+
wiki.build();
|
|
264
|
+
}
|
|
265
|
+
return planned.map(({ id, origin, alias, status, existing, issues }) => ({
|
|
266
|
+
id, origin, alias, status, existing, ...(issues.length ? { issues } : {}),
|
|
267
|
+
}));
|
|
268
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function tokenizeWiki(text: string): string[];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// From TencentDB Agent Memory Wiki manager (MIT).
|
|
2
|
+
const STOP_WORDS = new Set([
|
|
3
|
+
"的", "是", "了", "什么", "在", "有", "和", "与", "对", "从",
|
|
4
|
+
"the", "is", "a", "an", "what", "how", "are", "was", "were",
|
|
5
|
+
"do", "does", "did", "be", "been", "being", "have", "has", "had",
|
|
6
|
+
"it", "its", "in", "on", "at", "to", "for", "of", "with", "by",
|
|
7
|
+
"this", "that", "these", "those",
|
|
8
|
+
]);
|
|
9
|
+
export function tokenizeWiki(text) {
|
|
10
|
+
const rawTokens = text
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.split(/[\s,,。!?、;:""''()()\-_/\\·~~…\[\]【】{}《》<>]+/)
|
|
13
|
+
.filter((t) => t.length > 0);
|
|
14
|
+
const result = [];
|
|
15
|
+
for (const token of rawTokens) {
|
|
16
|
+
const hasCJK = /[\u4e00-\u9fff\u3400-\u4dbf]/.test(token);
|
|
17
|
+
const hasLatin = /[a-z]/.test(token);
|
|
18
|
+
if (hasCJK && hasLatin) {
|
|
19
|
+
// 混合 token(如 "l0录入"):拆分中英文部分分别处理
|
|
20
|
+
const parts = token.split(/(?<=[a-z0-9])(?=[\u4e00-\u9fff])|(?<=[\u4e00-\u9fff])(?=[a-z0-9])/);
|
|
21
|
+
for (const part of parts) {
|
|
22
|
+
if (/[\u4e00-\u9fff]/.test(part) && part.length > 1) {
|
|
23
|
+
const chars = [...part];
|
|
24
|
+
for (let i = 0; i < chars.length - 1; i++)
|
|
25
|
+
result.push(chars[i] + chars[i + 1]);
|
|
26
|
+
result.push(part);
|
|
27
|
+
}
|
|
28
|
+
else if (part.length > 0 && !STOP_WORDS.has(part)) {
|
|
29
|
+
result.push(part);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else if (hasCJK && token.length > 1) {
|
|
34
|
+
// 纯中文:bigram
|
|
35
|
+
const chars = [...token];
|
|
36
|
+
for (let i = 0; i < chars.length - 1; i++)
|
|
37
|
+
result.push(chars[i] + chars[i + 1]);
|
|
38
|
+
result.push(token);
|
|
39
|
+
}
|
|
40
|
+
else if (!STOP_WORDS.has(token) && token.length > 0) {
|
|
41
|
+
// 纯英文/数字:保留完整 token
|
|
42
|
+
result.push(token);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
package/dist/wiki.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type WikiContext, type WikiEntryMetadata } from './wiki-entry.js';
|
|
2
|
+
import type { WikiIngestOptions } from './wiki-ingest.js';
|
|
3
|
+
import type { KnowledgeReads } from './knowledge-reads.js';
|
|
4
|
+
interface Page {
|
|
5
|
+
id: string;
|
|
6
|
+
title: string;
|
|
7
|
+
type: string;
|
|
8
|
+
path: string;
|
|
9
|
+
snippet: string;
|
|
10
|
+
sources: string[];
|
|
11
|
+
entry?: WikiEntryMetadata;
|
|
12
|
+
}
|
|
13
|
+
interface PageFile extends Page {
|
|
14
|
+
body: string;
|
|
15
|
+
links: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface WikiSearchOptions {
|
|
18
|
+
limit?: number;
|
|
19
|
+
hop?: number;
|
|
20
|
+
decay?: number;
|
|
21
|
+
includeHistory?: boolean;
|
|
22
|
+
entryMode?: 'constraint' | 'reference';
|
|
23
|
+
}
|
|
24
|
+
export interface WikiReadOptions {
|
|
25
|
+
knowledgeReads?: KnowledgeReads;
|
|
26
|
+
fromSearchId?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface WikiHit extends Page {
|
|
29
|
+
score: number;
|
|
30
|
+
hop: number;
|
|
31
|
+
via?: string;
|
|
32
|
+
related: Array<{
|
|
33
|
+
id: string;
|
|
34
|
+
title: string;
|
|
35
|
+
path: string;
|
|
36
|
+
direction: 'in' | 'out' | 'both';
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
export declare const structuralPages: Set<string>;
|
|
40
|
+
export declare function readWikiPages(root: string, entriesOnly?: boolean): PageFile[];
|
|
41
|
+
export declare class Wiki {
|
|
42
|
+
readonly root: string;
|
|
43
|
+
readonly indexPath: string;
|
|
44
|
+
constructor(root: string);
|
|
45
|
+
ingest(source: string, options: WikiIngestOptions): Promise<{
|
|
46
|
+
pages: number;
|
|
47
|
+
edges: number;
|
|
48
|
+
written: string[];
|
|
49
|
+
warnings: string[];
|
|
50
|
+
}>;
|
|
51
|
+
constraints(context?: WikiContext): {
|
|
52
|
+
body: string;
|
|
53
|
+
id: string;
|
|
54
|
+
title: string;
|
|
55
|
+
type: string;
|
|
56
|
+
path: string;
|
|
57
|
+
snippet: string;
|
|
58
|
+
sources: string[];
|
|
59
|
+
entry?: WikiEntryMetadata;
|
|
60
|
+
}[];
|
|
61
|
+
getEntry(id: string, options?: WikiReadOptions): {
|
|
62
|
+
body: string;
|
|
63
|
+
id: string;
|
|
64
|
+
title: string;
|
|
65
|
+
type: string;
|
|
66
|
+
path: string;
|
|
67
|
+
snippet: string;
|
|
68
|
+
sources: string[];
|
|
69
|
+
entry?: WikiEntryMetadata;
|
|
70
|
+
} | null;
|
|
71
|
+
private visibleReferences;
|
|
72
|
+
getReference(id: string, context?: WikiContext, options?: WikiReadOptions): {
|
|
73
|
+
body: string;
|
|
74
|
+
id: string;
|
|
75
|
+
title: string;
|
|
76
|
+
type: string;
|
|
77
|
+
path: string;
|
|
78
|
+
snippet: string;
|
|
79
|
+
sources: string[];
|
|
80
|
+
entry?: WikiEntryMetadata;
|
|
81
|
+
} | null;
|
|
82
|
+
references(query: string, context?: WikiContext, options?: WikiSearchOptions): {
|
|
83
|
+
results: WikiHit[];
|
|
84
|
+
links: Array<{
|
|
85
|
+
source: string;
|
|
86
|
+
target: string;
|
|
87
|
+
}>;
|
|
88
|
+
};
|
|
89
|
+
getEntryByExactBody(body: string): PageFile | null;
|
|
90
|
+
getEntriesByExactBody(body: string): PageFile[];
|
|
91
|
+
build(): {
|
|
92
|
+
pages: number;
|
|
93
|
+
edges: number;
|
|
94
|
+
};
|
|
95
|
+
search(query: string, options?: WikiSearchOptions): {
|
|
96
|
+
results: WikiHit[];
|
|
97
|
+
links: Array<{
|
|
98
|
+
source: string;
|
|
99
|
+
target: string;
|
|
100
|
+
}>;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export {};
|