@ema.co/mcp-toolkit 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -203,11 +203,11 @@ environments:
203
203
  | Tool | Purpose |
204
204
  |------|---------|
205
205
  | `env` | List available environments |
206
- | `persona` | AI Employee management (get/list/create/update/compare) |
206
+ | `persona` | AI Employee management (get/list/create/clone/sanitize/compare) |
207
207
  | `workflow` | Generate/extend/optimize/analyze workflows |
208
208
  | `action` | Agent lookup, docs, and recommendations |
209
209
  | `template` | Patterns, widgets, qualifying questions |
210
- | `knowledge` | Data sources + embedding (upload/list/delete/toggle) |
210
+ | `knowledge` | Data sources + embedding + dashboard data (upload/list/dashboard_rows/dashboard_clone) |
211
211
  | `reference` | Concepts, guidance, validation, common mistakes |
212
212
  | `sync` | Sync across environments |
213
213
  | `demo` | Demo/RAG document utilities |
@@ -11,7 +11,9 @@ import { compileWorkflow } from "../sdk/workflow-generator.js";
11
11
  import { ensureActionRegistry } from "../sdk/action-registry.js";
12
12
  import { parseInput, intentToSpec, generateWorkflow } from "../sdk/workflow-intent.js";
13
13
  import { ensureSchemaRegistry, validateWorkflowSpec, generateActionCatalogForLLM } from "../sdk/workflow-validator.js";
14
+ import { runIntentArchitect } from "../sdk/intent-architect.js";
14
15
  import { analyzeExecutionFlow, generateASCIIFlow } from "../sdk/workflow-execution-analyzer.js";
16
+ import { SanitizationSession, sanitizePersona, buildConfirmationPrompt, detectWithPatterns, } from "../sdk/sanitizer.js";
15
17
  // ─────────────────────────────────────────────────────────────────────────────
16
18
  // Widget Validation Helpers
17
19
  // ─────────────────────────────────────────────────────────────────────────────
