@relazio/plugin-sdk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateEntityId = generateEntityId;
7
+ exports.generateEdgeId = generateEdgeId;
8
+ exports.generateRandomId = generateRandomId;
9
+ exports.normalizeValue = normalizeValue;
10
+ exports.isValidEntityId = isValidEntityId;
11
+ exports.isValidEdgeId = isValidEdgeId;
12
+ const crypto_1 = __importDefault(require("crypto"));
13
+ /**
14
+ * Genera un ID deterministico per un'entità
15
+ * Formato: {type}-{hash-short}
16
+ *
17
+ * @param type - Tipo di entità
18
+ * @param value - Valore dell'entità
19
+ * @returns ID univoco e deterministico
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * generateEntityId('ip', '8.8.8.8')
24
+ * // => "ip-c909e98d"
25
+ *
26
+ * generateEntityId('location', 'Mountain View, CA')
27
+ * // => "location-a3f42bc1"
28
+ * ```
29
+ */
30
+ function generateEntityId(type, value) {
31
+ const hash = crypto_1.default
32
+ .createHash('md5')
33
+ .update(value.toLowerCase().trim())
34
+ .digest('hex')
35
+ .substring(0, 8);
36
+ return `${type}-${hash}`;
37
+ }
38
+ /**
39
+ * Genera un ID deterministico per un edge
40
+ * Formato: edge-{hash-short}
41
+ *
42
+ * @param sourceId - ID dell'entità sorgente
43
+ * @param targetId - ID dell'entità target
44
+ * @param label - Label dell'edge (opzionale, per maggiore unicità)
45
+ * @returns ID univoco e deterministico
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * generateEdgeId('ip-c909e98d', 'location-a3f42bc1')
50
+ * // => "edge-4f8a2d1c"
51
+ *
52
+ * generateEdgeId('ip-c909e98d', 'org-5b3c1a2d', 'assigned by')
53
+ * // => "edge-7e9b3f4a"
54
+ * ```
55
+ */
56
+ function generateEdgeId(sourceId, targetId, label) {
57
+ const content = label
58
+ ? `${sourceId}-${targetId}-${label}`
59
+ : `${sourceId}-${targetId}`;
60
+ const hash = crypto_1.default
61
+ .createHash('md5')
62
+ .update(content.toLowerCase().trim())
63
+ .digest('hex')
64
+ .substring(0, 8);
65
+ return `edge-${hash}`;
66
+ }
67
+ /**
68
+ * Genera un ID casuale con prefisso
69
+ * Utile per entità temporanee o quando non serve deterministicità
70
+ *
71
+ * @param prefix - Prefisso per l'ID (es: 'note', 'temp')
72
+ * @returns ID univoco random
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * generateRandomId('note')
77
+ * // => "note-1735123456789"
78
+ * ```
79
+ */
80
+ function generateRandomId(prefix) {
81
+ return `${prefix}-${Date.now()}-${crypto_1.default.randomBytes(4).toString('hex')}`;
82
+ }
83
+ /**
84
+ * Normalizza un valore per la generazione di ID
85
+ * Rimuove spazi extra, converte a lowercase, rimuove caratteri speciali
86
+ *
87
+ * @param value - Valore da normalizzare
88
+ * @returns Valore normalizzato
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * normalizeValue(' Mountain View, CA ')
93
+ * // => "mountain-view-ca"
94
+ * ```
95
+ */
96
+ function normalizeValue(value) {
97
+ return value
98
+ .toLowerCase()
99
+ .trim()
100
+ .replace(/\s+/g, '-')
101
+ .replace(/[^a-z0-9-]/g, '')
102
+ .replace(/-+/g, '-')
103
+ .replace(/^-|-$/g, '');
104
+ }
105
+ /**
106
+ * Valida un ID di entità
107
+ *
108
+ * @param id - ID da validare
109
+ * @returns true se l'ID è valido
110
+ */
111
+ function isValidEntityId(id) {
112
+ return id.length > 0 && id.trim() === id;
113
+ }
114
+ /**
115
+ * Valida un ID di edge
116
+ *
117
+ * @param id - ID da validare
118
+ * @returns true se l'ID è valido
119
+ */
120
+ function isValidEdgeId(id) {
121
+ return id.length > 0 && id.trim() === id;
122
+ }
@@ -0,0 +1,109 @@
1
+ import type { OSINTEntity, OSINTEdge, TransformResult, TransformInput } from '../core/types';
2
+ /**
3
+ * Builder per costruire facilmente un TransformResult
4
+ * Gestisce automaticamente gli edge dall'entità di input alle nuove entità
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const result = new ResultBuilder(input)
9
+ * .addEntity(createIP('8.8.8.8'), 'resolves to')
10
+ * .addEntity(createLocation('Mountain View, CA'), 'located in')
11
+ * .setMessage('Found IP information')
12
+ * .build();
13
+ * ```
14
+ */
15
+ export declare class ResultBuilder {
16
+ private entities;
17
+ private edges;
18
+ private message?;
19
+ private metadata?;
20
+ private inputEntityId;
21
+ constructor(input: TransformInput);
22
+ /**
23
+ * Aggiungi un'entità con edge automatico dall'input
24
+ *
25
+ * @param entity - Entità da aggiungere
26
+ * @param edgeLabel - Label dell'edge (opzionale)
27
+ * @param edgeOptions - Opzioni per l'edge
28
+ */
29
+ addEntity(entity: OSINTEntity, edgeLabel?: string, edgeOptions?: {
30
+ relationship?: string;
31
+ metadata?: Record<string, any>;
32
+ }): this;
33
+ /**
34
+ * Aggiungi multiple entità con lo stesso tipo di edge
35
+ *
36
+ * @param entities - Array di entità da aggiungere
37
+ * @param edgeLabel - Label dell'edge (opzionale)
38
+ * @param edgeOptions - Opzioni per l'edge
39
+ */
40
+ addEntities(entities: OSINTEntity[], edgeLabel?: string, edgeOptions?: {
41
+ relationship?: string;
42
+ metadata?: Record<string, any>;
43
+ }): this;
44
+ /**
45
+ * Aggiungi un'entità senza edge automatico
46
+ */
47
+ addEntityOnly(entity: OSINTEntity): this;
48
+ /**
49
+ * Aggiungi un edge manualmente
50
+ */
51
+ addEdge(edge: OSINTEdge): this;
52
+ /**
53
+ * Aggiungi multiple edges
54
+ */
55
+ addEdges(edges: OSINTEdge[]): this;
56
+ /**
57
+ * Imposta il messaggio di successo
58
+ */
59
+ setMessage(message: string): this;
60
+ /**
61
+ * Imposta metadata addizionali
62
+ */
63
+ setMetadata(metadata: Record<string, any>): this;
64
+ /**
65
+ * Aggiungi metadata
66
+ */
67
+ addMetadata(key: string, value: any): this;
68
+ /**
69
+ * Costruisci il risultato finale
70
+ */
71
+ build(): TransformResult;
72
+ /**
73
+ * Ottieni il numero di entità aggiunte
74
+ */
75
+ getEntityCount(): number;
76
+ /**
77
+ * Ottieni il numero di edge aggiunti
78
+ */
79
+ getEdgeCount(): number;
80
+ /**
81
+ * Verifica se ci sono risultati
82
+ */
83
+ hasResults(): boolean;
84
+ }
85
+ /**
86
+ * Helper veloce per creare un risultato vuoto (no results)
87
+ */
88
+ export declare function emptyResult(message?: string): TransformResult;
89
+ /**
90
+ * Helper veloce per creare un risultato di errore
91
+ */
92
+ export declare function errorResult(error: string | Error): TransformResult;
93
+ /**
94
+ * Helper per creare un risultato con una singola entità
95
+ */
96
+ export declare function singleEntityResult(inputEntityId: string, entity: OSINTEntity, edgeLabel: string, options?: {
97
+ relationship?: string;
98
+ message?: string;
99
+ metadata?: Record<string, any>;
100
+ }): TransformResult;
101
+ /**
102
+ * Helper per creare un risultato con multiple entità dello stesso tipo
103
+ */
104
+ export declare function multiEntityResult(inputEntityId: string, entities: OSINTEntity[], edgeLabel: string, options?: {
105
+ relationship?: string;
106
+ message?: string;
107
+ metadata?: Record<string, any>;
108
+ }): TransformResult;
109
+ //# sourceMappingURL=result-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-builder.d.ts","sourceRoot":"","sources":["../../src/utils/result-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG7F;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAqB;IACrC,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,OAAO,CAAC,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAC,CAAsB;IACvC,OAAO,CAAC,aAAa,CAAS;gBAElB,KAAK,EAAE,cAAc;IAIjC;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,kBAAkB,CAChC,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,WAAW,EACnB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;IACR,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC,GACA,eAAe,CAYjB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,WAAW,EAAE,EACvB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;IACR,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC,GACA,eAAe,CAcjB"}
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResultBuilder = void 0;
4
+ exports.emptyResult = emptyResult;
5
+ exports.errorResult = errorResult;
6
+ exports.singleEntityResult = singleEntityResult;
7
+ exports.multiEntityResult = multiEntityResult;
8
+ const builders_1 = require("./builders");
9
+ /**
10
+ * Builder per costruire facilmente un TransformResult
11
+ * Gestisce automaticamente gli edge dall'entità di input alle nuove entità
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const result = new ResultBuilder(input)
16
+ * .addEntity(createIP('8.8.8.8'), 'resolves to')
17
+ * .addEntity(createLocation('Mountain View, CA'), 'located in')
18
+ * .setMessage('Found IP information')
19
+ * .build();
20
+ * ```
21
+ */
22
+ class ResultBuilder {
23
+ constructor(input) {
24
+ this.entities = [];
25
+ this.edges = [];
26
+ this.inputEntityId = input.entity.id;
27
+ }
28
+ /**
29
+ * Aggiungi un'entità con edge automatico dall'input
30
+ *
31
+ * @param entity - Entità da aggiungere
32
+ * @param edgeLabel - Label dell'edge (opzionale)
33
+ * @param edgeOptions - Opzioni per l'edge
34
+ */
35
+ addEntity(entity, edgeLabel, edgeOptions) {
36
+ this.entities.push(entity);
37
+ // Se viene specificata una label, crea automaticamente l'edge
38
+ if (edgeLabel) {
39
+ const edge = (0, builders_1.createEdge)(this.inputEntityId, entity.id, edgeLabel, edgeOptions);
40
+ this.edges.push(edge);
41
+ }
42
+ return this;
43
+ }
44
+ /**
45
+ * Aggiungi multiple entità con lo stesso tipo di edge
46
+ *
47
+ * @param entities - Array di entità da aggiungere
48
+ * @param edgeLabel - Label dell'edge (opzionale)
49
+ * @param edgeOptions - Opzioni per l'edge
50
+ */
51
+ addEntities(entities, edgeLabel, edgeOptions) {
52
+ for (const entity of entities) {
53
+ this.addEntity(entity, edgeLabel, edgeOptions);
54
+ }
55
+ return this;
56
+ }
57
+ /**
58
+ * Aggiungi un'entità senza edge automatico
59
+ */
60
+ addEntityOnly(entity) {
61
+ this.entities.push(entity);
62
+ return this;
63
+ }
64
+ /**
65
+ * Aggiungi un edge manualmente
66
+ */
67
+ addEdge(edge) {
68
+ this.edges.push(edge);
69
+ return this;
70
+ }
71
+ /**
72
+ * Aggiungi multiple edges
73
+ */
74
+ addEdges(edges) {
75
+ this.edges.push(...edges);
76
+ return this;
77
+ }
78
+ /**
79
+ * Imposta il messaggio di successo
80
+ */
81
+ setMessage(message) {
82
+ this.message = message;
83
+ return this;
84
+ }
85
+ /**
86
+ * Imposta metadata addizionali
87
+ */
88
+ setMetadata(metadata) {
89
+ this.metadata = metadata;
90
+ return this;
91
+ }
92
+ /**
93
+ * Aggiungi metadata
94
+ */
95
+ addMetadata(key, value) {
96
+ if (!this.metadata) {
97
+ this.metadata = {};
98
+ }
99
+ this.metadata[key] = value;
100
+ return this;
101
+ }
102
+ /**
103
+ * Costruisci il risultato finale
104
+ */
105
+ build() {
106
+ return {
107
+ success: true,
108
+ entities: this.entities,
109
+ edges: this.edges,
110
+ ...(this.message && { message: this.message }),
111
+ ...(this.metadata && { metadata: this.metadata }),
112
+ };
113
+ }
114
+ /**
115
+ * Ottieni il numero di entità aggiunte
116
+ */
117
+ getEntityCount() {
118
+ return this.entities.length;
119
+ }
120
+ /**
121
+ * Ottieni il numero di edge aggiunti
122
+ */
123
+ getEdgeCount() {
124
+ return this.edges.length;
125
+ }
126
+ /**
127
+ * Verifica se ci sono risultati
128
+ */
129
+ hasResults() {
130
+ return this.entities.length > 0;
131
+ }
132
+ }
133
+ exports.ResultBuilder = ResultBuilder;
134
+ /**
135
+ * Helper veloce per creare un risultato vuoto (no results)
136
+ */
137
+ function emptyResult(message) {
138
+ return {
139
+ success: true,
140
+ entities: [],
141
+ edges: [],
142
+ ...(message && { message }),
143
+ };
144
+ }
145
+ /**
146
+ * Helper veloce per creare un risultato di errore
147
+ */
148
+ function errorResult(error) {
149
+ return {
150
+ success: false,
151
+ entities: [],
152
+ edges: [],
153
+ message: error instanceof Error ? error.message : error,
154
+ };
155
+ }
156
+ /**
157
+ * Helper per creare un risultato con una singola entità
158
+ */
159
+ function singleEntityResult(inputEntityId, entity, edgeLabel, options) {
160
+ const edge = (0, builders_1.createEdge)(inputEntityId, entity.id, edgeLabel, {
161
+ relationship: options?.relationship,
162
+ });
163
+ return {
164
+ success: true,
165
+ entities: [entity],
166
+ edges: [edge],
167
+ ...(options?.message && { message: options.message }),
168
+ ...(options?.metadata && { metadata: options.metadata }),
169
+ };
170
+ }
171
+ /**
172
+ * Helper per creare un risultato con multiple entità dello stesso tipo
173
+ */
174
+ function multiEntityResult(inputEntityId, entities, edgeLabel, options) {
175
+ const edges = entities.map(entity => (0, builders_1.createEdge)(inputEntityId, entity.id, edgeLabel, {
176
+ relationship: options?.relationship,
177
+ }));
178
+ return {
179
+ success: true,
180
+ entities,
181
+ edges,
182
+ ...(options?.message && { message: options.message }),
183
+ ...(options?.metadata && { metadata: options.metadata }),
184
+ };
185
+ }