@elqnt/kg 2.1.0 → 3.0.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.
Files changed (48) hide show
  1. package/README.md +357 -39
  2. package/dist/api/index.d.mts +250 -2
  3. package/dist/api/index.d.ts +250 -2
  4. package/dist/api/index.js +4 -2
  5. package/dist/api/index.js.map +1 -1
  6. package/dist/api/index.mjs +3 -1
  7. package/dist/api/server.d.mts +219 -0
  8. package/dist/api/server.d.ts +219 -0
  9. package/dist/api/server.js +442 -0
  10. package/dist/api/server.js.map +1 -0
  11. package/dist/api/server.mjs +442 -0
  12. package/dist/api/server.mjs.map +1 -0
  13. package/dist/{chunk-JSMI4PFC.js → chunk-2TJCYLTP.js} +67 -65
  14. package/dist/chunk-2TJCYLTP.js.map +1 -0
  15. package/dist/chunk-7RW5MHP5.js +497 -0
  16. package/dist/chunk-7RW5MHP5.js.map +1 -0
  17. package/dist/chunk-ADIKUMMI.js +238 -0
  18. package/dist/chunk-ADIKUMMI.js.map +1 -0
  19. package/dist/chunk-CAXPQTKI.mjs +238 -0
  20. package/dist/chunk-CAXPQTKI.mjs.map +1 -0
  21. package/dist/{chunk-55R4PZ5A.mjs → chunk-HCDFJCQL.mjs} +65 -63
  22. package/dist/chunk-HCDFJCQL.mjs.map +1 -0
  23. package/dist/chunk-JZ7UXVRW.mjs +497 -0
  24. package/dist/chunk-JZ7UXVRW.mjs.map +1 -0
  25. package/dist/hooks/index.d.mts +109 -4
  26. package/dist/hooks/index.d.ts +109 -4
  27. package/dist/hooks/index.js +9 -3
  28. package/dist/hooks/index.js.map +1 -1
  29. package/dist/hooks/index.mjs +10 -4
  30. package/dist/index.d.mts +3 -2
  31. package/dist/index.d.ts +3 -2
  32. package/dist/index.js +23 -3
  33. package/dist/index.js.map +1 -1
  34. package/dist/index.mjs +24 -4
  35. package/dist/index.mjs.map +1 -1
  36. package/dist/utils/index.d.mts +277 -0
  37. package/dist/utils/index.d.ts +277 -0
  38. package/dist/utils/index.js +44 -0
  39. package/dist/utils/index.js.map +1 -0
  40. package/dist/utils/index.mjs +44 -0
  41. package/dist/utils/index.mjs.map +1 -0
  42. package/package.json +15 -5
  43. package/dist/chunk-55R4PZ5A.mjs.map +0 -1
  44. package/dist/chunk-BQZLJ5LD.mjs +0 -577
  45. package/dist/chunk-BQZLJ5LD.mjs.map +0 -1
  46. package/dist/chunk-JSMI4PFC.js.map +0 -1
  47. package/dist/chunk-KATHAUDG.js +0 -577
  48. package/dist/chunk-KATHAUDG.js.map +0 -1
package/README.md CHANGED
@@ -1,72 +1,390 @@
1
- # @eloquent/kg
1
+ # @elqnt/kg
2
2
 
3
- This package provides knowledge graph functionality for Eloquent applications.
3
+ Knowledge graph SDK for the Eloquent platform. Provides TypeScript types, browser and server APIs, React hooks, and query utilities for working with knowledge graphs.
4
4
 
5
5
  ## Installation
6
6
 
7
- Since this package is published to GitHub Packages, you'll need to configure npm to use GitHub Packages for the `@eloquent` scope.
7
+ ```bash
8
+ pnpm add @elqnt/kg
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ### Browser (React)
14
+
15
+ ```tsx
16
+ import { useGraphs, useKGQuery } from "@elqnt/kg/hooks";
8
17
 
