mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-08-08 11:17:02 +08:00
Add ChatGLM API (#567)
* Add ChatGLM API * add minimal build * a small patch --------- Co-authored-by: josc146 <josStorer@outlook.com>
This commit is contained in:
@@ -9,6 +9,7 @@ function createParser(onParse) {
|
||||
let eventId
|
||||
let eventName
|
||||
let data
|
||||
let extra
|
||||
reset()
|
||||
return {
|
||||
feed,
|
||||
@@ -78,17 +79,19 @@ function createParser(onParse) {
|
||||
|
||||
function parseEventStreamLine(lineBuffer, index, fieldLength, lineLength) {
|
||||
if (lineLength === 0) {
|
||||
if (data.length > 0) {
|
||||
if (data.length > 0 || extra) {
|
||||
onParse({
|
||||
type: 'event',
|
||||
id: eventId,
|
||||
event: eventName || void 0,
|
||||
data: data.slice(0, -1),
|
||||
extra: extra || void 0,
|
||||
// remove trailing newline
|
||||
})
|
||||
|
||||
data = ''
|
||||
eventId = void 0
|
||||
extra = void 0
|
||||
}
|
||||
eventName = void 0
|
||||
return
|
||||
@@ -120,6 +123,10 @@ function createParser(onParse) {
|
||||
value: retry,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
const str = `{"${field}":${value}}`
|
||||
extra = extra ?? []
|
||||
extra.push(JSON.parse(str))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
let jwtToken = null
|
||||
let tokenExpiration = null // Declare tokenExpiration in the module scope
|
||||
|
||||
function generateToken(apiKey, timeoutSeconds) {
|
||||
const parts = apiKey.split('.')
|
||||
if (parts.length !== 2) {
|
||||
throw new Error('Invalid API key')
|
||||
}
|
||||
|
||||
const ms = Date.now()
|
||||
const currentSeconds = Math.floor(ms / 1000)
|
||||
const [id, secret] = parts
|
||||
const payload = {
|
||||
api_key: id,
|
||||
exp: currentSeconds + timeoutSeconds,
|
||||
timestamp: currentSeconds,
|
||||
}
|
||||
|
||||
jwtToken = jwt.sign(payload, secret, {
|
||||
header: {
|
||||
alg: 'HS256',
|
||||
typ: 'JWT',
|
||||
sign_type: 'SIGN',
|
||||
},
|
||||
})
|
||||
tokenExpiration = ms + timeoutSeconds * 1000
|
||||
}
|
||||
|
||||
function shouldRegenerateToken() {
|
||||
const ms = Date.now()
|
||||
return !jwtToken || ms >= tokenExpiration
|
||||
}
|
||||
|
||||
function getToken(apiKey) {
|
||||
if (shouldRegenerateToken()) {
|
||||
generateToken(apiKey, 86400) // Hard-coded to regenerate the token every 24 hours
|
||||
}
|
||||
return jwtToken
|
||||
}
|
||||
|
||||
export { getToken }
|
||||
Reference in New Issue
Block a user