1
- import { Provider , IAgentRuntime , Memory , State , elizaLogger } from "@elizaos/core" ;
2
- import { GitBookResponse , GitBookClientConfig } from '../types' ;
1
+ import {
2
+ Provider ,
3
+ IAgentRuntime ,
4
+ Memory ,
5
+ State ,
6
+ elizaLogger ,
7
+ } from "@elizaos/core" ;
8
+ import { GitBookResponse , GitBookClientConfig } from "../types" ;
3
9
4
10
function cleanText ( text : string ) : string {
5
11
const cleaned = text
6
- . replace ( / < @ ! ? \d + > / g, '' ) // Discord mentions
7
- . replace ( / < # \d + > / g, '' ) // Discord channels
8
- . replace ( / < @ & \d + > / g, '' ) // Discord roles
9
- . replace ( / (?: ^ | \s ) @ [ \w _ ] + / g, '' ) // Platform mentions
12
+ . replace ( / < @ ! ? \d + > / g, "" ) // Discord mentions
13
+ . replace ( / < # \d + > / g, "" ) // Discord channels
14
+ . replace ( / < @ & \d + > / g, "" ) // Discord roles
15
+ . replace ( / (?: ^ | \s ) @ [ \w _ ] + / g, "" ) // Platform mentions
10
16
. trim ( ) ;
11
17
12
18
return cleaned ;
13
19
}
14
20
15
- async function validateQuery ( runtime : IAgentRuntime , text : string ) : Promise < boolean > {
21
+ async function validateQuery (
22
+ runtime : IAgentRuntime ,
23
+ text : string
24
+ ) : Promise < boolean > {
16
25
// Default general queries - everything else comes from config
17
26
const keywords = {
18
27
generalQueries : [
19
- 'how' , 'what' , 'where' , 'explain' , 'show' , 'tell' ,
20
- 'can' , 'does' , 'is' , 'are' , 'will' , 'why' ,
21
- 'benefits' , 'features' , 'cost' , 'price' ,
22
- 'use' , 'using' , 'work' , 'access' , 'get'
23
- ]
28
+ "how" ,
29
+ "what" ,
30
+ "where" ,
31
+ "explain" ,
32
+ "show" ,
33
+ "tell" ,
34
+ "can" ,
35
+ "does" ,
36
+ "is" ,
37
+ "are" ,
38
+ "will" ,
39
+ "why" ,
40
+ "benefits" ,
41
+ "features" ,
42
+ "cost" ,
43
+ "price" ,
44
+ "use" ,
45
+ "using" ,
46
+ "work" ,
47
+ "access" ,
48
+ "get" ,
49
+ ] ,
24
50
} ;
25
51
26
52
try {
27
- const gitbookConfig = runtime . character . clientConfig ?. gitbook as GitBookClientConfig ;
53
+ const gitbookConfig = runtime . character . clientConfig
54
+ ?. gitbook as GitBookClientConfig ;
28
55
29
56
// Get project terms and document triggers from config
30
57
const projectTerms = gitbookConfig ?. keywords ?. projectTerms || [ ] ;
@@ -34,18 +61,21 @@ async function validateQuery(runtime: IAgentRuntime, text: string): Promise<bool
34
61
if ( gitbookConfig ?. keywords ?. generalQueries ) {
35
62
keywords . generalQueries = [
36
63
...keywords . generalQueries ,
37
- ...gitbookConfig . keywords . generalQueries
64
+ ...gitbookConfig . keywords . generalQueries ,
38
65
] ;
39
66
}
40
67
41
68
const containsAnyWord = ( text : string , words : string [ ] = [ ] ) => {
42
- return words . length === 0 || words . some ( word => {
43
- if ( word . includes ( ' ' ) ) {
44
- return text . includes ( word . toLowerCase ( ) ) ;
45
- }
46
- const regex = new RegExp ( `\\b${ word } \\b` , 'i' ) ;
47
- return regex . test ( text ) ;
48
- } ) ;
69
+ return (
70
+ words . length === 0 ||
71
+ words . some ( ( word ) => {
72
+ if ( word . includes ( " " ) ) {
73
+ return text . includes ( word . toLowerCase ( ) ) ;
74
+ }
75
+ const regex = new RegExp ( `\\b${ word } \\b` , "i" ) ;
76
+ return regex . test ( text ) ;
77
+ } )
78
+ ) ;
49
79
} ;
50
80
51
81
const hasProjectTerm = containsAnyWord ( text , projectTerms ) ;
@@ -54,29 +84,32 @@ async function validateQuery(runtime: IAgentRuntime, text: string): Promise<bool
54
84
55
85
const isValid = hasProjectTerm || hasDocTrigger || hasGeneralQuery ;
56
86
57
- elizaLogger . info ( `✅ Is GitBook Validation Result: ${ isValid } ` )
87
+ elizaLogger . info ( `✅ Is GitBook Validation Result: ${ isValid } ` ) ;
58
88
return isValid ;
59
-
60
89
} catch ( error ) {
61
- elizaLogger . warn ( `❌ Error in GitBook validation:\n${ error } ` )
90
+ elizaLogger . warn ( `❌ Error in GitBook validation:\n${ error } ` ) ;
62
91
return false ;
63
92
}
64
93
}
65
94
66
95
export const gitbookProvider : Provider = {
67
- get : async ( runtime : IAgentRuntime , message : Memory , _state ?: State ) : Promise < string > => {
96
+ get : async (
97
+ runtime : IAgentRuntime ,
98
+ message : Memory ,
99
+ _state ?: State
100
+ ) : Promise < string > => {
68
101
try {
69
102
const spaceId = runtime . getSetting ( "GITBOOK_SPACE_ID" ) ;
70
103
if ( ! spaceId ) {
71
- elizaLogger . error ( ' ⚠️ GitBook Space ID not configured' )
104
+ elizaLogger . error ( " ⚠️ GitBook Space ID not configured" ) ;
72
105
return "" ;
73
106
}
74
107
75
108
const text = message . content . text . toLowerCase ( ) . trim ( ) ;
76
109
const isValidQuery = await validateQuery ( runtime , text ) ;
77
110
78
111
if ( ! isValidQuery ) {
79
- elizaLogger . info ( ' ⚠️ GitBook Query validation failed' )
112
+ elizaLogger . info ( " ⚠️ GitBook Query validation failed" ) ;
80
113
return "" ;
81
114
}
82
115
@@ -87,26 +120,26 @@ export const gitbookProvider: Provider = {
87
120
{
88
121
method : "POST" ,
89
122
headers : {
90
- "Content-Type" : "application/json"
123
+ "Content-Type" : "application/json" ,
91
124
} ,
92
125
body : JSON . stringify ( {
93
126
query : cleanedQuery ,
94
- variables : { }
95
- } )
127
+ variables : { } ,
128
+ } ) ,
96
129
}
97
130
) ;
98
131
99
132
if ( ! response . ok ) {
100
- console . error ( ' ❌ GitBook API error:' , response . status ) ;
133
+ elizaLogger . error ( " ❌ GitBook API error:" , response . status ) ;
101
134
return "" ;
102
135
}
103
136
104
137
const result : GitBookResponse = await response . json ( ) ;
105
138
106
139
return result . answer ?. text || "" ;
107
140
} catch ( error ) {
108
- console . error ( "❌ Error in GitBook provider:" , error ) ;
141
+ elizaLogger . error ( "❌ Error in GitBook provider:" , error ) ;
109
142
return "" ;
110
143
}
111
- }
144
+ } ,
112
145
} ;
0 commit comments