{"version":3,"file":"zhipuai.cjs","names":[],"sources":["../../src/utils/zhipuai.ts"],"sourcesContent":["import jsonwebtoken from \"jsonwebtoken\";\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60;\nconst CACHE_TTL_SECONDS = API_TOKEN_TTL_SECONDS - 30;\nconst tokenCache: {\n  [key: string]: {\n    token: string;\n    createAt: number;\n  };\n} = {};\n\nexport const encodeApiKey = (apiSecretKey?: string, cache = true): string => {\n  if (!apiSecretKey) throw new Error(\"Api_key is required\");\n  try {\n    if (\n      tokenCache[apiSecretKey] &&\n      Date.now() - tokenCache[apiSecretKey].createAt < CACHE_TTL_SECONDS * 1000\n    ) {\n      return tokenCache[apiSecretKey].token;\n    }\n\n    const [apiKey, secret] = apiSecretKey.split(\".\");\n    const payload = {\n      api_key: apiKey,\n      exp: Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n      timestamp: Math.round(Date.now() * 1000),\n    };\n    // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-ignore\n    const ret = jsonwebtoken.sign(payload, secret, {\n      algorithm: \"HS256\",\n      header: { alg: \"HS256\", sign_type: \"SIGN\" },\n    });\n    if (cache) {\n      tokenCache[apiSecretKey] = {\n        token: ret,\n        createAt: Date.now(),\n      };\n    }\n    return ret;\n  } catch {\n    throw new Error(\"invalid api_key\");\n  }\n};\n"],"mappings":";;;;AAEA,MAAM,wBAAwB;AAC9B,MAAM,oBAAoB,wBAAwB;AAClD,MAAM,aAKF,EAAE;AAEN,MAAa,gBAAgB,cAAuB,QAAQ,SAAiB;AAC3E,KAAI,CAAC,aAAc,OAAM,IAAI,MAAM,sBAAsB;AACzD,KAAI;AACF,MACE,WAAW,iBACX,KAAK,KAAK,GAAG,WAAW,cAAc,WAAW,oBAAoB,IAErE,QAAO,WAAW,cAAc;EAGlC,MAAM,CAAC,QAAQ,UAAU,aAAa,MAAM,IAAI;EAChD,MAAM,UAAU;GACd,SAAS;GACT,KAAK,KAAK,MAAM,KAAK,KAAK,GAAG,IAAK,GAAG,wBAAwB;GAC7D,WAAW,KAAK,MAAM,KAAK,KAAK,GAAG,IAAK;GACzC;EAID,MAAM,MAAM,aAAA,QAAa,KAAK,SAAS,QAAQ;GAC7C,WAAW;GACX,QAAQ;IAAE,KAAK;IAAS,WAAW;IAAQ;GAC5C,CAAC;AACF,MAAI,MACF,YAAW,gBAAgB;GACzB,OAAO;GACP,UAAU,KAAK,KAAK;GACrB;AAEH,SAAO;SACD;AACN,QAAM,IAAI,MAAM,kBAAkB"}