create-safest-tools 0.2.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/LICENSE +201 -0
- package/README.md +71 -0
- package/bin/create-safest-tools.mjs +12 -0
- package/package.json +34 -0
- package/src/cli.mjs +128 -0
- package/src/config.mjs +151 -0
- package/src/scaffold.mjs +30 -0
- package/template/LICENSE +201 -0
- package/template/README.md +35 -0
- package/template/console/README.md +28 -0
- package/template/console/analytics/AnalyticsWorkspace.tsx +55 -0
- package/template/console/analytics/controller.ts +133 -0
- package/template/console/analytics/events.ts +33 -0
- package/template/console/analytics/store.ts +33 -0
- package/template/console/analytics/types.ts +38 -0
- package/template/console/appeals/AppealWorkspace.tsx +73 -0
- package/template/console/appeals/controller.ts +125 -0
- package/template/console/appeals/events.ts +33 -0
- package/template/console/appeals/store.ts +35 -0
- package/template/console/appeals/types.ts +37 -0
- package/template/console/assistant/AssistantWorkspace.tsx +133 -0
- package/template/console/assistant/types.ts +34 -0
- package/template/console/auth/AccountJourney.tsx +69 -0
- package/template/console/auth/InvitationAcceptance.tsx +145 -0
- package/template/console/auth/OAuthButtons.tsx +78 -0
- package/template/console/auth/OperatorLogin.tsx +110 -0
- package/template/console/auth/PasswordRecovery.tsx +132 -0
- package/template/console/auth/account-route.ts +15 -0
- package/template/console/auth/browser-navigation.ts +7 -0
- package/template/console/auth/events.ts +15 -0
- package/template/console/command/CommandCentre.tsx +119 -0
- package/template/console/command/controller.ts +267 -0
- package/template/console/command/events.ts +19 -0
- package/template/console/command/store.ts +36 -0
- package/template/console/command/types.ts +61 -0
- package/template/console/components/AnalystIdentity.tsx +25 -0
- package/template/console/components/PageHeader.tsx +27 -0
- package/template/console/configuration/ConfigurationDialog.tsx +200 -0
- package/template/console/configuration/ConfigurationWorkspace.tsx +66 -0
- package/template/console/configuration/FormBuilder.tsx +143 -0
- package/template/console/configuration/api.ts +40 -0
- package/template/console/configuration/events.ts +14 -0
- package/template/console/configuration/types.ts +79 -0
- package/template/console/lib/format.ts +49 -0
- package/template/console/lib/http.ts +96 -0
- package/template/console/main.tsx +225 -0
- package/template/console/operations/OperationsWorkspace.tsx +126 -0
- package/template/console/operations/controller.ts +237 -0
- package/template/console/operations/events.ts +37 -0
- package/template/console/operations/store.ts +32 -0
- package/template/console/operations/types.ts +107 -0
- package/template/console/people/PeopleWorkspace.tsx +81 -0
- package/template/console/people/types.ts +25 -0
- package/template/console/profile/ProfileWorkspace.tsx +136 -0
- package/template/console/profile/events.ts +9 -0
- package/template/console/profile/types.ts +6 -0
- package/template/console/quality/QualityWorkspace.tsx +87 -0
- package/template/console/quality/controller.ts +154 -0
- package/template/console/quality/events.ts +34 -0
- package/template/console/quality/store.ts +36 -0
- package/template/console/quality/types.ts +70 -0
- package/template/console/queues/QueueEditor.tsx +173 -0
- package/template/console/queues/QueueWorkspace.tsx +109 -0
- package/template/console/queues/controller.ts +194 -0
- package/template/console/queues/events.ts +34 -0
- package/template/console/queues/store.ts +37 -0
- package/template/console/queues/types.ts +102 -0
- package/template/console/registry/RegistryDialog.tsx +198 -0
- package/template/console/registry/RegistryWorkspace.tsx +108 -0
- package/template/console/registry/events.ts +14 -0
- package/template/console/registry/types.ts +52 -0
- package/template/console/reports/ReportDrawer.tsx +159 -0
- package/template/console/reports/ReportWorkspace.tsx +90 -0
- package/template/console/reports/controller.ts +517 -0
- package/template/console/reports/events.ts +42 -0
- package/template/console/reports/store.ts +39 -0
- package/template/console/reports/types.ts +230 -0
- package/template/console/settings/BrandEditor.tsx +126 -0
- package/template/console/settings/ChannelDialog.tsx +241 -0
- package/template/console/settings/SettingsWorkspace.tsx +155 -0
- package/template/console/settings/events.ts +19 -0
- package/template/console/settings/handoff.ts +22 -0
- package/template/console/settings/types.ts +93 -0
- package/template/console/shell/WorkspaceShell.tsx +160 -0
- package/template/console/shell/controller.ts +182 -0
- package/template/console/shell/events.ts +25 -0
- package/template/console/shell/navigation.ts +61 -0
- package/template/console/shell/store.ts +56 -0
- package/template/console/shell/types.ts +86 -0
- package/template/console/workflows/CreateWorkflowDialog.tsx +87 -0
- package/template/console/workflows/WorkflowCanvas.tsx +42 -0
- package/template/console/workflows/WorkflowDialogs.tsx +6 -0
- package/template/console/workflows/WorkflowStudio.tsx +331 -0
- package/template/console/workflows/WorkflowWorkspace.tsx +82 -0
- package/template/console/workflows/events.ts +17 -0
- package/template/console/workflows/graph.ts +156 -0
- package/template/console/workflows/templates.ts +70 -0
- package/template/console/workflows/types.ts +201 -0
- package/template/gitignore.template +18 -0
- package/template/migrations/0001_reports_foundation.sql +444 -0
- package/template/migrations/0002_human_report_loop.sql +65 -0
- package/template/migrations/0003_delivery_reliability.sql +18 -0
- package/template/migrations/0004_public_intake.sql +14 -0
- package/template/migrations/0005_operations_visibility.sql +24 -0
- package/template/migrations/0006_ai_governance.sql +198 -0
- package/template/migrations/0007_ai_release_gates.sql +6 -0
- package/template/migrations/0008_retention_analytics_exports.sql +52 -0
- package/template/migrations/0009_retention_derived_copies.sql +14 -0
- package/template/migrations/0010_ai_quality_controls.sql +26 -0
- package/template/migrations/0011_analyst_presence.sql +28 -0
- package/template/migrations/0012_queue_policies.sql +87 -0
- package/template/migrations/0013_routing_agents.sql +63 -0
- package/template/migrations/0014_webhook_enrichments.sql +122 -0
- package/template/migrations/0015_queue_owned_ai.sql +55 -0
- package/template/migrations/0016_operator_accounts.sql +67 -0
- package/template/migrations/0017_operator_profiles_and_recovery.sql +33 -0
- package/template/migrations/0018_platform_configuration.sql +382 -0
- package/template/migrations/0019_workflow_authoring_runtime.sql +372 -0
- package/template/migrations/0020_tasks_findings_assistant_budgets.sql +427 -0
- package/template/migrations/0021_abuse_evidence_operations.sql +324 -0
- package/template/migrations/0022_workflow_dispatch_operations.sql +42 -0
- package/template/migrations/0023_component_connection_execution.sql +75 -0
- package/template/migrations/0024_access_runtime_integrity.sql +72 -0
- package/template/migrations/0025_installation_timezone.sql +11 -0
- package/template/migrations/0026_ai_and_egress_execution_controls.sql +49 -0
- package/template/migrations/0027_prompt_and_ai_registry.sql +37 -0
- package/template/migrations/0028_step_attempt_ai_provenance.sql +13 -0
- package/template/migrations/0029_evidence_fetch_transport.sql +5 -0
- package/template/migrations/0030_evidence_dlq_incidents.sql +45 -0
- package/template/migrations/0031_shadow_quality_integrity.sql +7 -0
- package/template/migrations/0032_action_delivery_outbox.sql +55 -0
- package/template/migrations/0033_configuration_and_assistant_drafts.sql +43 -0
- package/template/migrations/0034_installation_integrations.sql +31 -0
- package/template/migrations/0035_workspace_governance.sql +21 -0
- package/template/migrations/0036_published_routing_baseline.sql +15 -0
- package/template/migrations/0037_builtin_phishing_specialist.sql +71 -0
- package/template/migrations/0038_remove_deprecated_enrichment_runtime.sql +228 -0
- package/template/migrations/0039_secure_reporting_channels.sql +45 -0
- package/template/migrations/0040_notification_only_reporting.sql +24 -0
- package/template/migrations/0041_better_auth_credentials.sql +17 -0
- package/template/package.json +47 -0
- package/template/public/_headers +27 -0
- package/template/public/app-icon-192.png +0 -0
- package/template/public/app-icon-512.png +0 -0
- package/template/public/brand-icon.svg +7 -0
- package/template/public/brand-tokens.css +80 -0
- package/template/public/console/auth-shell.js +19340 -0
- package/template/public/customer-brand.js +58 -0
- package/template/public/embed/embed.css +139 -0
- package/template/public/embed/embed.js +408 -0
- package/template/public/embed/index.html +91 -0
- package/template/public/favicon.svg +7 -0
- package/template/public/fonts/Manrope-Variable.ttf +0 -0
- package/template/public/fonts/Newsreader-Italic-Variable.ttf +0 -0
- package/template/public/fonts/Newsreader-Variable.ttf +0 -0
- package/template/public/index.html +123 -0
- package/template/public/logo-primary.svg +7 -0
- package/template/public/logo-reversed.svg +7 -0
- package/template/public/manifest.webmanifest +21 -0
- package/template/public/public-report.js +184 -0
- package/template/public/report/index.html +39 -0
- package/template/public/report/public-report.css +20 -0
- package/template/public/social-card.png +0 -0
- package/template/public/styles.css +1521 -0
- package/template/public/widget.css +80 -0
- package/template/public/widget.js +220 -0
- package/template/reports.config.example.json +38 -0
- package/template/reports.schema.json +89 -0
- package/template/scripts/reports-auth-onboarding.mjs +180 -0
- package/template/scripts/reports-backup.mjs +276 -0
- package/template/scripts/reports-cloudflare-preflight.mjs +152 -0
- package/template/scripts/reports-deploy.mjs +173 -0
- package/template/scripts/reports-plan.mjs +226 -0
- package/template/scripts/reports-restore.mjs +180 -0
- package/template/scripts/reports-secrets.mjs +73 -0
- package/template/scripts/reports-uninstall-plan.mjs +32 -0
- package/template/src/agent-executor.ts +330 -0
- package/template/src/ai-observability.ts +163 -0
- package/template/src/ai-registry-validation.ts +244 -0
- package/template/src/ai-registry.ts +292 -0
- package/template/src/api-cursor.ts +84 -0
- package/template/src/assistant.ts +554 -0
- package/template/src/audit.ts +39 -0
- package/template/src/backup-service.ts +269 -0
- package/template/src/budget-control.ts +127 -0
- package/template/src/component-executor.ts +845 -0
- package/template/src/configuration-registry.ts +550 -0
- package/template/src/connection-egress.ts +430 -0
- package/template/src/connection-oauth.ts +368 -0
- package/template/src/evidence-service.ts +335 -0
- package/template/src/human-tasks.ts +316 -0
- package/template/src/index.ts +4114 -0
- package/template/src/installation-admin.ts +301 -0
- package/template/src/intake-abuse.ts +156 -0
- package/template/src/platform-registry-validation.ts +367 -0
- package/template/src/platform-registry.ts +753 -0
- package/template/src/report-admin.ts +342 -0
- package/template/src/report-ai-quality.ts +390 -0
- package/template/src/report-ai-validation.ts +80 -0
- package/template/src/report-ai.ts +857 -0
- package/template/src/report-auth.ts +292 -0
- package/template/src/report-better-auth.ts +583 -0
- package/template/src/report-context-schema.ts +132 -0
- package/template/src/report-context.ts +126 -0
- package/template/src/report-crypto.ts +142 -0
- package/template/src/report-delivery.ts +579 -0
- package/template/src/report-form-validation.ts +51 -0
- package/template/src/report-governance.ts +510 -0
- package/template/src/report-http.ts +90 -0
- package/template/src/report-operations.ts +685 -0
- package/template/src/report-operator-accounts.ts +810 -0
- package/template/src/report-presence.ts +398 -0
- package/template/src/report-queue-validation.ts +309 -0
- package/template/src/report-queues.ts +478 -0
- package/template/src/report-repository.ts +972 -0
- package/template/src/report-router-agent.ts +328 -0
- package/template/src/report-router-validation.ts +81 -0
- package/template/src/report-routing.ts +178 -0
- package/template/src/report-turnstile.ts +142 -0
- package/template/src/report-types.ts +224 -0
- package/template/src/report-validation.ts +279 -0
- package/template/src/report-workflow-validation.ts +119 -0
- package/template/src/report-workflow.ts +886 -0
- package/template/src/shadow-quality.ts +278 -0
- package/template/src/workflow-actions.ts +782 -0
- package/template/src/workflow-compiler.ts +230 -0
- package/template/src/workflow-dynamic-runtime.ts +613 -0
- package/template/src/workflow-effects.ts +316 -0
- package/template/src/workflow-expressions.ts +191 -0
- package/template/src/workflow-platform-types.ts +121 -0
- package/template/src/workflow-platform-validation.ts +540 -0
- package/template/src/workflow-repository.ts +916 -0
- package/template/src/workflow-runs.ts +686 -0
- package/template/src/workflow-simulator.ts +211 -0
- package/template/src/workspace-branding.ts +128 -0
- package/template/src/workspace-governance.ts +279 -0
- package/template/tsconfig.console.json +23 -0
- package/template/tsconfig.json +22 -0
- package/template/vite.console.config.ts +20 -0
- package/template/worker-configuration.d.ts +65 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ConsoleSession, WorkspaceView } from "./types";
|
|
2
|
+
|
|
3
|
+
export interface NavigationItem {
|
|
4
|
+
view: WorkspaceView;
|
|
5
|
+
label: string;
|
|
6
|
+
icon: string;
|
|
7
|
+
visible: (session: ConsoleSession) => boolean;
|
|
8
|
+
count?: "reports";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface NavigationGroup {
|
|
12
|
+
label: string;
|
|
13
|
+
items: NavigationItem[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const always = (): boolean => true;
|
|
17
|
+
const permission = (name: string) => (session: ConsoleSession): boolean => session.permissions[name] === true;
|
|
18
|
+
const anyPermission = (...names: string[]) => (session: ConsoleSession): boolean => names.some((name) => session.permissions[name] === true);
|
|
19
|
+
|
|
20
|
+
export const navigationGroups: NavigationGroup[] = [
|
|
21
|
+
{
|
|
22
|
+
label: "Work",
|
|
23
|
+
items: [
|
|
24
|
+
{ view: "command", label: "Command centre", icon: "i-home", visible: always },
|
|
25
|
+
{ view: "reports", label: "Reports", icon: "i-inbox", visible: always, count: "reports" },
|
|
26
|
+
{ view: "queues", label: "Queues and policies", icon: "i-queue", visible: permission("readReports") },
|
|
27
|
+
{ view: "appeals", label: "Appeals", icon: "i-appeal", visible: permission("reviewAppeals") },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
label: "Build",
|
|
32
|
+
items: [
|
|
33
|
+
{ view: "workflows", label: "Workflows", icon: "i-workflow", visible: permission("manageWorkflows") },
|
|
34
|
+
{ view: "components", label: "Components", icon: "i-component", visible: permission("manageComponents") },
|
|
35
|
+
{ view: "configuration", label: "Configuration", icon: "i-settings", visible: permission("manageConfiguration") },
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
label: "Observe",
|
|
40
|
+
items: [
|
|
41
|
+
{ view: "assistant", label: "Ask Safest", icon: "i-spark", visible: permission("useAssistant") },
|
|
42
|
+
{ view: "ai-quality", label: "AI quality", icon: "i-quality", visible: permission("decideReports") },
|
|
43
|
+
{ view: "operations", label: "Operations", icon: "i-activity", visible: permission("readOperations") },
|
|
44
|
+
{ view: "analytics", label: "Reports and outcomes", icon: "i-chart", visible: permission("readAnalytics") },
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
label: "Admin",
|
|
49
|
+
items: [
|
|
50
|
+
{ view: "settings", label: "Settings", icon: "i-settings", visible: anyPermission("manageInstallation", "manageConfiguration", "manageIntegrations") },
|
|
51
|
+
{ view: "people", label: "People and roles", icon: "i-people", visible: permission("managePeople") },
|
|
52
|
+
{ view: "profile", label: "My profile", icon: "i-user", visible: (session) => session.actor.type === "operator_user" },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
export function visibleNavigationGroups(session: ConsoleSession): NavigationGroup[] {
|
|
58
|
+
return navigationGroups
|
|
59
|
+
.map((group) => ({ ...group, items: group.items.filter((item) => item.visible(session)) }))
|
|
60
|
+
.filter((group) => group.items.length > 0);
|
|
61
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { isShellSnapshot, shellEvents } from "./events";
|
|
2
|
+
import { profileEvents } from "../profile/events";
|
|
3
|
+
import type { ShellSnapshot } from "./types";
|
|
4
|
+
|
|
5
|
+
const subscribers = new Set<() => void>();
|
|
6
|
+
|
|
7
|
+
let snapshot: ShellSnapshot = {
|
|
8
|
+
session: null,
|
|
9
|
+
currentView: "command",
|
|
10
|
+
reportCount: 0,
|
|
11
|
+
health: { status: "checking", label: "Checking storage" },
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
window.addEventListener(shellEvents.state, (event) => {
|
|
15
|
+
if (!(event instanceof CustomEvent) || !isShellSnapshot(event.detail)) return;
|
|
16
|
+
snapshot = { ...event.detail, reportCount: snapshot.reportCount };
|
|
17
|
+
for (const subscriber of subscribers) subscriber();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
window.addEventListener(profileEvents.updated, (event) => {
|
|
21
|
+
if (!(event instanceof CustomEvent) || !event.detail?.profile || !snapshot.session) return;
|
|
22
|
+
const profile = event.detail.profile;
|
|
23
|
+
if (profile.actor?.type !== "operator_user" || profile.actor.id !== snapshot.session.actor.id) return;
|
|
24
|
+
snapshot = {
|
|
25
|
+
...snapshot,
|
|
26
|
+
session: {
|
|
27
|
+
...snapshot.session,
|
|
28
|
+
...profile,
|
|
29
|
+
avatarUrl: profile.avatarUrl ?? undefined,
|
|
30
|
+
profile: {
|
|
31
|
+
...(snapshot.session.profile ?? {}),
|
|
32
|
+
actorId: profile.actor.id,
|
|
33
|
+
displayName: profile.displayName,
|
|
34
|
+
email: profile.email ?? null,
|
|
35
|
+
avatarUrl: profile.avatarUrl ?? null,
|
|
36
|
+
avatarColor: snapshot.session.profile?.avatarColor ?? "#315d4d",
|
|
37
|
+
roles: profile.roles,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
for (const subscriber of subscribers) subscriber();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export function getShellSnapshot(): ShellSnapshot {
|
|
45
|
+
return snapshot;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function setShellSnapshot(next: ShellSnapshot | ((current: ShellSnapshot) => ShellSnapshot)): void {
|
|
49
|
+
snapshot = typeof next === "function" ? next(snapshot) : next;
|
|
50
|
+
for (const subscriber of subscribers) subscriber();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function subscribeToShell(callback: () => void): () => void {
|
|
54
|
+
subscribers.add(callback);
|
|
55
|
+
return () => subscribers.delete(callback);
|
|
56
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export type WorkspaceView =
|
|
2
|
+
| "command"
|
|
3
|
+
| "reports"
|
|
4
|
+
| "queues"
|
|
5
|
+
| "appeals"
|
|
6
|
+
| "workflows"
|
|
7
|
+
| "components"
|
|
8
|
+
| "configuration"
|
|
9
|
+
| "assistant"
|
|
10
|
+
| "ai-quality"
|
|
11
|
+
| "operations"
|
|
12
|
+
| "analytics"
|
|
13
|
+
| "settings"
|
|
14
|
+
| "people"
|
|
15
|
+
| "profile";
|
|
16
|
+
|
|
17
|
+
export type ShellHealthStatus = "checking" | "ready" | "failed";
|
|
18
|
+
|
|
19
|
+
export interface ConsoleAnalystProfile {
|
|
20
|
+
actorId: string;
|
|
21
|
+
displayName: string;
|
|
22
|
+
email: string | null;
|
|
23
|
+
avatarUrl: string | null;
|
|
24
|
+
avatarColor: string;
|
|
25
|
+
roles: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ConsoleSession {
|
|
29
|
+
actor: {
|
|
30
|
+
type: "operator_user";
|
|
31
|
+
id: string;
|
|
32
|
+
};
|
|
33
|
+
authMethod: "cloudflare_access" | "better_auth";
|
|
34
|
+
displayName: string;
|
|
35
|
+
email?: string;
|
|
36
|
+
avatarUrl?: string;
|
|
37
|
+
roles: string[];
|
|
38
|
+
permissions: Record<string, boolean>;
|
|
39
|
+
queueScope?: string[] | null;
|
|
40
|
+
profile?: ConsoleAnalystProfile;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ShellSnapshot {
|
|
44
|
+
session: ConsoleSession | null;
|
|
45
|
+
configuration?: {
|
|
46
|
+
allowed_action_codes?: string[];
|
|
47
|
+
action_delivery_enabled?: boolean;
|
|
48
|
+
} | null;
|
|
49
|
+
currentView: WorkspaceView;
|
|
50
|
+
reportCount: number;
|
|
51
|
+
health: {
|
|
52
|
+
status: ShellHealthStatus;
|
|
53
|
+
label: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface CustomerBrand {
|
|
58
|
+
organization_name: string;
|
|
59
|
+
logo_url: string;
|
|
60
|
+
logo_alt: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface WorkspaceBrand extends CustomerBrand {
|
|
64
|
+
primary_color: string;
|
|
65
|
+
primary_contrast_color: string;
|
|
66
|
+
accent_color: string;
|
|
67
|
+
background_color: string;
|
|
68
|
+
surface_color: string;
|
|
69
|
+
text_color: string;
|
|
70
|
+
muted_color: string;
|
|
71
|
+
border_color: string;
|
|
72
|
+
body_font_family: string;
|
|
73
|
+
heading_font_family: string;
|
|
74
|
+
font_css_url: string;
|
|
75
|
+
border_radius: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
declare global {
|
|
79
|
+
interface Window {
|
|
80
|
+
SafestBrand?: {
|
|
81
|
+
defaults?: WorkspaceBrand;
|
|
82
|
+
applyBrand(value: Partial<WorkspaceBrand>, options?: { title?: string }): WorkspaceBrand;
|
|
83
|
+
loadBrand(path?: string, options?: { title?: string }): Promise<WorkspaceBrand>;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type FormEvent } from "react";
|
|
2
|
+
import { errorMessage, mutationHeaders, requestJson } from "../lib/http";
|
|
3
|
+
import { announceWorkflowChanged, openWorkflowStudio, workflowEvents } from "./events";
|
|
4
|
+
import { happyPathFixture, workflowTemplate, workflowTemplateOptions, type WorkflowTemplate } from "./templates";
|
|
5
|
+
import type { WorkflowCreateResponse, WorkflowPurpose } from "./types";
|
|
6
|
+
|
|
7
|
+
function slug(value: string): string {
|
|
8
|
+
return value.toLowerCase().trim().replace(/[^a-z0-9]+/gu, "-").replace(/^-|-$/gu, "").slice(0, 100);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function CreateWorkflowDialog() {
|
|
12
|
+
const dialogRef = useRef<HTMLDialogElement>(null);
|
|
13
|
+
const nameRef = useRef<HTMLInputElement>(null);
|
|
14
|
+
const [open, setOpen] = useState(false);
|
|
15
|
+
const [name, setName] = useState("");
|
|
16
|
+
const [key, setKey] = useState("");
|
|
17
|
+
const [keyEdited, setKeyEdited] = useState(false);
|
|
18
|
+
const [description, setDescription] = useState("");
|
|
19
|
+
const [purpose, setPurpose] = useState<WorkflowPurpose>("report");
|
|
20
|
+
const [template, setTemplate] = useState<WorkflowTemplate>("human");
|
|
21
|
+
const [pending, setPending] = useState(false);
|
|
22
|
+
const [error, setError] = useState("");
|
|
23
|
+
|
|
24
|
+
const reset = (): void => {
|
|
25
|
+
setName(""); setKey(""); setKeyEdited(false); setDescription(""); setPurpose("report"); setTemplate("human"); setError("");
|
|
26
|
+
};
|
|
27
|
+
const close = (): void => {
|
|
28
|
+
if (pending) return;
|
|
29
|
+
setOpen(false);
|
|
30
|
+
reset();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
const show = (): void => { reset(); setOpen(true); };
|
|
35
|
+
window.addEventListener(workflowEvents.openCreate, show);
|
|
36
|
+
return () => window.removeEventListener(workflowEvents.openCreate, show);
|
|
37
|
+
}, []);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const dialog = dialogRef.current;
|
|
40
|
+
if (!dialog) return;
|
|
41
|
+
if (open && !dialog.open) { dialog.showModal(); requestAnimationFrame(() => nameRef.current?.focus()); }
|
|
42
|
+
if (!open && dialog.open) dialog.close();
|
|
43
|
+
}, [open]);
|
|
44
|
+
|
|
45
|
+
const submit = async (event: FormEvent<HTMLFormElement>): Promise<void> => {
|
|
46
|
+
event.preventDefault(); setPending(true); setError("");
|
|
47
|
+
try {
|
|
48
|
+
const result = await requestJson<WorkflowCreateResponse>("/v1/admin/workflows", {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: mutationHeaders(),
|
|
51
|
+
body: JSON.stringify({ key, name, description, purpose, graph: workflowTemplate(template, purpose) }),
|
|
52
|
+
});
|
|
53
|
+
let fixtureWarning = "";
|
|
54
|
+
try {
|
|
55
|
+
await requestJson(`/v1/admin/workflows/${encodeURIComponent(result.workflowId)}/fixtures`, {
|
|
56
|
+
method: "POST", headers: mutationHeaders(), body: JSON.stringify(happyPathFixture),
|
|
57
|
+
});
|
|
58
|
+
} catch (cause) {
|
|
59
|
+
fixtureWarning = errorMessage(cause, "The starter fixture could not be created.");
|
|
60
|
+
}
|
|
61
|
+
setPending(false);
|
|
62
|
+
setOpen(false);
|
|
63
|
+
reset();
|
|
64
|
+
announceWorkflowChanged();
|
|
65
|
+
openWorkflowStudio(result.workflowId, fixtureWarning ? `The workflow was created, but its starter fixture was not: ${fixtureWarning}` : "");
|
|
66
|
+
} catch (cause) {
|
|
67
|
+
setError(errorMessage(cause, "The workflow could not be created."));
|
|
68
|
+
setPending(false);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<dialog ref={dialogRef} className="editor-dialog workflow-create-dialog" aria-labelledby="workflow-create-title" onCancel={(event) => { event.preventDefault(); close(); }} data-react-slice="workflow-create-dialog">
|
|
74
|
+
<div className="detail-head"><div><h2 id="workflow-create-title">Create a workflow</h2><p>Start from a template, then adjust every step.</p></div><button className="icon-button" type="button" aria-label="Close" disabled={pending} onClick={close}>×</button></div>
|
|
75
|
+
<form className="editor-form" onSubmit={(event) => void submit(event)}>
|
|
76
|
+
<div className="form-grid">
|
|
77
|
+
<label>Name<input ref={nameRef} maxLength={160} required disabled={pending} value={name} onChange={(event) => { const value = event.currentTarget.value; setName(value); if (!keyEdited) setKey(slug(value)); }} /></label>
|
|
78
|
+
<label>Key<input maxLength={100} pattern="[a-z0-9]+(?:-[a-z0-9]+)*" required disabled={pending} value={key} onChange={(event) => { setKeyEdited(true); setKey(event.currentTarget.value); }} /></label>
|
|
79
|
+
<label className="wide-field">Description<textarea maxLength={2000} disabled={pending} value={description} onChange={(event) => setDescription(event.currentTarget.value)} /></label>
|
|
80
|
+
<label>Purpose<select disabled={pending} value={purpose} onChange={(event) => setPurpose(event.currentTarget.value as WorkflowPurpose)}><option value="report">Report handling</option><option value="appeal">Appeal review</option><option value="quality">Quality review</option><option value="operations">Operations recovery</option></select></label>
|
|
81
|
+
</div>
|
|
82
|
+
<fieldset><legend>Starting template</legend>{workflowTemplateOptions.map((option) => <label className="template-choice" key={option.value}><input type="radio" name="workflow-template" value={option.value} checked={template === option.value} disabled={pending} onChange={() => setTemplate(option.value)} /><span><strong>{option.name}</strong><small>{option.detail}</small></span></label>)}</fieldset>
|
|
83
|
+
<div className="editor-footer"><p className="error" role="alert">{error}</p><div><button className="secondary" type="button" disabled={pending} onClick={close}>Cancel</button><button className="primary" type="submit" disabled={pending}>{pending ? "Creating…" : "Create workflow"}</button></div></div>
|
|
84
|
+
</form>
|
|
85
|
+
</dialog>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readable } from "../lib/format";
|
|
2
|
+
import { canvasSize, nodePosition, nodeVisualClass, workflowEdgeGeometry, workflowEdgeLabel, workflowEdgePath, workflowEdgeTone } from "./graph";
|
|
3
|
+
import type { WorkflowGraph } from "./types";
|
|
4
|
+
|
|
5
|
+
export function WorkflowCanvas({ graph, selectedNodeId, onSelect }: { graph: WorkflowGraph; selectedNodeId: string | null; onSelect: (nodeId: string) => void }) {
|
|
6
|
+
const { width, height, positions } = canvasSize(graph);
|
|
7
|
+
const groups = new Map<string, string[]>();
|
|
8
|
+
for (const edge of graph.edges) {
|
|
9
|
+
const key = `${edge.from.node_id}:${edge.to.node_id}`;
|
|
10
|
+
groups.set(key, [...(groups.get(key) ?? []), edge.id]);
|
|
11
|
+
}
|
|
12
|
+
return (
|
|
13
|
+
<div className="workflow-canvas" style={{ minWidth: width, minHeight: height }}>
|
|
14
|
+
<svg className="workflow-edge-layer" viewBox={`0 0 ${width} ${height}`} width={width} height={height} aria-hidden="true">
|
|
15
|
+
<defs>
|
|
16
|
+
{(["standard", "warning", "exception"] as const).map((tone) => <marker id={`workflow-arrow-${tone}`} markerWidth="13" markerHeight="11" refX="12" refY="5.5" orient="auto" markerUnits="userSpaceOnUse" key={tone}><path d="M 0 0 L 13 5.5 L 0 11 Z" className={`workflow-arrow-head ${tone}`} /></marker>)}
|
|
17
|
+
</defs>
|
|
18
|
+
{graph.edges.map((edge) => {
|
|
19
|
+
const from = positions.get(edge.from.node_id);
|
|
20
|
+
const to = positions.get(edge.to.node_id);
|
|
21
|
+
if (!from || !to) return null;
|
|
22
|
+
const siblings = groups.get(`${edge.from.node_id}:${edge.to.node_id}`) ?? [edge.id];
|
|
23
|
+
const lane = siblings.indexOf(edge.id) - (siblings.length - 1) / 2;
|
|
24
|
+
const geometry = workflowEdgeGeometry(from, to, lane);
|
|
25
|
+
const tone = workflowEdgeTone(edge);
|
|
26
|
+
const label = workflowEdgeLabel(edge);
|
|
27
|
+
const labelLength = Math.hypot(geometry.end.x - geometry.start.x, geometry.end.y - geometry.start.y);
|
|
28
|
+
return <g className={`workflow-edge-group ${tone}`} key={edge.id}>
|
|
29
|
+
<path className="workflow-edge-path" d={workflowEdgePath(geometry, lane)} markerEnd={`url(#workflow-arrow-${tone})`}><title>{edge.label || `${edge.from.port} → ${edge.to.port}`}</title></path>
|
|
30
|
+
{label && labelLength >= Math.max(70, label.length * 5.2 + 18) ? <text className="workflow-edge-label" x={(geometry.start.x + geometry.end.x) / 2} y={(geometry.start.y + geometry.end.y) / 2 - 7} textAnchor="middle">{label}</text> : null}
|
|
31
|
+
</g>;
|
|
32
|
+
})}
|
|
33
|
+
</svg>
|
|
34
|
+
{graph.nodes.map((node, index) => {
|
|
35
|
+
const position = positions.get(node.id) ?? nodePosition(node, index);
|
|
36
|
+
return <button className={`canvas-node ${nodeVisualClass(node.kind)}${selectedNodeId === node.id ? " selected" : ""}`} type="button" data-kind={node.kind} aria-pressed={selectedNodeId === node.id} style={{ left: position.x, top: position.y }} onClick={() => onSelect(node.id)} key={node.id}>
|
|
37
|
+
<span className="canvas-node-kind"><i />{readable(node.kind)}</span><h4>{node.name}</h4><p>{node.description || (node.kind === "action" ? "Customer-side effect" : "Typed workflow step")}</p>
|
|
38
|
+
</button>;
|
|
39
|
+
})}
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|