@curaious/uno-converse 0.1.16 → 0.1.18
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 +244 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +137 -17
- package/dist/index.d.ts +137 -17
- package/dist/index.js +242 -91
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var axios = require('axios');
|
|
4
3
|
var react = require('react');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
var axios = require('axios');
|
|
5
6
|
|
|
6
7
|
// Message Roles
|
|
7
8
|
exports.Role = void 0;
|
|
@@ -400,6 +401,203 @@ async function streamSSE(url, requestOptions, callbacks, abortSignal) {
|
|
|
400
401
|
}
|
|
401
402
|
}
|
|
402
403
|
|
|
404
|
+
function useProject(options) {
|
|
405
|
+
const { projectName, baseUrl, getHeaders, autoLoad = true } = options;
|
|
406
|
+
// Project state
|
|
407
|
+
const [projectId, setProjectId] = react.useState('');
|
|
408
|
+
const [projectLoading, setProjectLoading] = react.useState(false);
|
|
409
|
+
// Use ref to store current projectId for interceptor access
|
|
410
|
+
const projectIdRef = react.useRef('');
|
|
411
|
+
// Update ref whenever projectId changes
|
|
412
|
+
react.useEffect(() => {
|
|
413
|
+
projectIdRef.current = projectId;
|
|
414
|
+
}, [projectId]);
|
|
415
|
+
// Create axios instance with request interceptor for custom headers and project_id
|
|
416
|
+
const axiosInstance = react.useMemo(() => {
|
|
417
|
+
const instance = axios.create({
|
|
418
|
+
baseURL: baseUrl,
|
|
419
|
+
headers: {
|
|
420
|
+
'Content-Type': 'application/json',
|
|
421
|
+
},
|
|
422
|
+
});
|
|
423
|
+
// Add request interceptor to inject custom headers and project_id
|
|
424
|
+
instance.interceptors.request.use(async (config) => {
|
|
425
|
+
// Add custom headers if getHeaders function is provided
|
|
426
|
+
if (getHeaders) {
|
|
427
|
+
const customHeaders = await getHeaders();
|
|
428
|
+
Object.assign(config.headers, customHeaders);
|
|
429
|
+
}
|
|
430
|
+
// Automatically add project_id to query params if available
|
|
431
|
+
const currentProjectId = projectIdRef.current;
|
|
432
|
+
if (currentProjectId) {
|
|
433
|
+
if (config.params) {
|
|
434
|
+
config.params = {
|
|
435
|
+
...config.params,
|
|
436
|
+
project_id: currentProjectId,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
config.params = { project_id: currentProjectId };
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return config;
|
|
444
|
+
});
|
|
445
|
+
return instance;
|
|
446
|
+
}, [baseUrl, getHeaders]);
|
|
447
|
+
// ============================================
|
|
448
|
+
// API Helper Functions
|
|
449
|
+
// ============================================
|
|
450
|
+
/**
|
|
451
|
+
* Build query params (project_id is automatically added by axios interceptor)
|
|
452
|
+
*/
|
|
453
|
+
const buildParams = react.useCallback((params) => {
|
|
454
|
+
return params || {};
|
|
455
|
+
}, []);
|
|
456
|
+
/**
|
|
457
|
+
* Get headers for streaming requests (combines default + custom headers)
|
|
458
|
+
*/
|
|
459
|
+
const getRequestHeaders = react.useCallback(async () => {
|
|
460
|
+
const headers = {
|
|
461
|
+
'Content-Type': 'application/json',
|
|
462
|
+
};
|
|
463
|
+
// Add custom headers if getHeaders function is provided
|
|
464
|
+
if (getHeaders) {
|
|
465
|
+
const customHeaders = await getHeaders();
|
|
466
|
+
Object.assign(headers, customHeaders);
|
|
467
|
+
}
|
|
468
|
+
return headers;
|
|
469
|
+
}, [getHeaders]);
|
|
470
|
+
// ============================================
|
|
471
|
+
// Project Management
|
|
472
|
+
// ============================================
|
|
473
|
+
/**
|
|
474
|
+
* Fetch the project ID using the project name
|
|
475
|
+
*/
|
|
476
|
+
const fetchProjectId = react.useCallback(async () => {
|
|
477
|
+
if (!projectName) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
setProjectLoading(true);
|
|
481
|
+
try {
|
|
482
|
+
const response = await axiosInstance.get('/project/id', {
|
|
483
|
+
params: { name: projectName },
|
|
484
|
+
});
|
|
485
|
+
const id = typeof response.data === 'string' ? response.data : response.data.data;
|
|
486
|
+
setProjectId(id || '');
|
|
487
|
+
}
|
|
488
|
+
catch (error) {
|
|
489
|
+
console.error('Failed to fetch project ID:', error);
|
|
490
|
+
throw error;
|
|
491
|
+
}
|
|
492
|
+
finally {
|
|
493
|
+
setProjectLoading(false);
|
|
494
|
+
}
|
|
495
|
+
}, [axiosInstance, projectName]);
|
|
496
|
+
// ============================================
|
|
497
|
+
// Effects for auto-loading
|
|
498
|
+
// ============================================
|
|
499
|
+
// Fetch project ID on mount
|
|
500
|
+
react.useEffect(() => {
|
|
501
|
+
if (autoLoad && projectName) {
|
|
502
|
+
fetchProjectId();
|
|
503
|
+
}
|
|
504
|
+
}, [autoLoad, projectName, fetchProjectId]);
|
|
505
|
+
return {
|
|
506
|
+
// Project state
|
|
507
|
+
projectId,
|
|
508
|
+
projectLoading,
|
|
509
|
+
// API client
|
|
510
|
+
axiosInstance,
|
|
511
|
+
buildParams,
|
|
512
|
+
getRequestHeaders,
|
|
513
|
+
baseUrl,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Create the context with undefined as default to ensure it's used within provider
|
|
518
|
+
const ProjectContext = react.createContext(undefined);
|
|
519
|
+
/**
|
|
520
|
+
* ProjectProvider component that manages project state using React Context.
|
|
521
|
+
*
|
|
522
|
+
* This provider wraps the useProject hook and makes the project state available
|
|
523
|
+
* to all child components through context.
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* ```tsx
|
|
527
|
+
* import { ProjectProvider, useProjectContext } from '@praveen001/uno-converse';
|
|
528
|
+
*
|
|
529
|
+
* function App() {
|
|
530
|
+
* return (
|
|
531
|
+
* <ProjectProvider
|
|
532
|
+
* baseUrl="https://api.example.com/api/agent-server"
|
|
533
|
+
* projectName="my-project"
|
|
534
|
+
* getHeaders={() => ({
|
|
535
|
+
* 'Authorization': `Bearer ${getToken()}`,
|
|
536
|
+
* })}
|
|
537
|
+
* >
|
|
538
|
+
* <YourApp />
|
|
539
|
+
* </ProjectProvider>
|
|
540
|
+
* );
|
|
541
|
+
* }
|
|
542
|
+
*
|
|
543
|
+
* function YourApp() {
|
|
544
|
+
* const { projectId, projectLoading } = useProjectContext();
|
|
545
|
+
*
|
|
546
|
+
* if (projectLoading) {
|
|
547
|
+
* return <div>Loading project...</div>;
|
|
548
|
+
* }
|
|
549
|
+
*
|
|
550
|
+
* return <div>Project ID: {projectId}</div>;
|
|
551
|
+
* }
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
const ProjectProvider = ({ baseUrl, projectName, getHeaders, autoLoad = true, children, }) => {
|
|
555
|
+
// Use the useProject hook to manage project state
|
|
556
|
+
const projectState = useProject({
|
|
557
|
+
baseUrl,
|
|
558
|
+
projectName,
|
|
559
|
+
getHeaders,
|
|
560
|
+
autoLoad,
|
|
561
|
+
});
|
|
562
|
+
// Memoize the context value to prevent unnecessary re-renders
|
|
563
|
+
const contextValue = react.useMemo(() => projectState, [
|
|
564
|
+
projectState.projectId,
|
|
565
|
+
projectState.projectLoading,
|
|
566
|
+
projectState.axiosInstance,
|
|
567
|
+
projectState.buildParams,
|
|
568
|
+
projectState.getRequestHeaders,
|
|
569
|
+
projectState.baseUrl,
|
|
570
|
+
]);
|
|
571
|
+
return (jsxRuntime.jsx(ProjectContext.Provider, { value: contextValue, children: children }));
|
|
572
|
+
};
|
|
573
|
+
/**
|
|
574
|
+
* Hook to access the project context.
|
|
575
|
+
*
|
|
576
|
+
* Must be used within a ProjectProvider component.
|
|
577
|
+
*
|
|
578
|
+
* @throws {Error} If used outside of ProjectProvider
|
|
579
|
+
*
|
|
580
|
+
* @example
|
|
581
|
+
* ```tsx
|
|
582
|
+
* function MyComponent() {
|
|
583
|
+
* const { projectId, projectLoading } = useProjectContext();
|
|
584
|
+
*
|
|
585
|
+
* return (
|
|
586
|
+
* <div>
|
|
587
|
+
* {projectLoading ? 'Loading...' : `Project ID: ${projectId}`}
|
|
588
|
+
* </div>
|
|
589
|
+
* );
|
|
590
|
+
* }
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
const useProjectContext = () => {
|
|
594
|
+
const context = react.useContext(ProjectContext);
|
|
595
|
+
if (context === undefined) {
|
|
596
|
+
throw new Error('useProjectContext must be used within a ProjectProvider');
|
|
597
|
+
}
|
|
598
|
+
return context;
|
|
599
|
+
};
|
|
600
|
+
|
|
403
601
|
/**
|
|
404
602
|
* Simple ID generator for message IDs
|
|
405
603
|
*/
|
|
@@ -422,11 +620,6 @@ function generateId() {
|
|
|
422
620
|
* startNewChat,
|
|
423
621
|
* } = useConversation({
|
|
424
622
|
* namespace: 'my-app',
|
|
425
|
-
* projectName: 'my-project',
|
|
426
|
-
* baseUrl: 'https://my-uno-server.com/api/agent-server',
|
|
427
|
-
* getHeaders: () => ({
|
|
428
|
-
* 'Authorization': `Bearer ${getToken()}`,
|
|
429
|
-
* }),
|
|
430
623
|
* });
|
|
431
624
|
*
|
|
432
625
|
* const handleSend = async (text: string) => {
|
|
@@ -451,28 +644,9 @@ function generateId() {
|
|
|
451
644
|
* ```
|
|
452
645
|
*/
|
|
453
646
|
function useConversation(options) {
|
|
454
|
-
const { namespace,
|
|
455
|
-
//
|
|
456
|
-
const axiosInstance =
|
|
457
|
-
const instance = axios.create({
|
|
458
|
-
baseURL: baseUrl,
|
|
459
|
-
headers: {
|
|
460
|
-
'Content-Type': 'application/json',
|
|
461
|
-
},
|
|
462
|
-
});
|
|
463
|
-
// Add request interceptor to inject custom headers
|
|
464
|
-
if (getHeaders) {
|
|
465
|
-
instance.interceptors.request.use(async (config) => {
|
|
466
|
-
const customHeaders = await getHeaders();
|
|
467
|
-
Object.assign(config.headers, customHeaders);
|
|
468
|
-
return config;
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
return instance;
|
|
472
|
-
}, [baseUrl, getHeaders]);
|
|
473
|
-
// Project state
|
|
474
|
-
const [projectId, setProjectId] = react.useState('');
|
|
475
|
-
const [projectLoading, setProjectLoading] = react.useState(false);
|
|
647
|
+
const { namespace, autoLoad = true } = options;
|
|
648
|
+
// Get project context (axios instance, projectId, etc.)
|
|
649
|
+
const { axiosInstance, projectId, projectLoading, buildParams, getRequestHeaders, baseUrl, } = useProjectContext();
|
|
476
650
|
// Conversation list state
|
|
477
651
|
const [conversations, setConversations] = react.useState([]);
|
|
478
652
|
const [conversationsLoading, setConversationsLoading] = react.useState(false);
|
|
@@ -495,59 +669,7 @@ function useConversation(options) {
|
|
|
495
669
|
// ============================================
|
|
496
670
|
// API Helper Functions
|
|
497
671
|
// ============================================
|
|
498
|
-
|
|
499
|
-
* Build query params with project_id if available
|
|
500
|
-
*/
|
|
501
|
-
const buildParams = react.useCallback((params) => {
|
|
502
|
-
const result = {};
|
|
503
|
-
if (projectId) {
|
|
504
|
-
result.project_id = projectId;
|
|
505
|
-
}
|
|
506
|
-
if (params) {
|
|
507
|
-
Object.assign(result, params);
|
|
508
|
-
}
|
|
509
|
-
return result;
|
|
510
|
-
}, [projectId]);
|
|
511
|
-
/**
|
|
512
|
-
* Get headers for streaming requests (combines default + custom headers)
|
|
513
|
-
*/
|
|
514
|
-
const getRequestHeaders = react.useCallback(async () => {
|
|
515
|
-
const headers = {
|
|
516
|
-
'Content-Type': 'application/json',
|
|
517
|
-
};
|
|
518
|
-
// Add custom headers if getHeaders function is provided
|
|
519
|
-
if (getHeaders) {
|
|
520
|
-
const customHeaders = await getHeaders();
|
|
521
|
-
Object.assign(headers, customHeaders);
|
|
522
|
-
}
|
|
523
|
-
return headers;
|
|
524
|
-
}, [getHeaders]);
|
|
525
|
-
// ============================================
|
|
526
|
-
// Project Management
|
|
527
|
-
// ============================================
|
|
528
|
-
/**
|
|
529
|
-
* Fetch the project ID using the project name
|
|
530
|
-
*/
|
|
531
|
-
const fetchProjectId = react.useCallback(async () => {
|
|
532
|
-
if (!projectName) {
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
setProjectLoading(true);
|
|
536
|
-
try {
|
|
537
|
-
const response = await axiosInstance.get('/project/id', {
|
|
538
|
-
params: { name: projectName },
|
|
539
|
-
});
|
|
540
|
-
const id = typeof response.data === 'string' ? response.data : response.data.data;
|
|
541
|
-
setProjectId(id || '');
|
|
542
|
-
}
|
|
543
|
-
catch (error) {
|
|
544
|
-
console.error('Failed to fetch project ID:', error);
|
|
545
|
-
throw error;
|
|
546
|
-
}
|
|
547
|
-
finally {
|
|
548
|
-
setProjectLoading(false);
|
|
549
|
-
}
|
|
550
|
-
}, [axiosInstance, projectName]);
|
|
672
|
+
// Note: buildParams and getRequestHeaders are now provided by ProjectProvider
|
|
551
673
|
// ============================================
|
|
552
674
|
// Conversation Management
|
|
553
675
|
// ============================================
|
|
@@ -792,12 +914,6 @@ function useConversation(options) {
|
|
|
792
914
|
// ============================================
|
|
793
915
|
// Effects for auto-loading
|
|
794
916
|
// ============================================
|
|
795
|
-
// Fetch project ID on mount
|
|
796
|
-
react.useEffect(() => {
|
|
797
|
-
if (autoLoad && projectName) {
|
|
798
|
-
fetchProjectId();
|
|
799
|
-
}
|
|
800
|
-
}, [autoLoad, projectName, fetchProjectId]);
|
|
801
917
|
// Load conversations after project ID is fetched
|
|
802
918
|
react.useEffect(() => {
|
|
803
919
|
if (autoLoad && projectId) {
|
|
@@ -824,9 +940,6 @@ function useConversation(options) {
|
|
|
824
940
|
? [...messages, streamingMessage]
|
|
825
941
|
: messages;
|
|
826
942
|
return {
|
|
827
|
-
// Project state
|
|
828
|
-
projectId,
|
|
829
|
-
projectLoading,
|
|
830
943
|
// Conversation list state
|
|
831
944
|
conversations,
|
|
832
945
|
conversationsLoading,
|
|
@@ -858,7 +971,46 @@ function useConversation(options) {
|
|
|
858
971
|
};
|
|
859
972
|
}
|
|
860
973
|
|
|
974
|
+
function useAgent(options) {
|
|
975
|
+
const { name } = options;
|
|
976
|
+
// Get project context (axios instance, projectId, etc.)
|
|
977
|
+
const { axiosInstance, projectId, buildParams, } = useProjectContext();
|
|
978
|
+
// Agents state
|
|
979
|
+
const [agent, setAgent] = react.useState(null);
|
|
980
|
+
const [agentLoading, setAgentLoading] = react.useState(false);
|
|
981
|
+
// ============================================
|
|
982
|
+
// Agent Management
|
|
983
|
+
// ============================================
|
|
984
|
+
/**
|
|
985
|
+
* Fetch the agent
|
|
986
|
+
*/
|
|
987
|
+
const loadAgent = react.useCallback(async () => {
|
|
988
|
+
setAgentLoading(true);
|
|
989
|
+
try {
|
|
990
|
+
const response = await axiosInstance.get('/agent-configs/by-name', {
|
|
991
|
+
params: buildParams({ name }),
|
|
992
|
+
});
|
|
993
|
+
setAgent(response.data.data);
|
|
994
|
+
}
|
|
995
|
+
catch (error) {
|
|
996
|
+
console.error('Failed to load agent:', error);
|
|
997
|
+
throw error;
|
|
998
|
+
}
|
|
999
|
+
finally {
|
|
1000
|
+
setAgentLoading(false);
|
|
1001
|
+
}
|
|
1002
|
+
}, [axiosInstance, name]);
|
|
1003
|
+
// Fetch agent after project is fetched
|
|
1004
|
+
react.useEffect(() => {
|
|
1005
|
+
if (projectId) {
|
|
1006
|
+
loadAgent();
|
|
1007
|
+
}
|
|
1008
|
+
}, [projectId, loadAgent]);
|
|
1009
|
+
return { agent, agentLoading };
|
|
1010
|
+
}
|
|
1011
|
+
|
|
861
1012
|
exports.ChunkProcessor = ChunkProcessor;
|
|
1013
|
+
exports.ProjectProvider = ProjectProvider;
|
|
862
1014
|
exports.isEasyMessage = isEasyMessage;
|
|
863
1015
|
exports.isFunctionCallMessage = isFunctionCallMessage;
|
|
864
1016
|
exports.isFunctionCallOutputMessage = isFunctionCallOutputMessage;
|
|
@@ -866,5 +1018,7 @@ exports.isImageGenerationCallMessage = isImageGenerationCallMessage;
|
|
|
866
1018
|
exports.isInputMessage = isInputMessage;
|
|
867
1019
|
exports.isReasoningMessage = isReasoningMessage;
|
|
868
1020
|
exports.streamSSE = streamSSE;
|
|
1021
|
+
exports.useAgent = useAgent;
|
|
869
1022
|
exports.useConversation = useConversation;
|
|
1023
|
+
exports.useProjectContext = useProjectContext;
|
|
870
1024
|
//# sourceMappingURL=index.cjs.map
|