Files
waggle-os/packages/agent/src/skill-autoextract.ts
shumeiko.it 2e1999a3f6 refactor: remove self-learning and the optimizer package (P1.4)
Self-learning is excluded from the community edition (ADR-0004); the
project owner reworks it separately. Cost tracking stays.

- agent: delete behaviour evolution (evolve-schema, iterative GEPA,
  compose-evolution, evolution gates, orchestrator and deploy),
  improvement and correction detection, AgentLearning, the eval-dataset
  builder, optimization-capture and the agent_insights tool. Secret
  detection moves from eval-dataset to secret-patterns. The LLM wiring
  keeps only the Haiku adapter and judge call used by skill audit and
  hygiene, so @ax-llm/ax is now declared by agent instead of server.
- server: delete /api/evolution/*, the evolution and optimizer services,
  GEPA expansion of the first chat message, the prompt_optimization and
  monthly_assessment jobs, the idle-session reviewer and its
  session-reviewer persona, prompt injection of past corrections and
  thumbs-down recorded as correction signals. Existing job rows of the
  removed types are ignored, not deleted.
- core: delete OptimizationLogStore; stop re-exporting the improvement
  signal and evolution run stores.
- web: delete the Evolution screen and the two job types in the
  automation editor.
- delete packages/optimizer.

Also logs UF-027 (self-learning loops read data nothing collects) and
UF-028 (the Settings prompt-shape choice is ignored by chat).
2026-10-20 00:54:58 +02:00

118 lines
4.1 KiB
TypeScript

/**
* Skills 2.0 gap A — auto-extract skills from session history.
*
* Closes the loop between detectWorkflowPattern (session analysis) and
* generateSkillMarkdown (SKILL.md file generation). Previously these two
* primitives existed but were never wired together — SKILL.md files only
* got created via the explicit create_skill agent tool.
*
* This module provides a deterministic pipeline (no LLM required) that:
* 1. Runs detectWorkflowPattern on a message history
* 2. Generates a SKILL.md via generateSkillMarkdown
* 3. Writes it to the personal scope directory (scope: personal frontmatter)
* 4. Dedups against existing skill files by filename
*
* A v2 enhancement can insert an LLM prose-improvement step between
* detectWorkflowPattern and generateSkillMarkdown without changing this
* module's public surface.
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import {
detectWorkflowPattern,
generateSkillMarkdown,
type SkillTemplate,
} from './skill-creator.js';
import { redactSkillContent } from './skill-redaction.js';
export interface AutoExtractMessage {
role: string;
content: string;
toolsUsed?: string[];
}
export interface AutoExtractDeps {
/** Directory to write personal-scope SKILL.md files into */
personalSkillsDir: string;
/**
* Optional LLM enrichment hook. When provided, the template is passed
* through before serialization so the SKILL.md prose can be rewritten.
* Must return a SkillTemplate (may be the same object). If the hook
* throws, the deterministic template is used as a safe fallback.
*/
enrichTemplate?: (template: SkillTemplate, messages: AutoExtractMessage[]) => Promise<SkillTemplate>;
}
export interface AutoExtractResult {
/** Path to the SKILL.md written, or null when nothing was created */
filePath: string | null;
/** Why the extraction returned null (when applicable) */
reason?: 'no_pattern' | 'already_exists';
/** The template used (present when filePath is non-null) */
template?: SkillTemplate;
}
/** Produce a kebab-case filename stem from a SkillTemplate name. */
export function skillFilename(template: SkillTemplate): string {
return template.name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
/**
* Inject scope + auto-extracted provenance into the generated SKILL.md so
* gap-E promotion gates recognize this as a personal-scope skill with a
* clear origin marker. Operates on the frontmatter block only.
*/
function annotateWithScope(md: string): string {
// Insert the scope line right after the description line in the
// frontmatter block. detectWorkflowPattern + generateSkillMarkdown never
// emit scope, so no existing line is overwritten.
return md.replace(
/(description: [^\n]+)\n/,
'$1\nscope: personal\n',
);
}
/**
* Run the full pipeline end-to-end. Returns null.filePath when no pattern
* was detected or a skill with the same filename already exists.
*/
export async function autoExtractAndCreateSkill(
messages: AutoExtractMessage[],
deps: AutoExtractDeps,
): Promise<AutoExtractResult> {
const template = detectWorkflowPattern(messages);
if (!template) {
return { filePath: null, reason: 'no_pattern' };
}
const stem = skillFilename(template);
if (!fs.existsSync(deps.personalSkillsDir)) {
fs.mkdirSync(deps.personalSkillsDir, { recursive: true });
}
const filePath = path.join(deps.personalSkillsDir, `${stem}.md`);
if (fs.existsSync(filePath)) {
return { filePath: null, reason: 'already_exists', template };
}
// Optional LLM enrichment — isolated in try/catch so a flaky model
// never fails the deterministic fallback path.
let finalTemplate = template;
if (deps.enrichTemplate) {
try {
finalTemplate = await deps.enrichTemplate(template, messages);
} catch {
finalTemplate = template;
}
}
// Strip secrets + user-home paths — the auto-extracted pattern is derived
// from session messages, which can carry both.
const md = redactSkillContent(annotateWithScope(generateSkillMarkdown(finalTemplate))).content;
fs.writeFileSync(filePath, md, 'utf-8');
return { filePath, template: finalTemplate };
}