@synth-deploy/server 1.0.6 → 1.1.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.
Files changed (96) hide show
  1. package/dist/agent/envoy-client.d.ts +62 -7
  2. package/dist/agent/envoy-client.d.ts.map +1 -1
  3. package/dist/agent/envoy-client.js +56 -6
  4. package/dist/agent/envoy-client.js.map +1 -1
  5. package/dist/agent/stale-deployment-detector.js +1 -1
  6. package/dist/agent/stale-deployment-detector.js.map +1 -1
  7. package/dist/agent/synth-agent.d.ts +7 -5
  8. package/dist/agent/synth-agent.d.ts.map +1 -1
  9. package/dist/agent/synth-agent.js +42 -39
  10. package/dist/agent/synth-agent.js.map +1 -1
  11. package/dist/alert-webhooks/alert-parsers.d.ts +21 -0
  12. package/dist/alert-webhooks/alert-parsers.d.ts.map +1 -0
  13. package/dist/alert-webhooks/alert-parsers.js +184 -0
  14. package/dist/alert-webhooks/alert-parsers.js.map +1 -0
  15. package/dist/api/agent.d.ts +0 -6
  16. package/dist/api/agent.d.ts.map +1 -1
  17. package/dist/api/agent.js +6 -459
  18. package/dist/api/agent.js.map +1 -1
  19. package/dist/api/alert-webhooks.d.ts +13 -0
  20. package/dist/api/alert-webhooks.d.ts.map +1 -0
  21. package/dist/api/alert-webhooks.js +279 -0
  22. package/dist/api/alert-webhooks.js.map +1 -0
  23. package/dist/api/envoy-reports.js +2 -2
  24. package/dist/api/envoy-reports.js.map +1 -1
  25. package/dist/api/envoys.js +1 -1
  26. package/dist/api/envoys.js.map +1 -1
  27. package/dist/api/fleet.d.ts.map +1 -1
  28. package/dist/api/fleet.js +14 -15
  29. package/dist/api/fleet.js.map +1 -1
  30. package/dist/api/graph.js +3 -3
  31. package/dist/api/graph.js.map +1 -1
  32. package/dist/api/operations.d.ts +7 -0
  33. package/dist/api/operations.d.ts.map +1 -0
  34. package/dist/api/operations.js +1883 -0
  35. package/dist/api/operations.js.map +1 -0
  36. package/dist/api/partitions.js +1 -1
  37. package/dist/api/partitions.js.map +1 -1
  38. package/dist/api/schemas.d.ts +194 -10
  39. package/dist/api/schemas.d.ts.map +1 -1
  40. package/dist/api/schemas.js +38 -5
  41. package/dist/api/schemas.js.map +1 -1
  42. package/dist/api/system.d.ts.map +1 -1
  43. package/dist/api/system.js +22 -21
  44. package/dist/api/system.js.map +1 -1
  45. package/dist/artifact-analyzer.js +2 -2
  46. package/dist/artifact-analyzer.js.map +1 -1
  47. package/dist/fleet/fleet-executor.js +1 -1
  48. package/dist/fleet/fleet-executor.js.map +1 -1
  49. package/dist/graph/graph-executor.js +2 -2
  50. package/dist/graph/graph-executor.js.map +1 -1
  51. package/dist/index.js +44 -40
  52. package/dist/index.js.map +1 -1
  53. package/dist/mcp/resources.js +3 -3
  54. package/dist/mcp/resources.js.map +1 -1
  55. package/dist/mcp/tools.d.ts.map +1 -1
  56. package/dist/mcp/tools.js +2 -9
  57. package/dist/mcp/tools.js.map +1 -1
  58. package/dist/middleware/auth.js +1 -1
  59. package/dist/middleware/auth.js.map +1 -1
  60. package/package.json +1 -1
  61. package/src/agent/envoy-client.ts +107 -15
  62. package/src/agent/stale-deployment-detector.ts +1 -1
  63. package/src/agent/synth-agent.ts +59 -45
  64. package/src/alert-webhooks/alert-parsers.ts +291 -0
  65. package/src/api/agent.ts +9 -528
  66. package/src/api/alert-webhooks.ts +354 -0
  67. package/src/api/envoy-reports.ts +2 -2
  68. package/src/api/envoys.ts +1 -1
  69. package/src/api/fleet.ts +14 -15
  70. package/src/api/graph.ts +3 -3
  71. package/src/api/operations.ts +2240 -0
  72. package/src/api/partitions.ts +1 -1
  73. package/src/api/schemas.ts +43 -7
  74. package/src/api/system.ts +23 -21
  75. package/src/artifact-analyzer.ts +2 -2
  76. package/src/fleet/fleet-executor.ts +1 -1
  77. package/src/graph/graph-executor.ts +2 -2
  78. package/src/index.ts +46 -40
  79. package/src/mcp/resources.ts +3 -3
  80. package/src/mcp/tools.ts +5 -9
  81. package/src/middleware/auth.ts +1 -1
  82. package/tests/agent-mode.test.ts +5 -376
  83. package/tests/api-handlers.test.ts +27 -27
  84. package/tests/composite-operations.test.ts +557 -0
  85. package/tests/decision-diary.test.ts +62 -63
  86. package/tests/diary-reader.test.ts +14 -18
  87. package/tests/mcp-tools.test.ts +1 -1
  88. package/tests/orchestration.test.ts +34 -30
  89. package/tests/partition-isolation.test.ts +4 -9
  90. package/tests/rbac-enforcement.test.ts +8 -8
  91. package/tests/ui-journey.test.ts +9 -9
  92. package/dist/api/deployments.d.ts +0 -11
  93. package/dist/api/deployments.d.ts.map +0 -1
  94. package/dist/api/deployments.js +0 -1098
  95. package/dist/api/deployments.js.map +0 -1
  96. package/src/api/deployments.ts +0 -1347
