@trustgraph/react-state 0.3.1 → 1.1.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/dist/index.cjs +143 -288
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +143 -288
- package/dist/index.esm.js.map +1 -1
- package/dist/state/chat-session.d.ts.map +1 -1
- package/dist/state/conversation.d.ts +1 -0
- package/dist/state/conversation.d.ts.map +1 -1
- package/dist/state/inference.d.ts +13 -3
- package/dist/state/inference.d.ts.map +1 -1
- package/dist/utils/row.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { SocketProvider, SocketContext, ConnectionStateContext, } from "@trustgraph/react-provider";
|
|
2
2
|
export type { SocketProviderProps } from "@trustgraph/react-provider";
|
|
3
|
-
export type { BaseApi, Triple, Value, ConnectionState, } from "@trustgraph/client";
|
|
3
|
+
export type { BaseApi, Triple, Value, ConnectionState, StreamingMetadata, } from "@trustgraph/client";
|
|
4
4
|
export { NotificationProvider } from "./NotificationProvider";
|
|
5
5
|
export type { NotificationProviderProps } from "./NotificationProvider";
|
|
6
6
|
export type { NotificationHandler } from "./types";
|
|
@@ -27,7 +27,7 @@ export { useGraphEmbeddings } from "./state/graph-embeddings";
|
|
|
27
27
|
export { useVectorSearch } from "./state/vector-search";
|
|
28
28
|
export { useEntityDetail } from "./state/entity-query";
|
|
29
29
|
export { useInference } from "./state/inference";
|
|
30
|
-
export type { GraphRagOptions, GraphRagResult, AgentCallbacks, } from "./state/inference";
|
|
30
|
+
export type { GraphRagOptions, GraphRagResult, GraphRagCallbacks, TextCompletionCallbacks, AgentCallbacks, } from "./state/inference";
|
|
31
31
|
export { useChatSession, useChat } from "./state/chat-session";
|
|
32
32
|
export { useStructuredQuery } from "./state/structured-query";
|
|
33
33
|
export { useObjectsQuery } from "./state/objects-query";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,aAAa,EACb,sBAAsB,GACvB,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGtE,YAAY,EACV,OAAO,EACP,MAAM,EACN,KAAK,EACL,eAAe,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,aAAa,EACb,sBAAsB,GACvB,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGtE,YAAY,EACV,OAAO,EACP,MAAM,EACN,KAAK,EACL,eAAe,EACf,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,YAAY,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AACxE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGnD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAm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|
@@ -5,6 +5,7 @@ import { createContext, useContext, useEffect, useState, useMemo } from 'react';
|
|
|
5
5
|
import { create } from 'zustand';
|
|
6
6
|
import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
|
|
7
7
|
import { v4 } from 'uuid';
|
|
8
|
+
import similarity from 'compute-cosine-similarity';
|
|
8
9
|
|
|
9
10
|
// Create context for notification handler
|
|
10
11
|
const NotificationContext = createContext(null);
|
|
@@ -169,6 +170,16 @@ const useConversation = create()((set) => ({
|
|
|
169
170
|
},
|
|
170
171
|
],
|
|
171
172
|
})),
|
|
173
|
+
updateLastMessage: (text) => set((state) => {
|
|
174
|
+
if (state.messages.length === 0)
|
|
175
|
+
return state;
|
|
176
|
+
const messages = [...state.messages];
|
|
177
|
+
messages[messages.length - 1] = {
|
|
178
|
+
...messages[messages.length - 1],
|
|
179
|
+
text: text,
|
|
180
|
+
};
|
|
181
|
+
return { messages };
|
|
182
|
+
}),
|
|
172
183
|
setInput: (v) => set(() => ({
|
|
173
184
|
input: v,
|
|
174
185
|
})),
|
|
@@ -1408,273 +1419,6 @@ const useGraphEmbeddings = ({ flow, vecs, limit, collection }) => {
|
|
|
1408
1419
|
};
|
|
1409
1420
|
};
|
|
1410
1421
|
|
|
1411
|
-
function getDefaultExportFromCjs (x) {
|
|
1412
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
/**
|
|
1416
|
-
* FUNCTION: isArray( value )
|
|
1417
|
-
* Validates if a value is an array.
|
|
1418
|
-
*
|
|
1419
|
-
* @param {*} value - value to be validated
|
|
1420
|
-
* @returns {Boolean} boolean indicating whether value is an array
|
|
1421
|
-
*/
|
|
1422
|
-
function isArray$3( value ) {
|
|
1423
|
-
return Object.prototype.toString.call( value ) === '[object Array]';
|
|
1424
|
-
} // end FUNCTION isArray()
|
|
1425
|
-
|
|
1426
|
-
// EXPORTS //
|
|
1427
|
-
|
|
1428
|
-
var lib$4 = Array.isArray || isArray$3;
|
|
1429
|
-
|
|
1430
|
-
/**
|
|
1431
|
-
*
|
|
1432
|
-
* VALIDATE: function
|
|
1433
|
-
*
|
|
1434
|
-
*
|
|
1435
|
-
* DESCRIPTION:
|
|
1436
|
-
* - Validates if a value is a function.
|
|
1437
|
-
*
|
|
1438
|
-
*
|
|
1439
|
-
* NOTES:
|
|
1440
|
-
* [1]
|
|
1441
|
-
*
|
|
1442
|
-
*
|
|
1443
|
-
* TODO:
|
|
1444
|
-
* [1]
|
|
1445
|
-
*
|
|
1446
|
-
*
|
|
1447
|
-
* LICENSE:
|
|
1448
|
-
* MIT
|
|
1449
|
-
*
|
|
1450
|
-
* Copyright (c) 2014. Athan Reines.
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
* AUTHOR:
|
|
1454
|
-
* Athan Reines. kgryte@gmail.com. 2014.
|
|
1455
|
-
*
|
|
1456
|
-
*/
|
|
1457
|
-
|
|
1458
|
-
/**
|
|
1459
|
-
* FUNCTION: isFunction( value )
|
|
1460
|
-
* Validates if a value is a function.
|
|
1461
|
-
*
|
|
1462
|
-
* @param {*} value - value to be validated
|
|
1463
|
-
* @returns {Boolean} boolean indicating whether value is a function
|
|
1464
|
-
*/
|
|
1465
|
-
function isFunction$3( value ) {
|
|
1466
|
-
return ( typeof value === 'function' );
|
|
1467
|
-
} // end FUNCTION isFunction()
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
// EXPORTS //
|
|
1471
|
-
|
|
1472
|
-
var lib$3 = isFunction$3;
|
|
1473
|
-
|
|
1474
|
-
// MODULES //
|
|
1475
|
-
|
|
1476
|
-
var isArray$2 = lib$4,
|
|
1477
|
-
isFunction$2 = lib$3;
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
// DOT PRODUCT //
|
|
1481
|
-
|
|
1482
|
-
/**
|
|
1483
|
-
* FUNCTION: dot( x, y[, accessor] )
|
|
1484
|
-
* Computes the dot product between two arrays.
|
|
1485
|
-
*
|
|
1486
|
-
* @param {Array} x - input array
|
|
1487
|
-
* @param {Array} y - input array
|
|
1488
|
-
* @param {Function} [accessor] - accessor function for accessing array values
|
|
1489
|
-
* @returns {Number|Null} dot product
|
|
1490
|
-
*/
|
|
1491
|
-
function dot$1( x, y, clbk ) {
|
|
1492
|
-
if ( !isArray$2( x ) ) {
|
|
1493
|
-
throw new TypeError( 'dot()::invalid input argument. First argument must be an array. Value: `' + x + '`.' );
|
|
1494
|
-
}
|
|
1495
|
-
if ( !isArray$2( y ) ) {
|
|
1496
|
-
throw new TypeError( 'dot()::invalid input argument. Second argument must be an array. Value: `' + y + '`.' );
|
|
1497
|
-
}
|
|
1498
|
-
if ( arguments.length > 2 ) {
|
|
1499
|
-
if ( !isFunction$2( clbk ) ) {
|
|
1500
|
-
throw new TypeError( 'dot()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
|
|
1501
|
-
}
|
|
1502
|
-
}
|
|
1503
|
-
var len = x.length,
|
|
1504
|
-
sum = 0,
|
|
1505
|
-
i;
|
|
1506
|
-
|
|
1507
|
-
if ( len !== y.length ) {
|
|
1508
|
-
throw new Error( 'dot()::invalid input argument. Arrays must be of equal length.' );
|
|
1509
|
-
}
|
|
1510
|
-
if ( !len ) {
|
|
1511
|
-
return null;
|
|
1512
|
-
}
|
|
1513
|
-
if ( clbk ) {
|
|
1514
|
-
for ( i = 0; i < len; i++ ) {
|
|
1515
|
-
sum += clbk( x[ i ], i, 0 ) * clbk( y[ i ], i, 1 );
|
|
1516
|
-
}
|
|
1517
|
-
} else {
|
|
1518
|
-
for ( i = 0; i < len; i++ ) {
|
|
1519
|
-
sum += x[ i ] * y[ i ];
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
return sum;
|
|
1523
|
-
} // end FUNCTION dot()
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
// EXPORTS //
|
|
1527
|
-
|
|
1528
|
-
var lib$2 = dot$1;
|
|
1529
|
-
|
|
1530
|
-
// MODULES //
|
|
1531
|
-
|
|
1532
|
-
var isArray$1 = lib$4,
|
|
1533
|
-
isFunction$1 = lib$3;
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
// L2NORM //
|
|
1537
|
-
|
|
1538
|
-
/**
|
|
1539
|
-
* FUNCTION: l2norm( arr[, accessor] )
|
|
1540
|
-
* Calculates the L2 norm (Euclidean norm) of an array.
|
|
1541
|
-
*
|
|
1542
|
-
* @param {Array} arr - input array
|
|
1543
|
-
* @param {Function} [accessor] - accessor function for accessing array values
|
|
1544
|
-
* @returns {Number|Null} L2 norm or null
|
|
1545
|
-
*/
|
|
1546
|
-
function l2norm$1( arr, clbk ) {
|
|
1547
|
-
if ( !isArray$1( arr ) ) {
|
|
1548
|
-
throw new TypeError( 'l2norm()::invalid input argument. Must provide an array. Value: `' + arr + '`.' );
|
|
1549
|
-
}
|
|
1550
|
-
if ( arguments.length > 1 ) {
|
|
1551
|
-
if ( !isFunction$1( clbk ) ) {
|
|
1552
|
-
throw new TypeError( 'l2norm()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
|
|
1553
|
-
}
|
|
1554
|
-
}
|
|
1555
|
-
var len = arr.length,
|
|
1556
|
-
t = 0,
|
|
1557
|
-
s = 1,
|
|
1558
|
-
r,
|
|
1559
|
-
val,
|
|
1560
|
-
abs,
|
|
1561
|
-
i;
|
|
1562
|
-
|
|
1563
|
-
if ( !len ) {
|
|
1564
|
-
return null;
|
|
1565
|
-
}
|
|
1566
|
-
if ( clbk ) {
|
|
1567
|
-
for ( i = 0; i < len; i++ ) {
|
|
1568
|
-
val = clbk( arr[ i ], i );
|
|
1569
|
-
abs = ( val < 0 ) ? -val : val;
|
|
1570
|
-
if ( abs > 0 ) {
|
|
1571
|
-
if ( abs > t ) {
|
|
1572
|
-
r = t / val;
|
|
1573
|
-
s = 1 + s*r*r;
|
|
1574
|
-
t = abs;
|
|
1575
|
-
} else {
|
|
1576
|
-
r = val / t;
|
|
1577
|
-
s = s + r*r;
|
|
1578
|
-
}
|
|
1579
|
-
}
|
|
1580
|
-
}
|
|
1581
|
-
} else {
|
|
1582
|
-
for ( i = 0; i < len; i++ ) {
|
|
1583
|
-
val = arr[ i ];
|
|
1584
|
-
abs = ( val < 0 ) ? -val : val;
|
|
1585
|
-
if ( abs > 0 ) {
|
|
1586
|
-
if ( abs > t ) {
|
|
1587
|
-
r = t / val;
|
|
1588
|
-
s = 1 + s*r*r;
|
|
1589
|
-
t = abs;
|
|
1590
|
-
} else {
|
|
1591
|
-
r = val / t;
|
|
1592
|
-
s = s + r*r;
|
|
1593
|
-
}
|
|
1594
|
-
}
|
|
1595
|
-
}
|
|
1596
|
-
}
|
|
1597
|
-
return t * Math.sqrt( s );
|
|
1598
|
-
} // end FUNCTION l2norm()
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
// EXPORTS //
|
|
1602
|
-
|
|
1603
|
-
var lib$1 = l2norm$1;
|
|
1604
|
-
|
|
1605
|
-
// MODULES //
|
|
1606
|
-
|
|
1607
|
-
var dot = lib$2,
|
|
1608
|
-
l2norm = lib$1,
|
|
1609
|
-
isArray = lib$4,
|
|
1610
|
-
isFunction = lib$3;
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
// FUNCTIONS //
|
|
1614
|
-
|
|
1615
|
-
/**
|
|
1616
|
-
* Partially applied function from the right.
|
|
1617
|
-
*
|
|
1618
|
-
* @private
|
|
1619
|
-
* @param {Function} fn - input function
|
|
1620
|
-
* @param {number} j - array index
|
|
1621
|
-
* @returns {Function} partially applied function
|
|
1622
|
-
*/
|
|
1623
|
-
function partial( fn, j ) {
|
|
1624
|
-
return function accessor( d, i ) {
|
|
1625
|
-
return fn( d, i, j );
|
|
1626
|
-
};
|
|
1627
|
-
}
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
// MAIN //
|
|
1631
|
-
|
|
1632
|
-
/**
|
|
1633
|
-
* Computes the cosine similarity between two arrays.
|
|
1634
|
-
*
|
|
1635
|
-
* @param {number[]|Array} x - input array
|
|
1636
|
-
* @param {number[]|Array} y - input array
|
|
1637
|
-
* @param {Function} [accessor] - accessor function for accessing array values
|
|
1638
|
-
* @returns {number|null} cosine similarity or null
|
|
1639
|
-
*/
|
|
1640
|
-
function similarity( x, y, clbk ) {
|
|
1641
|
-
var a, b, c;
|
|
1642
|
-
if ( !isArray( x ) ) {
|
|
1643
|
-
throw new TypeError( 'cosine-similarity()::invalid input argument. First argument must be an array. Value: `' + x + '`.' );
|
|
1644
|
-
}
|
|
1645
|
-
if ( !isArray( y ) ) {
|
|
1646
|
-
throw new TypeError( 'cosine-similarity()::invalid input argument. Second argument must be an array. Value: `' + y + '`.' );
|
|
1647
|
-
}
|
|
1648
|
-
if ( arguments.length > 2 ) {
|
|
1649
|
-
if ( !isFunction( clbk ) ) {
|
|
1650
|
-
throw new TypeError( 'cosine-similarity()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
if ( x.length !== y.length ) {
|
|
1654
|
-
throw new Error( 'cosine-similarity()::invalid input argument. Input arrays must have the same length.' );
|
|
1655
|
-
}
|
|
1656
|
-
if ( !x.length ) {
|
|
1657
|
-
return null;
|
|
1658
|
-
}
|
|
1659
|
-
if ( clbk ) {
|
|
1660
|
-
a = dot( x, y, clbk );
|
|
1661
|
-
b = l2norm( x, partial( clbk, 0 ) );
|
|
1662
|
-
c = l2norm( y, partial( clbk, 1 ) );
|
|
1663
|
-
} else {
|
|
1664
|
-
a = dot( x, y );
|
|
1665
|
-
b = l2norm( x );
|
|
1666
|
-
c = l2norm( y );
|
|
1667
|
-
}
|
|
1668
|
-
return a / ( b*c );
|
|
1669
|
-
}
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
// EXPORTS //
|
|
1673
|
-
|
|
1674
|
-
var lib = similarity;
|
|
1675
|
-
|
|
1676
|
-
var similarity$1 = /*@__PURE__*/getDefaultExportFromCjs(lib);
|
|
1677
|
-
|
|
1678
1422
|
// Take the embeddings, and lookup entities using graph
|
|
1679
1423
|
// embeddings, add embedding to each entity row, just an easy
|
|
1680
1424
|
// place to put it
|
|
@@ -1794,7 +1538,7 @@ const addRowEmbeddings = (socket, add, remove) => (entities) => {
|
|
|
1794
1538
|
};
|
|
1795
1539
|
// Rest of the procecess is not async, so not adding progress
|
|
1796
1540
|
const computeCosineSimilarity = () => (entities) => entities.map((ent) => {
|
|
1797
|
-
const sim = similarity
|
|
1541
|
+
const sim = similarity(ent.target, ent.embeddings);
|
|
1798
1542
|
return {
|
|
1799
1543
|
uri: ent.uri,
|
|
1800
1544
|
label: ent.label,
|
|
@@ -1964,11 +1708,27 @@ const useInference = () => {
|
|
|
1964
1708
|
* Graph RAG inference with entity discovery
|
|
1965
1709
|
*/
|
|
1966
1710
|
const graphRagMutation = useMutation({
|
|
1967
|
-
mutationFn: async ({ input, options, collection, }) => {
|
|
1968
|
-
//
|
|
1969
|
-
const response =
|
|
1970
|
-
|
|
1971
|
-
|
|
1711
|
+
mutationFn: async ({ input, options, collection, callbacks, }) => {
|
|
1712
|
+
// If callbacks provided, use streaming API
|
|
1713
|
+
const response = callbacks
|
|
1714
|
+
? await new Promise((resolve, reject) => {
|
|
1715
|
+
let accumulated = "";
|
|
1716
|
+
const onChunk = (chunk, complete) => {
|
|
1717
|
+
accumulated += chunk;
|
|
1718
|
+
callbacks?.onChunk?.(chunk, complete);
|
|
1719
|
+
if (complete) {
|
|
1720
|
+
resolve(accumulated);
|
|
1721
|
+
}
|
|
1722
|
+
};
|
|
1723
|
+
const onError = (error) => {
|
|
1724
|
+
callbacks?.onError?.(error);
|
|
1725
|
+
reject(new Error(error));
|
|
1726
|
+
};
|
|
1727
|
+
socket
|
|
1728
|
+
.flow(flowId)
|
|
1729
|
+
.graphRagStreaming(input, onChunk, onError, options, collection);
|
|
1730
|
+
})
|
|
1731
|
+
: await socket.flow(flowId).graphRag(input, options || {}, collection);
|
|
1972
1732
|
// Get embeddings for entity discovery
|
|
1973
1733
|
const embeddings = await socket.flow(flowId).embeddings(input);
|
|
1974
1734
|
// Query graph embeddings to find entities
|
|
@@ -1982,8 +1742,27 @@ const useInference = () => {
|
|
|
1982
1742
|
* Basic LLM text completion
|
|
1983
1743
|
*/
|
|
1984
1744
|
const textCompletionMutation = useMutation({
|
|
1985
|
-
mutationFn: async ({ systemPrompt, input, }) => {
|
|
1986
|
-
|
|
1745
|
+
mutationFn: async ({ systemPrompt, input, callbacks, }) => {
|
|
1746
|
+
// If callbacks provided, use streaming API
|
|
1747
|
+
return callbacks
|
|
1748
|
+
? await new Promise((resolve, reject) => {
|
|
1749
|
+
let accumulated = "";
|
|
1750
|
+
const onChunk = (chunk, complete) => {
|
|
1751
|
+
accumulated += chunk;
|
|
1752
|
+
callbacks?.onChunk?.(chunk, complete);
|
|
1753
|
+
if (complete) {
|
|
1754
|
+
resolve(accumulated);
|
|
1755
|
+
}
|
|
1756
|
+
};
|
|
1757
|
+
const onError = (error) => {
|
|
1758
|
+
callbacks?.onError?.(error);
|
|
1759
|
+
reject(new Error(error));
|
|
1760
|
+
};
|
|
1761
|
+
socket
|
|
1762
|
+
.flow(flowId)
|
|
1763
|
+
.textCompletionStreaming(systemPrompt, input, onChunk, onError);
|
|
1764
|
+
})
|
|
1765
|
+
: await socket.flow(flowId).textCompletion(systemPrompt, input);
|
|
1987
1766
|
},
|
|
1988
1767
|
});
|
|
1989
1768
|
/**
|
|
@@ -1992,15 +1771,19 @@ const useInference = () => {
|
|
|
1992
1771
|
const agentMutation = useMutation({
|
|
1993
1772
|
mutationFn: async ({ input, callbacks, }) => {
|
|
1994
1773
|
return new Promise((resolve, reject) => {
|
|
1995
|
-
|
|
1996
|
-
|
|
1774
|
+
let fullAnswer = "";
|
|
1775
|
+
const onThink = (thought, complete) => {
|
|
1776
|
+
callbacks?.onThink?.(thought, complete);
|
|
1997
1777
|
};
|
|
1998
|
-
const onObserve = (observation) => {
|
|
1999
|
-
callbacks?.onObserve?.(observation);
|
|
1778
|
+
const onObserve = (observation, complete) => {
|
|
1779
|
+
callbacks?.onObserve?.(observation, complete);
|
|
2000
1780
|
};
|
|
2001
|
-
const onAnswer = (answer) => {
|
|
2002
|
-
|
|
2003
|
-
|
|
1781
|
+
const onAnswer = (answer, complete) => {
|
|
1782
|
+
fullAnswer += answer;
|
|
1783
|
+
callbacks?.onAnswer?.(answer, complete);
|
|
1784
|
+
if (complete) {
|
|
1785
|
+
resolve(fullAnswer);
|
|
1786
|
+
}
|
|
2004
1787
|
};
|
|
2005
1788
|
const onError = (error) => {
|
|
2006
1789
|
callbacks?.onError?.(error);
|
|
@@ -2032,6 +1815,7 @@ const useChatSession = () => {
|
|
|
2032
1815
|
const notify = useNotification();
|
|
2033
1816
|
// Conversation state
|
|
2034
1817
|
const addMessage = useConversation((state) => state.addMessage);
|
|
1818
|
+
const updateLastMessage = useConversation((state) => state.updateLastMessage);
|
|
2035
1819
|
const setInput = useConversation((state) => state.setInput);
|
|
2036
1820
|
const chatMode = useConversation((state) => state.chatMode);
|
|
2037
1821
|
// Progress and activity management
|
|
@@ -2051,8 +1835,10 @@ const useChatSession = () => {
|
|
|
2051
1835
|
const ragActivity = "Graph RAG: " + input;
|
|
2052
1836
|
const embActivity = "Find entities: " + input;
|
|
2053
1837
|
addActivity(ragActivity);
|
|
1838
|
+
let accumulated = "";
|
|
1839
|
+
let messageAdded = false;
|
|
2054
1840
|
try {
|
|
2055
|
-
// Execute Graph RAG with entity discovery
|
|
1841
|
+
// Execute Graph RAG with streaming and entity discovery
|
|
2056
1842
|
const result = await inference.graphRag({
|
|
2057
1843
|
input,
|
|
2058
1844
|
options: {
|
|
@@ -2062,8 +1848,21 @@ const useChatSession = () => {
|
|
|
2062
1848
|
pathLength: settings.graphrag.pathLength,
|
|
2063
1849
|
},
|
|
2064
1850
|
collection: settings.collection,
|
|
1851
|
+
callbacks: {
|
|
1852
|
+
onChunk: (chunk, complete) => {
|
|
1853
|
+
accumulated += chunk;
|
|
1854
|
+
if (!messageAdded) {
|
|
1855
|
+
// Add empty message on first chunk
|
|
1856
|
+
addMessage("ai", accumulated);
|
|
1857
|
+
messageAdded = true;
|
|
1858
|
+
}
|
|
1859
|
+
else {
|
|
1860
|
+
// Update existing message with accumulated text
|
|
1861
|
+
updateLastMessage(accumulated);
|
|
1862
|
+
}
|
|
1863
|
+
},
|
|
1864
|
+
},
|
|
2065
1865
|
});
|
|
2066
|
-
addMessage("ai", result.response);
|
|
2067
1866
|
removeActivity(ragActivity);
|
|
2068
1867
|
// Start embeddings activity
|
|
2069
1868
|
addActivity(embActivity);
|
|
@@ -2107,12 +1906,27 @@ const useChatSession = () => {
|
|
|
2107
1906
|
const handleBasicLlm = async (input) => {
|
|
2108
1907
|
const activity = "Text completion: " + input;
|
|
2109
1908
|
addActivity(activity);
|
|
1909
|
+
let accumulated = "";
|
|
1910
|
+
let messageAdded = false;
|
|
2110
1911
|
try {
|
|
2111
1912
|
const response = await inference.textCompletion({
|
|
2112
1913
|
systemPrompt: "You are a helpful assistant. Provide clear and concise responses.",
|
|
2113
1914
|
input,
|
|
1915
|
+
callbacks: {
|
|
1916
|
+
onChunk: (chunk, complete) => {
|
|
1917
|
+
accumulated += chunk;
|
|
1918
|
+
if (!messageAdded) {
|
|
1919
|
+
// Add empty message on first chunk
|
|
1920
|
+
addMessage("ai", accumulated);
|
|
1921
|
+
messageAdded = true;
|
|
1922
|
+
}
|
|
1923
|
+
else {
|
|
1924
|
+
// Update existing message with accumulated text
|
|
1925
|
+
updateLastMessage(accumulated);
|
|
1926
|
+
}
|
|
1927
|
+
},
|
|
1928
|
+
},
|
|
2114
1929
|
});
|
|
2115
|
-
addMessage("ai", response);
|
|
2116
1930
|
removeActivity(activity);
|
|
2117
1931
|
setEntities([]);
|
|
2118
1932
|
return response;
|
|
@@ -2128,13 +1942,54 @@ const useChatSession = () => {
|
|
|
2128
1942
|
const handleAgent = async (input) => {
|
|
2129
1943
|
const activity = "Agent: " + input;
|
|
2130
1944
|
addActivity(activity);
|
|
1945
|
+
let thinkingAccumulated = "";
|
|
1946
|
+
let thinkingMessageAdded = false;
|
|
1947
|
+
let observationAccumulated = "";
|
|
1948
|
+
let observationMessageAdded = false;
|
|
1949
|
+
let answerAccumulated = "";
|
|
1950
|
+
let answerMessageAdded = false;
|
|
2131
1951
|
try {
|
|
2132
1952
|
const response = await inference.agent({
|
|
2133
1953
|
input,
|
|
2134
1954
|
callbacks: {
|
|
2135
|
-
onThink: (thought) =>
|
|
2136
|
-
|
|
2137
|
-
|
|
1955
|
+
onThink: (thought, complete) => {
|
|
1956
|
+
thinkingAccumulated += thought;
|
|
1957
|
+
if (!thinkingMessageAdded) {
|
|
1958
|
+
addMessage("ai", thinkingAccumulated, "thinking");
|
|
1959
|
+
thinkingMessageAdded = true;
|
|
1960
|
+
}
|
|
1961
|
+
else {
|
|
1962
|
+
updateLastMessage(thinkingAccumulated);
|
|
1963
|
+
}
|
|
1964
|
+
if (complete) {
|
|
1965
|
+
thinkingAccumulated = "";
|
|
1966
|
+
thinkingMessageAdded = false;
|
|
1967
|
+
}
|
|
1968
|
+
},
|
|
1969
|
+
onObserve: (observation, complete) => {
|
|
1970
|
+
observationAccumulated += observation;
|
|
1971
|
+
if (!observationMessageAdded) {
|
|
1972
|
+
addMessage("ai", observationAccumulated, "observation");
|
|
1973
|
+
observationMessageAdded = true;
|
|
1974
|
+
}
|
|
1975
|
+
else {
|
|
1976
|
+
updateLastMessage(observationAccumulated);
|
|
1977
|
+
}
|
|
1978
|
+
if (complete) {
|
|
1979
|
+
observationAccumulated = "";
|
|
1980
|
+
observationMessageAdded = false;
|
|
1981
|
+
}
|
|
1982
|
+
},
|
|
1983
|
+
onAnswer: (answer, complete) => {
|
|
1984
|
+
answerAccumulated += answer;
|
|
1985
|
+
if (!answerMessageAdded) {
|
|
1986
|
+
addMessage("ai", answerAccumulated, "answer");
|
|
1987
|
+
answerMessageAdded = true;
|
|
1988
|
+
}
|
|
1989
|
+
else {
|
|
1990
|
+
updateLastMessage(answerAccumulated);
|
|
1991
|
+
}
|
|
1992
|
+
},
|
|
2138
1993
|
},
|
|
2139
1994
|
});
|
|
2140
1995
|
removeActivity(activity);
|