@contractspec/example.integration-hub 3.7.5 → 3.7.7

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.
@@ -495,6 +495,158 @@ var CreateIntegrationContract = defineCommand2({
495
495
  ]
496
496
  }
497
497
  });
498
+ // src/mcp-example.ts
499
+ import { randomUUID } from "node:crypto";
500
+ import {
501
+ createMcpToolsets
502
+ } from "@contractspec/lib.ai-agent/tools/mcp-client";
503
+ var DEFAULT_STDIO_ARGS = [
504
+ "-y",
505
+ "@modelcontextprotocol/server-filesystem",
506
+ "."
507
+ ];
508
+ async function runIntegrationHubMcpExampleFromEnv() {
509
+ const mode = resolveMode();
510
+ const transport = resolveTransport();
511
+ const config = buildMcpConfigFromEnv();
512
+ const toolset = await createMcpToolsets([config], {
513
+ onNameCollision: "error"
514
+ });
515
+ try {
516
+ const toolNames = Object.keys(toolset.tools).sort();
517
+ const output = {
518
+ mode,
519
+ server: {
520
+ name: config.name,
521
+ transport
522
+ },
523
+ authMethod: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_AUTH_METHOD,
524
+ apiVersion: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_API_VERSION,
525
+ tools: toolNames
526
+ };
527
+ if (mode === "call") {
528
+ const toolName = requireEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_NAME");
529
+ const toolArgs = parseRecordEnvOrDefault("CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_ARGS_JSON", {});
530
+ const tool = toolset.tools[toolName];
531
+ if (!tool?.execute) {
532
+ throw new Error(`Tool "${toolName}" was not found. Available tools: ${toolNames.join(", ")}`);
533
+ }
534
+ const toolOutput = await tool.execute(toolArgs, {
535
+ toolCallId: `integration-hub-${randomUUID()}`,
536
+ messages: []
537
+ });
538
+ output.toolCall = {
539
+ name: toolName,
540
+ args: toolArgs,
541
+ output: toolOutput
542
+ };
543
+ }
544
+ return output;
545
+ } finally {
546
+ await toolset.cleanup().catch(() => {
547
+ return;
548
+ });
549
+ }
550
+ }
551
+ function buildMcpConfigFromEnv() {
552
+ const name = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_NAME ?? "filesystem";
553
+ const transport = resolveTransport();
554
+ const toolPrefix = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_PREFIX;
555
+ if (transport === "stdio") {
556
+ return {
557
+ name,
558
+ transport,
559
+ command: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_COMMAND ?? "npx",
560
+ args: parseStringArrayEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_ARGS_JSON", DEFAULT_STDIO_ARGS),
561
+ toolPrefix
562
+ };
563
+ }
564
+ const accessToken = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_ACCESS_TOKEN;
565
+ const accessTokenEnvVar = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_ACCESS_TOKEN_ENV;
566
+ const mcpTransport = transport === "webhook" || transport === "http" ? "http" : "sse";
567
+ return {
568
+ name,
569
+ transport: mcpTransport,
570
+ url: requireEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_URL"),
571
+ headers: parseStringRecordEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_HEADERS_JSON"),
572
+ accessToken,
573
+ accessTokenEnvVar,
574
+ toolPrefix
575
+ };
576
+ }
577
+ function resolveMode() {
578
+ const rawMode = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_MODE?.toLowerCase() ?? "list";
579
+ if (rawMode === "list" || rawMode === "call") {
580
+ return rawMode;
581
+ }
582
+ throw new Error(`Unsupported CONTRACTSPEC_INTEGRATION_HUB_MCP_MODE: ${rawMode}. Use "list" or "call".`);
583
+ }
584
+ function resolveTransport() {
585
+ const rawTransport = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_TRANSPORT?.toLowerCase() ?? "stdio";
586
+ if (rawTransport === "stdio" || rawTransport === "http" || rawTransport === "sse" || rawTransport === "webhook") {
587
+ return rawTransport;
588
+ }
589
+ throw new Error(`Unsupported CONTRACTSPEC_INTEGRATION_HUB_MCP_TRANSPORT: ${rawTransport}. Use "stdio", "http", "sse", or "webhook".`);
590
+ }
591
+ function parseStringArrayEnv(key, fallback) {
592
+ const raw = process.env[key];
593
+ if (!raw) {
594
+ return fallback;
595
+ }
596
+ const parsed = parseJsonEnv(key);
597
+ if (!Array.isArray(parsed) || parsed.some((value) => typeof value !== "string")) {
598
+ throw new Error(`${key} must be a JSON string array.`);
599
+ }
600
+ return parsed;
601
+ }
602
+ function parseRecordEnv(key) {
603
+ const raw = process.env[key];
604
+ if (!raw) {
605
+ return;
606
+ }
607
+ const parsed = parseJsonEnv(key);
608
+ if (!isRecord(parsed)) {
609
+ throw new Error(`${key} must be a JSON object.`);
610
+ }
611
+ return parsed;
612
+ }
613
+ function parseRecordEnvOrDefault(key, fallback) {
614
+ return parseRecordEnv(key) ?? fallback;
615
+ }
616
+ function parseStringRecordEnv(key) {
617
+ const parsed = parseRecordEnv(key);
618
+ if (!parsed) {
619
+ return;
620
+ }
621
+ const entries = Object.entries(parsed);
622
+ const invalidEntry = entries.find(([, value]) => typeof value !== "string");
623
+ if (invalidEntry) {
624
+ throw new Error(`${key} must contain only string values.`);
625
+ }
626
+ return Object.fromEntries(entries);
627
+ }
628
+ function parseJsonEnv(key) {
629
+ const raw = process.env[key];
630
+ if (!raw) {
631
+ return;
632
+ }
633
+ try {
634
+ return JSON.parse(raw);
635
+ } catch {
636
+ throw new Error(`${key} contains invalid JSON.`);
637
+ }
638
+ }
639
+ function requireEnv(key) {
640
+ const value = process.env[key];
641
+ if (!value) {
642
+ throw new Error(`Missing required env var: ${key}`);
643
+ }
644
+ return value;
645
+ }
646
+ function isRecord(value) {
647
+ return typeof value === "object" && value !== null && !Array.isArray(value);
648
+ }
649
+
498
650
  // src/sync/sync.enum.ts
499
651
  import { defineEnum as defineEnum3 } from "@contractspec/lib.schema";
500
652
  var SyncDirectionEnum = defineEnum3("SyncDirection", [
@@ -810,277 +962,189 @@ var ListSyncRunsContract = defineQuery({
810
962
  ]
811
963
  }
812
964
  });
