-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathFereProService.ts
104 lines (91 loc) · 3.42 KB
/
FereProService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import WebSocket from "ws";
import { IAgentRuntime, Service } from "@elizaos/core";
interface ChatResponse {
answer: string;
chat_id: string;
representation?: Record<string, any>[];
agent_api_name: string;
query_summary: string;
agent_credits: number;
credits_available: number;
}
interface FereMessage {
message: string;
stream?: boolean;
debug?: boolean;
}
interface FereResponse {
success: boolean;
data?: ChatResponse;
error?: string;
}
export class FereProService extends Service {
private ws: WebSocket | null = null;
private user: string = "1a5b4a29-9d95-44c8-aef3-05a8e515f43e";
private runtime: IAgentRuntime | null = null;
async initialize(runtime: IAgentRuntime): Promise<void> {
console.log("Initializing FerePro WebSocket Service");
this.runtime = runtime;
this.user = runtime.getSetting("FERE_USER_ID") ?? this.user;
}
/**
* Connect to WebSocket and send a message
*/
async sendMessage(payload: FereMessage): Promise<FereResponse> {
return new Promise((resolve, reject) => {
try {
const url = `wss:/api.fereai.xyz/chat/v2/ws/${this.user}`;
this.ws = new WebSocket(url);
this.ws.on("open", () => {
console.log("Connected to FerePro WebSocket");
this.ws?.send(JSON.stringify(payload));
console.log("Message sent:", payload.message);
});
this.ws.on("message", (data) => {
try {
const response = JSON.parse(data.toString());
const chatResponse: ChatResponse = {
answer: response.answer,
chat_id: response.chat_id,
representation: response.representation || null,
agent_api_name: response.agent_api_name,
query_summary: response.query_summary,
agent_credits: response.agent_credits,
credits_available: response.credits_available,
};
console.log("Received ChatResponse:", chatResponse);
resolve({
success: true,
data: chatResponse,
});
} catch (err) {
console.error("Error parsing response:", err);
reject({
success: false,
error: "Invalid response format",
});
}
});
this.ws.on("close", () => {
console.log("Disconnected from FerePro WebSocket");
});
this.ws.on("error", (err) => {
console.error("WebSocket error:", err);
reject({
success: false,
error: err.message,
});
});
} catch (error) {
reject({
success: false,
error:
error instanceof Error
? error.message
: "Error Occured",
});
}
});
}
}
export default FereProService;