@cortexa/core 0.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/LICENSE +191 -0
- package/README.md +423 -0
- package/dist/chunk-6VDDIS65.js +21 -0
- package/dist/chunk-6VDDIS65.js.map +1 -0
- package/dist/chunk-T4NUBJ7T.js +13 -0
- package/dist/chunk-T4NUBJ7T.js.map +1 -0
- package/dist/cli/chunk-7HYSAZX4.js +15 -0
- package/dist/cli/chunk-7HYSAZX4.js.map +1 -0
- package/dist/cli/chunk-HVMZ6P54.js +23 -0
- package/dist/cli/chunk-HVMZ6P54.js.map +1 -0
- package/dist/cli/index.js +5921 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/mongodb-WTT5HVQH.js +145 -0
- package/dist/cli/mongodb-WTT5HVQH.js.map +1 -0
- package/dist/cli/mongodb-stream-Q5OBFWZP.js +93 -0
- package/dist/cli/mongodb-stream-Q5OBFWZP.js.map +1 -0
- package/dist/cli/mssql-KG7P3R2I.js +79 -0
- package/dist/cli/mssql-KG7P3R2I.js.map +1 -0
- package/dist/cli/mysql-stream-Z2MZS2KF.js +195 -0
- package/dist/cli/mysql-stream-Z2MZS2KF.js.map +1 -0
- package/dist/cli/postgres-stream-S4CRICEA.js +237 -0
- package/dist/cli/postgres-stream-S4CRICEA.js.map +1 -0
- package/dist/index.cjs +5733 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +670 -0
- package/dist/index.d.ts +670 -0
- package/dist/index.js +4805 -0
- package/dist/index.js.map +1 -0
- package/dist/mongodb-M5UGJZTC.js +143 -0
- package/dist/mongodb-M5UGJZTC.js.map +1 -0
- package/dist/mongodb-stream-Q23UHLTM.js +92 -0
- package/dist/mongodb-stream-Q23UHLTM.js.map +1 -0
- package/dist/mssql-I27XEHQ2.js +77 -0
- package/dist/mssql-I27XEHQ2.js.map +1 -0
- package/dist/mysql-stream-YCNLPPPG.js +194 -0
- package/dist/mysql-stream-YCNLPPPG.js.map +1 -0
- package/dist/postgres-stream-A7EVYUX2.js +236 -0
- package/dist/postgres-stream-A7EVYUX2.js.map +1 -0
- package/package.json +105 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Mohammed Sorguli
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# Cortexa
|
|
4
|
+
|
|
5
|
+
**The intelligence layer that turns any database into a self-analyzing, self-explaining system.**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@cortexa/core)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://nodejs.org)
|
|
10
|
+
[](https://www.typescriptlang.org)
|
|
11
|
+
|
|
12
|
+
[Documentation](#quick-start) · [CLI Reference](#cli-reference) · [API Reference](#programmatic-api) · [Bug Reports](https://github.com/Mohammed3MG/cortexa/issues)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Table of Contents
|
|
19
|
+
|
|
20
|
+
- [What is Cortexa?](#what-is-cortexa)
|
|
21
|
+
- [Quick Start](#quick-start)
|
|
22
|
+
- [Programmatic API](#programmatic-api)
|
|
23
|
+
- [Architecture](#architecture)
|
|
24
|
+
- [Supported Databases](#supported-databases)
|
|
25
|
+
- [CLI Reference](#cli-reference)
|
|
26
|
+
- [Configuration](#configuration)
|
|
27
|
+
- [Requirements](#requirements)
|
|
28
|
+
- [Contributing](#contributing)
|
|
29
|
+
- [License](#license)
|
|
30
|
+
|
|
31
|
+
## What is Cortexa?
|
|
32
|
+
|
|
33
|
+
Databases hold your application's truth, but they can't tell you what's happening inside them. You find out about problems after users complain, and understanding *why* something went wrong means digging through logs manually.
|
|
34
|
+
|
|
35
|
+
**Cortexa changes that.** It connects to your existing database as a **read-only** observer — no migrations, no ORMs, no schema changes. It discovers your schema automatically, watches for changes in real-time, learns what "normal" looks like, and alerts you when something is off. Then it explains *why* in plain English.
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
Your Database ──(read-only)──> Cortexa ──> Intelligence
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Key principles
|
|
42
|
+
|
|
43
|
+
- **Read-only by design** — Cortexa never writes to your database. All intelligence is stored locally in `.cortexa/cortexa.db` (SQLite).
|
|
44
|
+
- **Zero configuration schema** — Point it at your database and it introspects everything automatically. No models to define, no schemas to maintain.
|
|
45
|
+
- **Database-agnostic** — Works with PostgreSQL, MySQL, SQLite, MariaDB, CockroachDB, MongoDB, and SQL Server out of the box.
|
|
46
|
+
- **LLM-powered reasoning** — Uses AI to classify entities, explain anomalies, trace causal chains, and answer natural language questions about your data.
|
|
47
|
+
|
|
48
|
+
### What it does
|
|
49
|
+
|
|
50
|
+
| Capability | What it does |
|
|
51
|
+
|---|---|
|
|
52
|
+
| **Schema Discovery** | Introspects tables, columns, foreign keys. LLM classifies entity types (transaction, user, config, etc.) and maps relationships. |
|
|
53
|
+
| **Change Detection** | Polls or streams (CDC) for INSERTs, UPDATEs, DELETEs. Tracks per-table operation counts over time. |
|
|
54
|
+
| **Behavioral Baselines** | Learns normal rates (inserts/min, updates/min) per entity using rolling statistics. Adapts as your application evolves. |
|
|
55
|
+
| **Anomaly Detection** | Flags rate spikes, rate drops, and stuck records by comparing live activity against learned baselines. |
|
|
56
|
+
| **State Reasoning** | Tracks state machine transitions (e.g. `pending → confirmed → shipped`). Detects skipped states and stuck workflows. |
|
|
57
|
+
| **Cross-Entity Analytics** | Correlates activity across related entities (e.g. orders and payments). Tracks value distributions and temporal patterns. |
|
|
58
|
+
| **Knowledge Graph** | Connects entities, events, anomalies, and insights into a traversable causal graph. Find root causes by following edges. |
|
|
59
|
+
| **Autonomous Actions** | Rule-based recommendations with configurable governance: `advisory`, `autonomous`, or `manual`. |
|
|
60
|
+
| **Explain** | AI-powered root cause analysis. Ask "why did this anomaly happen?" and get a structured explanation. |
|
|
61
|
+
| **Ask** | Natural language interface. Query your entire intelligence stack in plain English. |
|
|
62
|
+
|
|
63
|
+
## Quick Start
|
|
64
|
+
|
|
65
|
+
### 1. Install
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install @cortexa/core
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. Initialize configuration
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npx cortexa init
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
This generates `cortexa.config.ts` in your project root:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { defineConfig } from '@cortexa/core';
|
|
81
|
+
|
|
82
|
+
export default defineConfig({
|
|
83
|
+
connection: {
|
|
84
|
+
type: 'postgres', // 'mysql' | 'sqlite' | 'mariadb' | 'cockroachdb' | 'mongodb' | 'mssql'
|
|
85
|
+
url: process.env.DATABASE_URL,
|
|
86
|
+
},
|
|
87
|
+
llm: {
|
|
88
|
+
provider: 'openai', // 'anthropic' | 'deepseek'
|
|
89
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. Test connection
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npx cortexa status
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
Connected to mydb (PostgreSQL)
|
|
102
|
+
Tables: 24
|
|
103
|
+
Storage: .cortexa/cortexa.db (initialized)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 4. Discover your schema
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npx cortexa discover
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Cortexa introspects every table, sends the schema to your LLM for entity classification, and maps relationships from foreign keys. Results are stored locally.
|
|
113
|
+
|
|
114
|
+
### 5. Watch for changes
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
npx cortexa watch
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Starts the intelligence pipeline: polls for changes, builds baselines, detects anomalies, tracks state transitions, and generates insights — all in real-time.
|
|
121
|
+
|
|
122
|
+
### 6. Ask questions
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
npx cortexa ask "What is the overall health of the database?"
|
|
126
|
+
npx cortexa ask "Are orders and payments correlated?"
|
|
127
|
+
npx cortexa explain anomaly 1
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Programmatic API
|
|
131
|
+
|
|
132
|
+
Everything available through the CLI is also available as a TypeScript API:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { Cortexa } from '@cortexa/core';
|
|
136
|
+
|
|
137
|
+
const cortexa = new Cortexa({
|
|
138
|
+
connection: {
|
|
139
|
+
type: 'postgres',
|
|
140
|
+
host: 'localhost',
|
|
141
|
+
port: 5432,
|
|
142
|
+
database: 'myapp',
|
|
143
|
+
user: 'readonly_user',
|
|
144
|
+
password: process.env.DB_PASSWORD,
|
|
145
|
+
},
|
|
146
|
+
llm: {
|
|
147
|
+
provider: 'openai',
|
|
148
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
149
|
+
},
|
|
150
|
+
knowledge: { enabled: true },
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await cortexa.connect();
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Schema Discovery
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const { entities, relationships } = await cortexa.discover();
|
|
160
|
+
// entities: [{ name: 'orders', type: 'transaction', columns: [...] }, ...]
|
|
161
|
+
// relationships: [{ from: 'orders', to: 'users', type: 'many-to-one' }, ...]
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Real-time Monitoring
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
cortexa.on('event', (event) => console.log('Change:', event));
|
|
168
|
+
cortexa.on('anomaly', (anomaly) => console.log('Anomaly:', anomaly));
|
|
169
|
+
cortexa.on('insight', (insight) => console.log('Insight:', insight));
|
|
170
|
+
|
|
171
|
+
await cortexa.watch();
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Querying Intelligence
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
const events = cortexa.getEvents({ entity: 'orders', last: 100 });
|
|
178
|
+
const anomalies = cortexa.getAnomalies({ severity: 'high' });
|
|
179
|
+
const baselines = cortexa.getBaselines();
|
|
180
|
+
const transitions = cortexa.getTransitions('orders');
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Knowledge Graph
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const graph = cortexa.graph();
|
|
187
|
+
const summary = graph.getSummary(); // node/edge counts, top entities
|
|
188
|
+
const causes = graph.causesOf(nodeId); // BFS traversal of causal chain
|
|
189
|
+
const impact = graph.impactOf(nodeId); // downstream effects
|
|
190
|
+
const intel = graph.entity('orders').intelligence(); // per-entity aggregation
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### AI-Powered Analysis
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
const explanation = await cortexa.explain({ type: 'anomaly', id: 1 });
|
|
197
|
+
const answer = await cortexa.ask('Why did order activity spike today?');
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
await cortexa.disconnect();
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Architecture
|
|
205
|
+
|
|
206
|
+
Cortexa builds intelligence through a layered pipeline. Each layer feeds into the next, producing progressively higher-level understanding of your database.
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
┌─────────────────────────────────────────────────────────┐
|
|
210
|
+
│ Your Database │
|
|
211
|
+
│ (read-only connection) │
|
|
212
|
+
└───────────────────────┬─────────────────────────────────┘
|
|
213
|
+
│
|
|
214
|
+
v
|
|
215
|
+
┌───────────────────────────────────────────────────────┐
|
|
216
|
+
│ Schema Discovery │
|
|
217
|
+
│ Tables, columns, FKs, indexes │
|
|
218
|
+
│ LLM classifies entity types & maps relationships │
|
|
219
|
+
└───────────────────────┬───────────────────────────────┘
|
|
220
|
+
│
|
|
221
|
+
v
|
|
222
|
+
┌───────────────────────────────────────────────────────┐
|
|
223
|
+
│ Change Detection │
|
|
224
|
+
│ Polling (hash-based diffing) or CDC streaming │
|
|
225
|
+
│ INSERT / UPDATE / DELETE per table │
|
|
226
|
+
└───────────┬───────────┬───────────┬───────────────────┘
|
|
227
|
+
│ │ │
|
|
228
|
+
v v v
|
|
229
|
+
┌──────────┐ ┌──────────┐ ┌──────────┐
|
|
230
|
+
│ Baselines│ │ State │ │ Analytics│
|
|
231
|
+
│ rolling │ │ Machines │ │ correlate│
|
|
232
|
+
│ stats │ │ workflow │ │ distribs │
|
|
233
|
+
│ per-op │ │ tracking │ │ temporal │
|
|
234
|
+
└────┬─────┘ └────┬─────┘ └────┬─────┘
|
|
235
|
+
│ │ │
|
|
236
|
+
v v v
|
|
237
|
+
┌───────────────────────────────────────────────────────┐
|
|
238
|
+
│ Anomaly Detection │
|
|
239
|
+
│ Rate spikes, rate drops, stuck records │
|
|
240
|
+
│ Skipped states, unexpected transitions │
|
|
241
|
+
└───────────────────────┬───────────────────────────────┘
|
|
242
|
+
│
|
|
243
|
+
v
|
|
244
|
+
┌───────────────────────────────────────────────────────┐
|
|
245
|
+
│ Knowledge Graph │
|
|
246
|
+
│ Entities, events, anomalies, insights │
|
|
247
|
+
│ Causal chains, traversal, per-entity intelligence │
|
|
248
|
+
└───────────┬───────────────────────┬───────────────────┘
|
|
249
|
+
│ │
|
|
250
|
+
v v
|
|
251
|
+
┌──────────────┐ ┌──────────────┐
|
|
252
|
+
│ Actions │ │ Explain / Ask│
|
|
253
|
+
│ rule-based │ │ AI-powered │
|
|
254
|
+
│ governance │ │ natural │
|
|
255
|
+
│ pipeline │ │ language │
|
|
256
|
+
└──────────────┘ └──────────────┘
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
**Data flow:** Your database is never modified. Cortexa reads schema metadata and change data, processes it through each layer, and stores all derived intelligence in a local SQLite file (`.cortexa/cortexa.db`). The LLM is called only for schema classification, explain, and ask — all other intelligence is computed locally.
|
|
260
|
+
|
|
261
|
+
## Supported Databases
|
|
262
|
+
|
|
263
|
+
| Database | Type | Polling | Streaming (CDC) | Driver |
|
|
264
|
+
|----------|------|:-------:|:---------------:|--------|
|
|
265
|
+
| **PostgreSQL** | Relational | Yes | Yes — Logical Replication | `pg` (included) |
|
|
266
|
+
| **MySQL** | Relational | Yes | Yes — Binlog | `mysql2` (included) |
|
|
267
|
+
| **SQLite** | Embedded | Yes | — | `better-sqlite3` (included) |
|
|
268
|
+
| **MariaDB** | Relational | Yes | Yes — Binlog | `mysql2` (included) |
|
|
269
|
+
| **CockroachDB** | Distributed | Yes | — | `pg` (included) |
|
|
270
|
+
| **MongoDB** | Document | Yes | Yes — Change Streams | `mongodb` (optional) |
|
|
271
|
+
| **SQL Server** | Relational | Yes | — | `mssql` (optional) |
|
|
272
|
+
|
|
273
|
+
**Included** drivers ship with Cortexa — no extra install needed. **Optional** drivers require a separate install:
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
# MongoDB
|
|
277
|
+
npm install mongodb
|
|
278
|
+
|
|
279
|
+
# SQL Server
|
|
280
|
+
npm install mssql
|
|
281
|
+
|
|
282
|
+
# PostgreSQL CDC streaming
|
|
283
|
+
npm install pg-logical-replication
|
|
284
|
+
|
|
285
|
+
# MySQL / MariaDB CDC streaming
|
|
286
|
+
npm install @powersync/mysql-zongji
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## CLI Reference
|
|
290
|
+
|
|
291
|
+
| Command | Description |
|
|
292
|
+
|---------|-------------|
|
|
293
|
+
| `cortexa init` | Generate config file (`--demo` for full example) |
|
|
294
|
+
| `cortexa status` | Test connection and show table count |
|
|
295
|
+
| `cortexa discover` | Discover and classify schema |
|
|
296
|
+
| `cortexa entities` | List classified entities |
|
|
297
|
+
| `cortexa relationships` | List entity relationships |
|
|
298
|
+
| `cortexa watch` | Start the intelligence pipeline (`--once` for single poll) |
|
|
299
|
+
| `cortexa events` | List recent change events |
|
|
300
|
+
| `cortexa baselines` | Show learned rate baselines |
|
|
301
|
+
| `cortexa anomalies` | List detected anomalies |
|
|
302
|
+
| `cortexa insights` | List insights from state analysis |
|
|
303
|
+
| `cortexa transitions` | Show state transition stats |
|
|
304
|
+
| `cortexa correlations` | Show cross-entity correlations |
|
|
305
|
+
| `cortexa distributions` | Show column value distributions |
|
|
306
|
+
| `cortexa graph` | Knowledge graph summary and traversal |
|
|
307
|
+
| `cortexa actions` | View and manage action recommendations |
|
|
308
|
+
| `cortexa explain <type> <id>` | AI explanation of anomaly, insight, or event |
|
|
309
|
+
| `cortexa ask "<question>"` | Ask a natural language question |
|
|
310
|
+
|
|
311
|
+
## Configuration
|
|
312
|
+
|
|
313
|
+
The config file supports fine-grained control over every layer of the pipeline:
|
|
314
|
+
|
|
315
|
+
<details>
|
|
316
|
+
<summary><strong>Full configuration reference (click to expand)</strong></summary>
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
import { defineConfig } from '@cortexa/core';
|
|
320
|
+
|
|
321
|
+
export default defineConfig({
|
|
322
|
+
// ── Database Connection ──────────────────────────────────
|
|
323
|
+
connection: {
|
|
324
|
+
type: 'postgres', // 'mysql' | 'sqlite' | 'mariadb' | 'cockroachdb' | 'mongodb' | 'mssql'
|
|
325
|
+
url: process.env.DATABASE_URL, // or use host/port/database/user/password
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
// ── LLM Provider ────────────────────────────────────────
|
|
329
|
+
llm: {
|
|
330
|
+
provider: 'openai', // 'anthropic' | 'deepseek'
|
|
331
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
332
|
+
model: 'gpt-4o-mini', // model to use for classification and analysis
|
|
333
|
+
retry: {
|
|
334
|
+
maxRetries: 3,
|
|
335
|
+
initialDelayMs: 1000,
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
// Number of tables to classify per LLM batch
|
|
340
|
+
batchSize: 5,
|
|
341
|
+
|
|
342
|
+
// ── State Machine Tracking ──────────────────────────────
|
|
343
|
+
reasoning: {
|
|
344
|
+
workflows: {
|
|
345
|
+
orders: {
|
|
346
|
+
stateColumn: 'status',
|
|
347
|
+
expectedTransitions: [
|
|
348
|
+
'pending -> confirmed',
|
|
349
|
+
'confirmed -> shipped',
|
|
350
|
+
'shipped -> delivered',
|
|
351
|
+
],
|
|
352
|
+
stuckThreshold: '24h', // flag records stuck in a state
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
|
|
357
|
+
// ── Cross-Entity Analytics ──────────────────────────────
|
|
358
|
+
analytics: {
|
|
359
|
+
correlations: {
|
|
360
|
+
'order-payment': {
|
|
361
|
+
entities: ['orders', 'payments'],
|
|
362
|
+
timeWindow: '5m', // correlate events within this window
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
distributions: {
|
|
366
|
+
orders: {
|
|
367
|
+
columns: ['total'], // track value distributions
|
|
368
|
+
bucketCount: 10,
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
|
|
373
|
+
// ── Knowledge Graph ─────────────────────────────────────
|
|
374
|
+
knowledge: {
|
|
375
|
+
enabled: true, // build causal graph from events + anomalies
|
|
376
|
+
},
|
|
377
|
+
|
|
378
|
+
// ── Autonomous Actions ──────────────────────────────────
|
|
379
|
+
actions: {
|
|
380
|
+
governance: 'advisory', // 'autonomous' | 'advisory' | 'manual'
|
|
381
|
+
rules: [{
|
|
382
|
+
trigger: 'anomaly',
|
|
383
|
+
condition: { severity: ['critical', 'high'] },
|
|
384
|
+
action: 'notify_team',
|
|
385
|
+
}],
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
// ── Webhook Notifications ───────────────────────────────
|
|
389
|
+
notifications: {
|
|
390
|
+
enabled: true,
|
|
391
|
+
rules: [{
|
|
392
|
+
triggers: ['anomaly', 'insight'],
|
|
393
|
+
targets: [{ url: process.env.SLACK_WEBHOOK_URL, type: 'slack' }],
|
|
394
|
+
filter: { severity: ['critical', 'high'] },
|
|
395
|
+
}],
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
</details>
|
|
401
|
+
|
|
402
|
+
## Requirements
|
|
403
|
+
|
|
404
|
+
- **Node.js** >= 18
|
|
405
|
+
- A [supported database](#supported-databases)
|
|
406
|
+
- An **LLM API key** (OpenAI, Anthropic, or DeepSeek) — required for schema classification, explain, and ask commands
|
|
407
|
+
|
|
408
|
+
## Contributing
|
|
409
|
+
|
|
410
|
+
Contributions are welcome. Please open an issue first to discuss what you'd like to change.
|
|
411
|
+
|
|
412
|
+
```bash
|
|
413
|
+
git clone https://github.com/Mohammed3MG/cortexa.git
|
|
414
|
+
cd cortexa
|
|
415
|
+
npm install
|
|
416
|
+
npm test # run unit tests
|
|
417
|
+
npm run build # build the project
|
|
418
|
+
npm run typecheck # verify types
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## License
|
|
422
|
+
|
|
423
|
+
[Apache 2.0](LICENSE)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/behavioral/streams/types.ts
|
|
2
|
+
var DEFAULT_MAX_ATTEMPTS = 5;
|
|
3
|
+
var DEFAULT_INITIAL_DELAY_MS = 1e3;
|
|
4
|
+
var DEFAULT_MAX_DELAY_MS = 6e4;
|
|
5
|
+
function resolveReconnectOptions(options) {
|
|
6
|
+
return {
|
|
7
|
+
maxAttempts: options?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS,
|
|
8
|
+
initialDelayMs: options?.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS,
|
|
9
|
+
maxDelayMs: options?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function computeBackoff(attempt, opts) {
|
|
13
|
+
const delay = opts.initialDelayMs * Math.pow(2, attempt);
|
|
14
|
+
return Math.min(delay, opts.maxDelayMs);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
resolveReconnectOptions,
|
|
19
|
+
computeBackoff
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=chunk-6VDDIS65.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/behavioral/streams/types.ts"],"sourcesContent":["import type { RawChange } from '../types.js';\n\nexport interface ReconnectOptions {\n maxAttempts?: number;\n initialDelayMs?: number;\n maxDelayMs?: number;\n}\n\nexport interface ChangeStream {\n start(onChange: (change: RawChange) => void): Promise<void>;\n stop(): Promise<void>;\n isRunning(): boolean;\n cleanup(): Promise<void>;\n}\n\nconst DEFAULT_MAX_ATTEMPTS = 5;\nconst DEFAULT_INITIAL_DELAY_MS = 1000;\nconst DEFAULT_MAX_DELAY_MS = 60000;\n\nexport function resolveReconnectOptions(options?: ReconnectOptions): Required<ReconnectOptions> {\n return {\n maxAttempts: options?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS,\n initialDelayMs: options?.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS,\n maxDelayMs: options?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS,\n };\n}\n\nexport function computeBackoff(attempt: number, opts: Required<ReconnectOptions>): number {\n const delay = opts.initialDelayMs * Math.pow(2, attempt);\n return Math.min(delay, opts.maxDelayMs);\n}\n"],"mappings":";AAeA,IAAM,uBAAuB;AAC7B,IAAM,2BAA2B;AACjC,IAAM,uBAAuB;AAEtB,SAAS,wBAAwB,SAAwD;AAC9F,SAAO;AAAA,IACL,aAAa,SAAS,eAAe;AAAA,IACrC,gBAAgB,SAAS,kBAAkB;AAAA,IAC3C,YAAY,SAAS,cAAc;AAAA,EACrC;AACF;AAEO,SAAS,eAAe,SAAiB,MAA0C;AACxF,QAAM,QAAQ,KAAK,iBAAiB,KAAK,IAAI,GAAG,OAAO;AACvD,SAAO,KAAK,IAAI,OAAO,KAAK,UAAU;AACxC;","names":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/core/logger.ts
|
|
2
|
+
import pino from "pino";
|
|
3
|
+
function createLogger(options = {}) {
|
|
4
|
+
return pino({
|
|
5
|
+
name: options.name ?? "cortexa",
|
|
6
|
+
level: options.level ?? "info"
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
createLogger
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=chunk-T4NUBJ7T.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/logger.ts"],"sourcesContent":["import pino from 'pino';\n\nexport interface LoggerOptions {\n name?: string;\n level?: string;\n}\n\nexport function createLogger(options: LoggerOptions = {}): pino.Logger {\n return pino({\n name: options.name ?? 'cortexa',\n level: options.level ?? 'info',\n });\n}\n"],"mappings":";AAAA,OAAO,UAAU;AAOV,SAAS,aAAa,UAAyB,CAAC,GAAgB;AACrE,SAAO,KAAK;AAAA,IACV,MAAM,QAAQ,QAAQ;AAAA,IACtB,OAAO,QAAQ,SAAS;AAAA,EAC1B,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/core/logger.ts
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
function createLogger(options = {}) {
|
|
6
|
+
return pino({
|
|
7
|
+
name: options.name ?? "cortexa",
|
|
8
|
+
level: options.level ?? "info"
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
createLogger
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=chunk-7HYSAZX4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/logger.ts"],"sourcesContent":["import pino from 'pino';\n\nexport interface LoggerOptions {\n name?: string;\n level?: string;\n}\n\nexport function createLogger(options: LoggerOptions = {}): pino.Logger {\n return pino({\n name: options.name ?? 'cortexa',\n level: options.level ?? 'info',\n });\n}\n"],"mappings":";;;AAAA,OAAO,UAAU;AAOV,SAAS,aAAa,UAAyB,CAAC,GAAgB;AACrE,SAAO,KAAK;AAAA,IACV,MAAM,QAAQ,QAAQ;AAAA,IACtB,OAAO,QAAQ,SAAS;AAAA,EAC1B,CAAC;AACH;","names":[]}
|