package/src/api/agent.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import type { FastifyInstance } from "fastify";
2
- import type { IPartitionStore, IEnvironmentStore, IArtifactStore, ISettingsStore, ITelemetryStore, DebriefWriter, DebriefReader, Artifact, Partition, Environment, Deployment } from "@synth-deploy/core";
2
+ import type { IPartitionStore, IEnvironmentStore, IArtifactStore, ISettingsStore, ITelemetryStore, DebriefWriter, DebriefReader, Partition, Environment, OperationInput } from "@synth-deploy/core";
3
3
  import type { LlmClient } from "@synth-deploy/core";
4
4
  import type { SynthAgent, DeploymentStore } from "../agent/synth-agent.js";
5
+
6
+ const getArtifactId = (op: { input: OperationInput }): string =>
7
+ op.input.type === "deploy" ? op.input.artifactId : "";
5
8
  import type { EnvoyRegistry } from "../agent/envoy-registry.js";
6
9
  import type { ArtifactAnalyzer } from "../artifact-analyzer.js";
7
10
  import { z } from "zod";
8
- import { QueryRequestSchema } from "./schemas.js";
9
11
  import { requirePermission } from "../middleware/permissions.js";
10
12
 
11
13
  // ---------------------------------------------------------------------------
@@ -36,75 +38,6 @@ interface DeploymentContext {
36
38
  }>;
37
39
  }
38
40
 
39
- // ---------------------------------------------------------------------------
40
- // Input sanitization — prevent prompt injection and control character abuse
41
- // ---------------------------------------------------------------------------
42
-
43
- /** @internal Exported for testing only */
44
- export function sanitizeUserInput(text: string): string {
45
- // Strip control characters except newline and tab
46
- let sanitized = text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
47
- // Truncate to prevent prompt stuffing
48
- if (sanitized.length > 1000) {
49
- sanitized = sanitized.slice(0, 1000);
50
- }
51
- // Escape angle brackets to prevent XML tag injection
52
- sanitized = sanitized.replace(/</g, '&lt;').replace(/>/g, '&gt;');
53
- return sanitized;
54
- }
55
-
56
- /** @internal Exported for testing only */
57
- export function validateExtractedVersion(version: string): boolean {
58
- // Accept semver and common pre-release formats
59
- return /^\d+\.\d+\.\d+(-[a-zA-Z0-9._]+)?$/.test(version);
60
- }
61
-
62
- /** @internal Exported for testing only */
63
- export function validateExtractedVariables(vars: Record<string, string>): Record<string, string> {
64
- const validated: Record<string, string> = {};
65
- for (const [key, value] of Object.entries(vars)) {
66
- // Key must be alphanumeric + underscore, value max 500 chars
67
- if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) && typeof value === 'string' && value.length <= 500) {
68
- validated[key] = value;
69
- }
70
- }
71
- return validated;
72
- }
73
-
74
- const MAX_ENTITY_LIST_SIZE = 100;
75
-
76
- function appendEntityNames(
77
- parts: string[],
78
- label: string,
79
- entities: { name: string }[],
80
- includeEntities: boolean,
81
- ): void {
82
- if (!includeEntities) {
83
- parts.push(`\n${label}: (entity data omitted by configuration)`);
84
- return;
85
- }
86
- parts.push(`\n${label}:`);
87
- const capped = entities.slice(0, MAX_ENTITY_LIST_SIZE);
88
- for (const e of capped) {
89
- parts.push(` - "${e.name}"`);
90
- }
91
- if (entities.length > MAX_ENTITY_LIST_SIZE) {
92
- parts.push(` (… and ${entities.length - MAX_ENTITY_LIST_SIZE} more)`);
93
- }
94
- if (entities.length === 0) parts.push(" (none configured)");
95
- }
96
-
97
- /** Build a case-insensitive name→ID map for a list of entities. */
98
- function buildNameMap(entities: { id: string; name: string }[]): Map<string, string> {
99
- const map = new Map<string, string>();
100
- for (const e of entities) {
101
- const key = e.name.toLowerCase();
102
- // First match wins — duplicates are inherently ambiguous
103
- if (!map.has(key)) map.set(key, e.id);
104
- }
105
- return map;
106
- }
107
-
108
41
  // ---------------------------------------------------------------------------
