Skip to content

Commit 3f5f66e

Browse files
committed
style: format code
1 parent d1cf148 commit 3f5f66e

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

libs/langchain-community/src/chat_models/zhipuai.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ export class ChatZhipuAI extends BaseChatModel implements ChatZhipuAIParams {
225225
constructor(fields: Partial<ChatZhipuAIParams> & BaseChatModelParams = {}) {
226226
super(fields);
227227

228-
this.zhipuAIApiKey = fields?.apiKey ?? fields?.zhipuAIApiKey ?? getEnvironmentVariable("ZHIPUAI_API_KEY");
228+
this.zhipuAIApiKey =
229+
fields?.apiKey ??
230+
fields?.zhipuAIApiKey ??
231+
getEnvironmentVariable("ZHIPUAI_API_KEY");
229232
if (!this.zhipuAIApiKey) {
230233
throw new Error("ZhipuAI API key not found");
231234
}
+39-36
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
import jsonwebtoken from "jsonwebtoken"
1+
import jsonwebtoken from "jsonwebtoken";
22

3-
const API_TOKEN_TTL_SECONDS = 3 * 60
4-
const CACHE_TTL_SECONDS = API_TOKEN_TTL_SECONDS - 30
3+
const API_TOKEN_TTL_SECONDS = 3 * 60;
4+
const CACHE_TTL_SECONDS = API_TOKEN_TTL_SECONDS - 30;
55
const tokenCache: {
6-
[key: string]: {
7-
token: string,
8-
createAt: number
9-
}
10-
} = {}
6+
[key: string]: {
7+
token: string;
8+
createAt: number;
9+
};
10+
} = {};
1111

1212
export const encodeApiKey = (apiSecretKey?: string, cache = true): string => {
13-
if (!apiSecretKey) throw new Error("Api_key is required");
14-
try {
15-
if (tokenCache[apiSecretKey] && Date.now() - tokenCache[apiSecretKey].createAt < (CACHE_TTL_SECONDS * 1000)) {
16-
return tokenCache[apiSecretKey].token
17-
}
13+
if (!apiSecretKey) throw new Error("Api_key is required");
14+
try {
15+
if (
16+
tokenCache[apiSecretKey] &&
17+
Date.now() - tokenCache[apiSecretKey].createAt < CACHE_TTL_SECONDS * 1000
18+
) {
19+
return tokenCache[apiSecretKey].token;
20+
}
1821

19-
const [apiKey, secret] = apiSecretKey.split(".")
20-
const payload = {
21-
api_key: apiKey,
22-
exp: Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,
23-
timestamp: Math.round(Date.now() * 1000),
24-
}
25-
// algorithm = "HS256", headers = { "alg": "HS256", "sign_type": "SIGN" }
26-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27-
// @ts-ignore
28-
const ret = jsonwebtoken.sign(payload, secret, {
29-
algorithm: "HS256",
30-
header: { alg: "HS256", sign_type: "SIGN" }
31-
})
32-
if (cache) {
33-
tokenCache[apiSecretKey] = {
34-
token: ret,
35-
createAt: Date.now()
36-
}
37-
}
38-
return ret
39-
} catch (e) {
40-
throw new Error("invalid api_key")
22+
const [apiKey, secret] = apiSecretKey.split(".");
23+
const payload = {
24+
api_key: apiKey,
25+
exp: Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,
26+
timestamp: Math.round(Date.now() * 1000),
27+
};
28+
// algorithm = "HS256", headers = { "alg": "HS256", "sign_type": "SIGN" }
29+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
30+
// @ts-ignore
31+
const ret = jsonwebtoken.sign(payload, secret, {
32+
algorithm: "HS256",
33+
header: { alg: "HS256", sign_type: "SIGN" },
34+
});
35+
if (cache) {
36+
tokenCache[apiSecretKey] = {
37+
token: ret,
38+
createAt: Date.now(),
39+
};
4140
}
42-
}
41+
return ret;
42+
} catch (e) {
43+
throw new Error("invalid api_key");
44+
}
45+
};

0 commit comments

Comments
 (0)