@kairos-sdk/core 0.3.1 → 0.4.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/README.md CHANGED
@@ -27,11 +27,21 @@ console.log(result.workflowId) // deployed workflow ID
27
27
  console.log(result.credentialsNeeded) // what the user still needs to configure
28
28
  ```
29
29
 
30
+ ### What Kairos does and does not do
31
+
32
+ | Kairos does | Kairos does not guarantee (yet) |
33
+ |---|---|
34
+ | Generates valid n8n workflow JSON | Perfect business logic |
35
+ | Validates structure before deploy (23 rules) | Correct credentials |
36
+ | Syncs node types from your live instance | Runtime success for every API |
37
+ | Learns from prior successful builds | That every workflow matches intent perfectly |
38
+ | Works through MCP, SDK, or CLI | Full replacement for human review |
39
+
30
40
  ---
31
41
 
32
42
  ## Use as MCP Server (no code required)
33
43
 
34
- Connect Kairos to any MCP-compatible host — Claude Code, Claude Desktop, ChatGPT, Cursor, or any agent that supports the Model Context Protocol. Your host LLM generates the workflow using Kairos's specialized context, then Kairos validates and deploys it. No Anthropic API key needed — no double-LLM calls, no wasted tokens. Kairos auto-syncs your n8n instance's node types so the catalog always matches your exact setup.
44
+ Connect Kairos to any MCP-compatible host — Claude Code, Claude Desktop, Cursor, or any agent that supports the Model Context Protocol. Your host LLM generates the workflow using Kairos's specialized context, then Kairos validates and deploys it. No Anthropic API key needed — no double-LLM calls, no wasted tokens. Kairos auto-syncs your n8n instance's node types so the catalog always matches your exact setup.
35
45
 
36
46
  ### Setup
37
47
 
@@ -131,9 +141,9 @@ npm install @kairos-sdk/core @anthropic-ai/sdk
131
141
  ## Requirements
132
142
 
133
143
  - Node.js 18+
134
- - **SDK only:** An [Anthropic API key](https://console.anthropic.com) — the SDK calls Claude internally
144
+ - **SDK:** An [Anthropic API key](https://console.anthropic.com) — the SDK calls Claude internally
135
145
  - **MCP:** No Anthropic key needed — your host LLM does the generation
136
- - An n8n instance with API access enabled (Cloud or self-hosted) — only needed for deployment, not for generation/validation
146
+ - An n8n instance with API access enabled (Cloud or self-hosted) — required for both SDK and MCP (Kairos syncs your instance's node types for accurate generation)
137
147
 
138
148
  ---
139
149
 
@@ -182,7 +192,7 @@ Tested against 20 workflow prompts of varying complexity (simple triggers, multi
182
192
  | Avg generation time | 30.6s | **20.7s** | -32% |
183
193
  | Failures | 0 | 0 | — |
184
194
 
185
- The baseline run used Claude with the 22-rule validator and correction loop but no library. The seeded run used the same validator plus a library of 105 workflows (16 organic + 89 ingested from the n8n community). Template seeding eliminated the correction loop entirely and cut generation time by a third.
195
+ The baseline run used Claude with the 23-rule validator and correction loop but no library. The seeded run used the same validator plus a library of 105 workflows (16 organic + 89 ingested from the n8n community). The broader local development library now contains 286+ generated/ingested workflows. Template seeding eliminated the correction loop entirely and cut generation time by a third.
186
196
 
187
197
  > **Note:** These results confirm that generated workflows are structurally valid and deployable to n8n. They do not verify runtime execution correctness, credential configuration, or whether the workflow output matches user intent.
188
198
 
@@ -214,7 +224,7 @@ The baseline run used Claude with the 22-rule validator and correction loop but
214
224
 
215
225
  ## Validator Rules
216
226
 
217
- The 22-rule validator is the core of what makes Kairos reliable. In baseline testing (no library), Claude needed the correction loop 45% of the time. Each rule targets a specific class of error:
227
+ The 23-rule validator is the core of what makes Kairos reliable. In baseline testing (no library), Claude needed the correction loop 45% of the time. Each rule targets a specific class of error:
218
228
 
219
229
  | Rule | Severity | What it checks |
220
230
  |------|----------|----------------|
@@ -359,6 +369,9 @@ try {
359
369
  for (const issue of err.issues) {
360
370
  console.error(`[Rule ${issue.rule}] ${issue.message}`)
361
371
  }
372
+ // Attempt metadata and warned rules are also available
373
+ console.log(err.attemptMetadata) // per-attempt timing, tokens, issues
374
+ console.log(err.warnedRules) // which pattern rules were warned about
362
375
  } else if (err instanceof GenerationError) {
363
376
  // Anthropic API call failed (auth, quota, timeout)
364
377
  console.error(err.message, err.cause)
@@ -376,7 +389,7 @@ try {
376
389
  |---|---|
377
390
  | `GenerationError` | Anthropic API call failed |
378
391
  | `ResponseParseError` | Claude responded but produced no usable tool call |
379
- | `ValidationError` | Workflow failed 22-rule validation after max retries |
392
+ | `ValidationError` | Workflow failed 23-rule validation after max retries (carries `.attemptMetadata` and `.warnedRules`) |
380
393
  | `ProviderError` | Network/auth failure talking to n8n |
381
394
  | `ApiError` | n8n returned a 4xx or 5xx (carries `.statusCode`) |
382
395
  | `GuardError` | Input validation failed (empty description) or `delete()` called without `{ confirm: true }` |
@@ -397,6 +410,10 @@ kairos build "Monitor a webhook and log payloads" --dry-run
397
410
  # Seed library with n8n community templates
398
411
  kairos sync-templates --max 200
399
412
 
413
+ # View pattern analysis
414
+ kairos patterns
415
+ kairos patterns --days 60 --json
416
+
400
417
  # Manage workflows
401
418
  kairos list
402
419
  kairos get <workflow-id>
@@ -438,7 +455,22 @@ telemetry: '/path/to/telemetry/dir'
438
455
 
439
456
  Each event includes timestamp, session ID, token counts, validation issues, and duration — useful for benchmarking and analyzing the correction loop.
440
457
 
441
- Kairos also reads telemetry data to compute **per-rule failure rates** across all builds. Rules that fail frequently (>= 15% of builds) are automatically surfaced as warnings in the generation prompt, helping Claude avoid systemic issues. Failure rates use distinct session counting to avoid inflation from retry loops, and results are cached for 5 minutes.
458
+ ### Pattern Learning
459
+
460
+ When telemetry is enabled, Kairos runs a **pattern analyzer** that learns from every build — successes and failures. The analyzer produces `patterns.json` which is fed back into future generations:
461
+
462
+ - **Composite scoring** — patterns are scored using `rawConfidence × impact × recency × (1 + stickinessBoost)`, so frequent, recent, sticky failures rank highest
463
+ - **Stickiness detection** — rules that persist across consecutive failed retry attempts (the LLM can't self-correct) get a scoring boost
464
+ - **State lifecycle** — patterns progress through `draft → confirmed → resolved`, with per-rule resolved thresholds (5 clean builds) and 90-day TTL on resolved patterns
465
+ - **Regression detection** — if a resolved rule starts failing again, it's flagged as regressed and prioritized in the prompt
466
+ - **Warning effectiveness** — tracks whether warning the LLM about a rule actually prevented the failure, with per-rule pass/fail rates
467
+ - **Schema migration** — pattern data auto-migrates across versions (currently v2) so no accumulated knowledge is lost on upgrades
468
+ - **Rule co-occurrence** — identifies pairs of rules that commonly fail together (e.g., rules 5+17 always break at the same time)
469
+ - **Session depth analysis** — tracks how many attempts each session needed (e.g., 80% are 1-attempt, 15% need 2, 5% need all 3)
470
+ - **Warning cap** — max 10 patterns in the LLM prompt, prioritized: regressed > confirmed > drafts
471
+ - **Analysis history** — each analysis run appends a summary to `pattern-history.jsonl` for trend tracking over time
472
+
473
+ Run `kairos patterns` to view the current analysis, or `kairos patterns --json` for raw output.
442
474
 
443
475
  For CLI usage, set `KAIROS_TELEMETRY=true` in your environment.
444
476
 
@@ -3,12 +3,13 @@ import {
3
3
  N8nApiClient,
4
4
  N8nFieldStripper,
5
5
  N8nValidator,
6
+ PatternAnalyzer,
6
7
  PromptBuilder,
7
8
  TelemetryReader,
8
9
  generateUUID,
9
10
  nullLogger,
10
11
  scoreToMode
11
- } from "./chunk-RYGYNOR6.js";
12
+ } from "./chunk-NJ6QZBIC.js";
12
13
 
13
14
  // src/library/null-library.ts
14
15
  var NullLibrary = class {
@@ -122,12 +123,16 @@ var ResponseParseError = class extends KairosError {
122
123
 
123
124
  // src/errors/validation-error.ts
124
125
  var ValidationError = class extends KairosError {
125
- constructor(message, issues) {
126
+ constructor(message, issues, attemptMetadata, warnedRules) {
126
127
  super(message);
127
128
  this.issues = issues;
129
+ this.attemptMetadata = attemptMetadata;
130
+ this.warnedRules = warnedRules;
128
131
  this.name = "ValidationError";
129
132
  }
130
133
  issues;
134
+ attemptMetadata;
135
+ warnedRules;
131
136
  };
132
137
 
133
138
  // src/telemetry/collector.ts
@@ -262,7 +267,7 @@ var WorkflowDesigner = class {
262
267
  issues: validation.issues
263
268
  });
264
269
  if (validation.valid) {
265
- return { workflow: parsed.workflow, credentialsNeeded: parsed.credentialsNeeded, attempts, attemptMetadata };
270
+ return { workflow: parsed.workflow, credentialsNeeded: parsed.credentialsNeeded, attempts, attemptMetadata, warnedRules: this.promptBuilder.getWarnedRules() };
266
271
  }
267
272
  lastErrors = errors;
268
273
  this.logger.warn(`WorkflowDesigner: validation failed on attempt ${attempt}`, {
@@ -272,7 +277,9 @@ var WorkflowDesigner = class {
272
277
  const finalIssues = attemptMetadata.at(-1)?.issues ?? lastErrors;
273
278
  throw new ValidationError(
274
279
  `Workflow failed validation after ${MAX_ATTEMPTS} attempts`,
275
- finalIssues
280
+ finalIssues,
281
+ attemptMetadata,
282
+ this.promptBuilder.getWarnedRules()
276
283
  );
277
284
  }
278
285
  async callClaude(system, userMessage, temperature) {
@@ -334,6 +341,7 @@ var Kairos = class {
334
341
  logger;
335
342
  telemetry;
336
343
  telemetryReader;
344
+ patternAnalyzer;
337
345
  model;
338
346
  saveQueue = Promise.resolve(null);
339
347
  constructor(options) {
@@ -359,12 +367,15 @@ var Kairos = class {
359
367
  if (options.telemetry === true) {
360
368
  this.telemetry = new TelemetryCollector();
361
369
  this.telemetryReader = new TelemetryReader();
370
+ this.patternAnalyzer = new PatternAnalyzer();
362
371
  } else if (typeof options.telemetry === "string") {
363
372
  this.telemetry = new TelemetryCollector(options.telemetry);
364
373
  this.telemetryReader = new TelemetryReader(options.telemetry);
374
+ this.patternAnalyzer = new PatternAnalyzer(options.telemetry);
365
375
  } else {
366
376
  this.telemetry = null;
367
377
  this.telemetryReader = null;
378
+ this.patternAnalyzer = null;
368
379
  }
369
380
  }
370
381
  requireProvider() {
@@ -402,11 +413,45 @@ var Kairos = class {
402
413
  this.logger.info(`Telemetry: ${highFreq.length} high-frequency failure rule(s) will be warned about`);
403
414
  }
404
415
  }
405
- const designResult = await this.designer.design(
406
- { description, ...options?.name ? { name: options.name } : {} },
407
- matches,
408
- globalFailureRates
409
- );
416
+ let designResult;
417
+ try {
418
+ designResult = await this.designer.design(
419
+ { description, ...options?.name ? { name: options.name } : {} },
420
+ matches,
421
+ globalFailureRates
422
+ );
423
+ } catch (err) {
424
+ if (err instanceof ValidationError && err.attemptMetadata) {
425
+ for (const meta of err.attemptMetadata) {
426
+ await this.telemetry?.emit("generation_attempt", {
427
+ description,
428
+ attempt: meta.attempt,
429
+ temperature: meta.temperature,
430
+ durationMs: meta.durationMs,
431
+ tokensInput: meta.tokensInput,
432
+ tokensOutput: meta.tokensOutput,
433
+ validationPassed: meta.validationPassed,
434
+ issueCount: meta.issues.length,
435
+ issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null }))
436
+ });
437
+ }
438
+ await this.telemetry?.emit("build_complete", {
439
+ description,
440
+ success: false,
441
+ totalAttempts: err.attemptMetadata.length,
442
+ totalDurationMs: Date.now() - buildStart,
443
+ totalTokensInput: err.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0),
444
+ totalTokensOutput: err.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0),
445
+ workflowName: null,
446
+ workflowId: null,
447
+ dryRun: options?.dryRun ?? false,
448
+ credentialsNeeded: 0,
449
+ warnedRules: err.warnedRules ?? []
450
+ });
451
+ this.updatePatterns();
452
+ }
453
+ throw err;
454
+ }
410
455
  await this.emitAttemptTelemetry(description, designResult);
411
456
  const workflow = options?.name ? { ...designResult.workflow, name: options.name } : designResult.workflow;
412
457
  this.saveToLibrary(workflow, description, designResult, matches);
@@ -423,8 +468,10 @@ var Kairos = class {
423
468
  workflowName: workflow.name,
424
469
  workflowId: null,
425
470
  dryRun: true,
426
- credentialsNeeded: designResult.credentialsNeeded.length
471
+ credentialsNeeded: designResult.credentialsNeeded.length,
472
+ warnedRules: designResult.warnedRules
427
473
  });
474
+ this.updatePatterns();
428
475
  return {
429
476
  workflowId: null,
430
477
  name: workflow.name,
@@ -453,8 +500,10 @@ var Kairos = class {
453
500
  workflowName: deployed.name,
454
501
  workflowId: deployed.workflowId,
455
502
  dryRun: false,
456
- credentialsNeeded: designResult.credentialsNeeded.length
503
+ credentialsNeeded: designResult.credentialsNeeded.length,
504
+ warnedRules: designResult.warnedRules
457
505
  });
506
+ this.updatePatterns();
458
507
  return {
459
508
  workflowId: deployed.workflowId,
460
509
  name: deployed.name,
@@ -477,7 +526,41 @@ var Kairos = class {
477
526
  await this.library.initialize();
478
527
  const matches = await this.library.search(description);
479
528
  const globalFailureRates = await this.telemetryReader?.getFailureRates() ?? [];
480
- const designResult = await this.designer.design({ description }, matches, globalFailureRates);
529
+ let designResult;
530
+ try {
531
+ designResult = await this.designer.design({ description }, matches, globalFailureRates);
532
+ } catch (err) {
533
+ if (err instanceof ValidationError && err.attemptMetadata) {
534
+ for (const meta of err.attemptMetadata) {
535
+ await this.telemetry?.emit("generation_attempt", {
536
+ description,
537
+ attempt: meta.attempt,
538
+ temperature: meta.temperature,
539
+ durationMs: meta.durationMs,
540
+ tokensInput: meta.tokensInput,
541
+ tokensOutput: meta.tokensOutput,
542
+ validationPassed: meta.validationPassed,
543
+ issueCount: meta.issues.length,
544
+ issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null }))
545
+ });
546
+ }
547
+ await this.telemetry?.emit("build_complete", {
548
+ description,
549
+ success: false,
550
+ totalAttempts: err.attemptMetadata.length,
551
+ totalDurationMs: Date.now() - buildStart,
552
+ totalTokensInput: err.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0),
553
+ totalTokensOutput: err.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0),
554
+ workflowName: null,
555
+ workflowId: null,
556
+ dryRun: false,
557
+ credentialsNeeded: 0,
558
+ warnedRules: err.warnedRules ?? []
559
+ });
560
+ this.updatePatterns();
561
+ }
562
+ throw err;
563
+ }
481
564
  await this.emitAttemptTelemetry(description, designResult);
482
565
  const provider = this.requireProvider();
483
566
  const deployed = await provider.update(id, designResult.workflow);
@@ -495,8 +578,10 @@ var Kairos = class {
495
578
  workflowName: deployed.name,
496
579
  workflowId: deployed.workflowId,
497
580
  dryRun: false,
498
- credentialsNeeded: designResult.credentialsNeeded.length
581
+ credentialsNeeded: designResult.credentialsNeeded.length,
582
+ warnedRules: designResult.warnedRules
499
583
  });
584
+ this.updatePatterns();
500
585
  return {
501
586
  workflowId: deployed.workflowId,
502
587
  name: deployed.name,
@@ -511,6 +596,13 @@ var Kairos = class {
511
596
  await this.saveQueue.catch(() => {
512
597
  });
513
598
  }
599
+ updatePatterns() {
600
+ if (!this.patternAnalyzer) return;
601
+ this.saveQueue = this.saveQueue.then(() => this.patternAnalyzer.analyzeAndSave()).then(() => null).catch((err) => {
602
+ this.logger.warn("Pattern analysis failed (non-fatal)", { err: String(err) });
603
+ return null;
604
+ });
605
+ }
514
606
  async emitAttemptTelemetry(description, designResult) {
515
607
  for (const meta of designResult.attemptMetadata) {
516
608
  await this.telemetry?.emit("generation_attempt", {
@@ -522,7 +614,7 @@ var Kairos = class {
522
614
  tokensOutput: meta.tokensOutput,
523
615
  validationPassed: meta.validationPassed,
524
616
  issueCount: meta.issues.length,
525
- issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message }))
617
+ issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null }))
526
618
  });
527
619
  }
528
620
  }
@@ -806,4 +898,4 @@ export {
806
898
  Kairos,
807
899
  TemplateSyncer
808
900
  };
809
- //# sourceMappingURL=chunk-KQSNT3HZ.js.map
901
+ //# sourceMappingURL=chunk-N6LRD2FN.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/library/null-library.ts","../src/errors/guard-error.ts","../src/providers/n8n/provider.ts","../src/errors/generation-error.ts","../src/errors/response-parse-error.ts","../src/errors/validation-error.ts","../src/telemetry/collector.ts","../src/telemetry/types.ts","../src/client.ts","../src/generation/designer.ts","../src/templates/safety.ts","../src/templates/syncer.ts"],"sourcesContent":["import type { IWorkflowLibrary, WorkflowMatch, StoredWorkflow, WorkflowMetadataInput, LibraryFilters, SearchOptions, OutcomeData } from './types.js'\nimport type { N8nWorkflow } from '../types/workflow.js'\nimport { generateUUID } from '../utils/uuid.js'\n\nexport class NullLibrary implements IWorkflowLibrary {\n async initialize(): Promise<void> {}\n\n async search(_description: string, _options?: SearchOptions): Promise<WorkflowMatch[]> {\n return []\n }\n\n async save(_workflow: N8nWorkflow, _metadata: WorkflowMetadataInput): Promise<string> {\n return generateUUID()\n }\n\n async recordDeployment(_id: string): Promise<void> {}\n\n async recordOutcome(_id: string, _outcome: OutcomeData): Promise<void> {}\n\n async get(_id: string): Promise<StoredWorkflow | null> {\n return null\n }\n\n async list(_filters?: LibraryFilters): Promise<StoredWorkflow[]> {\n return []\n }\n}\n","import { KairosError } from './base.js'\n\nexport class GuardError extends KairosError {\n constructor(message: string) {\n super(message)\n this.name = 'GuardError'\n }\n}\n","import type { N8nWorkflow, Tag } from '../../types/workflow.js'\nimport type { DeployResult, WorkflowListItem, ExecutionSummary, ExecutionDetail } from '../../types/result.js'\nimport type { DeleteOptions, ExecutionFilter } from '../../types/options.js'\nimport type { IProvider } from '../types.js'\nimport { GuardError } from '../../errors/guard-error.js'\nimport { N8nApiClient } from './api-client.js'\nimport { N8nFieldStripper } from './stripper.js'\n\nexport class N8nProvider implements IProvider {\n readonly platform = 'n8n'\n\n constructor(\n private readonly client: N8nApiClient,\n private readonly stripper: N8nFieldStripper,\n ) {}\n\n async deploy(workflow: N8nWorkflow): Promise<DeployResult> {\n const stripped = this.stripper.stripForCreate(workflow)\n const response = await this.client.createWorkflow(stripped)\n return { workflowId: response.id, name: response.name }\n }\n\n async update(id: string, workflow: N8nWorkflow): Promise<DeployResult> {\n const stripped = this.stripper.stripForUpdate(workflow)\n const response = await this.client.updateWorkflow(id, stripped)\n return { workflowId: response.id, name: response.name }\n }\n\n async get(id: string): Promise<N8nWorkflow> {\n const response = await this.client.getWorkflow(id)\n return {\n name: response.name,\n nodes: response.nodes,\n connections: response.connections,\n ...(response.settings !== undefined ? { settings: response.settings } : {}),\n ...(response.tags !== undefined ? { tags: response.tags } : {}),\n }\n }\n\n async list(): Promise<WorkflowListItem[]> {\n return this.client.listWorkflows()\n }\n\n async activate(id: string): Promise<void> {\n await this.client.activateWorkflow(id)\n }\n\n async deactivate(id: string): Promise<void> {\n await this.client.deactivateWorkflow(id)\n }\n\n async delete(id: string, options: DeleteOptions): Promise<void> {\n if (options.confirm !== true) {\n throw new GuardError('delete() requires { confirm: true } to prevent accidental deletion')\n }\n await this.client.deleteWorkflow(id)\n }\n\n async executions(workflowId?: string, filter?: ExecutionFilter): Promise<ExecutionSummary[]> {\n return this.client.getExecutions(workflowId, filter)\n }\n\n async execution(id: string): Promise<ExecutionDetail> {\n return this.client.getExecution(id)\n }\n\n async listTags(): Promise<Tag[]> {\n return this.client.listTags()\n }\n\n async createTag(name: string): Promise<Tag> {\n return this.client.createTag(name)\n }\n\n async tag(workflowId: string, tagIds: string[]): Promise<void> {\n await this.client.tagWorkflow(workflowId, tagIds)\n }\n\n async untag(workflowId: string, tagIds: string[]): Promise<void> {\n await this.client.untagWorkflow(workflowId, tagIds)\n }\n}\n","import { KairosError } from './base.js'\n\nexport class GenerationError extends KairosError {\n constructor(message: string, cause?: unknown) {\n super(message, cause)\n this.name = 'GenerationError'\n }\n}\n","import { KairosError } from './base.js'\n\nexport class ResponseParseError extends KairosError {\n constructor(message: string, cause?: unknown) {\n super(message, cause)\n this.name = 'ResponseParseError'\n }\n}\n","import { KairosError } from './base.js'\nimport type { ValidationIssue } from '../validation/types.js'\nimport type { AttemptMetadata } from '../telemetry/types.js'\n\nexport type { ValidationIssue }\n\nexport class ValidationError extends KairosError {\n constructor(\n message: string,\n public readonly issues: ValidationIssue[],\n public readonly attemptMetadata?: AttemptMetadata[],\n public readonly warnedRules?: number[],\n ) {\n super(message)\n this.name = 'ValidationError'\n }\n}\n","import { appendFile, mkdir } from 'node:fs/promises'\nimport { join } from 'node:path'\nimport { homedir } from 'node:os'\nimport { generateUUID } from '../utils/uuid.js'\nimport type { TelemetryEvent } from './types.js'\nimport { TELEMETRY_SCHEMA_VERSION } from './types.js'\n\nexport class TelemetryCollector {\n private readonly dir: string\n readonly sessionId: string\n private dirReady: Promise<void> | null = null\n\n constructor(dir?: string) {\n this.dir = dir ?? join(homedir(), '.kairos', 'telemetry')\n this.sessionId = generateUUID()\n }\n\n async emit(eventType: TelemetryEvent['eventType'], data: Record<string, unknown>): Promise<void> {\n const event: TelemetryEvent = {\n schemaVersion: TELEMETRY_SCHEMA_VERSION,\n timestamp: new Date().toISOString(),\n sessionId: this.sessionId,\n eventType,\n data,\n }\n\n if (!this.dirReady) {\n this.dirReady = mkdir(this.dir, { recursive: true }).then(() => {})\n }\n await this.dirReady\n const filename = new Date().toISOString().slice(0, 10) + '.jsonl'\n const filepath = join(this.dir, filename)\n await appendFile(filepath, JSON.stringify(event) + '\\n', 'utf-8')\n }\n}\n","import type { ValidationIssue } from '../errors/validation-error.js'\n\nexport interface TelemetryEvent {\n schemaVersion: number\n timestamp: string\n sessionId: string\n eventType: 'build_start' | 'generation_attempt' | 'build_complete'\n data: Record<string, unknown>\n}\n\nexport const TELEMETRY_SCHEMA_VERSION = 2\n\nexport interface AttemptMetadata {\n attempt: number\n temperature: number\n durationMs: number\n tokensInput: number\n tokensOutput: number\n validationPassed: boolean\n issues: ValidationIssue[]\n}\n\nexport interface BuildStartData {\n description: string\n model: string\n dryRun: boolean\n}\n\nexport interface GenerationAttemptData {\n description: string\n attempt: number\n temperature: number\n durationMs: number\n tokensInput: number\n tokensOutput: number\n validationPassed: boolean\n issueCount: number\n issues: Array<{ rule: number; message: string; nodeId?: string | null }>\n}\n\nexport interface BuildCompleteData {\n description: string\n success: boolean\n totalAttempts: number\n totalDurationMs: number\n totalTokensInput: number\n totalTokensOutput: number\n workflowName: string | null\n workflowId: string | null\n dryRun: boolean\n credentialsNeeded: number\n warnedRules: number[]\n}\n","import Anthropic from '@anthropic-ai/sdk'\nimport type { N8nWorkflow, Tag } from './types/workflow.js'\nimport type { BuildResult, WorkflowListItem, ExecutionSummary, ExecutionDetail } from './types/result.js'\nimport type { ClientOptions, BuildOptions, DeleteOptions, ExecutionFilter } from './types/options.js'\nimport type { IWorkflowLibrary, WorkflowMatch, WorkflowMetadataInput } from './library/types.js'\nimport { NullLibrary } from './library/null-library.js'\nimport { N8nApiClient } from './providers/n8n/api-client.js'\nimport { N8nFieldStripper } from './providers/n8n/stripper.js'\nimport { N8nProvider } from './providers/n8n/provider.js'\nimport { N8nValidator } from './validation/validator.js'\nimport { WorkflowDesigner } from './generation/designer.js'\nimport type { DesignResult } from './generation/types.js'\nimport { TelemetryCollector } from './telemetry/collector.js'\nimport { TelemetryReader } from './telemetry/reader.js'\nimport { PatternAnalyzer } from './telemetry/pattern-analyzer.js'\nimport { nullLogger } from './utils/logger.js'\nimport type { ILogger } from './utils/logger.js'\nimport { scoreToMode } from './utils/thresholds.js'\nimport { GuardError } from './errors/guard-error.js'\nimport { ValidationError } from './errors/validation-error.js'\n\nconst DEFAULT_MODEL = 'claude-sonnet-4-6'\n\nexport class Kairos {\n private readonly provider: N8nProvider | null\n private readonly designer: WorkflowDesigner\n private readonly validator: N8nValidator\n private readonly library: IWorkflowLibrary\n private readonly logger: ILogger\n private readonly telemetry: TelemetryCollector | null\n private readonly telemetryReader: TelemetryReader | null\n private readonly patternAnalyzer: PatternAnalyzer | null\n private readonly model: string\n private saveQueue: Promise<string | null> = Promise.resolve(null)\n\n constructor(options: ClientOptions) {\n const logger = options.logger ?? nullLogger\n this.model = options.model ?? DEFAULT_MODEL\n\n if (options.n8nBaseUrl && options.n8nApiKey) {\n try {\n new URL(options.n8nBaseUrl)\n } catch {\n throw new GuardError(`Invalid n8nBaseUrl: \"${options.n8nBaseUrl}\" — must be a valid URL`)\n }\n const apiClient = new N8nApiClient(options.n8nBaseUrl, options.n8nApiKey, logger)\n const stripper = new N8nFieldStripper()\n this.provider = new N8nProvider(apiClient, stripper)\n } else {\n this.provider = null\n }\n\n const anthropic = new Anthropic({ apiKey: options.anthropicApiKey })\n this.designer = new WorkflowDesigner(anthropic, this.model, logger)\n this.validator = new N8nValidator()\n this.library = options.library ?? new NullLibrary()\n this.logger = logger\n\n if (options.telemetry === true) {\n this.telemetry = new TelemetryCollector()\n this.telemetryReader = new TelemetryReader()\n this.patternAnalyzer = new PatternAnalyzer()\n } else if (typeof options.telemetry === 'string') {\n this.telemetry = new TelemetryCollector(options.telemetry)\n this.telemetryReader = new TelemetryReader(options.telemetry)\n this.patternAnalyzer = new PatternAnalyzer(options.telemetry)\n } else {\n this.telemetry = null\n this.telemetryReader = null\n this.patternAnalyzer = null\n }\n }\n\n private requireProvider(): N8nProvider {\n if (!this.provider) {\n throw new GuardError('n8nBaseUrl and n8nApiKey are required for this operation — set them in the Kairos constructor, or use { dryRun: true } for generation-only mode')\n }\n return this.provider\n }\n\n private validateDescription(description: string): void {\n if (!description || description.trim().length === 0) {\n throw new GuardError('Description is required and must be non-empty')\n }\n }\n\n async build(description: string, options?: BuildOptions): Promise<BuildResult> {\n this.validateDescription(description)\n this.logger.info('Kairos.build', { description, dryRun: options?.dryRun })\n const buildStart = Date.now()\n\n await this.telemetry?.emit('build_start', {\n description,\n model: this.model,\n dryRun: options?.dryRun ?? false,\n })\n\n await this.library.initialize()\n const matches = await this.library.search(description)\n\n if (matches.length > 0) {\n const top = matches[0]!\n this.logger.info(`Library: ${matches.length} match(es), top=\"${top.workflow.description.slice(0, 50)}\" score=${top.score.toFixed(2)} mode=${top.mode}`)\n } else {\n this.logger.info('Library: no matches (scratch mode)')\n }\n\n const globalFailureRates = await this.telemetryReader?.getFailureRates() ?? []\n\n if (globalFailureRates.length > 0) {\n const highFreq = globalFailureRates.filter((r) => r.rate >= 0.15)\n if (highFreq.length > 0) {\n this.logger.info(`Telemetry: ${highFreq.length} high-frequency failure rule(s) will be warned about`)\n }\n }\n\n let designResult: DesignResult\n try {\n designResult = await this.designer.design(\n { description, ...(options?.name ? { name: options.name } : {}) },\n matches,\n globalFailureRates,\n )\n } catch (err) {\n if (err instanceof ValidationError && err.attemptMetadata) {\n for (const meta of err.attemptMetadata) {\n await this.telemetry?.emit('generation_attempt', {\n description,\n attempt: meta.attempt,\n temperature: meta.temperature,\n durationMs: meta.durationMs,\n tokensInput: meta.tokensInput,\n tokensOutput: meta.tokensOutput,\n validationPassed: meta.validationPassed,\n issueCount: meta.issues.length,\n issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null })),\n })\n }\n await this.telemetry?.emit('build_complete', {\n description,\n success: false,\n totalAttempts: err.attemptMetadata.length,\n totalDurationMs: Date.now() - buildStart,\n totalTokensInput: err.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0),\n totalTokensOutput: err.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0),\n workflowName: null,\n workflowId: null,\n dryRun: options?.dryRun ?? false,\n credentialsNeeded: 0,\n warnedRules: err.warnedRules ?? [],\n })\n this.updatePatterns()\n }\n throw err\n }\n\n await this.emitAttemptTelemetry(description, designResult)\n\n const workflow = options?.name\n ? { ...designResult.workflow, name: options.name }\n : designResult.workflow\n\n this.saveToLibrary(workflow, description, designResult, matches)\n\n if (options?.dryRun) {\n const totalTokensInput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0)\n const totalTokensOutput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0)\n\n await this.telemetry?.emit('build_complete', {\n description,\n success: true,\n totalAttempts: designResult.attempts,\n totalDurationMs: Date.now() - buildStart,\n totalTokensInput,\n totalTokensOutput,\n workflowName: workflow.name,\n workflowId: null,\n dryRun: true,\n credentialsNeeded: designResult.credentialsNeeded.length,\n warnedRules: designResult.warnedRules,\n })\n\n this.updatePatterns()\n\n return {\n workflowId: null,\n name: workflow.name,\n workflow,\n credentialsNeeded: designResult.credentialsNeeded,\n activationRequired: true,\n generationAttempts: designResult.attempts,\n dryRun: true,\n }\n }\n\n const provider = this.requireProvider()\n const deployed = await provider.deploy(workflow)\n this.recordDeploy()\n\n if (options?.activate) {\n await provider.activate(deployed.workflowId)\n }\n\n const totalTokensInput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0)\n const totalTokensOutput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0)\n\n await this.telemetry?.emit('build_complete', {\n description,\n success: true,\n totalAttempts: designResult.attempts,\n totalDurationMs: Date.now() - buildStart,\n totalTokensInput,\n totalTokensOutput,\n workflowName: deployed.name,\n workflowId: deployed.workflowId,\n dryRun: false,\n credentialsNeeded: designResult.credentialsNeeded.length,\n warnedRules: designResult.warnedRules,\n })\n\n this.updatePatterns()\n\n return {\n workflowId: deployed.workflowId,\n name: deployed.name,\n workflow,\n credentialsNeeded: designResult.credentialsNeeded,\n activationRequired: !options?.activate,\n generationAttempts: designResult.attempts,\n dryRun: false,\n }\n }\n\n async replace(id: string, description: string): Promise<BuildResult> {\n this.validateDescription(description)\n this.logger.info('Kairos.update', { id, description })\n const buildStart = Date.now()\n\n await this.telemetry?.emit('build_start', {\n description,\n model: this.model,\n dryRun: false,\n })\n\n await this.library.initialize()\n const matches = await this.library.search(description)\n const globalFailureRates = await this.telemetryReader?.getFailureRates() ?? []\n\n let designResult: DesignResult\n try {\n designResult = await this.designer.design({ description }, matches, globalFailureRates)\n } catch (err) {\n if (err instanceof ValidationError && err.attemptMetadata) {\n for (const meta of err.attemptMetadata) {\n await this.telemetry?.emit('generation_attempt', {\n description,\n attempt: meta.attempt,\n temperature: meta.temperature,\n durationMs: meta.durationMs,\n tokensInput: meta.tokensInput,\n tokensOutput: meta.tokensOutput,\n validationPassed: meta.validationPassed,\n issueCount: meta.issues.length,\n issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null })),\n })\n }\n await this.telemetry?.emit('build_complete', {\n description,\n success: false,\n totalAttempts: err.attemptMetadata.length,\n totalDurationMs: Date.now() - buildStart,\n totalTokensInput: err.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0),\n totalTokensOutput: err.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0),\n workflowName: null,\n workflowId: null,\n dryRun: false,\n credentialsNeeded: 0,\n warnedRules: err.warnedRules ?? [],\n })\n this.updatePatterns()\n }\n throw err\n }\n\n await this.emitAttemptTelemetry(description, designResult)\n\n const provider = this.requireProvider()\n const deployed = await provider.update(id, designResult.workflow)\n\n this.saveToLibrary(designResult.workflow, description, designResult, matches)\n this.recordDeploy()\n\n const totalTokensInput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensInput, 0)\n const totalTokensOutput = designResult.attemptMetadata.reduce((s, m) => s + m.tokensOutput, 0)\n\n await this.telemetry?.emit('build_complete', {\n description,\n success: true,\n totalAttempts: designResult.attempts,\n totalDurationMs: Date.now() - buildStart,\n totalTokensInput,\n totalTokensOutput,\n workflowName: deployed.name,\n workflowId: deployed.workflowId,\n dryRun: false,\n credentialsNeeded: designResult.credentialsNeeded.length,\n warnedRules: designResult.warnedRules,\n })\n\n this.updatePatterns()\n\n return {\n workflowId: deployed.workflowId,\n name: deployed.name,\n workflow: designResult.workflow,\n credentialsNeeded: designResult.credentialsNeeded,\n activationRequired: true,\n generationAttempts: designResult.attempts,\n dryRun: false,\n }\n }\n\n async drain(): Promise<void> {\n await this.saveQueue.catch(() => {})\n }\n\n private updatePatterns(): void {\n if (!this.patternAnalyzer) return\n this.saveQueue = this.saveQueue\n .then(() => this.patternAnalyzer!.analyzeAndSave())\n .then(() => null)\n .catch((err: unknown) => {\n this.logger.warn('Pattern analysis failed (non-fatal)', { err: String(err) })\n return null\n })\n }\n\n private async emitAttemptTelemetry(description: string, designResult: DesignResult): Promise<void> {\n for (const meta of designResult.attemptMetadata) {\n await this.telemetry?.emit('generation_attempt', {\n description,\n attempt: meta.attempt,\n temperature: meta.temperature,\n durationMs: meta.durationMs,\n tokensInput: meta.tokensInput,\n tokensOutput: meta.tokensOutput,\n validationPassed: meta.validationPassed,\n issueCount: meta.issues.length,\n issues: meta.issues.map((i) => ({ rule: i.rule, message: i.message, nodeId: i.nodeId ?? null, nodeType: i.nodeType ?? null })),\n })\n }\n }\n\n private recordDeploy(): void {\n this.saveQueue = this.saveQueue\n .then(async (savedId) => {\n if (savedId) {\n await this.library.recordDeployment(savedId)\n }\n return savedId\n })\n .catch((err: unknown) => {\n this.logger.warn('Failed to record deployment (non-fatal)', { err: String(err) })\n return null\n })\n }\n\n private saveToLibrary(\n workflow: N8nWorkflow,\n description: string,\n designResult: DesignResult,\n matches: WorkflowMatch[],\n ): void {\n const failedAttempts = designResult.attemptMetadata.filter((m) => !m.validationPassed)\n const failurePatterns = failedAttempts.flatMap((m) =>\n m.issues.map((i) => ({ rule: i.rule, message: i.message })),\n )\n const topMatch = matches[0]\n const generationMode = topMatch ? scoreToMode(topMatch.score) : 'scratch' as const\n\n const autoTags = Array.from(new Set(\n workflow.nodes.flatMap((n) => {\n const bare = n.type.split('.').pop() ?? ''\n const tags = [bare]\n if (n.type.includes('Trigger') || n.type.includes('trigger')) tags.push(`trigger:${bare}`)\n if (n.type.includes('langchain')) tags.push('ai')\n return tags\n }),\n ))\n\n const metadata: WorkflowMetadataInput = {\n description,\n generationMode,\n generationAttempts: designResult.attempts,\n }\n if (autoTags.length > 0) metadata.tags = autoTags\n if (failurePatterns.length > 0) metadata.failurePatterns = failurePatterns\n if (matches.length > 0) metadata.sourceWorkflowIds = matches.map((m) => m.workflow.id)\n if (topMatch) metadata.topMatchScore = topMatch.score\n if (designResult.credentialsNeeded.length > 0) metadata.credentialsNeeded = designResult.credentialsNeeded\n\n const firstTryPass = designResult.attemptMetadata.length > 0\n && designResult.attemptMetadata[0]!.validationPassed\n const failedRules = Array.from(new Set(\n designResult.attemptMetadata\n .filter((m) => !m.validationPassed)\n .flatMap((m) => m.issues.map((i) => i.rule)),\n ))\n\n this.saveQueue = this.saveQueue\n .then(async () => {\n const savedId = await this.library.save(workflow, metadata)\n\n for (const match of matches) {\n if (match.mode === 'direct' || match.mode === 'reference') {\n await this.library.recordOutcome(match.workflow.id, {\n attempts: designResult.attempts,\n firstTryPass,\n failedRules,\n mode: match.mode,\n })\n }\n }\n\n return savedId\n })\n .catch((err: unknown) => {\n this.logger.warn('Failed to save workflow to library (non-fatal)', { err: String(err) })\n return null\n })\n }\n\n async get(id: string): Promise<N8nWorkflow> {\n return this.requireProvider().get(id)\n }\n\n async list(): Promise<WorkflowListItem[]> {\n return this.requireProvider().list()\n }\n\n async activate(id: string): Promise<void> {\n await this.requireProvider().activate(id)\n }\n\n async deactivate(id: string): Promise<void> {\n await this.requireProvider().deactivate(id)\n }\n\n async delete(id: string, options: DeleteOptions): Promise<void> {\n await this.requireProvider().delete(id, options)\n }\n\n async executions(workflowId?: string, filter?: ExecutionFilter): Promise<ExecutionSummary[]> {\n return this.requireProvider().executions(workflowId, filter)\n }\n\n async execution(id: string): Promise<ExecutionDetail> {\n return this.requireProvider().execution(id)\n }\n\n async listTags(): Promise<Tag[]> {\n return this.requireProvider().listTags()\n }\n\n async createTag(name: string): Promise<Tag> {\n return this.requireProvider().createTag(name)\n }\n\n async tag(workflowId: string, tagIds: string[]): Promise<void> {\n await this.requireProvider().tag(workflowId, tagIds)\n }\n\n async untag(workflowId: string, tagIds: string[]): Promise<void> {\n await this.requireProvider().untag(workflowId, tagIds)\n }\n}\n","import Anthropic from '@anthropic-ai/sdk'\nimport type { WorkflowMatch } from '../library/types.js'\nimport type { N8nWorkflow } from '../types/workflow.js'\nimport type { CredentialRequirement } from '../types/result.js'\nimport type { ILogger } from '../utils/logger.js'\nimport { GenerationError } from '../errors/generation-error.js'\nimport { ResponseParseError } from '../errors/response-parse-error.js'\nimport { ValidationError } from '../errors/validation-error.js'\nimport type { ValidationIssue } from '../errors/validation-error.js'\nimport { N8nValidator } from '../validation/validator.js'\nimport { PromptBuilder } from './prompt-builder.js'\nimport type { AttemptMetadata } from '../telemetry/types.js'\nimport type { RuleFailureRate } from '../telemetry/reader.js'\nimport type { DesignRequest, DesignResult, SystemPromptBlock } from './types.js'\n\nconst MAX_ATTEMPTS = 3\nconst BASE_TEMPERATURE = 0.2\nconst FINAL_TEMPERATURE = 0.1\n\nconst GENERATE_WORKFLOW_TOOL: Anthropic.Tool = {\n name: 'generate_workflow',\n description: 'Generate a valid n8n workflow JSON object',\n input_schema: {\n type: 'object',\n properties: {\n workflow: {\n type: 'object',\n description: 'The complete n8n workflow object',\n properties: {\n name: { type: 'string' },\n nodes: { type: 'array' },\n connections: { type: 'object' },\n settings: { type: 'object' },\n },\n required: ['name', 'nodes', 'connections'],\n },\n credentialsNeeded: {\n type: 'array',\n description: 'List of credentials the user must configure before activating',\n items: {\n type: 'object',\n properties: {\n service: { type: 'string' },\n credentialType: { type: 'string' },\n description: { type: 'string' },\n },\n required: ['service', 'credentialType', 'description'],\n },\n },\n error: {\n type: 'string',\n description: 'Set this if the request cannot be fulfilled — explain why',\n },\n },\n required: [],\n },\n}\n\ninterface ToolUseResult {\n workflow: N8nWorkflow\n credentialsNeeded: CredentialRequirement[]\n error?: string\n}\n\nexport class WorkflowDesigner {\n private readonly validator: N8nValidator\n private readonly promptBuilder: PromptBuilder\n\n constructor(\n private readonly anthropic: Anthropic,\n private readonly model: string,\n private readonly logger: ILogger,\n ) {\n this.validator = new N8nValidator()\n this.promptBuilder = new PromptBuilder()\n }\n\n async design(request: DesignRequest, matches: WorkflowMatch[], globalFailureRates: RuleFailureRate[] = []): Promise<DesignResult> {\n const attemptMetadata: AttemptMetadata[] = []\n let lastErrors: ValidationIssue[] = []\n let attempts = 0\n const built = this.promptBuilder.build(request, matches, globalFailureRates)\n\n for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {\n attempts = attempt\n const temperature = attempt === MAX_ATTEMPTS ? FINAL_TEMPERATURE : BASE_TEMPERATURE\n\n let userMessage: string\n if (attempt === 1) {\n userMessage = built.userMessage\n this.logger.debug('WorkflowDesigner: attempt 1', { description: request.description })\n } else {\n const issueLines = lastErrors.map(\n (i) => `- [Rule ${i.rule}] ${i.message}${i.nodeId ? ` (node: ${i.nodeId})` : ''}`,\n )\n userMessage = this.promptBuilder.buildCorrectionMessage(request, matches, issueLines, attempt - 1)\n this.logger.debug(`WorkflowDesigner: correction attempt ${attempt}`, { issueCount: lastErrors.length })\n }\n\n const start = Date.now()\n const message = await this.callClaude(built.system, userMessage, temperature)\n const durationMs = Date.now() - start\n const parsed = this.extractToolUse(message)\n\n if (parsed.error) {\n throw new GenerationError(`Claude declined to generate workflow: ${parsed.error}`)\n }\n\n const validation = this.validator.validate(parsed.workflow)\n const errors = validation.issues.filter((i) => i.severity === 'error')\n\n attemptMetadata.push({\n attempt,\n temperature,\n durationMs,\n tokensInput: message.usage.input_tokens,\n tokensOutput: message.usage.output_tokens,\n validationPassed: validation.valid,\n issues: validation.issues,\n })\n\n if (validation.valid) {\n return { workflow: parsed.workflow, credentialsNeeded: parsed.credentialsNeeded, attempts, attemptMetadata, warnedRules: this.promptBuilder.getWarnedRules() }\n }\n\n lastErrors = errors\n this.logger.warn(`WorkflowDesigner: validation failed on attempt ${attempt}`, {\n errorCount: errors.length,\n })\n }\n\n const finalIssues = attemptMetadata.at(-1)?.issues ?? lastErrors\n throw new ValidationError(\n `Workflow failed validation after ${MAX_ATTEMPTS} attempts`,\n finalIssues,\n attemptMetadata,\n this.promptBuilder.getWarnedRules(),\n )\n }\n\n private async callClaude(\n system: SystemPromptBlock[],\n userMessage: string,\n temperature: number,\n ): Promise<Anthropic.Message> {\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), 120_000)\n try {\n return await this.anthropic.messages.create(\n {\n model: this.model,\n max_tokens: 8192,\n temperature,\n system: system.map((b) => ({ type: b.type, text: b.text, ...(b.cache_control ? { cache_control: b.cache_control } : {}) })),\n messages: [{ role: 'user', content: userMessage }],\n tools: [GENERATE_WORKFLOW_TOOL],\n tool_choice: { type: 'tool', name: 'generate_workflow' },\n },\n { signal: controller.signal },\n )\n } catch (err) {\n const detail = err instanceof Error ? err.message : String(err)\n throw new GenerationError(`Anthropic API call failed: ${detail}`, err)\n } finally {\n clearTimeout(timer)\n }\n }\n\n private extractToolUse(message: Anthropic.Message): ToolUseResult {\n const toolUseBlock = message.content.find(\n (block): block is Anthropic.ToolUseBlock => block.type === 'tool_use',\n )\n if (!toolUseBlock) {\n throw new ResponseParseError(\n 'Claude response contained no tool_use block — forced tool_choice failed unexpectedly',\n )\n }\n\n const input = toolUseBlock.input as Record<string, unknown>\n\n if (typeof input['error'] === 'string') {\n return {\n workflow: { name: '', nodes: [], connections: {} },\n credentialsNeeded: [],\n error: input['error'],\n }\n }\n\n if (!input['workflow'] || typeof input['workflow'] !== 'object') {\n throw new ResponseParseError('generate_workflow tool call missing workflow field')\n }\n\n const workflow = input['workflow'] as N8nWorkflow\n const credentialsNeeded = (input['credentialsNeeded'] as CredentialRequirement[] | undefined) ?? []\n\n return { workflow, credentialsNeeded }\n }\n}\n","import type { TrustLevel } from '../library/types.js'\nimport type { N8nWorkflow } from '../types/workflow.js'\n\ninterface SafetyResult {\n trustLevel: TrustLevel\n reasons: string[]\n}\n\nconst BLOCKED_NODE_TYPES = new Set([\n 'n8n-nodes-base.code',\n 'n8n-nodes-base.executeCommand',\n 'n8n-nodes-base.ssh',\n])\n\nconst REVIEW_NODE_TYPES = new Set([\n 'n8n-nodes-base.httpRequest',\n])\n\nconst SECRET_PATTERNS = [\n /sk-[a-zA-Z0-9]{20,}/,\n /ghp_[a-zA-Z0-9]{36}/,\n /xoxb-[0-9]+-[0-9]+-[a-zA-Z0-9]+/,\n /AIza[a-zA-Z0-9_-]{35}/,\n /AKIA[A-Z0-9]{16}/,\n]\n\nexport function assessTemplateSafety(workflow: N8nWorkflow): SafetyResult {\n const reasons: string[] = []\n let worst: TrustLevel = 'safe'\n\n const escalate = (level: TrustLevel, reason: string) => {\n reasons.push(reason)\n if (level === 'blocked') worst = 'blocked'\n else if (level === 'review' && worst === 'safe') worst = 'review'\n }\n\n for (const node of workflow.nodes) {\n if (BLOCKED_NODE_TYPES.has(node.type)) {\n escalate('blocked', `Contains ${node.type} node \"${node.name}\"`)\n }\n\n if (REVIEW_NODE_TYPES.has(node.type)) {\n escalate('review', `Contains ${node.type} node \"${node.name}\"`)\n }\n\n const paramStr = JSON.stringify(node.parameters)\n for (const pattern of SECRET_PATTERNS) {\n if (pattern.test(paramStr)) {\n escalate('blocked', `Node \"${node.name}\" parameters contain a hardcoded secret`)\n break\n }\n }\n }\n\n return { trustLevel: worst, reasons }\n}\n","import type { IWorkflowLibrary, WorkflowMetadataInput } from '../library/types.js'\nimport type { N8nWorkflow, N8nNode } from '../types/workflow.js'\nimport type { ILogger } from '../utils/logger.js'\nimport type { TemplateSearchResponse, TemplateDetailResponse, SyncProgress } from './types.js'\nimport { N8nValidator } from '../validation/validator.js'\nimport { assessTemplateSafety } from './safety.js'\n\nconst N8N_TEMPLATE_API = 'https://api.n8n.io/api/templates'\nconst PAGE_SIZE = 50\nconst DELAY_BETWEEN_FETCHES_MS = 200\n\nconst DEFAULT_SETTINGS: N8nWorkflow['settings'] = {\n executionOrder: 'v1',\n saveManualExecutions: true,\n timezone: 'UTC',\n}\n\nexport interface SyncOptions {\n maxTemplates?: number\n onProgress?: (progress: SyncProgress) => void\n}\n\nexport class TemplateSyncer {\n private readonly validator: N8nValidator\n private readonly logger: ILogger\n\n constructor(\n private readonly library: IWorkflowLibrary,\n logger: ILogger,\n ) {\n this.validator = new N8nValidator()\n this.logger = logger\n }\n\n async sync(options?: SyncOptions): Promise<SyncProgress> {\n const maxTemplates = options?.maxTemplates ?? 500\n\n await this.library.initialize()\n\n const existing = await this.library.list()\n const existingSourceIds = new Set(\n existing\n .filter((w) => w.sourceKind === 'n8n-template' && w.sourceId)\n .map((w) => w.sourceId!),\n )\n\n const progress: SyncProgress = {\n total: 0,\n processed: 0,\n saved: 0,\n skippedPaid: 0,\n skippedDuplicate: 0,\n blocked: 0,\n reviewed: 0,\n }\n\n const templateIds = await this.fetchTemplateIds(maxTemplates, progress)\n\n for (const id of templateIds) {\n if (existingSourceIds.has(String(id))) {\n progress.skippedDuplicate++\n progress.processed++\n options?.onProgress?.(progress)\n continue\n }\n\n try {\n await this.processTemplate(id, progress)\n } catch (err) {\n this.logger.warn(`Failed to process template ${id}`, { err: String(err) })\n }\n\n progress.processed++\n options?.onProgress?.(progress)\n\n await new Promise((resolve) => setTimeout(resolve, DELAY_BETWEEN_FETCHES_MS))\n }\n\n return progress\n }\n\n private async fetchTemplateIds(max: number, progress: SyncProgress): Promise<number[]> {\n const ids: number[] = []\n let page = 1\n\n while (ids.length < max) {\n const url = `${N8N_TEMPLATE_API}/search?page=${page}&rows=${PAGE_SIZE}`\n const response = await fetch(url)\n if (!response.ok) break\n\n const data = (await response.json()) as TemplateSearchResponse\n progress.total = Math.min(data.totalWorkflows, max)\n\n for (const template of data.workflows) {\n if (ids.length >= max) break\n if (template.price && template.price > 0) {\n progress.skippedPaid++\n continue\n }\n ids.push(template.id)\n }\n\n if (data.workflows.length < PAGE_SIZE) break\n page++\n\n await new Promise((resolve) => setTimeout(resolve, DELAY_BETWEEN_FETCHES_MS))\n }\n\n return ids\n }\n\n private async processTemplate(id: number, progress: SyncProgress): Promise<void> {\n const url = `${N8N_TEMPLATE_API}/workflows/${id}`\n const response = await fetch(url)\n if (!response.ok) return\n\n const data = (await response.json()) as TemplateDetailResponse\n const templateMeta = data.workflow\n const rawWorkflow = templateMeta.workflow\n\n if (!rawWorkflow?.nodes?.length) return\n\n const workflow: N8nWorkflow = {\n name: templateMeta.name,\n nodes: rawWorkflow.nodes.filter((n) => n.type && n.name) as N8nNode[],\n connections: rawWorkflow.connections as N8nWorkflow['connections'],\n settings: rawWorkflow.settings\n ? { executionOrder: 'v1' as const, ...rawWorkflow.settings }\n : { ...DEFAULT_SETTINGS },\n }\n\n const validation = this.validator.validate(workflow)\n const validationErrors = validation.issues.filter((i) => i.severity === 'error')\n\n if (validationErrors.length > 0) {\n progress.blocked++\n this.logger.debug(`Template ${id} blocked: ${validationErrors.length} validation errors`)\n return\n }\n\n const safety = assessTemplateSafety(workflow)\n\n if (safety.trustLevel === 'blocked') {\n progress.blocked++\n this.logger.debug(`Template ${id} blocked: ${safety.reasons.join(', ')}`)\n return\n }\n\n if (safety.trustLevel === 'review') {\n progress.reviewed++\n }\n\n const description = this.cleanDescription(templateMeta.description)\n\n const autoTags = Array.from(new Set(\n workflow.nodes.flatMap((n) => {\n const bare = n.type.split('.').pop() ?? ''\n const tags = [bare]\n if (n.type.includes('Trigger') || n.type.includes('trigger')) tags.push(`trigger:${bare}`)\n if (n.type.includes('langchain')) tags.push('ai')\n return tags\n }),\n ))\n\n const metadata: WorkflowMetadataInput = {\n description,\n tags: autoTags,\n sourceKind: 'n8n-template',\n sourceId: String(id),\n sourceUrl: `https://n8n.io/workflows/${id}`,\n trustLevel: safety.trustLevel,\n }\n\n await this.library.save(workflow, metadata)\n progress.saved++\n this.logger.debug(`Template ${id} saved: \"${templateMeta.name}\" (${safety.trustLevel})`)\n }\n\n private cleanDescription(raw: string): string {\n return raw\n .replace(/#{1,6}\\s*/g, '')\n .replace(/\\*{1,2}([^*]+)\\*{1,2}/g, '$1')\n .replace(/\\[([^\\]]+)\\]\\([^)]+\\)/g, '$1')\n .replace(/\\n{3,}/g, '\\n\\n')\n .trim()\n .slice(0, 500)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAIO,IAAM,cAAN,MAA8C;AAAA,EACnD,MAAM,aAA4B;AAAA,EAAC;AAAA,EAEnC,MAAM,OAAO,cAAsB,UAAoD;AACrF,WAAO,CAAC;AAAA,EACV;AAAA,EAEA,MAAM,KAAK,WAAwB,WAAmD;AACpF,WAAO,aAAa;AAAA,EACtB;AAAA,EAEA,MAAM,iBAAiB,KAA4B;AAAA,EAAC;AAAA,EAEpD,MAAM,cAAc,KAAa,UAAsC;AAAA,EAAC;AAAA,EAExE,MAAM,IAAI,KAA6C;AACrD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,KAAK,UAAsD;AAC/D,WAAO,CAAC;AAAA,EACV;AACF;;;ACxBO,IAAM,aAAN,cAAyB,YAAY;AAAA,EAC1C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;;;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