1- import { fetch as httpFetch } from '@tauri-apps/plugin-http'
21import { Store } from '@tauri-apps/plugin-store'
3- import MD5 from 'crypto-js/md5.js'
42
53import type { AiConfig , ModelConfig , ModelType } from '@/app/core/setting/config'
6-
7- const UPGRADE_LINK_CONFIGURATION_KEY = 'Wg_BZFH1WUdx7atKagepXg'
8- const UPGRADE_LINK_ACCESS_KEY = 'wHi8Tkuc5i6v1UCAuVk48A'
9- const UPGRADE_LINK_SECRET_KEY = 'eg4upYo7ruJgaDVOtlHJGj4lyzG4Oh9IpLGwOc6Oehw'
10- const UPGRADE_LINK_UPGRADE_URL = 'https://api.upgrade.toolsetlink.com/v1/configuration/upgrade'
11- const UPGRADE_LINK_UPGRADE_URI = '/v1/configuration/upgrade'
12- const INITIAL_NOTEGEN_DEFAULT_MODELS_VERSION_CODE = 1
4+ import { fetchConfigCenterConfig } from '@/lib/config-center/client'
135
146export const NOTEGEN_DEFAULT_MODELS_CACHE_KEY = 'noteGenDefaultModelsCache'
157
@@ -58,46 +50,6 @@ function parseContentPayload(payload: unknown) {
5850 return null
5951}
6052
61- function buildUpgradeLinkSignature ( {
62- body,
63- nonce,
64- secretKey,
65- timestamp,
66- uri,
67- } : {
68- body ?: string
69- nonce : string
70- secretKey : string
71- timestamp : string
72- uri : string
73- } ) {
74- const source = body
75- ? `body=${ body } &nonce=${ nonce } &secretKey=${ secretKey } ×tamp=${ timestamp } &url=${ uri } `
76- : `nonce=${ nonce } &secretKey=${ secretKey } ×tamp=${ timestamp } &url=${ uri } `
77-
78- return MD5 ( source ) . toString ( )
79- }
80-
81- function buildSignedHeaders ( body : string ) {
82- const timestamp = new Date ( ) . toISOString ( )
83- const nonce = crypto . randomUUID ( )
84- const signature = buildUpgradeLinkSignature ( {
85- body,
86- nonce,
87- secretKey : UPGRADE_LINK_SECRET_KEY ,
88- timestamp,
89- uri : UPGRADE_LINK_UPGRADE_URI ,
90- } )
91-
92- return {
93- 'Content-Type' : 'application/json' ,
94- 'X-AccessKey' : UPGRADE_LINK_ACCESS_KEY ,
95- 'X-Timestamp' : timestamp ,
96- 'X-Nonce' : nonce ,
97- 'X-Signature' : signature ,
98- }
99- }
100-
10153function normalizeModelItem ( item : Record < string , unknown > ) : ModelConfig | null {
10254 if ( ! isNonEmptyString ( item . id ) || ! isNonEmptyString ( item . model ) ) {
10355 return null
@@ -166,39 +118,20 @@ function mergeNoteGenDefaultModels(config: AiConfig, remoteModels: ModelConfig[]
166118 }
167119}
168120
169- async function fetchRemoteNoteGenDefaultModels ( versionCode ?: number | null ) : Promise < NoteGenDefaultModelsCache | null > {
170- const body = JSON . stringify ( {
171- configurationKey : UPGRADE_LINK_CONFIGURATION_KEY ,
172- versionCode : versionCode || INITIAL_NOTEGEN_DEFAULT_MODELS_VERSION_CODE ,
173- appointVersionCode : 0 ,
174- } )
175-
176- const response = await httpFetch ( UPGRADE_LINK_UPGRADE_URL , {
177- method : 'POST' ,
178- headers : buildSignedHeaders ( body ) ,
179- body,
180- } )
181-
182- if ( ! response . ok ) {
183- throw new Error ( `NoteGen default models request failed: ${ response . status } ${ response . statusText } ` )
184- }
185-
186- const result = await response . json ( ) as {
187- data ?: {
188- versionCode ?: number
189- versionName ?: string
190- content ?: unknown
191- }
121+ async function fetchNoteGenDefaultModelsFromConfigCenter ( versionCode ?: number | null ) : Promise < NoteGenDefaultModelsCache | null > {
122+ const result = await fetchConfigCenterConfig ( 'noteGenDefaultModels' , versionCode )
123+ if ( result . status === 'not-modified' ) {
124+ return null
192125 }
193126
194- const models = normalizeNoteGenDefaultModelsPayload ( result ?. data ?. content )
127+ const models = normalizeNoteGenDefaultModelsPayload ( result . payload )
195128 if ( models . length === 0 ) {
196- return null
129+ throw new Error ( 'Config center NoteGen default models payload is empty' )
197130 }
198131
199132 return {
200- versionCode : result ?. data ? .versionCode ,
201- versionName : result ?. data ? .versionName ,
133+ versionCode : result . versionCode ,
134+ versionName : result . versionName ,
202135 fetchedAt : new Date ( ) . toISOString ( ) ,
203136 content : {
204137 models,
@@ -211,13 +144,18 @@ export async function loadNoteGenDefaultConfig(builtinConfig: AiConfig): Promise
211144 const cached = await store . get < NoteGenDefaultModelsCache > ( NOTEGEN_DEFAULT_MODELS_CACHE_KEY )
212145
213146 try {
214- const latest = await fetchRemoteNoteGenDefaultModels ( cached ?. versionCode )
147+ const latest = await fetchNoteGenDefaultModelsFromConfigCenter ( cached ?. versionCode )
215148 if ( latest ) {
216149 await store . set ( NOTEGEN_DEFAULT_MODELS_CACHE_KEY , latest )
150+ await store . save ( )
217151 return mergeNoteGenDefaultModels ( builtinConfig , normalizeNoteGenDefaultModelsPayload ( latest . content ) )
218152 }
153+
154+ if ( cached ?. content ?. models ?. length ) {
155+ return mergeNoteGenDefaultModels ( builtinConfig , normalizeNoteGenDefaultModelsPayload ( cached . content ) )
156+ }
219157 } catch ( error ) {
220- console . error ( '[notegen-default-models] failed to fetch remote models' , error )
158+ console . error ( '[notegen-default-models] failed to fetch config center models' , error )
221159 }
222160
223161 if ( cached ?. content ?. models ?. length ) {
0 commit comments