File size: 4,674 Bytes
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29cc386
121e67d
 
 
29cc386
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29cc386
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29cc386
 
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29cc386
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const axios = require("axios")
require('dotenv').config()

class Manager {
  constructor(accounts) {
    this.accounts = []
    this.init(accounts)
    this.current_account = 0
    this.interval = setInterval(() => {
      this.refreshToken()
    }, 1000 * 60 * 60 * 24 * 5)
  }

  async init(accounts) {
    accounts = accounts.split(",").filter(account => account.trim() !== "")
    for (const account of accounts) {
      const [username, password, token] = account.split(":")
      this.accounts.push({
        username,
        password,
        token: token,
        clientId: null,
        workspaceId: null,
        access_token: null,
        refresh: null
      })
    }
  }

  async login(username, password) {
    try {
      const response = await axios.post("https://api.promptlayer.com/login", {
        email: username,
        password: password
      }, {
        headers: {
          "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0"
        }
      })

      if (response.data) {
        return response.data.access_token
      }
      return false
    } catch (error) {
      if (error.status === 429) {
        await new Promise(resolve => setTimeout(resolve, 1000))
        return this.login(username, password)
      }
      return false
    }
  }

  async getClientId(token) {
    try {
      const response = await axios.post("https://api.promptlayer.com/ws-token-request", null, {
        headers: {
          Authorization: "Bearer " + token
        }
      })
      if (response.data.success) {
        const access_token = response.data.token_details.token
        const clientId = response.data.token_details.clientId
        return { access_token, clientId }
      }
    } catch (error) {
      console.error('获取clientId失败:', error)
      return false
    }
  }

  async getWorkspaceId(token) {
    try {
      const response = await axios.get("https://api.promptlayer.com/workspaces", {
        headers: {
          Authorization: "Bearer " + token
        }
      })
      if (response.data.success && response.data.workspaces.length > 0) {
        const workspaceId = response.data.workspaces[0].id
        return workspaceId
      }
    } catch (error) {
      // console.error('获取workspaceId失败:', error)
      return false
    }
  }

  async initAccount(account) {
    // const token = await this.login(account.username, account.password)
    const token = account.token
    if (!token) {
      return false
    }
    const { clientId, access_token } = await this.getClientId(token)
    if (!clientId || !access_token) {
      return false
    }
    const workspaceId = await this.getWorkspaceId(token)
    if (!workspaceId) {
      return false
    }
    account.token = token
    account.clientId = clientId
    account.workspaceId = workspaceId
    account.access_token = access_token
    account.refresh = setInterval(async () => {
      const { access_token, clientId } = await this.getClientId(account.token)
      account.access_token = access_token
      account.clientId = clientId
      console.log(`${account.username} 刷新token成功`)
    }, 1000 * 60 * 30)
    return account
  }

  async getAccount() {
    const account = this.accounts[this.current_account]
    if (!account) {
      console.error('没有可用的账户')
      return null
    }
    
    if (!account.access_token) {
      console.log(`初始化账户: ${account.username}`)
      const initialized = await this.initAccount(account)
      if (!initialized) {
        console.error(`账户初始化失败: ${account.username}`)
        // 尝试下一个账户
        this.current_account++
        if (this.current_account >= this.accounts.length) {
          this.current_account = 0
        }
        // 递归尝试下一个账户
        return await this.getAccount()
      }
    }
    
    console.log(`当前账户: ${account.username}, Token: ${account.token ? 'Valid' : 'Invalid'}`)
    this.current_account++
    if (this.current_account >= this.accounts.length) {
      this.current_account = 0
    }
    return account
  }

  async refreshToken() {
    this.accounts = []
    this.init(process.env.ACCOUNTS)
  }


}


if (!process.env.ACCOUNTS || process.env.ACCOUNTS === "" || process.env.AUTH_TOKEN === undefined) {
  console.error("ACCOUNTS 或 AUTH_TOKEN 未设置")
  process.exit(1)
}

const manager = new Manager(process.env.ACCOUNTS)

module.exports = manager