File size: 9,113 Bytes
440cddf
 
 
 
 
 
 
9284ad7
440cddf
 
 
 
9284ad7
440cddf
 
9284ad7
 
 
 
 
 
 
 
 
 
 
 
440cddf
9284ad7
440cddf
 
 
 
 
 
 
 
9284ad7
440cddf
 
 
 
 
 
 
 
 
 
 
 
 
 
9284ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440cddf
 
 
 
 
 
9284ad7
 
 
 
440cddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9284ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440cddf
9284ad7
 
440cddf
 
9284ad7
 
440cddf
 
9284ad7
440cddf
 
9284ad7
 
 
440cddf
 
9284ad7
 
440cddf
 
9284ad7
440cddf
 
9284ad7
 
440cddf
 
9284ad7
 
 
 
 
 
 
440cddf
 
9284ad7
440cddf
 
 
9284ad7
 
 
 
440cddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
// Node 20 includes native fetch support
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bot = void 0;
// AI Client for Groq with Failover Support + Circuit Breaker
// Fallback shims for Probot runtime
/* eslint-disable no-console */
const chatgpt_1 = require("chatgpt");
const p_retry_1 = __importDefault(require("p-retry"));
const opossum_1 = __importDefault(require("opossum"));
const options_1 = require("./options");
const limits_1 = require("./limits");
const pino_1 = __importDefault(require("pino"));
const logger = (0, pino_1.default)({ level: process.env.LOG_LEVEL || 'info' });
// Circuit breaker configuration
const CIRCUIT_BREAKER_OPTIONS = {
    timeout: 60000, // 60 seconds
    errorThresholdPercentage: 50,
    resetTimeout: 30000,
    volumeThreshold: 5,
};
const logInfo = (msg) => logger.info(msg);
const logWarning = (msg) => logger.warn(msg);
const logError = (msg) => logger.error(msg);
const setFailed = (msg) => {
    logger.error(msg);
    process.exit(1);
};
class Bot {
    api = null;
    options;
    aiOptions;
    currentModel;
    fallbackModels;
    circuitBreaker = null;
    constructor(options, aiOptions) {
        this.options = options;
        this.aiOptions = aiOptions;
        this.currentModel = aiOptions.model;
        // Setup fallback chain based on tier
        if (options_1.Options.HEAVY_MODELS.includes(this.currentModel)) {
            this.fallbackModels = [...options_1.Options.HEAVY_MODELS];
        }
        else {
            this.fallbackModels = [...options_1.Options.LIGHT_MODELS];
        }
        // Remove current model from fallbacks to avoid immediate repeat
        this.fallbackModels = this.fallbackModels.filter(m => m !== this.currentModel);
        this.initClient();
        this.initCircuitBreaker();
    }
    initCircuitBreaker() {
        if (!this.api)
            return;
        // Wrap the sendMessage method with circuit breaker
        this.circuitBreaker = new opossum_1.default(async ({ message, opts }) => {
            return this.api.sendMessage(message, opts);
        }, CIRCUIT_BREAKER_OPTIONS);
        // Circuit breaker event handlers
        this.circuitBreaker.on('open', () => {
            logWarning(`Circuit breaker opened for model: ${this.currentModel}`);
        });
        this.circuitBreaker.on('halfOpen', () => {
            logInfo(`Circuit breaker half-open for model: ${this.currentModel}`);
        });
        this.circuitBreaker.on('close', () => {
            logInfo(`Circuit breaker closed for model: ${this.currentModel}`);
        });
        this.circuitBreaker.on('fallback', (_result) => {
            logWarning(`Circuit breaker fallback executed for model: ${this.currentModel}`);
        });
    }
    initClient() {
        const apiKey = process.env.GROQ_API_KEY || process.env.AI_API_KEY;
        if (apiKey) {
            const currentDate = new Date().toISOString().split('T')[0];
            const limits = new limits_1.TokenLimits(this.currentModel);
            const systemMessage = `${this.options.systemMessage} 

Knowledge cutoff: ${limits.knowledgeCutOff}

Current date: ${currentDate}

IMPORTANT: Entire response must be in ISO code: ${this.options.language}

`;
            this.api = new chatgpt_1.ChatGPTAPI({
                apiBaseUrl: this.options.apiBaseUrl,
                systemMessage,
                apiKey,
                apiOrg: process.env.AI_API_ORG || undefined,
                debug: this.options.debug,
                maxModelTokens: limits.maxTokens,
                maxResponseTokens: limits.responseTokens,
                completionParams: {
                    temperature: this.options.modelTemperature,
                    model: this.currentModel
                }
            });
            logInfo(`Initialized AI client with model: ${this.currentModel}`);
        }
        else {
            throw new Error("Missing 'GROQ_API_KEY' or 'AI_API_KEY'");
        }
    }
    chat = async (message, ids) => {
        let res = ['', {}];
        try {
            res = await this.chat_(message, ids);
            return res;
        }
        catch (e) {
            if (e instanceof chatgpt_1.ChatGPTError) {
                logWarning(`AI communication error: ${e.message}`);
            }
            return res;
        }
    };
    chat_ = async (message, ids) => {
        const start = Date.now();
        if (!message)
            return ['', {}];
        let response;
        if (this.api != null) {
            const opts = {
                timeoutMs: this.options.timeoutMS
            };
            if (ids.parentMessageId) {
                opts.parentMessageId = ids.parentMessageId;
            }
            try {
                // Check if circuit breaker is open
                if (this.circuitBreaker?.opened) {
                    logWarning(`Circuit breaker is OPEN for ${this.currentModel}, attempting fallback...`);
                    if (this.fallbackModels.length > 0) {
                        const nextModel = this.fallbackModels.shift();
                        if (nextModel) {
                            this.currentModel = nextModel;
                            this.initClient();
                            this.initCircuitBreaker();
                            // Recursive retry with new model
                            return this.chat_(message, ids);
                        }
                    }
                    throw new Error(`Circuit breaker open and no fallback models available for ${this.currentModel}`);
                }
                const result = await (0, p_retry_1.default)(async () => {
                    if (!this.circuitBreaker) {
                        throw new Error('Circuit breaker not initialized');
                    }
                    try {
                        const messageResult = await this.circuitBreaker.fire({ message, opts });
                        return messageResult;
                    }
                    catch (e) {
                        // Check if circuit breaker opened
                        if (this.circuitBreaker.opened && this.fallbackModels.length > 0) {
                            const nextModel = this.fallbackModels.shift();
                            if (nextModel) {
                                logWarning(`Circuit breaker opened for ${this.currentModel}. Switching to fallback: ${nextModel}`);
                                this.currentModel = nextModel;
                                this.initClient();
                                this.initCircuitBreaker();
                                // Throw to trigger retry with new model
                                throw new Error(`Model switched to ${nextModel}, retrying...`);
                            }
                        }
                        // Handle rate limiting / context size
                        if ((e?.status === 429 || e?.status === 413) && this.fallbackModels.length > 0) {
                            const nextModel = this.fallbackModels.shift();
                            if (nextModel) {
                                logWarning(`Rate limit or context size error (${e.status}) hit for ${this.currentModel}. Switching to fallback: ${nextModel}`);
                                this.currentModel = nextModel;
                                this.initClient();
                                this.initCircuitBreaker();
                                throw new Error(`Model switched to ${nextModel}, retrying...`);
                            }
                        }
                        throw e;
                    }
                }, {
                    retries: this.options.retries,
                    minTimeout: 20000, // 20s delay to allow TPM refresh
                    onFailedAttempt: (err) => {
                        logInfo(`Attempt ${err.attemptNumber} failed. ${err.retriesLeft} retries left. Error: ${err.message}`);
                    }
                });
                response = result;
            }
            catch (e) {
                if (e instanceof chatgpt_1.ChatGPTError) {
                    logWarning(`AI Error after retries: ${e.message} (Last model used: ${this.currentModel})`);
                }
                else if (e instanceof Error) {
                    logError(`Failed to get AI response: ${e.message}`);
                }
            }
            const end = Date.now();
            logInfo(`AI response time: ${end - start} ms`);
        }
        else {
            setFailed('AI client is not initialized');
        }
        let responseText = '';
        if (response != null) {
            responseText = response.text;
        }
        const newIds = {
            parentMessageId: response?.id,
            conversationId: response?.conversationId
        };
        return [responseText, newIds];
    };
}
exports.Bot = Bot;
//# sourceMappingURL=bot.js.map