@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.js +128 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +128 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -16278,6 +16278,131 @@ async function handleReportCompRequest(data, components, sendMessage, anthropicA
|
|
|
16278
16278
|
logger.info(`[REPORT_COMP_REQ] Response sent to client ${response.wsId || data.from?.id}`);
|
|
16279
16279
|
}
|
|
16280
16280
|
|
|
16281
|
+
// src/handlers/schema-sanitize.ts
|
|
16282
|
+
var FIELD_KEYS = [
|
|
16283
|
+
"name",
|
|
16284
|
+
"type",
|
|
16285
|
+
"nativeType",
|
|
16286
|
+
"nullable",
|
|
16287
|
+
"isPrimaryKey",
|
|
16288
|
+
"isForeignKey",
|
|
16289
|
+
"description"
|
|
16290
|
+
];
|
|
16291
|
+
var ENTITY_KEYS = ["name", "fullName", "description", "rowCount"];
|
|
16292
|
+
var SOURCE_KEYS = ["id", "name", "type"];
|
|
16293
|
+
var RELATIONSHIP_KEYS = ["from", "to", "type", "keys"];
|
|
16294
|
+
var ROOT_KEYS = ["database", "databaseType", "description", "extractedAt"];
|
|
16295
|
+
function pick(source, keys) {
|
|
16296
|
+
const out = {};
|
|
16297
|
+
if (!source || typeof source !== "object") return out;
|
|
16298
|
+
for (const key of keys) {
|
|
16299
|
+
if (source[key] !== void 0) out[key] = source[key];
|
|
16300
|
+
}
|
|
16301
|
+
return out;
|
|
16302
|
+
}
|
|
16303
|
+
function asArray(value) {
|
|
16304
|
+
return Array.isArray(value) ? value : [];
|
|
16305
|
+
}
|
|
16306
|
+
function sanitizeField(field) {
|
|
16307
|
+
const out = pick(field, FIELD_KEYS);
|
|
16308
|
+
if (field?.references && typeof field.references === "object") {
|
|
16309
|
+
out.references = pick(field.references, ["table", "column"]);
|
|
16310
|
+
}
|
|
16311
|
+
return out;
|
|
16312
|
+
}
|
|
16313
|
+
function sanitizeEntity(entity) {
|
|
16314
|
+
const out = pick(entity, ENTITY_KEYS);
|
|
16315
|
+
if (Array.isArray(entity?.fields)) out.fields = entity.fields.map(sanitizeField);
|
|
16316
|
+
if (Array.isArray(entity?.columns)) out.columns = entity.columns.map(sanitizeField);
|
|
16317
|
+
return out;
|
|
16318
|
+
}
|
|
16319
|
+
function sanitizeRelationship(relationship) {
|
|
16320
|
+
return pick(relationship, RELATIONSHIP_KEYS);
|
|
16321
|
+
}
|
|
16322
|
+
function sanitizeSchemaBody(body) {
|
|
16323
|
+
const out = {};
|
|
16324
|
+
if (Array.isArray(body?.entities)) out.entities = body.entities.map(sanitizeEntity);
|
|
16325
|
+
if (Array.isArray(body?.tables)) out.tables = body.tables.map(sanitizeEntity);
|
|
16326
|
+
if (Array.isArray(body?.relationships)) {
|
|
16327
|
+
out.relationships = body.relationships.map(sanitizeRelationship);
|
|
16328
|
+
}
|
|
16329
|
+
return out;
|
|
16330
|
+
}
|
|
16331
|
+
function sanitizeSource(source) {
|
|
16332
|
+
const out = pick(source, SOURCE_KEYS);
|
|
16333
|
+
const inner = source?.schema;
|
|
16334
|
+
if (inner && typeof inner === "object") out.schema = sanitizeSchemaBody(inner);
|
|
16335
|
+
return out;
|
|
16336
|
+
}
|
|
16337
|
+
function sanitizeSchemaForClient(schemaData) {
|
|
16338
|
+
if (!schemaData || typeof schemaData !== "object") return null;
|
|
16339
|
+
if (Array.isArray(schemaData)) return schemaData.map(sanitizeSource);
|
|
16340
|
+
const out = pick(schemaData, ROOT_KEYS);
|
|
16341
|
+
if (Array.isArray(schemaData.schema)) {
|
|
16342
|
+
out.schema = schemaData.schema.map(sanitizeSource);
|
|
16343
|
+
} else if (typeof schemaData.schema === "string") {
|
|
16344
|
+
out.schema = schemaData.schema;
|
|
16345
|
+
}
|
|
16346
|
+
return { ...out, ...sanitizeSchemaBody(schemaData) };
|
|
16347
|
+
}
|
|
16348
|
+
function generateSanitizedSchemaDocumentation(sanitized) {
|
|
16349
|
+
if (!sanitized || typeof sanitized !== "object") return "No database schema available.";
|
|
16350
|
+
const lines = [];
|
|
16351
|
+
const renderEntities = (entities, heading) => {
|
|
16352
|
+
if (!entities.length) return;
|
|
16353
|
+
if (heading) {
|
|
16354
|
+
lines.push(`SOURCE: ${heading}`);
|
|
16355
|
+
lines.push("");
|
|
16356
|
+
}
|
|
16357
|
+
for (const entity of entities) {
|
|
16358
|
+
lines.push(`TABLE: ${entity.fullName || entity.name}`);
|
|
16359
|
+
if (entity.description) lines.push(`Description: ${entity.description}`);
|
|
16360
|
+
if (typeof entity.rowCount === "number") {
|
|
16361
|
+
lines.push(`Row Count: ~${entity.rowCount.toLocaleString()}`);
|
|
16362
|
+
}
|
|
16363
|
+
lines.push("Columns:");
|
|
16364
|
+
for (const field of asArray(entity.fields).concat(asArray(entity.columns))) {
|
|
16365
|
+
let line = ` - ${field.name}: ${field.type}`;
|
|
16366
|
+
if (field.isPrimaryKey) line += " (PRIMARY KEY)";
|
|
16367
|
+
if (field.isForeignKey && field.references) {
|
|
16368
|
+
line += ` (FK -> ${field.references.table}.${field.references.column})`;
|
|
16369
|
+
}
|
|
16370
|
+
if (field.nullable === false) line += " NOT NULL";
|
|
16371
|
+
if (field.description) line += ` - ${field.description}`;
|
|
16372
|
+
lines.push(line);
|
|
16373
|
+
}
|
|
16374
|
+
lines.push("");
|
|
16375
|
+
}
|
|
16376
|
+
};
|
|
16377
|
+
const renderRelationships = (relationships) => {
|
|
16378
|
+
if (!relationships.length) return;
|
|
16379
|
+
lines.push("TABLE RELATIONSHIPS:");
|
|
16380
|
+
lines.push("");
|
|
16381
|
+
for (const rel of relationships) {
|
|
16382
|
+
lines.push(`${rel.from} -> ${rel.to} (${rel.type}): ${asArray(rel.keys).join(" = ")}`);
|
|
16383
|
+
}
|
|
16384
|
+
lines.push("");
|
|
16385
|
+
};
|
|
16386
|
+
const sources = Array.isArray(sanitized) ? sanitized : Array.isArray(sanitized.schema) ? sanitized.schema : null;
|
|
16387
|
+
if (sources) {
|
|
16388
|
+
for (const source of sources) {
|
|
16389
|
+
renderEntities(
|
|
16390
|
+
asArray(source.schema?.entities).concat(asArray(source.schema?.tables)),
|
|
16391
|
+
`${source.name} (${source.type})`
|
|
16392
|
+
);
|
|
16393
|
+
renderRelationships(asArray(source.schema?.relationships));
|
|
16394
|
+
}
|
|
16395
|
+
return lines.join("\n");
|
|
16396
|
+
}
|
|
16397
|
+
if (sanitized.database) lines.push(`Database: ${sanitized.database}`);
|
|
16398
|
+
if (typeof sanitized.schema === "string") lines.push(`Schema: ${sanitized.schema}`);
|
|
16399
|
+
if (sanitized.description) lines.push(`Description: ${sanitized.description}`);
|
|
16400
|
+
lines.push("");
|
|
16401
|
+
renderEntities(asArray(sanitized.entities).concat(asArray(sanitized.tables)));
|
|
16402
|
+
renderRelationships(asArray(sanitized.relationships));
|
|
16403
|
+
return lines.join("\n");
|
|
16404
|
+
}
|
|
16405
|
+
|
|
16281
16406
|
// src/handlers/schema-request.ts
|
|
16282
16407
|
async function handleSchemaRequest(message, sendMessage) {
|
|
16283
16408
|
const startTime = Date.now();
|
|
@@ -16300,11 +16425,12 @@ async function handleSchemaRequest(message, sendMessage) {
|
|
|
16300
16425
|
sendMessage(response2);
|
|
16301
16426
|
return;
|
|
16302
16427
|
}
|
|
16428
|
+
const safeSchema = sanitizeSchemaForClient(schemaData);
|
|
16303
16429
|
const responseData = {
|
|
16304
|
-
schema:
|
|
16430
|
+
schema: safeSchema
|
|
16305
16431
|
};
|
|
16306
16432
|
if (formatted) {
|
|
16307
|
-
responseData.formatted =
|
|
16433
|
+
responseData.formatted = generateSanitizedSchemaDocumentation(safeSchema);
|
|
16308
16434
|
}
|
|
16309
16435
|
const executionMs = Date.now() - startTime;
|
|
16310
16436
|
logger.info(`[SchemaRequest] Schema retrieved successfully in ${executionMs}ms`);
|