import { decrypt, encrypt } from "@dm/crypto-wasm"; import { createXllm, type XProviderName } from "@dm/xllm"; import { encrypt as sm4Encrypt } from "./sm4"; /** 与 sm-crypto@0.4.0 + crypto-js@4.2.0(ECB/PKCS#7、密钥见 sm4.js)对同一段 UTF-8 明文应得到相同 Base64。 */ const plain = `{"username":"admin","password":"admin123","code":"40","uuid":"7e9d6dae9c6740c8864d91445b79741d","clientId":"ihF3cJj0Mxf0xYh9rUqrtcJs-f7iQMrF","clientSecret":"-hs4ENSrdMPGhJUxM3E4CogQaGGb_TCReB51KlWxklhJDP-Dkz5JgFTozeshWdqF1UFjKCOiKR--ny53tusqhw"}`; const validateText = "ryfanf2S7ADSCLhO1rCXLZxHYuPGMXJKJMdUfVzKTKGnsgdccIqABqviVPXsTAnKPWbqDBT0ceITDKfxWH+Aa9n+HPYwcabe1fGR3N0QhV/PI7p9mjLS1N+Sn1EWjrDbs78jPRFWCLRiOAFEmrSKTPaMX3u5zCq+F6Bwkg5S0KmmAwUEYnnL5STqRJpK+d86MRhKDQpNvzgkR4iJYXCBSi5SxtD7dH4uXtdOz/NlJZ7tPMYJ2BD83e2PzjTe2dxJlTx+cDAPfbruJOeivTNayiM3QevGtfbsJHRPy/Ge7dwyqcJSGVkcUiLvzkANBfiZ6YBUwSopYXY2aLKS5fIGWg==" const cipherWasm = encrypt(plain); const cipherJs = sm4Encrypt(plain); console.log("[align] wasm === sm-crypto:", cipherWasm === cipherJs); console.log("[hello-wasm] ciphertext (base64):", cipherWasm); console.log("[hello-wasm] validateText === cipherWasm:", validateText === cipherWasm); console.log("[hello-wasm] validateText === cipherJs:", validateText === cipherJs); const recovered = decrypt(cipherWasm); console.log("[hello-wasm] round-trip OK:", recovered === plain); const env = import.meta.env as Record; const xllmProvider = (env.VITE_XLLM_PROVIDER as XProviderName | undefined) ?? "deepseek"; const xllmModel = env.VITE_XLLM_MODEL ?? "deepseek-chat"; const xllmApiKey = env.VITE_XLLM_API_KEY; const xllmBaseUrl = env.VITE_XLLM_BASE_URL; async function runXllmStreamExample(): Promise { if (!xllmApiKey) { console.log( "[xllm-example] skip: set VITE_XLLM_API_KEY to run stream demo. Optional: VITE_XLLM_PROVIDER, VITE_XLLM_MODEL, VITE_XLLM_BASE_URL", ); return; } const xllm = createXllm({ provider: xllmProvider, model: xllmModel, apiKey: xllmApiKey, baseURL: xllmBaseUrl, }); console.log(`[xllm-example] provider=${xllmProvider}, model=${xllmModel}`); let streamedText = ""; for await (const event of xllm.stream({ messages: [ { role: "user", content: [{ type: "text", text: "请用一句话介绍你自己。" }], }, ], })) { if (event.type === "text.delta") { streamedText += event.text; console.log("[xllm-example] delta:", event.text); } if (event.type === "response.done") { console.log("[xllm-example] done:", streamedText); } } } runXllmStreamExample().catch((error) => { console.error("[xllm-example] failed:", error); });