9
- 1. Create or edit an `.npmrc` file in your project root:
18
+ function KnowledgeGraphExplorer() {
19
+ const { listGraphs, loading, error } = useGraphs({
20
+ baseUrl: process.env.NEXT_PUBLIC_API_GATEWAY_URL!,
21
+ orgId: currentOrgId,
22
+ });
10
23
 
24
+ const { query } = useKGQuery({
25
+ baseUrl: process.env.NEXT_PUBLIC_API_GATEWAY_URL!,
26
+ orgId: currentOrgId,
27
+ graphId: selectedGraphId,
28
+ });
29
+
30
+ // List all graphs
31
+ const graphs = await listGraphs();
32
+
33
+ // Query nodes
34
+ const result = await query({
35
+ label: "Person",
36
+ fields: [{ name: "age", operator: "gt", value: 21 }],
37
+ limit: 50,
38
+ depth: 1,
39
+ sortBy: "name",
40
+ sortOrder: "asc",
41
+ });
42
+ }
11
43
  ```
12
- @eloquent:registry=https://npm.pkg.github.com
13
- //npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
44
+
45
+ ### Server Actions
46
+
47
+ ```ts
48
+ // In a Next.js server action
49
+ "use server";
50
+
51
+ import { listGraphsServer, queryGraphServer } from "@elqnt/kg/api/server";
52
+
53
+ export async function getGraphs(orgId: string) {
54
+ const response = await listGraphsServer({
55
+ gatewayUrl: process.env.API_GATEWAY_URL!,
56
+ jwtSecret: process.env.JWT_SECRET!,
57
+ orgId,
58
+ });
59
+ return response.data?.graphs || [];
60
+ }
61
+
62
+ export async function searchNodes(orgId: string, graphId: string, label: string) {
63
+ const response = await queryGraphServer(
64
+ { label, fields: [], limit: 100, depth: 1, sortBy: "", sortOrder: "" },
65
+ {
66
+ gatewayUrl: process.env.API_GATEWAY_URL!,
67
+ jwtSecret: process.env.JWT_SECRET!,
68
+ orgId,
69
+ graphId,
70
+ }
71
+ );
72
+ return response.data;
73
+ }
14
74
  ```
15
75
 
16
- 2. Set your GitHub token as an environment variable:
76
+ ### Query Builder
17
77
 
18
- ```bash
19
- export GITHUB_TOKEN=your_github_token
78
+ ```ts
79
+ import { KGQueryBuilder, createNodeQuery } from "@elqnt/kg/utils";
80
+
81
+ // Fluent builder API
82
+ const query = new KGQueryBuilder()
83
+ .label("Person")
84
+ .field("age", "gt", 21)
85
+ .field("department", "eq", "Engineering")
86
+ .edge("WORKS_AT", "outgoing", "Company")
87
+ .limit(100)
88
+ .sortBy("name", "asc")
89
+ .summaryOnly()
90
+ .build();
91
+
92
+ // Helper function for simple queries
93
+ const simpleQuery = createNodeQuery("Product", { category: "electronics" }, { limit: 50 });
20
94
  ```
21
95
 
22
- 3. Install the package:
96
+ ## Architecture
23
97
 
24
- ```bash
25
- npm install @eloquent/kg
98
+ ```
99
+ Browser/React App Server Actions/SSR
100
+ │ │
101
+ ▼ ▼
102
+ @elqnt/kg/hooks @elqnt/kg/api/server
103
+ │ │
104
+ ▼ ▼
105
+ @elqnt/kg/api serverApiRequest
106
+ │ (JWT generation)
107
+ ▼ │
108
+ browserApiRequest │
109
+ │ │
110
+ └───────────────┬───────────────────┘
111
+
112
+ API Gateway
113
+
114
+
115
+ KG Service
26
116
  ```
27
117
 
28
- ## Usage
118
+ ## Exports
29
119
 
30
- ```typescript
31
- import {} from /* components or functions */ "@eloquent/kg";
120
+ | Import Path | Description |
121
+ |-------------|-------------|
122
+ | `@elqnt/kg` | All exports (types, API, hooks, utils) |
123
+ | `@elqnt/kg/api` | Browser API functions |
124
+ | `@elqnt/kg/api/server` | Server API functions |
125
+ | `@elqnt/kg/hooks` | React hooks |
126
+ | `@elqnt/kg/models` | TypeScript types |
127
+ | `@elqnt/kg/utils` | Query builders and helpers |
32
128
 
