1
1
import { Provider , IAgentRuntime , Memory , State } from "@ai16z/eliza" ;
2
- import { GitBookResponse } from '../types' ;
2
+ import { GitBookResponse , GitBookClientConfig } from '../types' ;
3
3
4
4
function cleanText ( text : string ) : string {
5
5
console . log ( '📝 Cleaning text input:' , text ) ;
@@ -15,18 +15,90 @@ function cleanText(text: string): string {
15
15
return cleaned ;
16
16
}
17
17
18
+ async function validateQuery ( runtime : IAgentRuntime , text : string ) : Promise < boolean > {
19
+ console . log ( '🔍 Validating query text:' , text ) ;
20
+
21
+ // Default keywords
22
+ let keywords = {
23
+ contractQueries : [ 'contract' , 'address' ] ,
24
+ generalQueries : [ 'how' , 'what' , 'where' , 'explain' , 'show' , 'list' , 'tell' ] ,
25
+ mustInclude : [ ] ,
26
+ shouldInclude : [ ]
27
+ } ;
28
+
29
+ let documentTriggers : string [ ] = [ ] ;
30
+
31
+ try {
32
+ const gitbookConfig = runtime . character . clientConfig ?. gitbook as GitBookClientConfig ;
33
+ console . log ( '📋 GitBook Config:' , gitbookConfig ) ;
34
+
35
+ if ( gitbookConfig ) {
36
+ if ( gitbookConfig . keywords ) {
37
+ keywords = {
38
+ ...keywords ,
39
+ ...gitbookConfig . keywords
40
+ } ;
41
+ }
42
+ if ( gitbookConfig . documentTriggers ) {
43
+ documentTriggers = gitbookConfig . documentTriggers ;
44
+ }
45
+ }
46
+
47
+ const containsAnyWord = ( text : string , words : string [ ] = [ ] ) => {
48
+ return words . length === 0 || words . some ( word => {
49
+ if ( word . includes ( ' ' ) ) {
50
+ return text . includes ( word . toLowerCase ( ) ) ;
51
+ }
52
+ const regex = new RegExp ( `\\b${ word } \\b` , 'i' ) ;
53
+ return regex . test ( text ) ;
54
+ } ) ;
55
+ } ;
56
+
57
+ const hasDocTrigger = containsAnyWord ( text , documentTriggers ) ;
58
+ const hasContractQuery = containsAnyWord ( text , keywords . contractQueries ) ;
59
+ const hasGeneralQuery = containsAnyWord ( text , keywords . generalQueries ) ;
60
+ const hasMustInclude = containsAnyWord ( text , keywords . mustInclude ) ;
61
+
62
+ const validationResults = {
63
+ hasDocTrigger,
64
+ hasContractQuery,
65
+ hasGeneralQuery,
66
+ hasMustInclude,
67
+ text
68
+ } ;
69
+
70
+ console . log ( '🔍 Validation Results:' , validationResults ) ;
71
+
72
+ const isValid = ( hasDocTrigger || hasContractQuery || hasGeneralQuery ) && hasMustInclude ;
73
+ console . log ( '✅ Validation Result:' , isValid ) ;
74
+ return isValid ;
75
+
76
+ } catch ( error ) {
77
+ console . error ( "❌ Error in validation:" , error ) ;
78
+ return false ;
79
+ }
80
+ }
81
+
18
82
export const gitbookProvider : Provider = {
19
- get : async ( runtime : IAgentRuntime , message : Memory , _state ?: State ) : Promise < string | null > => {
83
+ get : async ( runtime : IAgentRuntime , message : Memory , _state ?: State ) : Promise < string > => {
20
84
console . log ( '🔄 GitBook Provider executing' ) ;
21
85
22
86
try {
23
87
const spaceId = runtime . getSetting ( "GITBOOK_SPACE_ID" ) ;
24
88
if ( ! spaceId ) {
25
89
console . error ( "❌ GitBook Space ID not configured" ) ;
26
- return null ;
90
+ return "" ;
27
91
}
28
92
console . log ( '✓ SpaceID configured:' , spaceId ) ;
29
93
94
+ const text = message . content . text . toLowerCase ( ) . trim ( ) ;
95
+ const isValidQuery = await validateQuery ( runtime , text ) ;
96
+
97
+ if ( ! isValidQuery ) {
98
+ console . log ( '⚠️ Query validation failed' ) ;
99
+ return "" ;
100
+ }
101
+
30
102
const cleanedQuery = cleanText ( message . content . text ) ;
31
103
32
104
console . log ( '📡 Making GitBook API request...' ) ;
@@ -46,17 +118,17 @@ export const gitbookProvider: Provider = {
46
118
47
119
if ( ! response . ok ) {
48
120
console . error ( '❌ GitBook API error:' , response . status ) ;
49
- return null ;
121
+ return "" ;
50
122
}
51
123
52
124
console . log ( '✓ GitBook API response received' ) ;
53
125
const result : GitBookResponse = await response . json ( ) ;
54
126
console . log ( '📄 GitBook Response:' , result ?. answer ?. text || 'No answer text' ) ;
55
127
56
- return result . answer ?. text || null ;
128
+ return result . answer ?. text || "" ;
57
129
} catch ( error ) {
58
130
console . error ( "❌ Error in GitBook provider:" , error ) ;
59
- return null ;
131
+ return "" ;
60
132
}
61
133
}
62
134
} ;
0 commit comments