@@ -45,7 +47,7 @@ function validateWidgetsForApi(widgets) {
45
47
  // ═══════════════════════════════════════════════════════════════════════════
46
48
  // ENV Handler
47
49
  // ═══════════════════════════════════════════════════════════════════════════
48
- export async function handleEnv(_args, getEnvironments) {
50
+ export async function handleEnv(_args, getEnvironments, toolkit) {
49
51
  const envs = getEnvironments();
50
52
  return {
51
53
  environments: envs.map(e => ({
@@ -53,6 +55,7 @@ export async function handleEnv(_args, getEnvironments) {
53
55
  default: e.isDefault,
54
56
  })),
55
57
  count: envs.length,
58
+ toolkit: toolkit ?? { name: "unknown", version: "unknown" },
56
59
  };
57
60
  }
58
61
  // ═══════════════════════════════════════════════════════════════════════════
@@ -87,12 +90,17 @@ export async function handlePersona(args, client, getTemplateId, createClientFor
87
90
  // Standard persona operations (get, list, compare, version management)
88
91
  // ═══════════════════════════════════════════════════════════════════════════
89
92
  // Determine effective mode
93
+ // BUG FIX: clone_from and name+type now properly route to "create" mode
90
94
  let effectiveMode = mode;
91
95
  if (!effectiveMode) {
92
96
  if (args.templates)
93
97
  effectiveMode = "templates";
94
98
  else if (args.all || args.query || args.status || args.trigger_type)
95
99
  effectiveMode = "list";
100
+ else if (args.sanitize && idOrName)
101
+ effectiveMode = "sanitize"; // Standalone sanitize mode
102
+ else if (args.clone_from || (args.name && (args.type || args.template_id)))
103
+ effectiveMode = "create";
96
104
  else if (idOrName)
97
105
  effectiveMode = "get";
98
106
  else
@@ -176,18 +184,185 @@ export async function handlePersona(args, client, getTemplateId, createClientFor
176
184
  if (!templateId && args.type) {
177
185
  templateId = getTemplateId(args.type);
178
186
  }
187
+ // Check if cloning from dashboard persona
188
+ const cloneFrom = args.clone_from;
189
+ const cloneData = args.clone_data;
190
+ let sourcePersona = null;
191
+ let sourcePersonaType;
192
+ let sourceDashboardId;
193
+ if (cloneFrom && cloneData) {
194
+ try {
195
+ sourcePersona = await resolvePersona(client, cloneFrom);
196
+ if (sourcePersona) {
197
+ const protoConfig = sourcePersona.proto_config;
198
+ const projectSettings = protoConfig?.projectSettings;
199
+ const projectType = projectSettings?.projectType;
200
+ sourcePersonaType = projectType === 5 ? "voice" : projectType === 4 ? "chat" : projectType === 2 ? "dashboard" : undefined;
201
+ sourceDashboardId = sourcePersona.workflow_dashboard_id;
202
+ }
203
+ }
204
+ catch {
205
+ // Ignore errors in source persona detection
206
+ }
207
+ }
179
208
  const result = await client.createAiEmployee({
180
209
  name,
181
210
  description: args.description,
182
211
  template_id: templateId,
183
- source_persona_id: args.clone_from,
184
- clone_data: args.clone_data,
212
+ source_persona_id: cloneFrom,
213
+ clone_data: cloneData,
185
214
  });
186
- return {
215
+ const newPersonaId = result.persona_id ?? result.id;
216
+ // Automatically clone dashboard rows if source is a dashboard persona
217
+ let dashboardCloneResult;
218
+ if (sourcePersonaType === "dashboard" && sourceDashboardId && newPersonaId && cloneData) {
219
+ try {
220
+ // Dashboard operations require the persona to be enabled
221
+ // Auto-enable the persona temporarily for cloning
222
+ const newPersona = await client.getPersonaById(newPersonaId);
223
+ const newPersonaProtoConfig = newPersona?.proto_config;
224
+ // Enable the persona so we can add dashboard rows
225
+ await client.updateAiEmployee({
226
+ persona_id: newPersonaId,
227
+ proto_config: newPersonaProtoConfig ?? {},
228
+ enabled_by_user: true,
229
+ });
230
+ // Get source dashboard rows
231
+ const sourceRows = await client.getDashboardRows(sourceDashboardId, sourcePersona.id);
232
+ if (sourceRows.rows.length > 0) {
233
+ // Identify input columns
234
+ const inputColumns = sourceRows.schema.columns.filter(c => c.isInput);
235
+ // Clone each row
236
+ const results = [];
237
+ const shouldSanitize = args.sanitize;
238
+ const sanitizeExamples = args.sanitize_examples;
239
+ // Create sanitization session if needed
240
+ let sanitizationSession;
241
+ if (shouldSanitize) {
242
+ sanitizationSession = new SanitizationSession();
243
+ }
244
+ for (const row of sourceRows.rows) {
245
+ try {
246
+ // Build inputs from row's input column values
247
+ const inputs = [];
248
+ for (const inputCol of inputColumns) {
249
+ const colValue = row.columnValues.find(cv => cv.columnId === inputCol.columnId);
250
+ if (!colValue)
251
+ continue;
252
+ // Handle different column types
253
+ if (inputCol.columnType === "COLUMN_TYPE_DOCUMENT") {
254
+ // Skip document columns for now
255
+ continue;
256
+ }
257
+ else if (inputCol.columnType === "COLUMN_TYPE_STRING") {
258
+ let value = colValue.value.stringValue ?? "";
259
+ // Sanitize if enabled
260
+ if (sanitizationSession && value) {
261
+ const detected = detectWithPatterns(value);
262
+ for (const entity of detected) {
263
+ const replacement = sanitizationSession.getOrCreateReplacement(entity.value, entity.type);
264
+ value = value.split(entity.value).join(replacement);
265
+ }
266
+ if (sanitizeExamples) {
267
+ for (const example of sanitizeExamples) {
268
+ if (value.includes(example)) {
269
+ const replacement = sanitizationSession.getOrCreateReplacement(example, "unknown");
270
+ value = value.split(example).join(replacement);
271
+ }
272
+ }
273
+ }
274
+ }
275
+ inputs.push({ name: inputCol.name, string_value: value });
276
+ }
277
+ else if (inputCol.columnType === "COLUMN_TYPE_ARRAY") {
278
+ const arrayVals = colValue.value.arrayValue?.arrayValues ?? [];
279
+ if (arrayVals.length > 0) {
280
+ let value = arrayVals[0].stringValue ?? "";
281
+ if (sanitizationSession && value) {
282
+ const detected = detectWithPatterns(value);
283
+ for (const entity of detected) {
284
+ const replacement = sanitizationSession.getOrCreateReplacement(entity.value, entity.type);
285
+ value = value.split(entity.value).join(replacement);
286
+ }
287
+ }
288
+ inputs.push({ name: inputCol.name, string_value: value });
289
+ }
290
+ }
291
+ }
292
+ if (inputs.length === 0) {
293
+ results.push({ source_row_id: row.id, status: "skipped", error: "No clonable input values" });
294
+ continue;
295
+ }
296
+ // Upload the row to target dashboard
297
+ const uploadResult = await client.uploadAndRunDashboardRow(newPersonaId, inputs);
298
+ results.push({ source_row_id: row.id, target_row_id: uploadResult.row_id, status: "cloned" });
299
+ }
300
+ catch (err) {
301
+ results.push({ source_row_id: row.id, status: "error", error: err instanceof Error ? err.message : String(err) });
302
+ }
303
+ }
304
+ const clonedCount = results.filter(r => r.status === "cloned").length;
305
+ dashboardCloneResult = {
306
+ source_rows: sourceRows.rows.length,
307
+ cloned_rows: clonedCount,
308
+ skipped_rows: results.filter(r => r.status === "skipped").length,
309
+ error_rows: results.filter(r => r.status === "error").length,
310
+ sanitization_applied: !!shouldSanitize,
311
+ };
312
+ }
313
+ else {
314
+ dashboardCloneResult = { source_rows: 0, cloned_rows: 0, message: "Source dashboard was empty" };
315
+ }
316
+ }
317
+ catch (err) {
318
+ dashboardCloneResult = { error: `Dashboard clone failed: ${err instanceof Error ? err.message : String(err)}` };
319
+ }
320
+ }
321
+ // Handle sanitization for non-dashboard personas
322
+ const shouldSanitize = args.sanitize;
323
+ if (shouldSanitize && newPersonaId && sourcePersonaType !== "dashboard") {
324
+ const sanitizeResult = await sanitizePersonaById(client, newPersonaId, {
325
+ examples: args.sanitize_examples,
326
+ });
327
+ return {
328
+ success: true,
329
+ persona_id: newPersonaId,
330
+ name,
331
+ sanitization: sanitizeResult,
332
+ };
333
+ }
334
+ const createResult = {
187
335
  success: true,
188
- persona_id: result.persona_id,
336
+ persona_id: newPersonaId,
189
337
  name,
190
338
  };
339
+ if (sourcePersonaType) {
340
+ createResult.source_persona_type = sourcePersonaType;
341
+ }
342
+ if (dashboardCloneResult) {
343
+ createResult.dashboard_data_clone = dashboardCloneResult;
344
+ }
345
+ return createResult;
346
+ }
347
+ case "sanitize": {
348
+ // Standalone sanitize mode: sanitize an existing persona
349
+ if (!idOrName) {
350
+ return { error: "id required for sanitize mode" };
351
+ }
352
+ const persona = await resolvePersona(client, idOrName);
353
+ if (!persona) {
354
+ return { error: `Persona not found: ${idOrName}` };
355
+ }
356
+ const sanitizeResult = await sanitizePersonaById(client, persona.id, {
357
+ examples: args.sanitize_examples,
358
+ preview: args.preview,
359
+ });
360
+ return {
361
+ mode: "sanitize",
362
+ persona_id: persona.id,
363
+ persona_name: persona.name,
364
+ ...sanitizeResult,
365
+ };
191
366
  }
192
367
  case "update": {
193
368
  if (!idOrName) {
@@ -792,8 +967,6 @@ export async function handleWorkflow(args, client, getTemplateId) {
792
967
  if (args.type) {
793
968
  parseResult.intent.persona_type = args.type;
794
969
  }
795
- // Check if intent requires LLM-driven generation
796
- const genResult = generateWorkflow(parseResult.intent);
797
970
  // Load schema registry for API-driven validation (graceful degradation if unavailable)
798
971
  let schemaRegistry;
799
972
  try {
@@ -803,9 +976,66 @@ export async function handleWorkflow(args, client, getTemplateId) {
803
976
  // Schema registry unavailable - skip API validation
804
977
  schemaRegistry = null;
805
978
  }
979
+ // ═══════════════════════════════════════════════════════════════════════════
980
+ // PROGRESSIVE ENHANCEMENT: Use Intent Architect for moderate/complex requests
981
+ // ═══════════════════════════════════════════════════════════════════════════
982
+ //
983
+ // Uses the canonical runIntentArchitect() entrypoint which:
984
+ // - Detects complexity with typed scores (not enums)
985
+ // - Returns qualification questions or LLM prompt
986
+ // - Supports iterative refinement with previous_answers
987
+ //
988
+ // Configure rollout via max_complexity option (default: allow all levels)
989
+ const maxComplexity = args.max_complexity || undefined;
990
+ const inputStr = input; // Already validated above
991
+ const architectResult = runIntentArchitect(inputStr, {
992
+ persona_type: parseResult.intent.persona_type,
993
+ available_integrations: schemaRegistry?.getAllActions().slice(0, 20).map(a => a.displayName || a.name),
994
+ }, { max_complexity: maxComplexity });
995
+ // For moderate/complex: return Intent Architect result with questions or prompt
996
+ if (!architectResult.strategy.can_proceed) {
997
+ let availableActions = [];
998
+ let availableTemplates = [];
999
+ // Enhance prompt with action catalog if available
1000
+ let enhancedPrompt = architectResult.prompt_package;
1001
+ if (schemaRegistry && enhancedPrompt) {
1002
+ const actionCatalog = generateActionCatalogForLLM(schemaRegistry);
1003
+ enhancedPrompt = {
1004
+ system: enhancedPrompt.system + "\n\n## Available Actions\n" + actionCatalog,
1005
+ user: enhancedPrompt.user,
1006
+ };
1007
+ availableActions = schemaRegistry.getAllActions().map(a => a.name);
1008
+ availableTemplates = schemaRegistry.getAllTemplates().map(t => ({ id: t.id, name: t.name, type: t.type }));
1009
+ }
1010
+ return {
1011
+ status: "needs_intent_architect",
1012
+ // Typed assessment (new)
1013
+ assessment: architectResult.assessment,
1014
+ // Strategy decision (new)
1015
+ strategy: architectResult.strategy,
1016
+ // Qualification questions (new)
1017
+ questions: architectResult.questions,
1018
+ // LLM prompt (if full architect)
1019
+ llm_prompt: enhancedPrompt,
1020
+ // Hint for what to do next
1021
+ hint: architectResult.strategy.next_step,
1022
+ // Legacy fields for backward compatibility
1023
+ reason: architectResult.legacy?.signals.reason,
1024
+ complexity: architectResult.legacy?.complexity,
1025
+ approach: architectResult.strategy.approach,
1026
+ gates_to_ask: architectResult.strategy.gates_to_ask,
1027
+ // Also provide basic spec as fallback
1028
+ fallback_spec: intentToSpec(parseResult.intent),
1029
+ // Include catalogs for reference (if available)
1030
+ available_actions: availableActions,
1031
+ available_templates: availableTemplates,
1032
+ };
1033
+ }
1034
+ // SIMPLE complexity: Check if intent requires LLM-driven generation (legacy path)
1035
+ const genResult = generateWorkflow(parseResult.intent);
806
1036
  if (genResult.needs_llm) {
807
- // Complex workflow - return prompt for LLM to generate
808
- // Enhanced with API-driven action catalog if available
1037
+ // Complex workflow detected by legacy system - return prompt for LLM to generate
1038
+ // This is a fallback for cases the new complexity analyzer might miss
809
1039
  let enhancedPrompt = genResult.llm_prompt;
810
1040
  let availableActions = [];
811
1041
  let availableTemplates = [];
@@ -1772,6 +2002,205 @@ export async function handleKnowledge(args, client, readFile) {
1772
2002
  message: `Data source '${widgetName}' attached to node '${nodeName}'`,
1773
2003
  };
1774
2004
  }
2005
+ case "dashboard_rows": {
2006
+ // Get rows from a dashboard persona
2007
+ const persona = await client.getPersonaById(personaId);
2008
+ if (!persona) {
2009
+ return { error: `Persona not found: ${personaId}` };
2010
+ }
2011
+ const dashboardId = persona.workflow_dashboard_id;
2012
+ if (!dashboardId) {
2013
+ return {
2014
+ error: "This persona has no dashboard. Use dashboard_rows only for Dashboard-type AI Employees.",
2015
+ hint: "Check that the persona has trigger_type=DASHBOARD",
2016
+ };
2017
+ }
2018
+ const limit = args.limit ?? 100;
2019
+ const result = await client.getDashboardRows(dashboardId, personaId, { limit });
2020
+ // Return schema and rows in a structured format
2021
+ return {
2022
+ persona_id: personaId,
2023
+ dashboard_id: dashboardId,
2024
+ dashboard_name: result.dashboardName,
2025
+ total_rows: result.totalCount,
2026
+ schema: {
2027
+ input_columns: result.schema.columns.filter(c => c.isInput),
2028
+ output_columns: result.schema.columns.filter(c => !c.isInput),
2029
+ },
2030
+ rows: result.rows.map(row => ({
2031
+ id: row.id,
2032
+ state: row.state,
2033
+ created_at: row.dashboardRowMetadata.createdAt,
2034
+ input_values: row.columnValues
2035
+ .filter(cv => result.schema.columns.find(c => c.columnId === cv.columnId)?.isInput)
2036
+ .map(cv => ({
2037
+ column: result.schema.columns.find(c => c.columnId === cv.columnId)?.name ?? cv.columnId,
2038
+ value: cv.value.stringValue ?? cv.value.documentCellValue?.documentValues?.[0]?.name ?? cv.value.arrayValue?.arrayValues?.[0]?.stringValue ?? "(complex)",
2039
+ })),
2040
+ })),
2041
+ };
2042
+ }
2043
+ case "dashboard_clone": {
2044
+ const sourcePersonaId = args.source_persona_id;
2045
+ if (!sourcePersonaId) {
2046
+ return { error: "source_persona_id required for dashboard_clone" };
2047
+ }
2048
+ // Get source and target personas
2049
+ const [sourcePersona, targetPersona] = await Promise.all([
2050
+ client.getPersonaById(sourcePersonaId),
2051
+ client.getPersonaById(personaId),
2052
+ ]);
2053
+ if (!sourcePersona) {
2054
+ return { error: `Source persona not found: ${sourcePersonaId}` };
2055
+ }
2056
+ if (!targetPersona) {
2057
+ return { error: `Target persona not found: ${personaId}` };
2058
+ }
2059
+ const sourceDashboardId = sourcePersona.workflow_dashboard_id;
2060
+ if (!sourceDashboardId) {
2061
+ return { error: "Source persona has no dashboard" };
2062
+ }
2063
+ // Dashboard operations require the persona to be enabled
2064
+ // Auto-enable the target persona for cloning
2065
+ const targetProtoConfig = targetPersona.proto_config;
2066
+ await client.updateAiEmployee({
2067
+ persona_id: personaId,
2068
+ proto_config: targetProtoConfig ?? {},
2069
+ enabled_by_user: true,
2070
+ });
2071
+ // Get source dashboard data
2072
+ const sourceRows = await client.getDashboardRows(sourceDashboardId, sourcePersonaId);
2073
+ if (sourceRows.rows.length === 0) {
2074
+ return {
2075
+ success: true,
2076
+ message: "No rows to clone - source dashboard is empty",
2077
+ source_rows: 0,
2078
+ cloned_rows: 0,
2079
+ };
2080
+ }
2081
+ // Identify input columns
2082
+ const inputColumns = sourceRows.schema.columns.filter(c => c.isInput);
2083
+ // Clone each row
2084
+ const results = [];
2085
+ const sanitize = args.sanitize;
2086
+ const sanitizeExamples = args.sanitize_examples;
2087
+ // Create sanitization session if needed
2088
+ let sanitizationSession;
2089
+ if (sanitize) {
2090
+ sanitizationSession = new SanitizationSession();
2091
+ }
2092
+ for (const row of sourceRows.rows) {
2093
+ try {
2094
+ // Build inputs from row's input column values
2095
+ const inputs = [];
2096
+ for (const inputCol of inputColumns) {
2097
+ const colValue = row.columnValues.find(cv => cv.columnId === inputCol.columnId);
2098
+ if (!colValue)
2099
+ continue;
2100
+ // Handle different column types
2101
+ if (inputCol.columnType === "COLUMN_TYPE_DOCUMENT") {
2102
+ // Document columns - we need to fetch the actual file content
2103
+ const docs = colValue.value.documentCellValue?.documentValues ?? [];
2104
+ if (docs.length > 0) {
2105
+ // For documents, we'd need to download from source and re-upload
2106
+ // For now, add as placeholder - requires file content fetching
2107
+ results.push({
2108
+ source_row_id: row.id,
2109
+ status: "skipped",
2110
+ error: "Document cloning requires file content transfer (not yet implemented)",
2111
+ });
2112
+ continue;
2113
+ }
2114
+ }
2115
+ else if (inputCol.columnType === "COLUMN_TYPE_STRING") {
2116
+ let value = colValue.value.stringValue ?? "";
2117
+ // Sanitize if enabled
2118
+ if (sanitizationSession && value) {
2119
+ // Simple pattern-based sanitization for known types
2120
+ const detected = detectWithPatterns(value);
2121
+ for (const entity of detected) {
2122
+ const replacement = sanitizationSession.getOrCreateReplacement(entity.value, entity.type);
2123
+ value = value.split(entity.value).join(replacement);
2124
+ }
2125
+ // Also apply any user-provided examples
2126
+ if (sanitizeExamples) {
2127
+ for (const example of sanitizeExamples) {
2128
+ if (value.includes(example)) {
2129
+ const replacement = sanitizationSession.getOrCreateReplacement(example, "unknown");
2130
+ value = value.split(example).join(replacement);
2131
+ }
2132
+ }
2133
+ }
2134
+ }
2135
+ inputs.push({
2136
+ name: inputCol.name,
2137
+ string_value: value,
2138
+ });
2139
+ }
2140
+ else if (inputCol.columnType === "COLUMN_TYPE_ARRAY") {
2141
+ // Array values - take first value as string for simplicity
2142
+ const arrayVals = colValue.value.arrayValue?.arrayValues ?? [];
2143
+ if (arrayVals.length > 0) {
2144
+ let value = arrayVals[0].stringValue ?? "";
2145
+ if (sanitizationSession && value) {
2146
+ const detected = detectWithPatterns(value);
2147
+ for (const entity of detected) {
2148
+ const replacement = sanitizationSession.getOrCreateReplacement(entity.value, entity.type);
2149
+ value = value.split(entity.value).join(replacement);
2150
+ }
2151
+ }
2152
+ inputs.push({
2153
+ name: inputCol.name,
2154
+ string_value: value,
2155
+ });
2156
+ }
2157
+ }
2158
+ }
2159
+ if (inputs.length === 0) {
2160
+ results.push({
2161
+ source_row_id: row.id,
2162
+ status: "skipped",
2163
+ error: "No clonable input values found",
2164
+ });
2165
+ continue;
2166
+ }
2167
+ // Upload the row to target dashboard
2168
+ const uploadResult = await client.uploadAndRunDashboardRow(personaId, inputs);
2169
+ results.push({
2170
+ source_row_id: row.id,
2171
+ target_row_id: uploadResult.row_id,
2172
+ status: "cloned",
2173
+ });
2174
+ }
2175
+ catch (err) {
2176
+ results.push({
2177
+ source_row_id: row.id,
2178
+ status: "error",
2179
+ error: err instanceof Error ? err.message : String(err),
2180
+ });
2181
+ }
2182
+ }
2183
+ const clonedCount = results.filter(r => r.status === "cloned").length;
2184
+ const skippedCount = results.filter(r => r.status === "skipped").length;
2185
+ const errorCount = results.filter(r => r.status === "error").length;
2186
+ return {
2187
+ success: true,
2188
+ source_persona_id: sourcePersonaId,
2189
+ target_persona_id: personaId,
2190
+ source_rows: sourceRows.rows.length,
2191
+ cloned_rows: clonedCount,
2192
+ skipped_rows: skippedCount,
2193
+ error_rows: errorCount,
2194
+ sanitization_applied: !!sanitize,
2195
+ details: results,
2196
+ notes: [
2197
+ "Dashboard clone creates NEW rows in the target dashboard",
2198
+ "Workflows will re-run on the new rows to generate output columns",
2199
+ "Document columns require manual re-upload (file content transfer not implemented)",
2200
+ sanitize ? "Sanitization applied to string/array values" : null,
2201
+ ].filter(Boolean),
2202
+ };
2203
+ }
1775
2204
  default:
1776
2205
  return { error: `Unknown mode: ${mode}` };
1777
2206
  }
@@ -2733,6 +3162,94 @@ function sanitizeWorkflowForDeploy(workflow) {
2733
3162
  // ═══════════════════════════════════════════════════════════════════════════
2734
3163
  // Helper Functions
2735
3164
  // ═══════════════════════════════════════════════════════════════════════════
3165
+ /**
3166
+ * Sanitize a persona's content (PII, company names, etc.)
3167
+ *
3168
+ * Uses two-pass approach:
3169
+ * 1. Detection pass: Find sensitive entities in all text fields
3170
+ * 2. Alignment pass: Apply ALL mappings for cross-field consistency
3171
+ *
3172
+ * Supports typed examples: strings (type inferred) or objects with explicit type.
3173
+ */
3174
+ async function sanitizePersonaById(client, personaId, options = {}) {
3175
+ try {
3176
+ // Fetch full persona
3177
+ const persona = await client.getPersonaById(personaId);
3178
+ if (!persona) {
3179
+ return { success: false, preview: true, replacements_applied: 0, items_needing_review: 0, error: "Persona not found" };
3180
+ }
3181
+ // Build persona object for sanitization - include ALL text-containing fields
3182
+ const personaData = {
3183
+ name: persona.name,
3184
+ description: persona.description,
3185
+ proto_config: persona.proto_config,
3186
+ workflow_def: persona.workflow_def,
3187
+ };
3188
+ // Create sanitization session with default policy
3189
+ const session = new SanitizationSession();
3190
+ // Normalize examples to TypedExample format
3191
+ const typedExamples = options.examples?.map(ex => {
3192
+ if (typeof ex === "string")
3193
+ return ex;
3194
+ return {
3195
+ value: ex.value,
3196
+ type: ex.type,
3197
+ replacement: ex.replacement,
3198
+ };
3199
+ });
3200
+ // Run sanitization with two-pass approach
3201
+ const sanitizationOptions = {
3202
+ examples: typedExamples,
3203
+ auto_replace_threshold: "high", // Only auto-replace high confidence
3204
+ };
3205
+ const result = sanitizePersona(personaData, session, sanitizationOptions);
3206
+ // If preview mode or there are items needing review, return preview
3207
+ const isPreview = options.preview !== false;
3208
+ if (isPreview || result.summary.items_needing_review > 0) {
3209
+ // Build confirmation prompt with all replacements and items needing review
3210
+ const allReplacements = result.results.flatMap(r => r.result.replacements);
3211
+ const allNeedsReview = result.results.flatMap(r => r.result.needs_review);
3212
+ const confirmation = buildConfirmationPrompt(allReplacements, allNeedsReview);
3213
+ return {
3214
+ success: true,
3215
+ preview: true,
3216
+ replacements_applied: 0,
3217
+ items_needing_review: allNeedsReview.length,
3218
+ by_class: result.summary.by_class,
3219
+ by_type: result.summary.by_type,
3220
+ confirmation,
3221
+ };
3222
+ }
3223
+ // Apply sanitization by updating the persona
3224
+ const sanitizedProtoConfig = result.sanitized.proto_config;
3225
+ const sanitizedWorkflow = result.sanitized.workflow_def;
3226
+ const sanitizedDescription = result.sanitized.description;
3227
+ // Update the persona with sanitized data
3228
+ await client.updateAiEmployee({
3229
+ persona_id: personaId,
3230
+ description: sanitizedDescription ?? persona.description,
3231
+ proto_config: (sanitizedProtoConfig ?? persona.proto_config ?? {}),
3232
+ workflow: sanitizedWorkflow ?? persona.workflow_def,
3233
+ });
3234
+ return {
3235
+ success: true,
3236
+ preview: false,
3237
+ by_class: result.summary.by_class,
3238
+ by_type: result.summary.by_type,
3239
+ replacements_applied: result.summary.total_replacements,
3240
+ items_needing_review: result.summary.items_needing_review,
3241
+ };
3242
+ }
3243
+ catch (error) {
3244
+ return {
3245
+ success: false,
3246
+ preview: true,
3247
+ replacements_applied: 0,
3248
+ items_needing_review: 0,
3249
+ error: error instanceof Error ? error.message : String(error),
3250
+ };
3251
+ }
3252
+ }
2736
3253
  async function resolvePersona(client, identifier) {
2737
3254
  // Try as UUID first
2738
3255
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;