@synth-deploy/server 0.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.
- package/dist/agent/debrief-retention.d.ts +12 -0
- package/dist/agent/debrief-retention.d.ts.map +1 -0
- package/dist/agent/debrief-retention.js +27 -0
- package/dist/agent/debrief-retention.js.map +1 -0
- package/dist/agent/envoy-client.d.ts +216 -0
- package/dist/agent/envoy-client.d.ts.map +1 -0
- package/dist/agent/envoy-client.js +266 -0
- package/dist/agent/envoy-client.js.map +1 -0
- package/dist/agent/envoy-registry.d.ts +102 -0
- package/dist/agent/envoy-registry.d.ts.map +1 -0
- package/dist/agent/envoy-registry.js +319 -0
- package/dist/agent/envoy-registry.js.map +1 -0
- package/dist/agent/health-checker.d.ts +39 -0
- package/dist/agent/health-checker.d.ts.map +1 -0
- package/dist/agent/health-checker.js +49 -0
- package/dist/agent/health-checker.js.map +1 -0
- package/dist/agent/mcp-client-manager.d.ts +36 -0
- package/dist/agent/mcp-client-manager.d.ts.map +1 -0
- package/dist/agent/mcp-client-manager.js +106 -0
- package/dist/agent/mcp-client-manager.js.map +1 -0
- package/dist/agent/stale-deployment-detector.d.ts +15 -0
- package/dist/agent/stale-deployment-detector.d.ts.map +1 -0
- package/dist/agent/stale-deployment-detector.js +50 -0
- package/dist/agent/stale-deployment-detector.js.map +1 -0
- package/dist/agent/step-runner.d.ts +31 -0
- package/dist/agent/step-runner.d.ts.map +1 -0
- package/dist/agent/step-runner.js +80 -0
- package/dist/agent/step-runner.js.map +1 -0
- package/dist/agent/synth-agent.d.ts +168 -0
- package/dist/agent/synth-agent.d.ts.map +1 -0
- package/dist/agent/synth-agent.js +1195 -0
- package/dist/agent/synth-agent.js.map +1 -0
- package/dist/api/agent.d.ts +36 -0
- package/dist/api/agent.d.ts.map +1 -0
- package/dist/api/agent.js +867 -0
- package/dist/api/agent.js.map +1 -0
- package/dist/api/api-keys.d.ts +4 -0
- package/dist/api/api-keys.d.ts.map +1 -0
- package/dist/api/api-keys.js +118 -0
- package/dist/api/api-keys.js.map +1 -0
- package/dist/api/artifacts.d.ts +5 -0
- package/dist/api/artifacts.d.ts.map +1 -0
- package/dist/api/artifacts.js +142 -0
- package/dist/api/artifacts.js.map +1 -0
- package/dist/api/auth.d.ts +4 -0
- package/dist/api/auth.d.ts.map +1 -0
- package/dist/api/auth.js +280 -0
- package/dist/api/auth.js.map +1 -0
- package/dist/api/deployments.d.ts +11 -0
- package/dist/api/deployments.d.ts.map +1 -0
- package/dist/api/deployments.js +1098 -0
- package/dist/api/deployments.js.map +1 -0
- package/dist/api/environments.d.ts +5 -0
- package/dist/api/environments.d.ts.map +1 -0
- package/dist/api/environments.js +69 -0
- package/dist/api/environments.js.map +1 -0
- package/dist/api/envoy-reports.d.ts +17 -0
- package/dist/api/envoy-reports.d.ts.map +1 -0
- package/dist/api/envoy-reports.js +138 -0
- package/dist/api/envoy-reports.js.map +1 -0
- package/dist/api/envoys.d.ts +5 -0
- package/dist/api/envoys.d.ts.map +1 -0
- package/dist/api/envoys.js +192 -0
- package/dist/api/envoys.js.map +1 -0
- package/dist/api/fleet.d.ts +11 -0
- package/dist/api/fleet.d.ts.map +1 -0
- package/dist/api/fleet.js +394 -0
- package/dist/api/fleet.js.map +1 -0
- package/dist/api/graph.d.ts +8 -0
- package/dist/api/graph.d.ts.map +1 -0
- package/dist/api/graph.js +355 -0
- package/dist/api/graph.js.map +1 -0
- package/dist/api/health.d.ts +20 -0
- package/dist/api/health.d.ts.map +1 -0
- package/dist/api/health.js +248 -0
- package/dist/api/health.js.map +1 -0
- package/dist/api/idp-schemas.d.ts +41 -0
- package/dist/api/idp-schemas.d.ts.map +1 -0
- package/dist/api/idp-schemas.js +17 -0
- package/dist/api/idp-schemas.js.map +1 -0
- package/dist/api/idp.d.ts +6 -0
- package/dist/api/idp.d.ts.map +1 -0
- package/dist/api/idp.js +620 -0
- package/dist/api/idp.js.map +1 -0
- package/dist/api/intake.d.ts +10 -0
- package/dist/api/intake.d.ts.map +1 -0
- package/dist/api/intake.js +418 -0
- package/dist/api/intake.js.map +1 -0
- package/dist/api/partitions.d.ts +5 -0
- package/dist/api/partitions.d.ts.map +1 -0
- package/dist/api/partitions.js +113 -0
- package/dist/api/partitions.js.map +1 -0
- package/dist/api/progress-event-store.d.ts +62 -0
- package/dist/api/progress-event-store.d.ts.map +1 -0
- package/dist/api/progress-event-store.js +118 -0
- package/dist/api/progress-event-store.js.map +1 -0
- package/dist/api/schemas.d.ts +1000 -0
- package/dist/api/schemas.d.ts.map +1 -0
- package/dist/api/schemas.js +328 -0
- package/dist/api/schemas.js.map +1 -0
- package/dist/api/security-boundaries.d.ts +4 -0
- package/dist/api/security-boundaries.d.ts.map +1 -0
- package/dist/api/security-boundaries.js +32 -0
- package/dist/api/security-boundaries.js.map +1 -0
- package/dist/api/settings.d.ts +4 -0
- package/dist/api/settings.d.ts.map +1 -0
- package/dist/api/settings.js +99 -0
- package/dist/api/settings.js.map +1 -0
- package/dist/api/system.d.ts +75 -0
- package/dist/api/system.d.ts.map +1 -0
- package/dist/api/system.js +558 -0
- package/dist/api/system.js.map +1 -0
- package/dist/api/telemetry.d.ts +4 -0
- package/dist/api/telemetry.d.ts.map +1 -0
- package/dist/api/telemetry.js +24 -0
- package/dist/api/telemetry.js.map +1 -0
- package/dist/api/users.d.ts +4 -0
- package/dist/api/users.d.ts.map +1 -0
- package/dist/api/users.js +173 -0
- package/dist/api/users.js.map +1 -0
- package/dist/archive-unpacker.d.ts +24 -0
- package/dist/archive-unpacker.d.ts.map +1 -0
- package/dist/archive-unpacker.js +239 -0
- package/dist/archive-unpacker.js.map +1 -0
- package/dist/artifact-analyzer.d.ts +59 -0
- package/dist/artifact-analyzer.d.ts.map +1 -0
- package/dist/artifact-analyzer.js +334 -0
- package/dist/artifact-analyzer.js.map +1 -0
- package/dist/auth/idp/index.d.ts +9 -0
- package/dist/auth/idp/index.d.ts.map +1 -0
- package/dist/auth/idp/index.js +5 -0
- package/dist/auth/idp/index.js.map +1 -0
- package/dist/auth/idp/ldap.d.ts +56 -0
- package/dist/auth/idp/ldap.d.ts.map +1 -0
- package/dist/auth/idp/ldap.js +276 -0
- package/dist/auth/idp/ldap.js.map +1 -0
- package/dist/auth/idp/oidc.d.ts +27 -0
- package/dist/auth/idp/oidc.d.ts.map +1 -0
- package/dist/auth/idp/oidc.js +97 -0
- package/dist/auth/idp/oidc.js.map +1 -0
- package/dist/auth/idp/role-mapping.d.ts +9 -0
- package/dist/auth/idp/role-mapping.d.ts.map +1 -0
- package/dist/auth/idp/role-mapping.js +16 -0
- package/dist/auth/idp/role-mapping.js.map +1 -0
- package/dist/auth/idp/saml.d.ts +40 -0
- package/dist/auth/idp/saml.d.ts.map +1 -0
- package/dist/auth/idp/saml.js +117 -0
- package/dist/auth/idp/saml.js.map +1 -0
- package/dist/auth/idp/types.d.ts +23 -0
- package/dist/auth/idp/types.d.ts.map +1 -0
- package/dist/auth/idp/types.js +2 -0
- package/dist/auth/idp/types.js.map +1 -0
- package/dist/fleet/fleet-executor.d.ts +35 -0
- package/dist/fleet/fleet-executor.d.ts.map +1 -0
- package/dist/fleet/fleet-executor.js +228 -0
- package/dist/fleet/fleet-executor.js.map +1 -0
- package/dist/fleet/fleet-store.d.ts +13 -0
- package/dist/fleet/fleet-store.d.ts.map +1 -0
- package/dist/fleet/fleet-store.js +13 -0
- package/dist/fleet/fleet-store.js.map +1 -0
- package/dist/fleet/index.d.ts +5 -0
- package/dist/fleet/index.d.ts.map +1 -0
- package/dist/fleet/index.js +4 -0
- package/dist/fleet/index.js.map +1 -0
- package/dist/fleet/representative-selector.d.ts +15 -0
- package/dist/fleet/representative-selector.d.ts.map +1 -0
- package/dist/fleet/representative-selector.js +71 -0
- package/dist/fleet/representative-selector.js.map +1 -0
- package/dist/graph/graph-executor.d.ts +36 -0
- package/dist/graph/graph-executor.d.ts.map +1 -0
- package/dist/graph/graph-executor.js +348 -0
- package/dist/graph/graph-executor.js.map +1 -0
- package/dist/graph/graph-inference.d.ts +22 -0
- package/dist/graph/graph-inference.d.ts.map +1 -0
- package/dist/graph/graph-inference.js +149 -0
- package/dist/graph/graph-inference.js.map +1 -0
- package/dist/graph/graph-store.d.ts +12 -0
- package/dist/graph/graph-store.d.ts.map +1 -0
- package/dist/graph/graph-store.js +61 -0
- package/dist/graph/graph-store.js.map +1 -0
- package/dist/graph/index.d.ts +5 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +4 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +837 -0
- package/dist/index.js.map +1 -0
- package/dist/intake/index.d.ts +6 -0
- package/dist/intake/index.d.ts.map +1 -0
- package/dist/intake/index.js +5 -0
- package/dist/intake/index.js.map +1 -0
- package/dist/intake/intake-processor.d.ts +17 -0
- package/dist/intake/intake-processor.d.ts.map +1 -0
- package/dist/intake/intake-processor.js +99 -0
- package/dist/intake/intake-processor.js.map +1 -0
- package/dist/intake/intake-store.d.ts +7 -0
- package/dist/intake/intake-store.d.ts.map +1 -0
- package/dist/intake/intake-store.js +7 -0
- package/dist/intake/intake-store.js.map +1 -0
- package/dist/intake/registry-poller.d.ts +41 -0
- package/dist/intake/registry-poller.d.ts.map +1 -0
- package/dist/intake/registry-poller.js +202 -0
- package/dist/intake/registry-poller.js.map +1 -0
- package/dist/intake/webhook-handlers.d.ts +37 -0
- package/dist/intake/webhook-handlers.d.ts.map +1 -0
- package/dist/intake/webhook-handlers.js +268 -0
- package/dist/intake/webhook-handlers.js.map +1 -0
- package/dist/logger.d.ts +5 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +15 -0
- package/dist/logger.js.map +1 -0
- package/dist/mcp/resources.d.ts +9 -0
- package/dist/mcp/resources.d.ts.map +1 -0
- package/dist/mcp/resources.js +72 -0
- package/dist/mcp/resources.js.map +1 -0
- package/dist/mcp/server.d.ts +15 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +20 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools.d.ts +9 -0
- package/dist/mcp/tools.d.ts.map +1 -0
- package/dist/mcp/tools.js +88 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/middleware/auth.d.ts +29 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +76 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/permissions.d.ts +13 -0
- package/dist/middleware/permissions.d.ts.map +1 -0
- package/dist/middleware/permissions.js +32 -0
- package/dist/middleware/permissions.js.map +1 -0
- package/dist/pattern-store.d.ts +104 -0
- package/dist/pattern-store.d.ts.map +1 -0
- package/dist/pattern-store.js +299 -0
- package/dist/pattern-store.js.map +1 -0
- package/package.json +54 -0
- package/src/agent/debrief-retention.ts +44 -0
- package/src/agent/envoy-client.ts +474 -0
- package/src/agent/envoy-registry.ts +384 -0
- package/src/agent/health-checker.ts +70 -0
- package/src/agent/mcp-client-manager.ts +131 -0
- package/src/agent/stale-deployment-detector.ts +79 -0
- package/src/agent/step-runner.ts +124 -0
- package/src/agent/synth-agent.ts +1567 -0
- package/src/api/agent.ts +1075 -0
- package/src/api/api-keys.ts +129 -0
- package/src/api/artifacts.ts +194 -0
- package/src/api/auth.ts +320 -0
- package/src/api/deployments.ts +1347 -0
- package/src/api/environments.ts +97 -0
- package/src/api/envoy-reports.ts +159 -0
- package/src/api/envoys.ts +237 -0
- package/src/api/fleet.ts +510 -0
- package/src/api/graph.ts +516 -0
- package/src/api/health.ts +311 -0
- package/src/api/idp-schemas.ts +19 -0
- package/src/api/idp.ts +735 -0
- package/src/api/intake.ts +537 -0
- package/src/api/partitions.ts +147 -0
- package/src/api/progress-event-store.ts +153 -0
- package/src/api/schemas.ts +376 -0
- package/src/api/security-boundaries.ts +54 -0
- package/src/api/settings.ts +118 -0
- package/src/api/system.ts +704 -0
- package/src/api/telemetry.ts +32 -0
- package/src/api/users.ts +210 -0
- package/src/archive-unpacker.ts +271 -0
- package/src/artifact-analyzer.ts +438 -0
- package/src/auth/idp/index.ts +8 -0
- package/src/auth/idp/ldap.ts +340 -0
- package/src/auth/idp/oidc.ts +117 -0
- package/src/auth/idp/role-mapping.ts +22 -0
- package/src/auth/idp/saml.ts +148 -0
- package/src/auth/idp/types.ts +22 -0
- package/src/fleet/fleet-executor.ts +309 -0
- package/src/fleet/fleet-store.ts +13 -0
- package/src/fleet/index.ts +4 -0
- package/src/fleet/representative-selector.ts +83 -0
- package/src/graph/graph-executor.ts +446 -0
- package/src/graph/graph-inference.ts +184 -0
- package/src/graph/graph-store.ts +75 -0
- package/src/graph/index.ts +4 -0
- package/src/index.ts +916 -0
- package/src/intake/index.ts +5 -0
- package/src/intake/intake-processor.ts +111 -0
- package/src/intake/intake-store.ts +7 -0
- package/src/intake/registry-poller.ts +230 -0
- package/src/intake/webhook-handlers.ts +328 -0
- package/src/logger.ts +19 -0
- package/src/mcp/resources.ts +98 -0
- package/src/mcp/server.ts +34 -0
- package/src/mcp/tools.ts +117 -0
- package/src/middleware/auth.ts +103 -0
- package/src/middleware/permissions.ts +35 -0
- package/src/pattern-store.ts +409 -0
- package/tests/agent-mode.test.ts +536 -0
- package/tests/api-handlers.test.ts +1245 -0
- package/tests/archive-unpacker.test.ts +179 -0
- package/tests/artifact-analyzer.test.ts +240 -0
- package/tests/auth-middleware.test.ts +189 -0
- package/tests/decision-diary.test.ts +957 -0
- package/tests/diary-reader.test.ts +782 -0
- package/tests/envoy-client.test.ts +342 -0
- package/tests/envoy-reports.test.ts +156 -0
- package/tests/mcp-tools.test.ts +213 -0
- package/tests/orchestration.test.ts +536 -0
- package/tests/partition-deletion.test.ts +143 -0
- package/tests/partition-isolation.test.ts +830 -0
- package/tests/pattern-store.test.ts +371 -0
- package/tests/rbac-enforcement.test.ts +409 -0
- package/tests/ssrf-validation.test.ts +56 -0
- package/tests/stale-deployment.test.ts +85 -0
- package/tests/step-runner.test.ts +308 -0
- package/tests/ui-journey.test.ts +330 -0
- package/tsconfig.json +11 -0
- package/vitest.config.ts +27 -0
package/src/api/agent.ts
ADDED
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
import type { FastifyInstance } from "fastify";
|
|
2
|
+
import type { IPartitionStore, IEnvironmentStore, IArtifactStore, ISettingsStore, ITelemetryStore, DebriefWriter, DebriefReader, Artifact, Partition, Environment, Deployment } from "@synth-deploy/core";
|
|
3
|
+
import type { LlmClient } from "@synth-deploy/core";
|
|
4
|
+
import type { SynthAgent, DeploymentStore } from "../agent/synth-agent.js";
|
|
5
|
+
import type { EnvoyRegistry } from "../agent/envoy-registry.js";
|
|
6
|
+
import type { ArtifactAnalyzer } from "../artifact-analyzer.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { QueryRequestSchema } from "./schemas.js";
|
|
9
|
+
import { requirePermission } from "../middleware/permissions.js";
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Types
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
interface ContextSignal {
|
|
16
|
+
type: "trend" | "health" | "drift";
|
|
17
|
+
severity: "info" | "warning" | "critical";
|
|
18
|
+
title: string;
|
|
19
|
+
detail: string;
|
|
20
|
+
relatedEntity?: { type: string; id: string; name: string };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface DeploymentContext {
|
|
24
|
+
signals: ContextSignal[];
|
|
25
|
+
recentActivity: {
|
|
26
|
+
deploymentsLast24h: number;
|
|
27
|
+
successRate: string;
|
|
28
|
+
lastDeployment: { version: string; environment: string; status: string; ago: string } | null;
|
|
29
|
+
};
|
|
30
|
+
environmentSummary: Array<{
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
lastDeployStatus: string | null;
|
|
34
|
+
deployCount: number;
|
|
35
|
+
variableCount: number;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
|
|
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, '<').replace(/>/g, '>');
|
|
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
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// Context generation — signals from deployment data
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
function generateContext(
|
|
113
|
+
deployments: DeploymentStore,
|
|
114
|
+
environmentStore: IEnvironmentStore,
|
|
115
|
+
partitionStore: IPartitionStore,
|
|
116
|
+
): DeploymentContext {
|
|
117
|
+
const allDeployments = deployments.list();
|
|
118
|
+
const allEnvironments = environmentStore.list();
|
|
119
|
+
|
|
120
|
+
const signals: ContextSignal[] = [];
|
|
121
|
+
|
|
122
|
+
// --- Deployment trends ---
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
const last24h = allDeployments.filter(
|
|
125
|
+
(d) => now - new Date(d.createdAt).getTime() < 24 * 60 * 60 * 1000,
|
|
126
|
+
);
|
|
127
|
+
const recentFailed = last24h.filter((d) => d.status === "failed");
|
|
128
|
+
|
|
129
|
+
if (recentFailed.length > 0) {
|
|
130
|
+
const rate = Math.round((recentFailed.length / Math.max(last24h.length, 1)) * 100);
|
|
131
|
+
signals.push({
|
|
132
|
+
type: "trend",
|
|
133
|
+
severity: rate > 50 ? "critical" : "warning",
|
|
134
|
+
title: `${recentFailed.length} failed deployment${recentFailed.length > 1 ? "s" : ""} in last 24h`,
|
|
135
|
+
detail: `${rate}% failure rate across ${last24h.length} recent deployments`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (last24h.length === 0 && allDeployments.length > 0) {
|
|
140
|
+
signals.push({
|
|
141
|
+
type: "trend",
|
|
142
|
+
severity: "info",
|
|
143
|
+
title: "No deployments in last 24 hours",
|
|
144
|
+
detail: `Last deployment was ${allDeployments.length > 0 ? formatAgo(new Date(allDeployments[allDeployments.length - 1].createdAt)) : "never"}`,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// --- Environment health signals ---
|
|
149
|
+
for (const env of allEnvironments) {
|
|
150
|
+
const envDeployments = allDeployments
|
|
151
|
+
.filter((d) => d.environmentId === env.id)
|
|
152
|
+
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
153
|
+
|
|
154
|
+
if (envDeployments.length > 0 && envDeployments[0].status === "failed") {
|
|
155
|
+
signals.push({
|
|
156
|
+
type: "health",
|
|
157
|
+
severity: "warning",
|
|
158
|
+
title: `Last deployment to ${env.name} failed`,
|
|
159
|
+
detail: envDeployments[0].failureReason ?? "Unknown failure",
|
|
160
|
+
relatedEntity: { type: "environment", id: env.id, name: env.name },
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Consecutive failures
|
|
165
|
+
const consecutiveFails = envDeployments.filter((d, i) => {
|
|
166
|
+
if (i > 2) return false;
|
|
167
|
+
return d.status === "failed";
|
|
168
|
+
}).length;
|
|
169
|
+
|
|
170
|
+
if (consecutiveFails >= 2) {
|
|
171
|
+
signals.push({
|
|
172
|
+
type: "health",
|
|
173
|
+
severity: "critical",
|
|
174
|
+
title: `${env.name}: ${consecutiveFails} consecutive failures`,
|
|
175
|
+
detail: `Environment may have an infrastructure issue. Last ${consecutiveFails} deployments all failed.`,
|
|
176
|
+
relatedEntity: { type: "environment", id: env.id, name: env.name },
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// --- Configuration drift warnings ---
|
|
182
|
+
const partitions = partitionStore.list();
|
|
183
|
+
for (const partition of partitions) {
|
|
184
|
+
for (const env of allEnvironments) {
|
|
185
|
+
const conflicts = detectDrift(partition, env);
|
|
186
|
+
if (conflicts.length > 0) {
|
|
187
|
+
signals.push({
|
|
188
|
+
type: "drift",
|
|
189
|
+
severity: "warning",
|
|
190
|
+
title: `Config drift: ${partition.name} / ${env.name}`,
|
|
191
|
+
detail: `${conflicts.length} variable${conflicts.length > 1 ? "s" : ""} may conflict: ${conflicts.join(", ")}`,
|
|
192
|
+
relatedEntity: { type: "partition", id: partition.id, name: partition.name },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// --- Recent activity summary ---
|
|
199
|
+
const sorted = [...allDeployments].sort(
|
|
200
|
+
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
|
|
201
|
+
);
|
|
202
|
+
const lastDeploy = sorted[0];
|
|
203
|
+
const succeeded = allDeployments.filter((d) => d.status === "succeeded").length;
|
|
204
|
+
|
|
205
|
+
const environmentSummary = allEnvironments.map((env) => {
|
|
206
|
+
const envDeploys = allDeployments.filter((d) => d.environmentId === env.id);
|
|
207
|
+
const lastEnvDeploy = envDeploys.sort(
|
|
208
|
+
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
|
|
209
|
+
)[0];
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
id: env.id,
|
|
213
|
+
name: env.name,
|
|
214
|
+
lastDeployStatus: lastEnvDeploy?.status ?? null,
|
|
215
|
+
deployCount: envDeploys.length,
|
|
216
|
+
variableCount: Object.keys(env.variables).length,
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
signals,
|
|
222
|
+
recentActivity: {
|
|
223
|
+
deploymentsLast24h: last24h.length,
|
|
224
|
+
successRate: allDeployments.length > 0
|
|
225
|
+
? `${Math.round((succeeded / allDeployments.length) * 100)}%`
|
|
226
|
+
: "—",
|
|
227
|
+
lastDeployment: lastDeploy
|
|
228
|
+
? {
|
|
229
|
+
version: lastDeploy.version,
|
|
230
|
+
environment: allEnvironments.find((e) => e.id === lastDeploy.environmentId)?.name ?? lastDeploy.environmentId ?? "—",
|
|
231
|
+
status: lastDeploy.status,
|
|
232
|
+
ago: formatAgo(new Date(lastDeploy.createdAt)),
|
|
233
|
+
}
|
|
234
|
+
: null,
|
|
235
|
+
},
|
|
236
|
+
environmentSummary,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function detectDrift(partition: Partition, environment: Environment): string[] {
|
|
241
|
+
const conflicts: string[] = [];
|
|
242
|
+
const envPatterns: Record<string, RegExp[]> = {
|
|
243
|
+
production: [/\bstag/i, /\bdev\b/i],
|
|
244
|
+
staging: [/\bprod/i],
|
|
245
|
+
development: [/\bprod/i, /\bstag/i],
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const patternsToCheck = envPatterns[environment.name.toLowerCase()];
|
|
249
|
+
if (!patternsToCheck) return conflicts;
|
|
250
|
+
|
|
251
|
+
for (const [key, value] of Object.entries(partition.variables)) {
|
|
252
|
+
if (patternsToCheck.some((p) => p.test(value))) {
|
|
253
|
+
conflicts.push(key);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return conflicts;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function formatAgo(date: Date): string {
|
|
261
|
+
const ms = Date.now() - date.getTime();
|
|
262
|
+
const seconds = Math.floor(ms / 1000);
|
|
263
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
264
|
+
const minutes = Math.floor(seconds / 60);
|
|
265
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
266
|
+
const hours = Math.floor(minutes / 60);
|
|
267
|
+
if (hours < 24) return `${hours}h ago`;
|
|
268
|
+
const days = Math.floor(hours / 24);
|
|
269
|
+
return `${days}d ago`;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ---------------------------------------------------------------------------
|
|
273
|
+
// Route registration
|
|
274
|
+
// ---------------------------------------------------------------------------
|
|
275
|
+
|
|
276
|
+
export function registerAgentRoutes(
|
|
277
|
+
app: FastifyInstance,
|
|
278
|
+
agent: SynthAgent,
|
|
279
|
+
partitions: IPartitionStore,
|
|
280
|
+
environments: IEnvironmentStore,
|
|
281
|
+
artifacts: IArtifactStore,
|
|
282
|
+
deployments: DeploymentStore,
|
|
283
|
+
debrief: DebriefWriter & DebriefReader,
|
|
284
|
+
settings: ISettingsStore,
|
|
285
|
+
llm?: LlmClient,
|
|
286
|
+
envoyRegistry?: EnvoyRegistry,
|
|
287
|
+
telemetry?: ITelemetryStore,
|
|
288
|
+
analyzer?: ArtifactAnalyzer,
|
|
289
|
+
): void {
|
|
290
|
+
/**
|
|
291
|
+
* Get deployment context — signals, trends, health, drift.
|
|
292
|
+
* Fills the space where manual action buttons collapse.
|
|
293
|
+
*/
|
|
294
|
+
app.get("/api/agent/context", { preHandler: [requirePermission("deployment.view")] }, async () => {
|
|
295
|
+
return generateContext(deployments, environments, partitions);
|
|
296
|
+
});
|
|
297
|
+
|
|
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
|
+
// -------------------------------------------------------------------------
|
|
531
|
+
// Pre-flight context — deterministic data + LLM editorialization
|
|
532
|
+
// -------------------------------------------------------------------------
|
|
533
|
+
|
|
534
|
+
const PreFlightRequestSchema = z.object({
|
|
535
|
+
artifactId: z.string().min(1),
|
|
536
|
+
environmentId: z.string().min(1),
|
|
537
|
+
partitionId: z.string().optional(),
|
|
538
|
+
version: z.string().optional(),
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
app.post("/api/agent/pre-flight", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
|
|
542
|
+
const parsed = PreFlightRequestSchema.safeParse(request.body);
|
|
543
|
+
if (!parsed.success) {
|
|
544
|
+
return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const { artifactId, environmentId, partitionId, version } = parsed.data;
|
|
548
|
+
|
|
549
|
+
// --- 1. Target health: check envoy health for the environment ---
|
|
550
|
+
let targetHealth: PreFlightContext["targetHealth"] = {
|
|
551
|
+
status: "healthy",
|
|
552
|
+
details: "No envoys registered — health check not applicable",
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
if (envoyRegistry) {
|
|
556
|
+
const envName = environments.get(environmentId)?.name ?? environmentId;
|
|
557
|
+
const envoy = envoyRegistry.findForEnvironment(envName);
|
|
558
|
+
if (envoy) {
|
|
559
|
+
const healthStatus = envoy.lastHealthStatus;
|
|
560
|
+
if (healthStatus === "healthy") {
|
|
561
|
+
targetHealth = { status: "healthy", details: `Envoy "${envoy.name}" is healthy` };
|
|
562
|
+
} else if (healthStatus === "degraded") {
|
|
563
|
+
targetHealth = { status: "degraded", details: `Envoy "${envoy.name}" is degraded` };
|
|
564
|
+
} else if (healthStatus === "unreachable") {
|
|
565
|
+
targetHealth = { status: "unreachable", details: `Envoy "${envoy.name}" is unreachable` };
|
|
566
|
+
} else {
|
|
567
|
+
targetHealth = { status: "healthy", details: `Envoy "${envoy.name}" registered, health not yet checked` };
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// --- 2. Recent history ---
|
|
573
|
+
const now = new Date();
|
|
574
|
+
const twentyFourHoursAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
575
|
+
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
576
|
+
|
|
577
|
+
const latestToEnv = deployments.findLatestByEnvironment(environmentId);
|
|
578
|
+
const deploymentsToday = deployments.countByEnvironment(environmentId, twentyFourHoursAgo);
|
|
579
|
+
const recentArtifactDeploys = deployments.findRecentByArtifact(artifactId, sevenDaysAgo);
|
|
580
|
+
const recentFailures = recentArtifactDeploys.filter((d) => d.status === "failed").length;
|
|
581
|
+
|
|
582
|
+
const recentHistory: PreFlightContext["recentHistory"] = {
|
|
583
|
+
lastDeployment: latestToEnv
|
|
584
|
+
? {
|
|
585
|
+
status: latestToEnv.status,
|
|
586
|
+
completedAt: (latestToEnv.completedAt ?? latestToEnv.createdAt).toISOString(),
|
|
587
|
+
version: latestToEnv.version,
|
|
588
|
+
}
|
|
589
|
+
: undefined,
|
|
590
|
+
recentFailures,
|
|
591
|
+
deploymentsToday,
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
// --- 3. Cross-system context queries ---
|
|
595
|
+
const crossSystemContext: string[] = [];
|
|
596
|
+
|
|
597
|
+
// Check if this version was rolled back anywhere
|
|
598
|
+
if (version) {
|
|
599
|
+
const rolledBack = deployments.findByArtifactVersion(artifactId, version, "rolled_back");
|
|
600
|
+
if (rolledBack.length > 0) {
|
|
601
|
+
const envNames = rolledBack.map((d) => environments.get(d.environmentId ?? "")?.name ?? d.environmentId ?? "unknown");
|
|
602
|
+
crossSystemContext.push(
|
|
603
|
+
`This version (${version}) was rolled back from ${envNames.join(", ")} previously`,
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const failed = deployments.findByArtifactVersion(artifactId, version, "failed");
|
|
608
|
+
if (failed.length > 0) {
|
|
609
|
+
const envNames = failed.map((d) => environments.get(d.environmentId ?? "")?.name ?? d.environmentId ?? "unknown");
|
|
610
|
+
crossSystemContext.push(
|
|
611
|
+
`This version (${version}) failed deployment to ${envNames.join(", ")}`,
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Check recent failure patterns for this artifact
|
|
617
|
+
if (recentFailures > 2) {
|
|
618
|
+
crossSystemContext.push(
|
|
619
|
+
`${recentFailures} failed deployments for this artifact in the last 7 days — investigate before proceeding`,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Check if the last deployment to this environment failed
|
|
624
|
+
if (latestToEnv && latestToEnv.status === "failed") {
|
|
625
|
+
crossSystemContext.push(
|
|
626
|
+
`The last deployment to this environment failed (${latestToEnv.failureReason ?? "unknown reason"})`,
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// Check deployment volume
|
|
631
|
+
if (deploymentsToday >= 5) {
|
|
632
|
+
crossSystemContext.push(
|
|
633
|
+
`High deployment volume: ${deploymentsToday} deployments to this environment in the last 24 hours`,
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// --- 4. LLM recommendation ---
|
|
638
|
+
let recommendation: PreFlightContext["recommendation"] = {
|
|
639
|
+
action: "proceed",
|
|
640
|
+
reasoning: "Agent recommendation unavailable — review the context above and decide.",
|
|
641
|
+
confidence: 0,
|
|
642
|
+
};
|
|
643
|
+
let llmAvailable = false;
|
|
644
|
+
|
|
645
|
+
if (llm && llm.isAvailable()) {
|
|
646
|
+
const artifactName = artifacts.get(artifactId)?.name ?? artifactId;
|
|
647
|
+
const envName = environments.get(environmentId)?.name ?? environmentId;
|
|
648
|
+
const partitionName = partitionId ? (partitions.get(partitionId)?.name ?? partitionId) : null;
|
|
649
|
+
|
|
650
|
+
const promptParts = [
|
|
651
|
+
`You are an intelligent deployment advisor. Analyze the following pre-flight context and provide a directional recommendation.`,
|
|
652
|
+
`\nArtifact: ${artifactName}`,
|
|
653
|
+
`Target environment: ${envName}`,
|
|
654
|
+
partitionName ? `Partition: ${partitionName}` : null,
|
|
655
|
+
version ? `Version: ${version}` : null,
|
|
656
|
+
`\nTarget health: ${targetHealth.status} — ${targetHealth.details}`,
|
|
657
|
+
`\nRecent history:`,
|
|
658
|
+
` Deployments to this environment in last 24h: ${deploymentsToday}`,
|
|
659
|
+
` Recent failures for this artifact (7d): ${recentFailures}`,
|
|
660
|
+
latestToEnv
|
|
661
|
+
? ` Last deployment to this env: ${latestToEnv.status} (${latestToEnv.version}, ${formatAgo(latestToEnv.completedAt ?? latestToEnv.createdAt)})`
|
|
662
|
+
: ` No previous deployments to this environment`,
|
|
663
|
+
crossSystemContext.length > 0
|
|
664
|
+
? `\nCross-system observations:\n${crossSystemContext.map((c) => ` - ${c}`).join("\n")}`
|
|
665
|
+
: `\nNo cross-system concerns detected.`,
|
|
666
|
+
].filter(Boolean);
|
|
667
|
+
|
|
668
|
+
const systemPrompt = `You are a deployment advisor for Synth. Given pre-flight context, you MUST respond with ONLY a JSON object (no markdown, no explanation) with this schema:
|
|
669
|
+
{
|
|
670
|
+
"action": "proceed" | "wait" | "investigate",
|
|
671
|
+
"reasoning": "<1-2 sentences, directional — 'I recommend proceeding' / 'I'd wait' / 'Investigate first' style>",
|
|
672
|
+
"confidence": <0-1 number>
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
Be directional: say what you recommend, not "here are some data points." Use first person. Be specific.`;
|
|
676
|
+
|
|
677
|
+
try {
|
|
678
|
+
const timeout = new Promise<never>((_, reject) =>
|
|
679
|
+
setTimeout(() => reject(new Error("Pre-flight LLM timeout (15s)")), 15000),
|
|
680
|
+
);
|
|
681
|
+
|
|
682
|
+
const llmResult = await Promise.race([
|
|
683
|
+
llm.classify({
|
|
684
|
+
prompt: promptParts.join("\n"),
|
|
685
|
+
systemPrompt,
|
|
686
|
+
promptSummary: `Pre-flight recommendation for ${artifactName} → ${envName}`,
|
|
687
|
+
partitionId: partitionId ?? null,
|
|
688
|
+
maxTokens: 512,
|
|
689
|
+
}),
|
|
690
|
+
timeout,
|
|
691
|
+
]);
|
|
692
|
+
|
|
693
|
+
if (llmResult.ok) {
|
|
694
|
+
try {
|
|
695
|
+
let text = llmResult.text.trim();
|
|
696
|
+
if (text.startsWith("```")) {
|
|
697
|
+
text = text.replace(/^```(?:json)?\n?/, "").replace(/\n?```$/, "");
|
|
698
|
+
}
|
|
699
|
+
const parsed = JSON.parse(text);
|
|
700
|
+
if (parsed.action && parsed.reasoning && typeof parsed.confidence === "number") {
|
|
701
|
+
recommendation = {
|
|
702
|
+
action: parsed.action,
|
|
703
|
+
reasoning: parsed.reasoning,
|
|
704
|
+
confidence: Math.max(0, Math.min(1, parsed.confidence)),
|
|
705
|
+
};
|
|
706
|
+
llmAvailable = true;
|
|
707
|
+
}
|
|
708
|
+
} catch {
|
|
709
|
+
// JSON parse failed — use deterministic fallback
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
} catch (llmError) {
|
|
713
|
+
// LLM call failed or timed out — record to debrief and use deterministic fallback
|
|
714
|
+
debrief.record({
|
|
715
|
+
partitionId: partitionId ?? null,
|
|
716
|
+
deploymentId: null,
|
|
717
|
+
agent: "server",
|
|
718
|
+
decisionType: "pre-flight-llm-failure",
|
|
719
|
+
decision: "Pre-flight LLM recommendation failed",
|
|
720
|
+
reasoning: llmError instanceof Error ? llmError.message : String(llmError),
|
|
721
|
+
context: { artifactId, environmentId, partitionId: partitionId ?? null },
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// --- 5. Deterministic fallback recommendation if LLM was unavailable ---
|
|
727
|
+
if (!llmAvailable) {
|
|
728
|
+
if (targetHealth.status === "unreachable") {
|
|
729
|
+
recommendation = {
|
|
730
|
+
action: "investigate",
|
|
731
|
+
reasoning: "The target envoy is unreachable. Investigate infrastructure health before deploying.",
|
|
732
|
+
confidence: 0,
|
|
733
|
+
};
|
|
734
|
+
} else if (recentFailures > 2 || (latestToEnv && latestToEnv.status === "failed")) {
|
|
735
|
+
recommendation = {
|
|
736
|
+
action: "investigate",
|
|
737
|
+
reasoning: "Recent failures detected. Review the failure history before proceeding.",
|
|
738
|
+
confidence: 0,
|
|
739
|
+
};
|
|
740
|
+
} else if (targetHealth.status === "degraded") {
|
|
741
|
+
recommendation = {
|
|
742
|
+
action: "wait",
|
|
743
|
+
reasoning: "The target envoy is degraded. Consider waiting for it to stabilize.",
|
|
744
|
+
confidence: 0,
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const result: PreFlightContext = {
|
|
750
|
+
targetHealth,
|
|
751
|
+
recentHistory,
|
|
752
|
+
crossSystemContext,
|
|
753
|
+
recommendation,
|
|
754
|
+
llmAvailable,
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
// --- 6. Debrief + telemetry ---
|
|
758
|
+
debrief.record({
|
|
759
|
+
partitionId: partitionId ?? null,
|
|
760
|
+
deploymentId: null,
|
|
761
|
+
agent: "server",
|
|
762
|
+
decisionType: "cross-system-context",
|
|
763
|
+
decision: `Pre-flight context generated: ${recommendation.action} (confidence: ${recommendation.confidence})`,
|
|
764
|
+
reasoning: recommendation.reasoning,
|
|
765
|
+
context: {
|
|
766
|
+
artifactId,
|
|
767
|
+
environmentId,
|
|
768
|
+
partitionId: partitionId ?? null,
|
|
769
|
+
version: version ?? null,
|
|
770
|
+
targetHealth: targetHealth.status,
|
|
771
|
+
recentFailures,
|
|
772
|
+
deploymentsToday,
|
|
773
|
+
crossSystemSignals: crossSystemContext.length,
|
|
774
|
+
llmAvailable,
|
|
775
|
+
},
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
if (telemetry) {
|
|
779
|
+
telemetry.record({
|
|
780
|
+
actor: "agent",
|
|
781
|
+
action: "agent.pre-flight.generated",
|
|
782
|
+
target: { type: "deployment", id: `${artifactId}:${environmentId}` },
|
|
783
|
+
details: {
|
|
784
|
+
recommendation: recommendation.action,
|
|
785
|
+
confidence: recommendation.confidence,
|
|
786
|
+
llmAvailable,
|
|
787
|
+
},
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
return result;
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
// -------------------------------------------------------------------------
|
|
795
|
+
// Pre-flight user response — records what the user did after seeing context
|
|
796
|
+
// -------------------------------------------------------------------------
|
|
797
|
+
|
|
798
|
+
const PreFlightResponseSchema = z.object({
|
|
799
|
+
artifactId: z.string().min(1),
|
|
800
|
+
environmentId: z.string().min(1),
|
|
801
|
+
partitionId: z.string().optional(),
|
|
802
|
+
action: z.enum(["proceeded", "waited", "canceled"]),
|
|
803
|
+
recommendedAction: z.enum(["proceed", "wait", "investigate"]),
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
app.post("/api/agent/pre-flight/response", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
|
|
807
|
+
const parsed = PreFlightResponseSchema.safeParse(request.body);
|
|
808
|
+
if (!parsed.success) {
|
|
809
|
+
return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
const { artifactId, environmentId, partitionId, action, recommendedAction } = parsed.data;
|
|
813
|
+
|
|
814
|
+
debrief.record({
|
|
815
|
+
partitionId: partitionId ?? null,
|
|
816
|
+
deploymentId: null,
|
|
817
|
+
agent: "server",
|
|
818
|
+
decisionType: "cross-system-context",
|
|
819
|
+
decision: `User ${action} after pre-flight recommendation to ${recommendedAction}`,
|
|
820
|
+
reasoning: `System recommended "${recommendedAction}", user chose to "${action}".`,
|
|
821
|
+
context: { artifactId, environmentId, partitionId: partitionId ?? null, recommendedAction, userAction: action },
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
return { ok: true };
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// ---------------------------------------------------------------------------
|
|
829
|
+
// Pre-flight context types
|
|
830
|
+
// ---------------------------------------------------------------------------
|
|
831
|
+
|
|
832
|
+
export interface PreFlightContext {
|
|
833
|
+
targetHealth: {
|
|
834
|
+
status: "healthy" | "degraded" | "unreachable";
|
|
835
|
+
details: string;
|
|
836
|
+
};
|
|
837
|
+
recentHistory: {
|
|
838
|
+
lastDeployment?: {
|
|
839
|
+
status: string;
|
|
840
|
+
completedAt: string;
|
|
841
|
+
version: string;
|
|
842
|
+
};
|
|
843
|
+
recentFailures: number;
|
|
844
|
+
deploymentsToday: number;
|
|
845
|
+
};
|
|
846
|
+
crossSystemContext: string[];
|
|
847
|
+
recommendation: {
|
|
848
|
+
action: "proceed" | "wait" | "investigate";
|
|
849
|
+
reasoning: string;
|
|
850
|
+
confidence: number;
|
|
851
|
+
};
|
|
852
|
+
llmAvailable: boolean;
|
|
853
|
+
}
|
|
854
|
+
|
|
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
|
+
|
|
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
|
+
}
|