33
- // Your code here
129
+ ## API Reference
130
+
131
+ ### Browser API (`@elqnt/kg/api`)
132
+
133
+ All functions accept `ApiClientOptions` or `KGApiOptions`:
134
+
135
+ ```ts
136
+ interface ApiClientOptions {
137
+ baseUrl: string; // API Gateway URL
138
+ orgId: string; // Organization ID
139
+ userId?: string; // Optional user ID
140
+ headers?: Record<string, string>;
141
+ }
142
+
143
+ interface KGApiOptions extends ApiClientOptions {
144
+ graphId?: string; // Graph ID for graph-scoped operations
145
+ }
34
146
  ```
35
147
 
36
- ## Development
148
+ #### Graph Operations
37
149
 
38
- To build the package locally:
150
+ | Function | Description |
151
+ |----------|-------------|
152
+ | `listGraphsApi(options)` | List all graphs |
153
+ | `getGraphApi(graphId, options)` | Get a specific graph |
154
+ | `createGraphApi(graph, options)` | Create a new graph |
155
+ | `updateGraphApi(graphId, updates, options)` | Update a graph |
156
+ | `deleteGraphApi(graphId, options)` | Delete a graph |
39
157
 
40
- ```bash
41
- npm run build
158
+ #### Query Operations
159
+
160
+ | Function | Description |
161
+ |----------|-------------|
162
+ | `queryGraphApi(query, options)` | Query nodes |
163
+ | `getGraphLabelsApi(options)` | Get all node labels |
164
+
165
+ #### Node Operations
166
+
167
+ | Function | Description |
168
+ |----------|-------------|
169
+ | `getKGNodeApi(nodeId, options)` | Get a node by ID |
170
+ | `ingestKGNodeApi(node, options)` | Ingest a new node |
171
+ | `updateKGNodeApi(nodeId, updates, options)` | Update a node |
172
+
173
+ #### Designer Operations
174
+
175
+ | Function | Description |
176
+ |----------|-------------|
177
+ | `listDesignerNodesApi(options)` | List node definitions |
178
+ | `createDesignerNodeApi(node, options)` | Create node definition |
179
+ | `listDesignerEdgesApi(options)` | List edge definitions |
180
+ | `createDesignerEdgeApi(edge, options)` | Create edge definition |
181
+
182
+ ### Server API (`@elqnt/kg/api/server`)
183
+
184
+ All functions accept `ServerApiOptions`:
185
+
186
+ ```ts
187
+ interface ServerApiOptions {
188
+ gatewayUrl: string; // API Gateway URL
189
+ jwtSecret: string; // JWT secret for token generation
190
+ orgId: string; // Organization ID
191
+ userId?: string; // Optional user ID (default: "system")
192
+ graphId?: string; // Graph ID for graph-scoped operations
193
+ timeout?: number; // Request timeout in ms
194
+ cache?: RequestCache;
195
+ }
42
196
  ```
43
197
 
44
- ## Publishing
198
+ Functions mirror the browser API with `Server` suffix:
45
199
 
46
- This package is automatically published to GitHub Packages when changes are pushed to the main branch that affect files in the `packages/eloquent/kg` directory.
200
+ - `listGraphsServer`, `getGraphServer`, `createGraphServer`, etc.
201
+ - `queryGraphServer`, `getGraphLabelsServer`
202
+ - `getKGNodeServer`, `ingestKGNodeServer`, `updateKGNodeServer`
203
+ - `listDesignerNodesServer`, `listDesignerEdgesServer`, etc.
47
204
 
48
- For manual publishing:
205
+ ### React Hooks (`@elqnt/kg/hooks`)
49
206
 
50
- 1. Make sure you have the correct GitHub token set up
51
- 2. Update the version in package.json
52
- 3. Run:
207
+ #### `useGraphs(options: ApiClientOptions)`
53
208
 
54
- ```bash
55
- cd packages/eloquent/kg
56
- npm run build
57
- npm publish
209
+ ```tsx
210
+ const {
211
+ loading, // boolean - any operation in progress
212
+ error, // string | null - last error message
213
+ listGraphs, // () => Promise<Graph[]>
214
+ getGraph, // (graphId) => Promise<Graph | null>
215
+ createGraph, // (graph) => Promise<Graph | null>
216
+ updateGraph, // (graphId, updates) => Promise<Graph | null>
217
+ deleteGraph, // (graphId) => Promise<boolean>
218
+ } = useGraphs(options);
58
219
  ```