109
42
  // Context generation — signals from deployment data
110
43
  // ---------------------------------------------------------------------------
@@ -226,7 +159,7 @@ function generateContext(
226
159
  : "—",
227
160
  lastDeployment: lastDeploy
228
161
  ? {
229
- version: lastDeploy.version,
162
+ version: lastDeploy.version ?? "",
230
163
  environment: allEnvironments.find((e) => e.id === lastDeploy.environmentId)?.name ?? lastDeploy.environmentId ?? "—",
231
164
  status: lastDeploy.status,
232
165
  ago: formatAgo(new Date(lastDeploy.createdAt)),
@@ -295,238 +228,6 @@ export function registerAgentRoutes(
295
228
  return generateContext(deployments, environments, partitions);
296
229
  });
297
230
 
298
- /**
299
- * Canvas query — classifies a natural language query and returns
300
- * a structured action telling the UI what view to render.
301
- * Navigation/data intents resolve entities and return view params.
302
- */
303
- app.post("/api/agent/query", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
304
- const parsed = QueryRequestSchema.safeParse(request.body);
305
- if (!parsed.success) {
306
- return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
307
- }
308
-
309
- const query = parsed.data.query.trim();
310
- const lower = query.toLowerCase();
311
- const allArtifacts = artifacts.list();
312
- const allPartitions = partitions.list();
313
- const allEnvironments = environments.list();
314
- const artifactMap = new Map(allArtifacts.map((a) => [a.id, a.name]));
315
- const environmentMap = new Map(allEnvironments.map((e) => [e.id, e.name]));
316
- const partitionMap = new Map(allPartitions.map((p) => [p.id, p.name]));
317
-
318
- // --- LLM classification (when available) ---
319
- const queryEntityExposure = settings.get().agent.llmEntityExposure ?? "names";
320
- if (llm && llm.isAvailable()) {
321
- const llmAction = await classifyQueryWithLlm(
322
- llm, query, allArtifacts, allPartitions, allEnvironments,
323
- deployments, debrief, queryEntityExposure !== "none",
324
- );
325
- if (llmAction) {
326
- // For "annotate" action: save annotation to artifact, trigger re-analysis
327
- if (llmAction.action === "annotate") {
328
- const { artifactName, field, correction } = llmAction.params as Record<string, string>;
329
- const target = allArtifacts.find(
330
- (a) => a.name.toLowerCase() === (artifactName ?? "").toLowerCase(),
331
- );
332
- if (target && correction) {
333
- artifacts.addAnnotation(target.id, {
334
- field: field || "summary",
335
- correction,
336
- annotatedBy: "channel",
337
- annotatedAt: new Date(),
338
- });
339
- debrief.record({
340
- partitionId: null,
341
- deploymentId: null,
342
- agent: "server",
343
- decisionType: "artifact-analysis",
344
- decision: `User correction recorded for "${target.name}" via channel: ${correction}`,
345
- reasoning: `Operator typed a natural-language correction into the Synth Channel. Field: ${field || "summary"}.`,
346
- context: { artifactName: target.name, field: field || "summary", correction, source: "channel" },
347
- });
348
- // Trigger async re-analysis with the new annotation
349
- if (analyzer) {
350
- const updated = artifacts.get(target.id);
351
- if (updated) {
352
- analyzer.reanalyzeWithAnnotations(updated).then((revised) => {
353
- if (revised) artifacts.update(target.id, { analysis: revised });
354
- }).catch(() => {});
355
- }
356
- }
357
- return {
358
- action: "answer" as const,
359
- view: "",
360
- params: {},
361
- title: "Correction recorded",
362
- content: `Got it — I've noted that **${target.name}** ${correction}. Re-analyzing now to update my understanding.`,
363
- };
364
- }
365
- // Artifact not found — fall through to answer
366
- }
367
-
368
- // For "answer" action: fetch real data and generate a markdown response
369
- if (llmAction.action === "answer") {
370
- const answered = await answerQueryWithData(
371
- llm, query, deployments.list(), allArtifacts, allPartitions, allEnvironments,
372
- );
373
- if (answered) {
374
- debrief.record({
375
- partitionId: null,
376
- deploymentId: null,
377
- agent: "server",
378
- decisionType: "system",
379
- decision: `Canvas query answered analytically`,
380
- reasoning: `LLM classified "${query}" as analytical answer, responded with ${answered.content.length} chars of markdown`,
381
- context: { query, action: "answer" },
382
- });
383
- return answered;
384
- }
385
- // Fall through to regex fallback if answer generation failed
386
- } else {
387
- debrief.record({
388
- partitionId: null,
389
- deploymentId: null,
390
- agent: "server",
391
- decisionType: "system",
392
- decision: `Canvas query classified as ${llmAction.action}: ${llmAction.view}`,
393
- reasoning: `LLM classified "${query}" → ${llmAction.action}/${llmAction.view}`,
394
- context: { query, action: llmAction },
395
- });
396
- return llmAction;
397
- }
398
- }
399
- }
400
-
401
- // --- Regex fallback classification ---
402
-
403
- // Artifact correction: "Dockerfile.server is actually for nginx", "api-service is a nodejs app"
404
- const correctionPatterns = [
405
- /\b(?:is\s+actually|is\s+really|should\s+be|is\s+a|is\s+an)\b/i,
406
- /\bcorrect(?:ion)?:/i,
407
- /\bactually\s+(?:a|an|the)\b/i,
408
- ];
409
- if (correctionPatterns.some((p) => p.test(query))) {
410
- for (const art of allArtifacts) {
411
- if (query.toLowerCase().includes(art.name.toLowerCase())) {
412
- artifacts.addAnnotation(art.id, {
413
- field: "summary",
414
- correction: query,
415
- annotatedBy: "channel",
416
- annotatedAt: new Date(),
417
- });
418
- if (analyzer) {
419
- const updated = artifacts.get(art.id);
420
- if (updated) {
421
- analyzer.reanalyzeWithAnnotations(updated).then((revised) => {
422
- if (revised) artifacts.update(art.id, { analysis: revised });
423
- }).catch(() => {});
424
- }
425
- }
426
- return {
427
- action: "answer" as const,
428
- view: "",
429
- params: {},
430
- title: "Correction recorded",
431
- content: `Got it — I've noted your correction about **${art.name}**. Re-analyzing now.`,
432
- };
433
- }
434
- }
435
- }
436
-
437
- // Create partition: "create partition Acme Corp" → return create intent for UI confirmation
438
- const createPartitionMatch = query.match(/\bcreate\s+partition\s+(.+)/i);
439
- if (createPartitionMatch) {
440
- const name = createPartitionMatch[1].trim();
441
- return { action: "create" as const, view: "partition-detail", params: { name }, title: `Create "${name}"` };
442
- }
443
-
444
- // Create artifact: "create artifact api-service" or "create operation api-service" → return create intent for UI confirmation
445
- const createArtifactMatch = query.match(/\bcreate\s+(?:artifact|operation)\s+(.+)/i);
446
- if (createArtifactMatch) {
447
- const name = createArtifactMatch[1].trim();
448
- return { action: "create" as const, view: "artifact-list", params: { name }, title: `Create "${name}"` };
449
- }
450
-
451
- // Show specific partition
452
- for (const p of allPartitions) {
453
- const name = p.name.toLowerCase();
454
- if (lower.includes(name) && (lower.includes("partition") || lower.includes("show"))) {
455
- return { action: "navigate" as const, view: "partition-detail", params: { id: p.id }, title: p.name };
456
- }
457
- }
458
-
459
- // Show specific environment
460
- for (const e of allEnvironments) {
461
- const name = e.name.toLowerCase();
462
- if (lower.includes(name) && (lower.includes("environment") || lower.includes("env"))) {
463
- return { action: "navigate" as const, view: "environment-detail", params: { id: e.id }, title: e.name };
464
- }
465
- }
466
-
467
- // Show specific deployment by ID
468
- const deployIdMatch = lower.match(/(?:deployment|deploy)\s+([a-f0-9-]{36})/);
469
- if (deployIdMatch) {
470
- return { action: "navigate" as const, view: "deployment-detail", params: { id: deployIdMatch[1] }, title: "Deployment" };
471
- }
472
-
473
- // Failed deployments / what failed → markdown table
474
- if (/\b(fail|failed|failures|what failed|broken)\b/.test(lower)) {
475
- const failed = deployments.list().filter((d) => d.status === "failed")
476
- .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
477
- const content = buildDeploymentTable(failed, artifactMap, environmentMap, partitionMap);
478
- return { action: "answer" as const, view: "", params: {}, title: "Failed Deployments", content };
479
- }
480
-
481
- // Settings / configuration
482
- if (/\b(settings|preferences|configure)\b/.test(lower) || (lower.includes("config") && !/\bconfiguration-resolved\b/.test(lower))) {
483
- return { action: "navigate" as const, view: "settings", params: {}, title: "Settings" };
484
- }
485
-
486
- // Artifacts list → markdown table
487
- if (/\b(artifacts|artifact list|operations|operation list|manage artifacts)\b/.test(lower)) {
488
- const rows = allArtifacts.map((a) => `| ${a.name} | ${a.type} |`).join("\n");
489
- const content = allArtifacts.length > 0
490
- ? `| Artifact | Type |\n|----------|------|\n${rows}`
491
- : "_No artifacts configured._";
492
- return { action: "answer" as const, view: "", params: {}, title: "Artifacts", content };
493
- }
494
-
495
- // Debrief / decision diary
496
- if (/\b(debrief|decision diary|decisions|decision log|decision history)\b/.test(lower)) {
497
- return { action: "navigate" as const, view: "debrief", params: {}, title: "Debrief" };
498
- }
499
-
500
- // Deployment history / recent deployments → markdown table
501
- if (/\b(deployment|history|recent|deployments)\b/.test(lower)) {
502
- let deps = deployments.list()
503
- .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
504
- // Scope to a specific artifact if mentioned
505
- for (const a of allArtifacts) {
506
- if (lower.includes(a.name.toLowerCase())) {
507
- deps = deps.filter((d) => d.artifactId === a.id);
508
- break;
509
- }
510
- }
511
- const content = buildDeploymentTable(deps, artifactMap, environmentMap, partitionMap);
512
- const title = deps.length < deployments.list().length ? "Deployment History" : "Recent Deployments";
513
- return { action: "answer" as const, view: "", params: {}, title, content };
514
- }
515
-
516
- // Signals / drift / health
517
- if (/\b(signal|signals|drift|health|alert|alerts)\b/.test(lower)) {
518
- return { action: "navigate" as const, view: "overview", params: { focus: "signals" }, title: "Signals" };
519
- }
520
-
521
- // Show all partitions
522
- if (/\b(partitions|all partitions|partition list|manage partitions)\b/.test(lower)) {
523
- return { action: "navigate" as const, view: "partition-list", params: {}, title: "Partitions" };
524
- }
525
-
526
- // Fallback: navigate to overview
527
- return { action: "navigate" as const, view: "overview", params: {}, title: "Overview" };
528
- });
529
-
530
231
  // -------------------------------------------------------------------------
