Skip to content

Commit e5c93be

Browse files
committed
updated gitbook
1 parent 85f9042 commit e5c93be

File tree

5 files changed

+83
-187
lines changed

5 files changed

+83
-187
lines changed

packages/plugin-gitbook/src/actions/query.ts

-153
This file was deleted.

packages/plugin-gitbook/src/index.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import { Plugin } from "@ai16z/eliza";
2-
import { queryAction } from "./actions/query";
32
import { gitbookProvider } from "./providers/gitbook";
4-
import { gitbookTemplate } from "./templates";
5-
6-
// Export the template for use by runtime
7-
export { gitbookTemplate };
83

94
export const gitbookPlugin: Plugin = {
105
name: "GitBook Documentation",
116
description: "Plugin for querying GitBook documentation",
12-
actions: [queryAction],
7+
actions: [],
138
providers: [gitbookProvider],
149
evaluators: []
1510
};
1611

1712
export default gitbookPlugin;
1813

19-
// Export all types and components
14+
// Export types for external use
2015
export * from './types';

packages/plugin-gitbook/src/providers/gitbook.ts

+78-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Provider, IAgentRuntime, Memory, State } from "@ai16z/eliza";
2-
import { GitBookResponse } from '../types';
2+
import { GitBookResponse, GitBookClientConfig } from '../types';
33

44
function cleanText(text: string): string {
55
console.log('📝 Cleaning text input:', text);
@@ -15,18 +15,90 @@ function cleanText(text: string): string {
1515
return cleaned;
1616
}
1717

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+
1882
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> => {
2084
console.log('🔄 GitBook Provider executing');
2185

2286
try {
2387
const spaceId = runtime.getSetting("GITBOOK_SPACE_ID");
2488
if (!spaceId) {
2589
console.error("❌ GitBook Space ID not configured");
26-
return null;
90+
return "";
2791
}
2892
console.log('✓ SpaceID configured:', spaceId);
2993

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+
30102
const cleanedQuery = cleanText(message.content.text);
31103

32104
console.log('📡 Making GitBook API request...');
@@ -46,17 +118,17 @@ export const gitbookProvider: Provider = {
46118

47119
if (!response.ok) {
48120
console.error('❌ GitBook API error:', response.status);
49-
return null;
121+
return "";
50122
}
51123

52124
console.log('✓ GitBook API response received');
53125
const result: GitBookResponse = await response.json();
54126
console.log('📄 GitBook Response:', result?.answer?.text || 'No answer text');
55127

56-
return result.answer?.text || null;
128+
return result.answer?.text || "";
57129
} catch (error) {
58130
console.error("❌ Error in GitBook provider:", error);
59-
return null;
131+
return "";
60132
}
61133
}
62134
};

packages/plugin-gitbook/src/templates/index.ts

-19
This file was deleted.

packages/plugin-gitbook/src/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { Content } from "@ai16z/eliza";
2-
1+
// Keywords configuration interface
32
export interface GitBookKeywords {
43
contractQueries?: string[];
54
generalQueries?: string[];
65
mustInclude?: string[];
76
shouldInclude?: string[];
87
}
98

9+
// Client configuration in character.json
1010
export interface GitBookClientConfig {
1111
keywords?: GitBookKeywords;
1212
documentTriggers?: string[];
1313
}
1414

15+
// GitBook API response type
1516
export interface GitBookResponse {
1617
answer?: {
1718
text: string;

0 commit comments

Comments
 (0)