59
220
 
60
- ## Monorepo Integration
221
+ #### `useKGQuery(options: UseKGOptions)`
61
222
 
62
- This package is part of the auto-beam-frontend monorepo. When making changes:
223
+ ```tsx
224
+ const {
225
+ loading,
226
+ error,
227
+ query, // (KGQuery) => Promise<KGQueryResult | null>
228
+ getLabels, // () => Promise<KGLabelInfo[]>
229
+ getNode, // (nodeId) => Promise<KGNode | null>
230
+ ingestNode, // (node) => Promise<string | null>
231
+ updateNode, // (nodeId, updates) => Promise<boolean>
232
+ } = useKGQuery(options);
233
+ ```
63
234
 
64
- 1. The package will be automatically published when changes are pushed to the main branch
65
- 2. The package follows the early return pattern in its build process
66
- 3. To use this package in other packages within the monorepo, add it as a dependency in the package.json file:
235
+ #### `useKGDesigner(options: UseKGOptions)`
67
236
 
68
- ```json
69
- "dependencies": {
70
- "@eloquent/kg": "*"
71
- }
237
+ ```tsx
238
+ const {
239
+ loading,
240
+ error,
241
+ // Node definitions
242
+ listNodes, // () => Promise<GraphNodeDefinition[]>
243
+ getNode, // (label) => Promise<GraphNodeDefinition | null>
244
+ createNode, // (node) => Promise<GraphNodeDefinition | null>
245
+ updateNode, // (label, updates) => Promise<GraphNodeDefinition | null>
246
+ deleteNode, // (label) => Promise<boolean>
247
+ // Edge definitions
248
+ listEdges, // () => Promise<GraphEdgeDefinition[]>
249
+ createEdge, // (edge) => Promise<GraphEdgeDefinition | null>
250
+ updateEdge, // (label, updates) => Promise<GraphEdgeDefinition | null>
251
+ deleteEdge, // (label) => Promise<boolean>
252
+ } = useKGDesigner(options);
72
253
  ```
254
+
255
+ #### `useCrawlJobs(options: UseKGOptions)`
256
+
257
+ ```tsx
258
+ const {
259
+ loading,
260
+ error,
261
+ listJobs, // (params?) => Promise<{ jobs, total }>
262
+ startJob, // (params) => Promise<string | null>
263
+ getJobStatus, // (jobId) => Promise<CrawlJob | null>
264
+ cancelJob, // (jobId) => Promise<boolean>
265
+ getCrawledPages, // (jobId) => Promise<Page[]>
266
+ } = useCrawlJobs(options);
267
+ ```
268
+
269
+ ### Query Builder (`@elqnt/kg/utils`)
270
+
271
+ #### `KGQueryBuilder`
272
+
273
+ Fluent API for building queries:
274
+
275
+ ```ts
276
+ const builder = new KGQueryBuilder();
277
+
278
+ // Methods (all return `this` for chaining)
279
+ builder.label(label: string)
280
+ builder.field(name: string, operator: KGFieldQueryOperator, value: unknown)
281
+ builder.where(name: string, value: unknown) // Shorthand for equality
282
+ builder.whereAll(fields: Record<string, unknown>)
283
+ builder.edge(label: string, direction: "incoming" | "outgoing", toLabel?: string)
284
+ builder.outgoing(label: string, toLabel?: string)
285
+ builder.incoming(label: string, toLabel?: string)
286
+ builder.limit(n: number)
287
+ builder.depth(n: number)
288
+ builder.offset(n: number)
289
+ builder.sortBy(field: string, order?: "asc" | "desc")
290
+ builder.embeddingsSource(source: string)
291
+ builder.skipEmbedding()
292
+ builder.summaryOnly()
293
+ builder.build(): KGQuery
294
+ ```
295
+
296
+ #### Helper Functions
297
+
298
+ ```ts
299
+ // Create a simple query with equality filters
300
+ createNodeQuery(
301
+ label: string,
302
+ fields?: Record<string, unknown>,
303
+ options?: { limit?, depth?, sortBy?, sortOrder?, summaryOnly? }
304
+ ): KGQuery
305
+
306
+ // Create an edge query object
307
+ createEdgeQuery(
308
+ label: string,
309
+ direction?: "incoming" | "outgoing",
310
+ options?: { toLabel?, toFieldKey?, toFieldValue?, fields? }
311
+ ): KGEdgeQuery
312
+
313
+ // Create a field query object
314
+ createFieldQuery(
315
+ name: string,
316
+ operator: KGFieldQueryOperator,
317
+ value: unknown
318
+ ): KGFieldQuery
319
+
320
+ // Create a similarity search query
321
+ createSimilarityQuery(
322
+ label: string,
323
+ searchText: string,
324
+ options?: { embeddingsSource?, limit?, summaryOnly? }
325
+ ): KGQuery
326
+ ```
327
+
328
+ ### Types (`@elqnt/kg/models`)
329
+
330
+ Key types exported:
331
+
332
+ ```ts
333
+ // Graphs
334
+ interface Graph { id, name, description, isDefault, createdAt, updatedAt }
335
+ interface CreateGraphRequest { id?, name, description }
336
+
337
+ // Nodes & Edges
338
+ interface KGNode { id, label, fields, relationships?, score? }
339
+ interface KGEdge { id, label, fields, from, to }
340
+ interface KGNodeIngestRequest { label, fields, edges?, keyField?, ... }
341
+
342
+ // Queries
343
+ interface KGQuery { label, fields, edges?, limit, depth, sortBy, sortOrder, ... }
344
+ interface KGFieldQuery { name, operator, value }
345
+ interface KGEdgeQuery { label, direction, toLabel, toFieldKey, toFieldValue, fields? }
346
+ interface KGQueryResult { nodes, edges }
347
+
348
+ // Designer
349
+ interface GraphNodeDefinition { label, description, schema, createdAt, updatedAt }
350
+ interface GraphEdgeDefinition { label, description, fromNode, toNode, schema, ... }
351
+
352
+ // Labels
353
+ interface KGLabelInfo { label, count }
354
+ ```
355
+
356
+ ## Migration from v2.x
357
+
358
+ ### Breaking Changes in v3.0.0
359
+
360
+ 1. **graphId handling**: Now consistently uses `X-Graph-ID` header instead of query params for POST/PUT operations
361
+
362
+ 2. **New exports**:
363
+ - `@elqnt/kg/api/server` - Server-side API functions
364
+ - `@elqnt/kg/utils` - Query builder utilities
365
+
366
+ 3. **Hook utilities exposed**:
367
+ - `useApiAsync`, `useAsync`, `useOptionsRef` now exported from `@elqnt/kg/hooks`
368
+
369
+ ### Migration Steps
370
+
371
+ 1. Update import paths if using internal utilities
372
+ 2. Server actions can now use `@elqnt/kg/api/server` instead of manual API calls
373
+ 3. Consider using `KGQueryBuilder` for complex queries
374
+
375
+ ## Development
376
+
377
+ ```bash
378
+ # Build the package
379
+ pnpm build
380
+
381
+ # Run type checking
382
+ pnpm typecheck
383
+
384
+ # Watch mode
385
+ pnpm dev
386
+ ```
387
+
388
+ ## License
389
+
390
+ Private - Eloquent Platform