Spaces:
Paused
Paused
File size: 1,651 Bytes
ca5b3b3 | 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 | import UserAgent from "user-agents";
import { getNewSession } from "./openai";
import { AppLogger, randomIntTargetOffset } from "./tools";
import { ENV } from "../env";
import { sleep } from "bun";
export class AgentManager {
static instance: AgentManager;
private userAgent: UserAgent;
private session?: Session;
private constructor() {
this.userAgent = new UserAgent();
}
/**
* Rolls the agent by generating a new session ID and updating the token and device ID.
*/
async roll(tries = 0): Promise<Session> {
AppLogger.i("AgentManager", "Trigger rolling agent");
this.session = undefined;
if (tries === ENV.MAX_SESSION_TRIES) {
throw new Error(
`Failed to get session ID after ${ENV.MAX_SESSION_TRIES} retries`
);
}
return await getNewSession()
.then((session) => {
this.session = session;
return session;
})
.catch(async (e) => {
AppLogger.w(
"AgentManager",
"Failed to get session ID, retry after 3s",
e
);
await sleep(randomIntTargetOffset(3000, 500));
return await this.roll(tries + 1);
});
}
get userAgentString() {
return this.userAgent.toString();
}
get openAiHeaders(): HeadersInit {
return {
"oai-device-id": this.session?.deviceId ?? "",
"openai-sentinel-chat-requirements-token": this.session?.token ?? "",
};
}
static getInstance(): AgentManager {
if (!AgentManager.instance) {
AgentManager.instance = new AgentManager();
}
return AgentManager.instance;
}
get crrSession() {
return this.session;
}
}
|