mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-26 02:04:37 +08:00
refactor(http): organize shared route package
This commit is contained in:
1 parent
bf7004d0e0
commit
4b52a7d2f8
35 files changed
+84
-217
No files matched your search
@@ -17,6 +17,7 @@
|
||||
"@earendil-works/pi-agent-core": "0.74.2",
|
||||
"@earendil-works/pi-ai": "0.74.2",
|
||||
"@huggingface/transformers": "^4.2.0",
|
||||
"@openchatlab/config": "workspace:*",
|
||||
"@openchatlab/core": "workspace:*",
|
||||
"better-sqlite3": "^12.4.6",
|
||||
"gray-matter": "^4.0.3",
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import type { ConfigStorage } from './llm-config-store'
|
||||
import { createAuthProfileLlmConfigStore } from './auth-profile-llm-config-store'
|
||||
|
||||
function createMemoryStorage(): ConfigStorage & { data: Map<string, unknown> } {
|
||||
const data = new Map<string, unknown>()
|
||||
return {
|
||||
data,
|
||||
readJson: <T>(key: string) => (data.get(key) as T | undefined) ?? null,
|
||||
writeJson: <T>(key: string, value: T) => data.set(key, value),
|
||||
}
|
||||
}
|
||||
|
||||
test('createAuthProfileLlmConfigStore shares auth-profile creation, resolution, and deletion wiring', () => {
|
||||
const storage = createMemoryStorage()
|
||||
const profiles = new Map<string, string>()
|
||||
const deleted: string[] = []
|
||||
const store = createAuthProfileLlmConfigStore(storage, {
|
||||
resolveApiKey: (_provider, profileName) => (profileName ? profiles.get(profileName) : undefined),
|
||||
writeAuthProfile: (name, profile) => profiles.set(name, profile.type === 'api_key' ? profile.key : ''),
|
||||
deleteAuthProfile: (name) => {
|
||||
profiles.delete(name)
|
||||
deleted.push(name)
|
||||
},
|
||||
})
|
||||
|
||||
const added = store.addConfig({
|
||||
name: 'Team OpenAI',
|
||||
provider: 'openai',
|
||||
apiKey: 'secret-key',
|
||||
model: 'gpt-test',
|
||||
})
|
||||
|
||||
assert.equal(added.success, true)
|
||||
assert.equal(profiles.get('team-openai'), 'secret-key')
|
||||
const persisted = storage.data.get('llm-config') as { configs: Array<Record<string, unknown>> }
|
||||
assert.equal(persisted.configs[0].apiKey, '')
|
||||
assert.equal(persisted.configs[0].authProfile, 'team-openai')
|
||||
assert.equal(store.getAllConfigs()[0].apiKey, 'secret-key')
|
||||
|
||||
assert.equal(store.deleteConfig(added.config!.id).success, true)
|
||||
assert.deepEqual(deleted, ['team-openai'])
|
||||
})
|
||||
@@ -0,0 +1,36 @@
|
||||
import { deleteAuthProfile, resolveApiKey, writeAuthProfile, type AuthProfile } from '@openchatlab/config'
|
||||
import { LLMConfigStore, type AIServiceConfig, type ConfigStorage } from './llm-config-store'
|
||||
|
||||
export interface AuthProfileLlmConfigStoreDeps {
|
||||
resolveApiKey: (provider: string, authProfile?: string) => string | undefined
|
||||
writeAuthProfile: (name: string, profile: AuthProfile) => void
|
||||
deleteAuthProfile: (name: string) => void
|
||||
}
|
||||
|
||||
const defaultDeps: AuthProfileLlmConfigStoreDeps = {
|
||||
resolveApiKey: (provider, authProfile) => resolveApiKey(provider, authProfile) || undefined,
|
||||
writeAuthProfile,
|
||||
deleteAuthProfile,
|
||||
}
|
||||
|
||||
function getAuthProfile(config: AIServiceConfig): string | undefined {
|
||||
return (config as unknown as Record<string, unknown>).authProfile as string | undefined
|
||||
}
|
||||
|
||||
export function createAuthProfileLlmConfigStore(
|
||||
storage: ConfigStorage,
|
||||
deps: AuthProfileLlmConfigStoreDeps = defaultDeps
|
||||
): LLMConfigStore {
|
||||
return new LLMConfigStore(storage, {
|
||||
resolveApiKey: deps.resolveApiKey,
|
||||
onApiKeyCreated: (config, apiKey) => {
|
||||
const profileName = config.name?.toLowerCase().replace(/\s+/g, '-') || config.provider
|
||||
deps.writeAuthProfile(profileName, { type: 'api_key', provider: config.provider, key: apiKey })
|
||||
return profileName
|
||||
},
|
||||
onApiKeyDeleted: (config) => {
|
||||
const profileName = getAuthProfile(config)
|
||||
if (profileName) deps.deleteAuthProfile(profileName)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -164,6 +164,8 @@ export type { SummaryDeps, SummaryMessage, SummaryOptions, SummaryResult, Summar
|
||||
export { LLMConfigStore, MAX_CONFIG_COUNT } from './llm-config-store'
|
||||
export type { AIServiceConfig, AIConfigStore, ConfigStorage, LLMConfigStoreDeps } from './llm-config-store'
|
||||
export { createFileConfigStorage } from './file-config-storage'
|
||||
export { createAuthProfileLlmConfigStore } from './auth-profile-llm-config-store'
|
||||
export type { AuthProfileLlmConfigStoreDeps } from './auth-profile-llm-config-store'
|
||||
|
||||
// Custom Provider/Model Store
|
||||
export { CustomProviderStore, CustomModelStore } from './custom-store'
|
||||
|
||||
@@ -157,7 +157,8 @@ export type { AssistantConfig, AssistantSummary, SkillDef, SkillSummary } from '
|
||||
export { parseAssistantFile, serializeAssistant, parseSkillFile, extractSkillId } from './ai'
|
||||
export { AIChatManager } from './ai'
|
||||
export { countTokens, countMessagesTokens, initTokenizer } from './ai'
|
||||
export { createFileConfigStorage } from './ai'
|
||||
export { createFileConfigStorage, createAuthProfileLlmConfigStore } from './ai'
|
||||
export type { AuthProfileLlmConfigStoreDeps } from './ai'
|
||||
|
||||
// Assistant Manager
|
||||
export { AssistantManager } from './ai'
|
||||
|
||||
Reference in new issue
Block a user