@superatomai/sdk-node 0.0.48-mds → 0.0.49-mds

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -16218,6 +16218,131 @@ async function handleReportCompRequest(data, components, sendMessage, anthropicA
16218
16218
  logger.info(`[REPORT_COMP_REQ] Response sent to client ${response.wsId || data.from?.id}`);
16219
16219
  }
16220
16220
 
16221
+ // src/handlers/schema-sanitize.ts
16222
+ var FIELD_KEYS = [
16223
+ "name",
16224
+ "type",
16225
+ "nativeType",
16226
+ "nullable",
16227
+ "isPrimaryKey",
16228
+ "isForeignKey",
16229
+ "description"
16230
+ ];
16231
+ var ENTITY_KEYS = ["name", "fullName", "description", "rowCount"];
16232
+ var SOURCE_KEYS = ["id", "name", "type"];
16233
+ var RELATIONSHIP_KEYS = ["from", "to", "type", "keys"];
16234
+ var ROOT_KEYS = ["database", "databaseType", "description", "extractedAt"];
16235
+ function pick(source, keys) {
16236
+ const out = {};
16237
+ if (!source || typeof source !== "object") return out;
16238
+ for (const key of keys) {
16239
+ if (source[key] !== void 0) out[key] = source[key];
16240
+ }
16241
+ return out;
16242
+ }
16243
+ function asArray(value) {
16244
+ return Array.isArray(value) ? value : [];
16245
+ }
16246
+ function sanitizeField(field) {
16247
+ const out = pick(field, FIELD_KEYS);
16248
+ if (field?.references && typeof field.references === "object") {
16249
+ out.references = pick(field.references, ["table", "column"]);
16250
+ }
16251
+ return out;
16252
+ }
16253
+ function sanitizeEntity(entity) {
16254
+ const out = pick(entity, ENTITY_KEYS);
16255
+ if (Array.isArray(entity?.fields)) out.fields = entity.fields.map(sanitizeField);
16256
+ if (Array.isArray(entity?.columns)) out.columns = entity.columns.map(sanitizeField);
16257
+ return out;
16258
+ }
16259
+ function sanitizeRelationship(relationship) {
16260
+ return pick(relationship, RELATIONSHIP_KEYS);
16261
+ }
16262
+ function sanitizeSchemaBody(body) {
16263
+ const out = {};
16264
+ if (Array.isArray(body?.entities)) out.entities = body.entities.map(sanitizeEntity);
16265
+ if (Array.isArray(body?.tables)) out.tables = body.tables.map(sanitizeEntity);
16266
+ if (Array.isArray(body?.relationships)) {
16267
+ out.relationships = body.relationships.map(sanitizeRelationship);
16268
+ }
16269
+ return out;
16270
+ }
16271
+ function sanitizeSource(source) {
16272
+ const out = pick(source, SOURCE_KEYS);
16273
+ const inner = source?.schema;
16274
+ if (inner && typeof inner === "object") out.schema = sanitizeSchemaBody(inner);
16275
+ return out;
16276
+ }
16277
+ function sanitizeSchemaForClient(schemaData) {
16278
+ if (!schemaData || typeof schemaData !== "object") return null;
16279
+ if (Array.isArray(schemaData)) return schemaData.map(sanitizeSource);
16280
+ const out = pick(schemaData, ROOT_KEYS);
16281
+ if (Array.isArray(schemaData.schema)) {
16282
+ out.schema = schemaData.schema.map(sanitizeSource);
16283
+ } else if (typeof schemaData.schema === "string") {
16284
+ out.schema = schemaData.schema;
16285
+ }
16286
+ return { ...out, ...sanitizeSchemaBody(schemaData) };
16287
+ }
16288
+ function generateSanitizedSchemaDocumentation(sanitized) {
16289
+ if (!sanitized || typeof sanitized !== "object") return "No database schema available.";
16290
+ const lines = [];
16291
+ const renderEntities = (entities, heading) => {
16292
+ if (!entities.length) return;
16293
+ if (heading) {
16294
+ lines.push(`SOURCE: ${heading}`);
16295
+ lines.push("");
16296
+ }
16297
+ for (const entity of entities) {
16298
+ lines.push(`TABLE: ${entity.fullName || entity.name}`);
16299
+ if (entity.description) lines.push(`Description: ${entity.description}`);
16300
+ if (typeof entity.rowCount === "number") {
16301
+ lines.push(`Row Count: ~${entity.rowCount.toLocaleString()}`);
16302
+ }
16303
+ lines.push("Columns:");
16304
+ for (const field of asArray(entity.fields).concat(asArray(entity.columns))) {
16305
+ let line = ` - ${field.name}: ${field.type}`;
16306
+ if (field.isPrimaryKey) line += " (PRIMARY KEY)";
16307
+ if (field.isForeignKey && field.references) {
16308
+ line += ` (FK -> ${field.references.table}.${field.references.column})`;
16309
+ }
16310
+ if (field.nullable === false) line += " NOT NULL";
16311
+ if (field.description) line += ` - ${field.description}`;
16312
+ lines.push(line);
16313
+ }
16314
+ lines.push("");
16315
+ }
16316
+ };
16317
+ const renderRelationships = (relationships) => {
16318
+ if (!relationships.length) return;
16319
+ lines.push("TABLE RELATIONSHIPS:");
16320
+ lines.push("");
16321
+ for (const rel of relationships) {
16322
+ lines.push(`${rel.from} -> ${rel.to} (${rel.type}): ${asArray(rel.keys).join(" = ")}`);
16323
+ }
16324
+ lines.push("");
16325
+ };
16326
+ const sources = Array.isArray(sanitized) ? sanitized : Array.isArray(sanitized.schema) ? sanitized.schema : null;
16327
+ if (sources) {
16328
+ for (const source of sources) {
16329
+ renderEntities(
16330
+ asArray(source.schema?.entities).concat(asArray(source.schema?.tables)),
16331
+ `${source.name} (${source.type})`
16332
+ );
16333
+ renderRelationships(asArray(source.schema?.relationships));
16334
+ }
16335
+ return lines.join("\n");
16336
+ }
16337
+ if (sanitized.database) lines.push(`Database: ${sanitized.database}`);
16338
+ if (typeof sanitized.schema === "string") lines.push(`Schema: ${sanitized.schema}`);
16339
+ if (sanitized.description) lines.push(`Description: ${sanitized.description}`);
16340
+ lines.push("");
16341
+ renderEntities(asArray(sanitized.entities).concat(asArray(sanitized.tables)));
16342
+ renderRelationships(asArray(sanitized.relationships));
16343
+ return lines.join("\n");
16344
+ }
16345
+
16221
16346
  // src/handlers/schema-request.ts
16222
16347
  async function handleSchemaRequest(message, sendMessage) {
16223
16348
  const startTime = Date.now();
@@ -16240,11 +16365,12 @@ async function handleSchemaRequest(message, sendMessage) {
16240
16365
  sendMessage(response2);
16241
16366
  return;
16242
16367
  }
16368
+ const safeSchema = sanitizeSchemaForClient(schemaData);
16243
16369
  const responseData = {
16244
- schema: schemaData
16370
+ schema: safeSchema
16245
16371
  };
16246
16372
  if (formatted) {
16247
- responseData.formatted = schema.generateSchemaDocumentation();
16373
+ responseData.formatted = generateSanitizedSchemaDocumentation(safeSchema);
16248
16374
  }
16249
16375
  const executionMs = Date.now() - startTime;
16250
16376
  logger.info(`[SchemaRequest] Schema retrieved successfully in ${executionMs}ms`);