531
232
  // Pre-flight context — deterministic data + LLM editorialization
532
233
  // -------------------------------------------------------------------------
@@ -584,7 +285,7 @@ export function registerAgentRoutes(
584
285
  ? {
585
286
  status: latestToEnv.status,
586
287
  completedAt: (latestToEnv.completedAt ?? latestToEnv.createdAt).toISOString(),
587
- version: latestToEnv.version,
288
+ version: latestToEnv.version ?? "",
588
289
  }
589
290
  : undefined,
590
291
  recentFailures,
@@ -713,7 +414,7 @@ Be directional: say what you recommend, not "here are some data points." Use fir
713
414
  // LLM call failed or timed out — record to debrief and use deterministic fallback
714
415
  debrief.record({
715
416
  partitionId: partitionId ?? null,
716
- deploymentId: null,
417
+ operationId: null,
717
418
  agent: "server",
718
419
  decisionType: "pre-flight-llm-failure",
719
420
  decision: "Pre-flight LLM recommendation failed",
@@ -757,7 +458,7 @@ Be directional: say what you recommend, not "here are some data points." Use fir
757
458
  // --- 6. Debrief + telemetry ---
758
459
  debrief.record({
759
460
  partitionId: partitionId ?? null,
760
- deploymentId: null,
461
+ operationId: null,
761
462
  agent: "server",
762
463
  decisionType: "cross-system-context",
763
464
  decision: `Pre-flight context generated: ${recommendation.action} (confidence: ${recommendation.confidence})`,
@@ -813,7 +514,7 @@ Be directional: say what you recommend, not "here are some data points." Use fir
813
514
 
814
515
  debrief.record({
815
516
  partitionId: partitionId ?? null,
816
- deploymentId: null,
517
+ operationId: null,
817
518
  agent: "server",
818
519
  decisionType: "cross-system-context",
819
520
  decision: `User ${action} after pre-flight recommendation to ${recommendedAction}`,
@@ -852,224 +553,4 @@ export interface PreFlightContext {
852
553
  llmAvailable: boolean;
853
554
  }
854
555
 
855
- // ---------------------------------------------------------------------------
856
- // Deterministic markdown table builders (used in regex fallback)
857
- // ---------------------------------------------------------------------------
858
-
859
- function buildDeploymentTable(
860
- deps: Deployment[],
861
- artifactMap: Map<string, string>,
862
- environmentMap: Map<string, string>,
863
- partitionMap: Map<string, string>,
864
- ): string {
865
- if (deps.length === 0) return "_No matching deployments found._";
866
- const rows = deps
867
- .slice(0, 50)
868
- .map((d) => {
869
- const art = artifactMap.get(d.artifactId) ?? d.artifactId;
870
- const env = (d.environmentId ? environmentMap.get(d.environmentId) : undefined) ?? d.environmentId ?? "—";
871
- const part = d.partitionId ? (partitionMap.get(d.partitionId) ?? d.partitionId) : "—";
872
- const date = new Date(d.createdAt).toLocaleString();
873
- // Embed a synth:// deep-link so the UI can navigate to the deployment detail
874
- const versionLink = `[v${d.version}](synth://deployment-detail?id=${d.id})`;
875
- return `| ${d.status} | ${art} | ${versionLink} | ${env} | ${part} | ${date} |`;
876
- })
877
- .join("\n");
878
- return [
879
- "| Status | Artifact | Version | Environment | Partition | Date |",
880
- "|--------|----------|---------|-------------|-----------|------|",
881
- rows,
882
- ].join("\n");
883
- }
884
-
885
- // ---------------------------------------------------------------------------
886
- // LLM-powered query classification
887
- // ---------------------------------------------------------------------------
888
-
889
- function buildQueryClassificationPrompt(): string {
890
- return `You are a query classifier for Synth's agent canvas. Given a natural language query from a deployment engineer, classify it into one of these actions:
891
-
892
- 1. "navigate" — The user wants to drill into a specific named entity (e.g., "show partition Alpha", "open environment staging", "view deployment abc-123"). Only use this when there is a specific entity to navigate to.
893
- 2. "create" — The user wants to create a new entity (e.g., "create partition Acme Corp", "create operation api-service")
894
- 3. "answer" — Use this for EVERYTHING ELSE: data requests, lists, filters, analysis, comparisons, summaries. Examples: "show me failed deployments", "what failed", "recent deployments", "how many succeeded last week", "give me all deployments for api-service", "compare environments", "summarize activity". The response will be rendered as a formatted markdown table or narrative — NOT a navigation panel.
895
- 4. "annotate" — The user is providing a correction or clarification about a specific artifact. Examples: "Dockerfile.server is actually for nginx", "that artifact is a Node.js app not a docker image", "the api-service type should be nodejs", "correct: Dockerfile.envoy is the load balancer". Use this when the user is teaching Synth something about an artifact.
896
-
897
- Return a JSON object with this exact schema:
898
- {
899
- "action": "navigate" | "create" | "answer" | "annotate",
900
- "view": "<view-name or empty string for answer/create/annotate>",
901
- "params": { ... },
902
- "title": "<human-readable title, e.g. 'Failed Deployments' or 'Deployment History'>"
903
- }
904
-
905
- View names (for navigate only):
906
- - "partition-detail" — show specific partition (params: { "id": "<partition-name>" })
907
- - "environment-detail" — show specific environment (params: { "id": "<environment-name>" })
908
- - "deployment-detail" — show specific deployment (params: { "id": "<deployment-id>" })
909
- - "overview" — show the operational overview (params: {})
910
- - "settings" — show application settings (params: {})
911
-
912
- For "annotate" actions, set view to "" and params to:
913
- { "artifactName": "<exact artifact name from the known artifacts list>", "field": "summary|type|deploymentIntent", "correction": "<the user's correction in their own words>" }
914
-
915
- Rules:
916
- - ONLY use entity names from the provided lists. Never invent names.
917
- - If the query is a data/list/filter request, ALWAYS use "answer" — never "navigate".
918
- - If the query is ambiguous between navigation and data, prefer "answer".
919
- - For "create" actions, include the entity name in params: { "name": "..." }.
920
- - For "answer" and "create" actions, set view to "" and params to {}.
921
- - For "annotate", artifactName MUST match an artifact from the known list exactly.
922
- - Return ONLY valid JSON, no markdown, no explanation.`;
923
- }
924
-
925
- async function classifyQueryWithLlm(
926
- llm: LlmClient,
927
- query: string,
928
- allArtifacts: Artifact[],
929
- allPartitions: Partition[],
930
- allEnvironments: Environment[],
931
- deploymentStore: DeploymentStore,
932
- _debrief: DebriefReader,
933
- includeEntities: boolean,
934
- ): Promise<{ action: string; view: string; params: Record<string, string>; title?: string } | null> {
935
- const parts: string[] = [`<user-query>${sanitizeUserInput(query)}</user-query>`];
936
-
937
- appendEntityNames(parts, "Known partitions", allPartitions, includeEntities);
938
- appendEntityNames(parts, "Known environments", allEnvironments, includeEntities);
939
- appendEntityNames(parts, "Known artifacts", allArtifacts, includeEntities);
940
-
941
- const llmResult = await llm.classify({
942
- prompt: parts.join("\n"),
943
- systemPrompt: buildQueryClassificationPrompt(),
944
- promptSummary: `Canvas query classification: "${query}"`,
945
- partitionId: null,
946
- maxTokens: 512,
947
- });
948
-
949
- if (!llmResult.ok) return null;
950
-
951
- try {
952
- let text = llmResult.text.trim();
953
- if (text.startsWith("```")) {
954
- text = text.replace(/^```(?:json)?\n?/, "").replace(/\n?```$/, "");
955
- }
956
- const parsed = JSON.parse(text);
957
- if (!parsed.action || !parsed.view) return null;
958
-
959
- // Build name→ID maps for local resolution
960
- const partitionNameMap = buildNameMap(allPartitions);
961
- const environmentNameMap = buildNameMap(allEnvironments);
962
-
963
- // The LLM now returns names in params — resolve to IDs locally
964
- if (parsed.params?.id) {
965
- const idLower = parsed.params.id.toLowerCase();
966
- if (parsed.view === "partition-detail") {
967
- const resolvedId = partitionNameMap.get(idLower);
968
- if (!resolvedId) return null;
969
- parsed.params.id = resolvedId;
970
- } else if (parsed.view === "environment-detail") {
971
- const resolvedId = environmentNameMap.get(idLower);
972
- if (!resolvedId) return null;
973
- parsed.params.id = resolvedId;
974
- }
975
- }
976
- if (parsed.params?.partitionId) {
977
- const resolvedId = partitionNameMap.get(parsed.params.partitionId.toLowerCase());
978
- if (!resolvedId) {
979
- delete parsed.params.partitionId;
980
- } else {
981
- parsed.params.partitionId = resolvedId;
982
- }
983
- }
984
-
985
- return parsed;
986
- } catch {
987
- return null;
988
- }
989
- }
990
-
991
- // ---------------------------------------------------------------------------
992
- // LLM-powered analytical answer with real DB data
993
- // ---------------------------------------------------------------------------
994
-
995
- async function answerQueryWithData(
996
- llm: LlmClient,
997
- query: string,
998
- allDeployments: Deployment[],
999
- allArtifacts: Artifact[],
1000
- allPartitions: Partition[],
1001
- allEnvironments: Environment[],
1002
- ): Promise<{ action: "answer"; view: string; params: Record<string, string>; title: string; content: string } | null> {
1003
- const now = Date.now();
1004
-
1005
- // Build a concise data context from real records (last 50 deployments)
1006
- const recentDeployments = [...allDeployments]
1007
- .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
1008
- .slice(0, 50);
1009
-
1010
- const artifactMap = new Map(allArtifacts.map((a) => [a.id, a.name]));
1011
- const environmentMap = new Map(allEnvironments.map((e) => [e.id, e.name]));
1012
- const partitionMap = new Map(allPartitions.map((p) => [p.id, p.name]));
1013
-
1014
- const deploymentRows = recentDeployments.map((d) => {
1015
- const ageMs = now - new Date(d.createdAt).getTime();
1016
- const ageHours = Math.round(ageMs / (1000 * 60 * 60));
1017
- const age = ageHours < 24 ? `${ageHours}h ago` : `${Math.round(ageHours / 24)}d ago`;
1018
- const art = artifactMap.get(d.artifactId) ?? d.artifactId;
1019
- const env = (d.environmentId ? environmentMap.get(d.environmentId) : undefined) ?? d.environmentId ?? "—";
1020
- const part = d.partitionId ? ` (${partitionMap.get(d.partitionId) ?? d.partitionId})` : "";
1021
- // Include synth:// deep-link for UI navigation
1022
- return `- id:${d.id} | ${art} v${d.version} → ${env}${part}: ${d.status} (${age})`;
1023
- }).join("\n");
1024
-
1025
- const artifactRows = allArtifacts.map((a) => `- ${a.name} (${a.type})`).join("\n");
1026
- const partitionRows = allPartitions.map((p) => `- ${p.name}`).join("\n");
1027
- const environmentRows = allEnvironments.map((e) => `- ${e.name}`).join("\n");
1028
-
1029
- const contextBlock = [
1030
- `<deployments-recent count="${recentDeployments.length}">`,
1031
- deploymentRows || "(none)",
1032
- `</deployments-recent>`,
1033
- `<artifacts count="${allArtifacts.length}">`,
1034
- artifactRows || "(none)",
1035
- `</artifacts>`,
1036
- `<environments count="${allEnvironments.length}">`,
1037
- environmentRows || "(none)",
1038
- `</environments>`,
1039
- `<partitions count="${allPartitions.length}">`,
1040
- partitionRows || "(none)",
1041
- `</partitions>`,
1042
- ].join("\n");
1043
-
1044
- const systemPrompt = `You are Synth, an intelligent deployment system. A deployment engineer has asked you a question. Answer it using the real deployment data provided — do not fabricate records or invent names.
1045
-
1046
- Format your response as markdown:
1047
- - Use tables for tabular/comparative data. When a deployment is listed in a table, make its version a markdown link using the format: [v1.2.3](synth://deployment-detail?id=<deployment-id>) — use the actual id from the data (the id: prefix in each row).
1048
- - For partition or environment rows, you may link using: [Name](synth://partition-detail?id=<partition-name>) or [Name](synth://environment-detail?id=<env-name>)
1049
- - Use numbered lists for sequences or steps
1050
- - Use code blocks for configs, IDs, or technical strings
1051
- - Be specific and factual — reference actual artifact names, environments, and statuses from the data
1052
- - If the data doesn't contain enough information to answer precisely, say so clearly
1053
-
1054
- Keep the response concise and directly useful to an engineer.`;
1055
-
1056
- const prompt = [
1057
- `<user-query>${sanitizeUserInput(query)}</user-query>`,
1058
- contextBlock,
1059
- ].join("\n");
1060
-
1061
- const llmResult = await llm.reason({
1062
- prompt,
1063
- systemPrompt,
1064
- promptSummary: `Analytical answer for: "${query}"`,
1065
- partitionId: null,
1066
- maxTokens: 2048,
1067
- });
1068
556
 
1069
- if (!llmResult.ok) return null;
1070
-
1071
- const content = llmResult.text.trim();
1072
- const title = query.length > 60 ? query.slice(0, 57) + "..." : query;
1073
-
1074
- return { action: "answer", view: "", params: {}, title, content };
1075
- }