813
- // src/ui/renderers/integration.markdown.ts
814
- var mockIntegrations = [
815
- {
816
- id: "int-1",
817
- name: "Salesforce",
818
- type: "CRM",
819
- status: "ACTIVE",
820
- connectionCount: 3
821
- },
822
- {
823
- id: "int-2",
824
- name: "HubSpot",
825
- type: "MARKETING",
826
- status: "ACTIVE",
827
- connectionCount: 2
828
- },
829
- {
830
- id: "int-3",
831
- name: "Stripe",
832
- type: "PAYMENT",
833
- status: "ACTIVE",
834
- connectionCount: 1
835
- },
836
- {
837
- id: "int-4",
838
- name: "Slack",
839
- type: "COMMUNICATION",
840
- status: "INACTIVE",
841
- connectionCount: 0
842
- },
843
- {
844
- id: "int-5",
845
- name: "Google Sheets",
846
- type: "DATA",
847
- status: "ACTIVE",
848
- connectionCount: 5
849
- },
850
- {
851
- id: "int-6",
852
- name: "PostHog",
853
- type: "ANALYTICS",
854
- status: "ACTIVE",
855
- connectionCount: 1
965
+ // src/sync-engine/index.ts
966
+ class BasicFieldTransformer {
967
+ transform(value, expression) {
968
+ try {
969
+ if (expression.startsWith("uppercase")) {
970
+ return typeof value === "string" ? value.toUpperCase() : value;
971
+ }
972
+ if (expression.startsWith("lowercase")) {
973
+ return typeof value === "string" ? value.toLowerCase() : value;
974
+ }
975
+ if (expression.startsWith("trim")) {
976
+ return typeof value === "string" ? value.trim() : value;
977
+ }
978
+ if (expression.startsWith("default:")) {
979
+ const defaultVal = expression.replace("default:", "");
980
+ return value ?? JSON.parse(defaultVal);
981
+ }
982
+ if (expression.startsWith("concat:")) {
983
+ const separator = expression.replace("concat:", "") || " ";
984
+ if (Array.isArray(value)) {
985
+ return value.join(separator);
986
+ }
987
+ return value;
988
+ }
989
+ if (expression.startsWith("split:")) {
990
+ const separator = expression.replace("split:", "") || ",";
991
+ if (typeof value === "string") {
992
+ return value.split(separator);
993
+ }
994
+ return value;
995
+ }
996
+ if (expression.startsWith("number")) {
997
+ return Number(value);
998
+ }
999
+ if (expression.startsWith("boolean")) {
1000
+ return Boolean(value);
1001
+ }
1002
+ if (expression.startsWith("string")) {
1003
+ return String(value);
1004
+ }
1005
+ return value;
1006
+ } catch {
1007
+ return value;
1008
+ }
856
1009
  }
857
- ];
858
- var mockConnections = [
859
- {
860
- id: "conn-1",
861
- integrationId: "int-1",
862
- name: "Production Salesforce",
863
- status: "CONNECTED",
864
- lastSyncAt: "2024-01-16T10:00:00Z"
865
- },
866
- {
867
- id: "conn-2",
868
- integrationId: "int-1",
869
- name: "Sandbox Salesforce",
870
- status: "CONNECTED",
871
- lastSyncAt: "2024-01-15T14:00:00Z"
872
- },
873
- {
874
- id: "conn-3",
875
- integrationId: "int-2",
876
- name: "Marketing HubSpot",
877
- status: "CONNECTED",
878
- lastSyncAt: "2024-01-16T08:00:00Z"
879
- },
880
- {
881
- id: "conn-4",
882
- integrationId: "int-3",
883
- name: "Stripe Live",
884
- status: "CONNECTED",
885
- lastSyncAt: "2024-01-16T12:00:00Z"
886
- },
887
- {
888
- id: "conn-5",
889
- integrationId: "int-5",
890
- name: "Analytics Sheet",
891
- status: "ERROR",
892
- lastSyncAt: "2024-01-14T09:00:00Z",
893
- error: "Authentication expired"
894
- },
895
- {
896
- id: "conn-6",
897
- integrationId: "int-6",
898
- name: "PostHog Workspace",
899
- status: "CONNECTED",
900
- lastSyncAt: "2024-01-16T11:45:00Z"
1010
+ }
1011
+
1012
+ class BasicSyncEngine {
1013
+ transformer;
1014
+ constructor(transformer) {
1015
+ this.transformer = transformer ?? new BasicFieldTransformer;
901
1016
  }
902
- ];
903
- var mockSyncConfigs = [
904
- {
905
- id: "sync-1",
906
- connectionId: "conn-1",
907
- name: "Contacts Sync",
908
- frequency: "HOURLY",
909
- lastRunAt: "2024-01-16T10:00:00Z",
910
- status: "SUCCESS",
911
- recordsSynced: 1250
912
- },
913
- {
914
- id: "sync-2",
915
- connectionId: "conn-1",
916
- name: "Opportunities Sync",
917
- frequency: "DAILY",
918
- lastRunAt: "2024-01-16T00:00:00Z",
919
- status: "SUCCESS",
920
- recordsSynced: 340
921
- },
922
- {
923
- id: "sync-3",
924
- connectionId: "conn-3",
925
- name: "Orders Sync",
926
- frequency: "REALTIME",
927
- lastRunAt: "2024-01-16T12:30:00Z",
928
- status: "SUCCESS",
929
- recordsSynced: 89
930
- },
931
- {
932
- id: "sync-4",
933
- connectionId: "conn-5",
934
- name: "Metrics Export",
935
- frequency: "DAILY",
936
- lastRunAt: "2024-01-14T09:00:00Z",
937
- status: "FAILED",
938
- recordsSynced: 0
1017
+ async sync(_context) {
1018
+ const result = {
1019
+ success: true,
1020
+ recordsProcessed: 0,
1021
+ recordsCreated: 0,
1022
+ recordsUpdated: 0,
1023
+ recordsDeleted: 0,
1024
+ recordsFailed: 0,
1025
+ recordsSkipped: 0,
1026
+ errors: []
1027
+ };
1028
+ return result;
939
1029
  }
940
- ];
941
- var integrationDashboardMarkdownRenderer = {
942
- target: "markdown",
943
- render: async (desc) => {
944
- if (desc.source.type !== "component" || desc.source.componentKey !== "IntegrationDashboard") {
945
- throw new Error("integrationDashboardMarkdownRenderer: not IntegrationDashboard");
946
- }
947
- const integrations = mockIntegrations;
948
- const connections = mockConnections;
949
- const syncs = mockSyncConfigs;
950
- const activeIntegrations = integrations.filter((i) => i.status === "ACTIVE");
951
- const connectedConnections = connections.filter((c) => c.status === "CONNECTED");
952
- const errorConnections = connections.filter((c) => c.status === "ERROR");
953
- const successfulSyncs = syncs.filter((s) => s.status === "SUCCESS");
954
- const totalRecordsSynced = successfulSyncs.reduce((sum, s) => sum + s.recordsSynced, 0);
955
- const lines = [
956
- "# Integration Hub",
957
- "",
958
- "> Connect and sync data with external services",
959
- "",
960
- "## Overview",
961
- "",
962
- "| Metric | Value |",
963
- "|--------|-------|",
964
- `| Active Integrations | ${activeIntegrations.length} |`,
965
- `| Connected Services | ${connectedConnections.length} |`,
966
- `| Error Connections | ${errorConnections.length} |`,
967
- `| Sync Configs | ${syncs.length} |`,
968
- `| Records Synced (24h) | ${totalRecordsSynced.toLocaleString()} |`,
969
- "",
970
- "## Integrations",
971
- "",
972
- "| Name | Type | Connections | Status |",
973
- "|------|------|-------------|--------|"
974
- ];
975
- for (const integration of integrations) {
976
- const statusIcon = integration.status === "ACTIVE" ? "\uD83D\uDFE2" : "⚫";
977
- lines.push(`| ${integration.name} | ${integration.type} | ${integration.connectionCount} | ${statusIcon} ${integration.status} |`);
978
- }
979
- lines.push("");
980
- lines.push("## Recent Sync Activity");
981
- lines.push("");
982
- lines.push("| Sync | Frequency | Last Run | Records | Status |");
983
- lines.push("|------|-----------|----------|---------|--------|");
984
- for (const sync of syncs) {
985
- const lastRun = new Date(sync.lastRunAt).toLocaleString();
986
- const statusIcon = sync.status === "SUCCESS" ? "✅" : "❌";
987
- lines.push(`| ${sync.name} | ${sync.frequency} | ${lastRun} | ${sync.recordsSynced} | ${statusIcon} ${sync.status} |`);
988
- }
989
- if (errorConnections.length > 0) {
990
- lines.push("");
991
- lines.push("## ⚠️ Connections with Errors");
992
- lines.push("");
993
- for (const conn of errorConnections) {
994
- const integration = integrations.find((i) => i.id === conn.integrationId);
995
- lines.push(`- **${conn.name}** (${integration?.name ?? "Unknown"}): ${conn.error ?? "Unknown error"}`);
1030
+ transformRecord(sourceRecord, mappings, _context) {
1031
+ const targetData = {};
1032
+ for (const mapping of mappings) {
1033
+ let value;
1034
+ let sourceValue;
1035
+ switch (mapping.mappingType) {
1036
+ case "DIRECT":
1037
+ value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1038
+ break;
1039
+ case "TRANSFORM":
1040
+ sourceValue = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1041
+ value = mapping.transformExpression ? this.transformer.transform(sourceValue, mapping.transformExpression) : sourceValue;
1042
+ break;
1043
+ case "CONSTANT":
1044
+ value = mapping.constantValue;
1045
+ break;
1046
+ case "LOOKUP":
1047
+ value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1048
+ break;
1049
+ case "COMPUTED":
1050
+ value = mapping.transformExpression ? this.evaluateComputed(sourceRecord.data, mapping.transformExpression) : null;
1051
+ break;
1052
+ default:
1053
+ value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1054
+ }
1055
+ if (value === undefined || value === null) {
1056
+ value = mapping.defaultValue;
996
1057
  }
1058
+ this.setNestedValue(targetData, mapping.targetField, value);
997
1059
  }
998
1060
  return {
999
- mimeType: "text/markdown",
1000
- body: lines.join(`
1001
- `)
1061
+ id: sourceRecord.id,
1062
+ data: targetData
1002
1063
  };
1003
1064
  }
1004
- };
1005
- var connectionListMarkdownRenderer = {
1006
- target: "markdown",
1007
- render: async (desc) => {
1008
- if (desc.source.type !== "component" || desc.source.componentKey !== "ConnectionList") {
1009
- throw new Error("connectionListMarkdownRenderer: not ConnectionList");
1010
- }
1011
- const connections = mockConnections;
1012
- const integrations = mockIntegrations;
1013
- const lines = [
1014
- "# Connections",
1015
- "",
1016
- "> Manage connections to external services",
1017
- ""
1018
- ];
1019
- for (const integration of integrations) {
1020
- const intConnections = connections.filter((c) => c.integrationId === integration.id);
1021
- if (intConnections.length === 0)
1022
- continue;
1023
- lines.push(`## ${integration.name}`);
1024
- lines.push("");
1025
- lines.push("| Connection | Status | Last Sync |");
1026
- lines.push("|------------|--------|-----------|");
1027
- for (const conn of intConnections) {
1028
- const lastSync = new Date(conn.lastSyncAt).toLocaleString();
1029
- const statusIcon = conn.status === "CONNECTED" ? "\uD83D\uDFE2" : conn.status === "ERROR" ? "\uD83D\uDD34" : "⚫";
1030
- lines.push(`| ${conn.name} | ${statusIcon} ${conn.status} | ${lastSync} |`);
1065
+ validateRecord(record, mappings) {
1066
+ const errors = [];
1067
+ for (const mapping of mappings) {
1068
+ if (mapping.isRequired) {
1069
+ const value = this.getNestedValue(record.data, mapping.targetField);
1070
+ if (value === undefined || value === null) {
1071
+ errors.push({
1072
+ recordId: record.id,
1073
+ field: mapping.targetField,
1074
+ message: `Required field ${mapping.targetField} is missing`,
1075
+ code: "REQUIRED_FIELD_MISSING"
1076
+ });
1077
+ }
1031
1078
  }
1032
- lines.push("");
1033
1079
  }
1034
1080
  return {
1035
- mimeType: "text/markdown",
1036
- body: lines.join(`
1037
- `)
1081
+ valid: errors.length === 0,
1082
+ errors
1038
1083
  };
1039
1084
  }
1040
- };
1041
- var syncConfigMarkdownRenderer = {
1042
- target: "markdown",
1043
- render: async (desc) => {
1044
- if (desc.source.type !== "component" || desc.source.componentKey !== "SyncConfigEditor") {
1045
- throw new Error("syncConfigMarkdownRenderer: not SyncConfigEditor");
1046
- }
1047
- const syncs = mockSyncConfigs;
1048
- const connections = mockConnections;
1049
- const lines = [
1050
- "# Sync Configurations",
1051
- "",
1052
- "> Configure automated data synchronization",
1053
- ""
1054
- ];
1055
- for (const sync of syncs) {
1056
- const connection = connections.find((c) => c.id === sync.connectionId);
1057
- const statusIcon = sync.status === "SUCCESS" ? "✅" : "❌";
1058
- lines.push(`## ${sync.name}`);
1059
- lines.push("");
1060
- lines.push(`**Connection:** ${connection?.name ?? "Unknown"}`);
1061
- lines.push(`**Frequency:** ${sync.frequency}`);
1062
- lines.push(`**Status:** ${statusIcon} ${sync.status}`);
1063
- lines.push(`**Last Run:** ${new Date(sync.lastRunAt).toLocaleString()}`);
1064
- lines.push(`**Records Synced:** ${sync.recordsSynced.toLocaleString()}`);
1065
- lines.push("");
1085
+ getNestedValue(obj, path) {
1086
+ const parts = path.split(".");
1087
+ let current = obj;
1088
+ for (const part of parts) {
1089
+ if (current === null || current === undefined) {
1090
+ return;
1091
+ }
1092
+ current = current[part];
1066
1093
  }
1067
- lines.push("## Frequency Options");
1068
- lines.push("");
1069
- lines.push("- **REALTIME**: Sync on every change");
1070
- lines.push("- **HOURLY**: Sync every hour");
1071
- lines.push("- **DAILY**: Sync once per day");
1072
- lines.push("- **WEEKLY**: Sync once per week");
1073
- lines.push("- **MANUAL**: Sync only when triggered");
1074
- return {
1075
- mimeType: "text/markdown",
1076
- body: lines.join(`
1077
- `)
1078
- };
1094
+ return current;
1079
1095
  }
1080
- };
1096
+ setNestedValue(obj, path, value) {
1097
+ const parts = path.split(".");
1098
+ let current = obj;
1099
+ for (let i = 0;i < parts.length - 1; i++) {
1100
+ const part = parts[i];
1101
+ if (part === undefined)
1102
+ continue;
1103
+ if (!(part in current)) {
1104
+ current[part] = {};
1105
+ }
1106
+ current = current[part];
1107
+ }
1108
+ const lastPart = parts[parts.length - 1];
1109
+ if (lastPart !== undefined) {
1110
+ current[lastPart] = value;
1111
+ }
1112
+ }
1113
+ evaluateComputed(data, expression) {
1114
+ try {
1115
+ const result = expression.replace(/\$\{([^}]+)\}/g, (_, path) => {
1116
+ const value = this.getNestedValue(data, path);
1117
+ return String(value ?? "");
1118
+ });
1119
+ return result;
1120
+ } catch {
1121
+ return null;
1122
+ }
1123
+ }
1124
+ }
1125
+ function createSyncEngine(transformer) {
1126
+ return new BasicSyncEngine(transformer);
1127
+ }
1128
+ function computeChecksum(data) {
1129
+ const str = JSON.stringify(data, Object.keys(data).sort());
1130
+ let hash = 0;
1131
+ for (let i = 0;i < str.length; i++) {
1132
+ const char = str.charCodeAt(i);
1133
+ hash = (hash << 5) - hash + char;
1134
+ hash = hash & hash;
1135
+ }
1136
+ return hash.toString(16);
1137
+ }
1138
+ function hasChanges(sourceChecksum, targetChecksum) {
1139
+ if (!sourceChecksum || !targetChecksum) {
1140
+ return true;
1141
+ }
1142
+ return sourceChecksum !== targetChecksum;
1143
+ }
1144
+
1081
1145
  // src/ui/hooks/useIntegrationData.ts
1082
- import { useCallback, useEffect, useState } from "react";
1083
1146
  import { useTemplateRuntime } from "@contractspec/lib.example-shared-ui";
1147
+ import { useCallback, useEffect, useState } from "react";
1084
1148
  "use client";
1085
1149
  function useIntegrationData(projectId = "local-project") {
1086
1150
  const { handlers } = useTemplateRuntime();
@@ -1130,6 +1194,9 @@ function useIntegrationData(projectId = "local-project") {
1130
1194
  };
1131
1195
  }
1132
1196
 
1197
+ // src/ui/hooks/index.ts
1198
+ "use client";
1199
+
1133
1200
  // src/ui/IntegrationHubChat.tsx
1134
1201
  import { ChatWithSidebar } from "@contractspec/module.ai-chat";
1135
1202
  import { jsxDEV } from "react/jsx-dev-runtime";
@@ -1163,7 +1230,6 @@ function IntegrationHubChat({
1163
1230
  }
1164
1231
 
1165
1232
  // src/ui/IntegrationDashboard.tsx
1166
- import { useState as useState2 } from "react";
1167
1233
  import {
1168
1234
  Button,
1169
1235
  ErrorState,
@@ -1171,6 +1237,7 @@ import {
1171
1237
  StatCard,
1172
1238
  StatCardGroup
1173
1239
  } from "@contractspec/lib.design-system";
1240
+ import { useState as useState2 } from "react";
1174
1241
  import { jsxDEV as jsxDEV2 } from "react/jsx-dev-runtime";
1175
1242
  "use client";
1176
1243
  var STATUS_COLORS = {
@@ -1227,7 +1294,7 @@ function IntegrationDashboard() {
1227
1294
  className: "flex items-center justify-between",
1228
1295
  children: [
1229
1296
  /* @__PURE__ */ jsxDEV2("h2", {
1230
- className: "text-2xl font-bold",
1297
+ className: "font-bold text-2xl",
1231
1298
  children: "Integration Hub"
1232
1299
  }, undefined, false, undefined, this),
1233
1300
  /* @__PURE__ */ jsxDEV2(Button, {
@@ -1262,14 +1329,14 @@ function IntegrationDashboard() {
1262
1329
  ]
1263
1330
  }, undefined, true, undefined, this),
1264
1331
  /* @__PURE__ */ jsxDEV2("nav", {
1265
- className: "bg-muted flex gap-1 rounded-lg p-1",
1332
+ className: "flex gap-1 rounded-lg bg-muted p-1",
1266
1333
  role: "tablist",
1267
1334
  children: tabs.map((tab) => /* @__PURE__ */ jsxDEV2(Button, {
1268
1335
  type: "button",
1269
1336
  role: "tab",
1270
1337
  "aria-selected": activeTab === tab.id,
1271
1338
  onClick: () => setActiveTab(tab.id),
1272
- className: `flex flex-1 items-center justify-center gap-2 rounded-md px-4 py-2 text-sm font-medium transition-colors ${activeTab === tab.id ? "bg-background text-foreground shadow-sm" : "text-muted-foreground hover:text-foreground"}`,
1339
+ className: `flex flex-1 items-center justify-center gap-2 rounded-md px-4 py-2 font-medium text-sm transition-colors ${activeTab === tab.id ? "bg-background text-foreground shadow-sm" : "text-muted-foreground hover:text-foreground"}`,
1273
1340
  children: [
1274
1341
  /* @__PURE__ */ jsxDEV2("span", {
1275
1342
  children: tab.icon
@@ -1286,7 +1353,7 @@ function IntegrationDashboard() {
1286
1353
  className: "grid gap-4 sm:grid-cols-2 lg:grid-cols-3",
1287
1354
  children: [
1288
1355
  integrations.map((integration) => /* @__PURE__ */ jsxDEV2("div", {
1289
- className: "border-border bg-card hover:bg-muted/50 cursor-pointer rounded-lg border p-4 transition-colors",
1356
+ className: "cursor-pointer rounded-lg border border-border bg-card p-4 transition-colors hover:bg-muted/50",
1290
1357
  children: [
1291
1358
  /* @__PURE__ */ jsxDEV2("div", {
1292
1359
  className: "mb-3 flex items-center gap-3",
@@ -1313,7 +1380,7 @@ function IntegrationDashboard() {
1313
1380
  className: "flex items-center justify-between",
1314
1381
  children: [
1315
1382
  /* @__PURE__ */ jsxDEV2("span", {
1316
- className: `inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${STATUS_COLORS[integration.status] ?? ""}`,
1383
+ className: `inline-flex rounded-full px-2 py-0.5 font-medium text-xs ${STATUS_COLORS[integration.status] ?? ""}`,
1317
1384
  children: integration.status
1318
1385
  }, undefined, false, undefined, this),
1319
1386
  /* @__PURE__ */ jsxDEV2("span", {
@@ -1325,37 +1392,37 @@ function IntegrationDashboard() {
1325
1392
  ]
1326
1393
  }, integration.id, true, undefined, this)),
1327
1394
  integrations.length === 0 && /* @__PURE__ */ jsxDEV2("div", {
1328
- className: "text-muted-foreground col-span-full flex h-64 items-center justify-center",
1395
+ className: "col-span-full flex h-64 items-center justify-center text-muted-foreground",
1329
1396
  children: "No integrations configured"
1330
1397
  }, undefined, false, undefined, this)
1331
1398
  ]
1332
1399
  }, undefined, true, undefined, this),
1333
1400
  activeTab === "connections" && /* @__PURE__ */ jsxDEV2("div", {
1334
- className: "border-border rounded-lg border",
1401
+ className: "rounded-lg border border-border",
1335
1402
  children: /* @__PURE__ */ jsxDEV2("table", {
1336
1403
  className: "w-full",
1337
1404
  children: [
1338
1405
  /* @__PURE__ */ jsxDEV2("thead", {
1339
- className: "border-border bg-muted/30 border-b",
1406
+ className: "border-border border-b bg-muted/30",
1340
1407
  children: /* @__PURE__ */ jsxDEV2("tr", {
1341
1408
  children: [
1342
1409
  /* @__PURE__ */ jsxDEV2("th", {
1343
- className: "px-4 py-3 text-left text-sm font-medium",
1410
+ className: "px-4 py-3 text-left font-medium text-sm",
1344
1411
  children: "Connection"
1345
1412
  }, undefined, false, undefined, this),
1346
1413
  /* @__PURE__ */ jsxDEV2("th", {
1347
- className: "px-4 py-3 text-left text-sm font-medium",
1414
+ className: "px-4 py-3 text-left font-medium text-sm",
1348
1415
  children: "Status"
1349
1416
  }, undefined, false, undefined, this),
1350
1417
  /* @__PURE__ */ jsxDEV2("th", {
1351
- className: "px-4 py-3 text-left text-sm font-medium",
1418
+ className: "px-4 py-3 text-left font-medium text-sm",
1352
1419
  children: "Last Sync"
1353
1420
  }, undefined, false, undefined, this)
1354
1421
  ]
1355
1422
  }, undefined, true, undefined, this)
1356
1423
  }, undefined, false, undefined, this),
1357
1424
  /* @__PURE__ */ jsxDEV2("tbody", {
1358
- className: "divide-border divide-y",
1425
+ className: "divide-y divide-border",
1359
1426
  children: [
1360
1427
  connections.map((conn) => /* @__PURE__ */ jsxDEV2("tr", {
1361
1428
  className: "hover:bg-muted/50",
@@ -1370,12 +1437,12 @@ function IntegrationDashboard() {
1370
1437
  /* @__PURE__ */ jsxDEV2("td", {
1371
1438
  className: "px-4 py-3",
1372
1439
  children: /* @__PURE__ */ jsxDEV2("span", {
1373
- className: `inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${STATUS_COLORS[conn.status] ?? ""}`,
1440
+ className: `inline-flex rounded-full px-2 py-0.5 font-medium text-xs ${STATUS_COLORS[conn.status] ?? ""}`,
1374
1441
  children: conn.status
1375
1442
  }, undefined, false, undefined, this)
1376
1443
  }, undefined, false, undefined, this),
1377
1444
  /* @__PURE__ */ jsxDEV2("td", {
1378
- className: "text-muted-foreground px-4 py-3 text-sm",
1445
+ className: "px-4 py-3 text-muted-foreground text-sm",
1379
1446
  children: conn.lastSyncAt?.toLocaleString() ?? "Never"
1380
1447
  }, undefined, false, undefined, this)
1381
1448
  ]
@@ -1383,7 +1450,7 @@ function IntegrationDashboard() {
1383
1450
  connections.length === 0 && /* @__PURE__ */ jsxDEV2("tr", {
1384
1451
  children: /* @__PURE__ */ jsxDEV2("td", {
1385
1452
  colSpan: 3,
1386
- className: "text-muted-foreground px-4 py-8 text-center",
1453
+ className: "px-4 py-8 text-center text-muted-foreground",
1387
1454
  children: "No connections found"
1388
1455
  }, undefined, false, undefined, this)
1389
1456
  }, undefined, false, undefined, this)
@@ -1403,35 +1470,35 @@ function IntegrationDashboard() {
1403
1470
  className: "min-h-[400px]"
1404
1471
  }, undefined, false, undefined, this),
1405
1472
  activeTab === "syncs" && /* @__PURE__ */ jsxDEV2("div", {
1406
- className: "border-border rounded-lg border",
1473
+ className: "rounded-lg border border-border",
1407
1474
  children: /* @__PURE__ */ jsxDEV2("table", {
1408
1475
  className: "w-full",
1409
1476
  children: [
1410
1477
  /* @__PURE__ */ jsxDEV2("thead", {
1411
- className: "border-border bg-muted/30 border-b",
1478
+ className: "border-border border-b bg-muted/30",
1412
1479
  children: /* @__PURE__ */ jsxDEV2("tr", {
1413
1480
  children: [
1414
1481
  /* @__PURE__ */ jsxDEV2("th", {
1415
- className: "px-4 py-3 text-left text-sm font-medium",
1482
+ className: "px-4 py-3 text-left font-medium text-sm",
1416
1483
  children: "Sync Config"
1417
1484
  }, undefined, false, undefined, this),
1418
1485
  /* @__PURE__ */ jsxDEV2("th", {
1419
- className: "px-4 py-3 text-left text-sm font-medium",
1486
+ className: "px-4 py-3 text-left font-medium text-sm",
1420
1487
  children: "Frequency"
1421
1488
  }, undefined, false, undefined, this),
1422
1489
  /* @__PURE__ */ jsxDEV2("th", {
1423
- className: "px-4 py-3 text-left text-sm font-medium",
1490
+ className: "px-4 py-3 text-left font-medium text-sm",
1424
1491
  children: "Status"
1425
1492
  }, undefined, false, undefined, this),
1426
1493
  /* @__PURE__ */ jsxDEV2("th", {
1427
- className: "px-4 py-3 text-left text-sm font-medium",
1494
+ className: "px-4 py-3 text-left font-medium text-sm",
1428
1495
  children: "Records"
1429
1496
  }, undefined, false, undefined, this)
1430
1497
  ]
1431
1498
  }, undefined, true, undefined, this)
1432
1499
  }, undefined, false, undefined, this),
1433
1500
  /* @__PURE__ */ jsxDEV2("tbody", {
1434
- className: "divide-border divide-y",
1501
+ className: "divide-y divide-border",
1435
1502
  children: [
1436
1503
  syncConfigs.map((sync) => /* @__PURE__ */ jsxDEV2("tr", {
1437
1504
  className: "hover:bg-muted/50",
@@ -1460,12 +1527,12 @@ function IntegrationDashboard() {
1460
1527
  /* @__PURE__ */ jsxDEV2("td", {
1461
1528
  className: "px-4 py-3",
1462
1529
  children: /* @__PURE__ */ jsxDEV2("span", {
1463
- className: `inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${STATUS_COLORS[sync.status] ?? ""}`,
1530
+ className: `inline-flex rounded-full px-2 py-0.5 font-medium text-xs ${STATUS_COLORS[sync.status] ?? ""}`,
1464
1531
  children: sync.status
1465
1532
  }, undefined, false, undefined, this)
1466
1533
  }, undefined, false, undefined, this),
1467
1534
  /* @__PURE__ */ jsxDEV2("td", {
1468
- className: "text-muted-foreground px-4 py-3 text-sm",
1535
+ className: "px-4 py-3 text-muted-foreground text-sm",
1469
1536
  children: sync.recordsSynced.toLocaleString()
1470
1537
  }, undefined, false, undefined, this)
1471
1538
  ]
@@ -1473,7 +1540,7 @@ function IntegrationDashboard() {
1473
1540
  syncConfigs.length === 0 && /* @__PURE__ */ jsxDEV2("tr", {
1474
1541
  children: /* @__PURE__ */ jsxDEV2("td", {
1475
1542
  colSpan: 4,
1476
- className: "text-muted-foreground px-4 py-8 text-center",
1543
+ className: "px-4 py-8 text-center text-muted-foreground",
1477
1544
  children: "No sync configurations found"
1478
1545
  }, undefined, false, undefined, this)
1479
1546
  }, undefined, false, undefined, this)
@@ -1488,339 +1555,274 @@ function IntegrationDashboard() {
1488
1555
  }, undefined, true, undefined, this);
1489
1556
  }
1490
1557
 
1491
- // src/ui/hooks/index.ts
1492
- "use client";
1493
- // src/sync-engine/index.ts
1494
- class BasicFieldTransformer {
1495
- transform(value, expression) {
1496
- try {
1497
- if (expression.startsWith("uppercase")) {
1498
- return typeof value === "string" ? value.toUpperCase() : value;
1499
- }
1500
- if (expression.startsWith("lowercase")) {
1501
- return typeof value === "string" ? value.toLowerCase() : value;
1502
- }
1503
- if (expression.startsWith("trim")) {
1504
- return typeof value === "string" ? value.trim() : value;
1505
- }
1506
- if (expression.startsWith("default:")) {
1507
- const defaultVal = expression.replace("default:", "");
1508
- return value ?? JSON.parse(defaultVal);
1509
- }
1510
- if (expression.startsWith("concat:")) {
1511
- const separator = expression.replace("concat:", "") || " ";
1512
- if (Array.isArray(value)) {
1513
- return value.join(separator);
1514
- }
1515
- return value;
1516
- }
1517
- if (expression.startsWith("split:")) {
1518
- const separator = expression.replace("split:", "") || ",";
1519
- if (typeof value === "string") {
1520
- return value.split(separator);
1521
- }
1522
- return value;
1523
- }
1524
- if (expression.startsWith("number")) {
1525
- return Number(value);
1526
- }
1527
- if (expression.startsWith("boolean")) {
1528
- return Boolean(value);
1529
- }
1530
- if (expression.startsWith("string")) {
1531
- return String(value);
1532
- }
1533
- return value;
1534
- } catch {
1535
- return value;
1536
- }
1558
+ // src/ui/renderers/integration.markdown.ts
1559
+ var mockIntegrations = [
1560
+ {
1561
+ id: "int-1",
1562
+ name: "Salesforce",
1563
+ type: "CRM",
1564
+ status: "ACTIVE",
1565
+ connectionCount: 3
1566
+ },
1567
+ {
1568
+ id: "int-2",
1569
+ name: "HubSpot",
1570
+ type: "MARKETING",
1571
+ status: "ACTIVE",
1572
+ connectionCount: 2
1573
+ },
1574
+ {
1575
+ id: "int-3",
1576
+ name: "Stripe",
1577
+ type: "PAYMENT",
1578
+ status: "ACTIVE",
1579
+ connectionCount: 1
1580
+ },
1581
+ {
1582
+ id: "int-4",
1583
+ name: "Slack",
1584
+ type: "COMMUNICATION",
1585
+ status: "INACTIVE",
1586
+ connectionCount: 0
1587
+ },
1588
+ {
1589
+ id: "int-5",
1590
+ name: "Google Sheets",
1591
+ type: "DATA",
1592
+ status: "ACTIVE",
1593
+ connectionCount: 5
1594
+ },
1595
+ {
1596
+ id: "int-6",
1597
+ name: "PostHog",
1598
+ type: "ANALYTICS",
1599
+ status: "ACTIVE",
1600
+ connectionCount: 1
1537
1601
  }
1538
- }
1539
-
1540
- class BasicSyncEngine {
1541
- transformer;
1542
- constructor(transformer) {
1543
- this.transformer = transformer ?? new BasicFieldTransformer;
1602
+ ];
1603
+ var mockConnections = [
1604
+ {
1605
+ id: "conn-1",
1606
+ integrationId: "int-1",
1607
+ name: "Production Salesforce",
1608
+ status: "CONNECTED",
1609
+ lastSyncAt: "2024-01-16T10:00:00Z"
1610
+ },
1611
+ {
1612
+ id: "conn-2",
1613
+ integrationId: "int-1",
1614
+ name: "Sandbox Salesforce",
1615
+ status: "CONNECTED",
1616
+ lastSyncAt: "2024-01-15T14:00:00Z"
1617
+ },
1618
+ {
1619
+ id: "conn-3",
1620
+ integrationId: "int-2",
1621
+ name: "Marketing HubSpot",
1622
+ status: "CONNECTED",
1623
+ lastSyncAt: "2024-01-16T08:00:00Z"
1624
+ },
1625
+ {
1626
+ id: "conn-4",
1627
+ integrationId: "int-3",
1628
+ name: "Stripe Live",
1629
+ status: "CONNECTED",
1630
+ lastSyncAt: "2024-01-16T12:00:00Z"
1631
+ },
1632
+ {
1633
+ id: "conn-5",
1634
+ integrationId: "int-5",
1635
+ name: "Analytics Sheet",
1636
+ status: "ERROR",
1637
+ lastSyncAt: "2024-01-14T09:00:00Z",
1638
+ error: "Authentication expired"
1639
+ },
1640
+ {
1641
+ id: "conn-6",
1642
+ integrationId: "int-6",
1643
+ name: "PostHog Workspace",
1644
+ status: "CONNECTED",
1645
+ lastSyncAt: "2024-01-16T11:45:00Z"
1544
1646
  }
1545
- async sync(_context) {
1546
- const result = {
1547
- success: true,
1548
- recordsProcessed: 0,
1549
- recordsCreated: 0,
1550
- recordsUpdated: 0,
1551
- recordsDeleted: 0,
1552
- recordsFailed: 0,
1553
- recordsSkipped: 0,
1554
- errors: []
1555
- };
1556
- return result;
1647
+ ];
1648
+ var mockSyncConfigs = [
1649
+ {
1650
+ id: "sync-1",
1651
+ connectionId: "conn-1",
1652
+ name: "Contacts Sync",
1653
+ frequency: "HOURLY",
1654
+ lastRunAt: "2024-01-16T10:00:00Z",
1655
+ status: "SUCCESS",
1656
+ recordsSynced: 1250
1657
+ },
1658
+ {
1659
+ id: "sync-2",
1660
+ connectionId: "conn-1",
1661
+ name: "Opportunities Sync",
1662
+ frequency: "DAILY",
1663
+ lastRunAt: "2024-01-16T00:00:00Z",
1664
+ status: "SUCCESS",
1665
+ recordsSynced: 340
1666
+ },
1667
+ {
1668
+ id: "sync-3",
1669
+ connectionId: "conn-3",
1670
+ name: "Orders Sync",
1671
+ frequency: "REALTIME",
1672
+ lastRunAt: "2024-01-16T12:30:00Z",
1673
+ status: "SUCCESS",
1674
+ recordsSynced: 89
1675
+ },
1676
+ {
1677
+ id: "sync-4",
1678
+ connectionId: "conn-5",
1679
+ name: "Metrics Export",
1680
+ frequency: "DAILY",
1681
+ lastRunAt: "2024-01-14T09:00:00Z",
1682
+ status: "FAILED",
1683
+ recordsSynced: 0
1557
1684
  }
1558
- transformRecord(sourceRecord, mappings, _context) {
1559
- const targetData = {};
1560
- for (const mapping of mappings) {
1561
- let value;
1562
- let sourceValue;
1563
- switch (mapping.mappingType) {
1564
- case "DIRECT":
1565
- value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1566
- break;
1567
- case "TRANSFORM":
1568
- sourceValue = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1569
- value = mapping.transformExpression ? this.transformer.transform(sourceValue, mapping.transformExpression) : sourceValue;
1570
- break;
1571
- case "CONSTANT":
1572
- value = mapping.constantValue;
1573
- break;
1574
- case "LOOKUP":
1575
- value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1576
- break;
1577
- case "COMPUTED":
1578
- value = mapping.transformExpression ? this.evaluateComputed(sourceRecord.data, mapping.transformExpression) : null;
1579
- break;
1580
- default:
1581
- value = this.getNestedValue(sourceRecord.data, mapping.sourceField);
1582
- }
1583
- if (value === undefined || value === null) {
1584
- value = mapping.defaultValue;
1585
- }
1586
- this.setNestedValue(targetData, mapping.targetField, value);
1685
+ ];
1686
+ var integrationDashboardMarkdownRenderer = {
1687
+ target: "markdown",
1688
+ render: async (desc) => {
1689
+ if (desc.source.type !== "component" || desc.source.componentKey !== "IntegrationDashboard") {
1690
+ throw new Error("integrationDashboardMarkdownRenderer: not IntegrationDashboard");
1587
1691
  }
1588
- return {
1589
- id: sourceRecord.id,
1590
- data: targetData
1591
- };
1592
- }
1593
- validateRecord(record, mappings) {
1594
- const errors = [];
1595
- for (const mapping of mappings) {
1596
- if (mapping.isRequired) {
1597
- const value = this.getNestedValue(record.data, mapping.targetField);
1598
- if (value === undefined || value === null) {
1599
- errors.push({
1600
- recordId: record.id,
1601
- field: mapping.targetField,
1602
- message: `Required field ${mapping.targetField} is missing`,
1603
- code: "REQUIRED_FIELD_MISSING"
1604
- });
1605
- }
1692
+ const integrations = mockIntegrations;
1693
+ const connections = mockConnections;
1694
+ const syncs = mockSyncConfigs;
1695
+ const activeIntegrations = integrations.filter((i) => i.status === "ACTIVE");
1696
+ const connectedConnections = connections.filter((c) => c.status === "CONNECTED");
1697
+ const errorConnections = connections.filter((c) => c.status === "ERROR");
1698
+ const successfulSyncs = syncs.filter((s) => s.status === "SUCCESS");
1699
+ const totalRecordsSynced = successfulSyncs.reduce((sum, s) => sum + s.recordsSynced, 0);
1700
+ const lines = [
1701
+ "# Integration Hub",
1702
+ "",
1703
+ "> Connect and sync data with external services",
1704
+ "",
1705
+ "## Overview",
1706
+ "",
1707
+ "| Metric | Value |",
1708
+ "|--------|-------|",
1709
+ `| Active Integrations | ${activeIntegrations.length} |`,
1710
+ `| Connected Services | ${connectedConnections.length} |`,
1711
+ `| Error Connections | ${errorConnections.length} |`,
1712
+ `| Sync Configs | ${syncs.length} |`,
1713
+ `| Records Synced (24h) | ${totalRecordsSynced.toLocaleString()} |`,
1714
+ "",
1715
+ "## Integrations",
1716
+ "",
1717
+ "| Name | Type | Connections | Status |",
1718
+ "|------|------|-------------|--------|"
1719
+ ];
1720
+ for (const integration of integrations) {
1721
+ const statusIcon = integration.status === "ACTIVE" ? "\uD83D\uDFE2" : "⚫";
1722
+ lines.push(`| ${integration.name} | ${integration.type} | ${integration.connectionCount} | ${statusIcon} ${integration.status} |`);
1723
+ }
1724
+ lines.push("");
1725
+ lines.push("## Recent Sync Activity");
1726
+ lines.push("");
1727
+ lines.push("| Sync | Frequency | Last Run | Records | Status |");
1728
+ lines.push("|------|-----------|----------|---------|--------|");
1729
+ for (const sync of syncs) {
1730
+ const lastRun = new Date(sync.lastRunAt).toLocaleString();
1731
+ const statusIcon = sync.status === "SUCCESS" ? "✅" : "❌";
1732
+ lines.push(`| ${sync.name} | ${sync.frequency} | ${lastRun} | ${sync.recordsSynced} | ${statusIcon} ${sync.status} |`);
1733
+ }
1734
+ if (errorConnections.length > 0) {
1735
+ lines.push("");
1736
+ lines.push("## ⚠️ Connections with Errors");
1737
+ lines.push("");
1738
+ for (const conn of errorConnections) {
1739
+ const integration = integrations.find((i) => i.id === conn.integrationId);
1740
+ lines.push(`- **${conn.name}** (${integration?.name ?? "Unknown"}): ${conn.error ?? "Unknown error"}`);
1606
1741
  }
1607
1742
  }
1608
1743
  return {
1609
- valid: errors.length === 0,
1610
- errors
1744
+ mimeType: "text/markdown",
1745
+ body: lines.join(`
1746
+ `)
1611
1747
  };
1612
1748
  }
1613
- getNestedValue(obj, path) {
1614
- const parts = path.split(".");
1615
- let current = obj;
1616
- for (const part of parts) {
1617
- if (current === null || current === undefined) {
1618
- return;
1619
- }
1620
- current = current[part];
1749
+ };
1750
+ var connectionListMarkdownRenderer = {
1751
+ target: "markdown",
1752
+ render: async (desc) => {
1753
+ if (desc.source.type !== "component" || desc.source.componentKey !== "ConnectionList") {
1754
+ throw new Error("connectionListMarkdownRenderer: not ConnectionList");
1621
1755
  }
1622
- return current;
1623
- }
1624
- setNestedValue(obj, path, value) {
1625
- const parts = path.split(".");
1626
- let current = obj;
1627
- for (let i = 0;i < parts.length - 1; i++) {
1628
- const part = parts[i];
1629
- if (part === undefined)
1756
+ const connections = mockConnections;
1757
+ const integrations = mockIntegrations;
1758
+ const lines = [
1759
+ "# Connections",
1760
+ "",
1761
+ "> Manage connections to external services",
1762
+ ""
1763
+ ];
1764
+ for (const integration of integrations) {
1765
+ const intConnections = connections.filter((c) => c.integrationId === integration.id);
1766
+ if (intConnections.length === 0)
1630
1767
  continue;
1631
- if (!(part in current)) {
1632
- current[part] = {};
1768
+ lines.push(`## ${integration.name}`);
1769
+ lines.push("");
1770
+ lines.push("| Connection | Status | Last Sync |");
1771
+ lines.push("|------------|--------|-----------|");
1772
+ for (const conn of intConnections) {
1773
+ const lastSync = new Date(conn.lastSyncAt).toLocaleString();
1774
+ const statusIcon = conn.status === "CONNECTED" ? "\uD83D\uDFE2" : conn.status === "ERROR" ? "\uD83D\uDD34" : "⚫";
1775
+ lines.push(`| ${conn.name} | ${statusIcon} ${conn.status} | ${lastSync} |`);
1633
1776
  }
1634
- current = current[part];
1635
- }
1636
- const lastPart = parts[parts.length - 1];
1637
- if (lastPart !== undefined) {
1638
- current[lastPart] = value;
1777
+ lines.push("");
1639
1778
  }
1779
+ return {
1780
+ mimeType: "text/markdown",
1781
+ body: lines.join(`
1782
+ `)
1783
+ };
1640
1784
  }
1641
- evaluateComputed(data, expression) {
1642
- try {
1643
- const result = expression.replace(/\$\{([^}]+)\}/g, (_, path) => {
1644
- const value = this.getNestedValue(data, path);
1645
- return String(value ?? "");
1646
- });
1647
- return result;
1648
- } catch {
1649
- return null;
1785
+ };
1786
+ var syncConfigMarkdownRenderer = {
1787
+ target: "markdown",
1788
+ render: async (desc) => {
1789
+ if (desc.source.type !== "component" || desc.source.componentKey !== "SyncConfigEditor") {
1790
+ throw new Error("syncConfigMarkdownRenderer: not SyncConfigEditor");
1650
1791
  }
1651
- }
1652
- }
1653
- function createSyncEngine(transformer) {
1654
- return new BasicSyncEngine(transformer);
1655
- }
1656
- function computeChecksum(data) {
1657
- const str = JSON.stringify(data, Object.keys(data).sort());
1658
- let hash = 0;
1659
- for (let i = 0;i < str.length; i++) {
1660
- const char = str.charCodeAt(i);
1661
- hash = (hash << 5) - hash + char;
1662
- hash = hash & hash;
1663
- }
1664
- return hash.toString(16);
1665
- }
1666
- function hasChanges(sourceChecksum, targetChecksum) {
1667
- if (!sourceChecksum || !targetChecksum) {
1668
- return true;
1669
- }
1670
- return sourceChecksum !== targetChecksum;
1671
- }
1672
-
1673
- // src/mcp-example.ts
1674
- import { randomUUID } from "node:crypto";
1675
- import {
1676
- createMcpToolsets
1677
- } from "@contractspec/lib.ai-agent/tools/mcp-client";
1678
- var DEFAULT_STDIO_ARGS = [
1679
- "-y",
1680
- "@modelcontextprotocol/server-filesystem",
1681
- "."
1682
- ];
1683
- async function runIntegrationHubMcpExampleFromEnv() {
1684
- const mode = resolveMode();
1685
- const transport = resolveTransport();
1686
- const config = buildMcpConfigFromEnv();
1687
- const toolset = await createMcpToolsets([config], {
1688
- onNameCollision: "error"
1689
- });
1690
- try {
1691
- const toolNames = Object.keys(toolset.tools).sort();
1692
- const output = {
1693
- mode,
1694
- server: {
1695
- name: config.name,
1696
- transport
1697
- },
1698
- authMethod: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_AUTH_METHOD,
1699
- apiVersion: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_API_VERSION,
1700
- tools: toolNames
1701
- };
1702
- if (mode === "call") {
1703
- const toolName = requireEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_NAME");
1704
- const toolArgs = parseRecordEnvOrDefault("CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_ARGS_JSON", {});
1705
- const tool = toolset.tools[toolName];
1706
- if (!tool?.execute) {
1707
- throw new Error(`Tool "${toolName}" was not found. Available tools: ${toolNames.join(", ")}`);
1708
- }
1709
- const toolOutput = await tool.execute(toolArgs, {
1710
- toolCallId: `integration-hub-${randomUUID()}`,
1711
- messages: []
1712
- });
1713
- output.toolCall = {
1714
- name: toolName,
1715
- args: toolArgs,
1716
- output: toolOutput
1717
- };
1792
+ const syncs = mockSyncConfigs;
1793
+ const connections = mockConnections;
1794
+ const lines = [
1795
+ "# Sync Configurations",
1796
+ "",
1797
+ "> Configure automated data synchronization",
1798
+ ""
1799
+ ];
1800
+ for (const sync of syncs) {
1801
+ const connection = connections.find((c) => c.id === sync.connectionId);
1802
+ const statusIcon = sync.status === "SUCCESS" ? "✅" : "❌";
1803
+ lines.push(`## ${sync.name}`);
1804
+ lines.push("");
1805
+ lines.push(`**Connection:** ${connection?.name ?? "Unknown"}`);
1806
+ lines.push(`**Frequency:** ${sync.frequency}`);
1807
+ lines.push(`**Status:** ${statusIcon} ${sync.status}`);
1808
+ lines.push(`**Last Run:** ${new Date(sync.lastRunAt).toLocaleString()}`);
1809
+ lines.push(`**Records Synced:** ${sync.recordsSynced.toLocaleString()}`);
1810
+ lines.push("");
1718
1811
  }
1719
- return output;
1720
- } finally {
1721
- await toolset.cleanup().catch(() => {
1722
- return;
1723
- });
1724
- }
1725
- }
1726
- function buildMcpConfigFromEnv() {
1727
- const name = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_NAME ?? "filesystem";
1728
- const transport = resolveTransport();
1729
- const toolPrefix = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_TOOL_PREFIX;
1730
- if (transport === "stdio") {
1812
+ lines.push("## Frequency Options");
1813
+ lines.push("");
1814
+ lines.push("- **REALTIME**: Sync on every change");
1815
+ lines.push("- **HOURLY**: Sync every hour");
1816
+ lines.push("- **DAILY**: Sync once per day");
1817
+ lines.push("- **WEEKLY**: Sync once per week");
1818
+ lines.push("- **MANUAL**: Sync only when triggered");
1731
1819
  return {
1732
- name,
1733
- transport,
1734
- command: process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_COMMAND ?? "npx",
1735
- args: parseStringArrayEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_ARGS_JSON", DEFAULT_STDIO_ARGS),
1736
- toolPrefix
1820
+ mimeType: "text/markdown",
1821
+ body: lines.join(`
1822
+ `)
1737
1823
  };
1738
1824
  }
1739
- const accessToken = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_ACCESS_TOKEN;
1740
- const accessTokenEnvVar = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_ACCESS_TOKEN_ENV;
1741
- const mcpTransport = transport === "webhook" || transport === "http" ? "http" : "sse";
1742
- return {
1743
- name,
1744
- transport: mcpTransport,
1745
- url: requireEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_URL"),
1746
- headers: parseStringRecordEnv("CONTRACTSPEC_INTEGRATION_HUB_MCP_HEADERS_JSON"),
1747
- accessToken,
1748
- accessTokenEnvVar,
1749
- toolPrefix
1750
- };
1751
- }
1752
- function resolveMode() {
1753
- const rawMode = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_MODE?.toLowerCase() ?? "list";
1754
- if (rawMode === "list" || rawMode === "call") {
1755
- return rawMode;
1756
- }
1757
- throw new Error(`Unsupported CONTRACTSPEC_INTEGRATION_HUB_MCP_MODE: ${rawMode}. Use "list" or "call".`);
1758
- }
1759
- function resolveTransport() {
1760
- const rawTransport = process.env.CONTRACTSPEC_INTEGRATION_HUB_MCP_TRANSPORT?.toLowerCase() ?? "stdio";
1761
- if (rawTransport === "stdio" || rawTransport === "http" || rawTransport === "sse" || rawTransport === "webhook") {
1762
- return rawTransport;
1763
- }
1764
- throw new Error(`Unsupported CONTRACTSPEC_INTEGRATION_HUB_MCP_TRANSPORT: ${rawTransport}. Use "stdio", "http", "sse", or "webhook".`);
1765
- }
1766
- function parseStringArrayEnv(key, fallback) {
1767
- const raw = process.env[key];
1768
- if (!raw) {
1769
- return fallback;
1770
- }
1771
- const parsed = parseJsonEnv(key);
1772
- if (!Array.isArray(parsed) || parsed.some((value) => typeof value !== "string")) {
1773
- throw new Error(`${key} must be a JSON string array.`);
1774
- }
1775
- return parsed;
1776
- }
1777
- function parseRecordEnv(key) {
1778
- const raw = process.env[key];
1779
- if (!raw) {
1780
- return;
1781
- }
1782
- const parsed = parseJsonEnv(key);
1783
- if (!isRecord(parsed)) {
1784
- throw new Error(`${key} must be a JSON object.`);
1785
- }
1786
- return parsed;
1787
- }
1788
- function parseRecordEnvOrDefault(key, fallback) {
1789
- return parseRecordEnv(key) ?? fallback;
1790
- }
1791
- function parseStringRecordEnv(key) {
1792
- const parsed = parseRecordEnv(key);
1793
- if (!parsed) {
1794
- return;
1795
- }
1796
- const entries = Object.entries(parsed);
1797
- const invalidEntry = entries.find(([, value]) => typeof value !== "string");
1798
- if (invalidEntry) {
1799
- throw new Error(`${key} must contain only string values.`);
1800
- }
1801
- return Object.fromEntries(entries);
1802
- }
1803
- function parseJsonEnv(key) {
1804
- const raw = process.env[key];
1805
- if (!raw) {
1806
- return;
1807
- }
1808
- try {
1809
- return JSON.parse(raw);
1810
- } catch {
1811
- throw new Error(`${key} contains invalid JSON.`);
1812
- }
1813
- }
1814
- function requireEnv(key) {
1815
- const value = process.env[key];
1816
- if (!value) {
1817
- throw new Error(`Missing required env var: ${key}`);
1818
- }
1819
- return value;
1820
- }
1821
- function isRecord(value) {
1822
- return typeof value === "object" && value !== null && !Array.isArray(value);
1823
- }
1825
+ };
1824
1826
  export {
1825
1827
  useIntegrationData,
1826
1828
  syncConfigMarkdownRenderer,