Compare commits
275 Commits
main
...
feat/v2/ca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67f726afb7 | ||
|
|
d98d69e28d | ||
|
|
3f671ba6be | ||
|
|
78e593fac4 | ||
|
|
9933b0b12f | ||
|
|
bceeef5190 | ||
|
|
cf7b4dd07b | ||
|
|
fe88cfe106 | ||
|
|
e3bf63d7a0 | ||
|
|
9a356cb27d | ||
|
|
53883a27be | ||
|
|
24c9c157f9 | ||
|
|
55727e2adf | ||
|
|
1e4239d189 | ||
|
|
5ccb16a0be | ||
|
|
34c9a6b350 | ||
|
|
ab99366a0a | ||
|
|
7419cadd80 | ||
|
|
46f2726a63 | ||
|
|
7bd3e047d2 | ||
|
|
1ea19adfec | ||
|
|
1685590a07 | ||
|
|
db10bdd539 | ||
|
|
d79602325d | ||
|
|
a19419e597 | ||
|
|
a7686f61c7 | ||
|
|
e694ae68e3 | ||
|
|
02a65daa27 | ||
|
|
1a9fd77599 | ||
|
|
7fa97f8a2b | ||
|
|
838bb385fd | ||
|
|
583e4e9db7 | ||
|
|
5fdfa5a594 | ||
|
|
ad939f4b77 | ||
|
|
6abe5ab8c3 | ||
|
|
1156b12ac6 | ||
|
|
4410599dfa | ||
|
|
bce8e5cc7f | ||
|
|
9d75b0972e | ||
|
|
a3062d6e38 | ||
|
|
43fb232cca | ||
|
|
d610943f0f | ||
|
|
680fcb4b9d | ||
|
|
d82e004f57 | ||
|
|
aa13ad4fac | ||
|
|
02d79f47b3 | ||
|
|
75c0923636 | ||
|
|
7dd1ecd4a5 | ||
|
|
a2299fa2ab | ||
|
|
a830d05790 | ||
|
|
8429e678bc | ||
|
|
91215c899d | ||
|
|
1309b194e9 | ||
|
|
1b1f85b35f | ||
|
|
4407c0f675 | ||
|
|
30947c6bc1 | ||
|
|
0b3cefb125 | ||
|
|
b382b06c57 | ||
|
|
8246f46e7d | ||
|
|
f32fa08c41 | ||
|
|
56df52d850 | ||
|
|
846a7f5ecf | ||
|
|
f2c2a27622 | ||
|
|
348e0dfc80 | ||
|
|
77c848035d | ||
|
|
cd6a38ebeb | ||
|
|
b57ed07d00 | ||
|
|
f7c8fb8d56 | ||
|
|
dae10cf673 | ||
|
|
a50da9fc80 | ||
|
|
7d5d9964d7 | ||
|
|
059f821584 | ||
|
|
1c38e31e9e | ||
|
|
12e3a22726 | ||
|
|
2b1269af92 | ||
|
|
c4fa975b89 | ||
|
|
1b67b851b7 | ||
|
|
811e702568 | ||
|
|
4ef4297391 | ||
|
|
292f7f7b75 | ||
|
|
e56edbaa4f | ||
|
|
e06142b89a | ||
|
|
fb6b326947 | ||
|
|
f9b7ff7d0e | ||
|
|
14706ec4d7 | ||
|
|
09f2fb6538 | ||
|
|
62aedcaa23 | ||
|
|
62ccb6105d | ||
|
|
5101488d65 | ||
|
|
7c0b03dbdc | ||
|
|
b3dc2d0422 | ||
|
|
1828ef8997 | ||
|
|
b3f88a7fc2 | ||
|
|
2c07ea0dd8 | ||
|
|
6042ee8ca8 | ||
|
|
d164d7c8bf | ||
|
|
23f7b39753 | ||
|
|
fe188ba8fb | ||
|
|
cf008ca22e | ||
|
|
851ff8992f | ||
|
|
91f9088436 | ||
|
|
c971daf23c | ||
|
|
0c7cee2700 | ||
|
|
3e9d9f16d6 | ||
|
|
f3a279d8de | ||
|
|
b9a947d2fd | ||
|
|
57b9ca111a | ||
|
|
709f264ac9 | ||
|
|
736aef22c4 | ||
|
|
d0ed4cc1f2 | ||
|
|
8c6a577cca | ||
|
|
27b6ad75df | ||
|
|
c617a0b51a | ||
|
|
75d7ed075b | ||
|
|
b5b577dc79 | ||
|
|
e754b5a863 | ||
|
|
82dd771110 | ||
|
|
8a4a34a946 | ||
|
|
fb62ae18b7 | ||
|
|
e59990d24e | ||
|
|
b08228bdb5 | ||
|
|
d2b6433609 | ||
|
|
3417acafe2 | ||
|
|
f42afe28d7 | ||
|
|
0da9252eb7 | ||
|
|
de5fa5e09c | ||
|
|
8d64bb0316 | ||
|
|
d7eb88f7e2 | ||
|
|
b41e1d712f | ||
|
|
c258035f6a | ||
|
|
569572bfdc | ||
|
|
b821ac5390 | ||
|
|
534c2ce485 | ||
|
|
bab1a5445c | ||
|
|
742f901052 | ||
|
|
cb12bb5137 | ||
|
|
06b6f2b9d8 | ||
|
|
2c102ed3b4 | ||
|
|
767e22c58d | ||
|
|
dee397f6ac | ||
|
|
a00aba23bd | ||
|
|
de5fb03efb | ||
|
|
a6e58776d2 | ||
|
|
bebe745e69 | ||
|
|
ec8c24a1c2 | ||
|
|
db4fcac768 | ||
|
|
6c71b92d1d | ||
|
|
d470fd8b88 | ||
|
|
99962b740c | ||
|
|
ef4bede062 | ||
|
|
e6e1fb0404 | ||
|
|
e6696def10 | ||
|
|
e5a3363021 | ||
|
|
f6ff436294 | ||
|
|
8a9b633af2 | ||
|
|
0a37146ba8 | ||
|
|
ac3dfcbfbe | ||
|
|
5ac09d5311 | ||
|
|
d4fd8ffdcc | ||
|
|
84274d9d85 | ||
|
|
a72feebead | ||
|
|
e930d3de43 | ||
|
|
ecc9923050 | ||
|
|
e469016775 | ||
|
|
15569387c7 | ||
|
|
4f746842a5 | ||
|
|
aab941d89c | ||
|
|
1b04fd065d | ||
|
|
76b3ba5d7e | ||
|
|
355e5b269d | ||
|
|
d4b0272fe7 | ||
|
|
59bf94b118 | ||
|
|
bd7cd22220 | ||
|
|
f48674b2c7 | ||
|
|
56af6f43c0 | ||
|
|
f83c3e171e | ||
|
|
d397a43806 | ||
|
|
8353f331f1 | ||
|
|
8cc6b08831 | ||
|
|
ffe897d58c | ||
|
|
182ac3bc98 | ||
|
|
c0cca4ae44 | ||
|
|
8981d0a09d | ||
|
|
de44938d9b | ||
|
|
75d5dcf275 | ||
|
|
d8f4825e5e | ||
|
|
c242abd81a | ||
|
|
79c9ed963f | ||
|
|
6079961f44 | ||
|
|
04ef5edea2 | ||
|
|
046ed3edef | ||
|
|
6eb9ab30b0 | ||
|
|
1c27481813 | ||
|
|
a6e19f7757 | ||
|
|
6d89f94335 | ||
|
|
2e07b4ea58 | ||
|
|
bf2f6ddd7f | ||
|
|
c936bddfe7 | ||
|
|
d3028f1dd1 | ||
|
|
0038280fba | ||
|
|
0a94609f78 | ||
|
|
f9f8390540 | ||
|
|
91dd6482ce | ||
|
|
016bbff79f | ||
|
|
32f41391c4 | ||
|
|
78a8ebc777 | ||
|
|
57fd73e51a | ||
|
|
bd448b5108 | ||
|
|
a7d12abd1f | ||
|
|
9e3618bc17 | ||
|
|
8cb270ca86 | ||
|
|
d321cd23ef | ||
|
|
9da3e82c47 | ||
|
|
2931e558b3 | ||
|
|
9a847dc5a3 | ||
|
|
c2a1178dff | ||
|
|
7f114ade4d | ||
|
|
7b633641d1 | ||
|
|
1dacdc3178 | ||
|
|
566dd14fed | ||
|
|
68cd87e069 | ||
|
|
1b57ffeb56 | ||
|
|
5d789ef394 | ||
|
|
820d6a6e96 | ||
|
|
0a67ab4103 | ||
|
|
5cc7390bb6 | ||
|
|
2ce4fabc7d | ||
|
|
7b2570974e | ||
|
|
0ef3852029 | ||
|
|
0dce1c57fc | ||
|
|
190ee76cf1 | ||
|
|
83fea49ed2 | ||
|
|
ccc50dbf2b | ||
|
|
6b503c4080 | ||
|
|
40fe381aa5 | ||
|
|
65c24a2f4b | ||
|
|
b15778b16b | ||
|
|
087e825086 | ||
|
|
3dd2bc1a40 | ||
|
|
9bde833419 | ||
|
|
e15005d1cf | ||
|
|
30e6883333 | ||
|
|
99be38c325 | ||
|
|
df876651b9 | ||
|
|
85bdcdc206 | ||
|
|
2860935e5b | ||
|
|
b219e96544 | ||
|
|
c02f93e6b9 | ||
|
|
72f32e4b8f | ||
|
|
a81f13848c | ||
|
|
81538d5709 | ||
|
|
54449e7130 | ||
|
|
c217a0bf02 | ||
|
|
39257f64b1 | ||
|
|
06dab978f7 | ||
|
|
c3f61533f7 | ||
|
|
8715eb1f41 | ||
|
|
92eb5aed7f | ||
|
|
ff965402cd | ||
|
|
973f26f9dd | ||
|
|
4e3f8a8f76 | ||
|
|
21e40db086 | ||
|
|
92cd012037 | ||
|
|
7cd937888e | ||
|
|
ec491f5f24 | ||
|
|
c0efb46c2b | ||
|
|
aa47fc3ed7 | ||
|
|
c3c9f9b3f2 | ||
|
|
d486b56595 | ||
|
|
4bb5ff8086 | ||
|
|
a748162e67 | ||
|
|
610e7481b3 | ||
|
|
9105e0f5c1 | ||
|
|
a248517520 | ||
|
|
d31f35b16d |
8
.github/CODEOWNERS
vendored
8
.github/CODEOWNERS
vendored
@@ -3,4 +3,12 @@
|
||||
/src/main/services/ConfigManager.ts @0xfullex
|
||||
/packages/shared/IpcChannel.ts @0xfullex
|
||||
/src/main/ipc.ts @0xfullex
|
||||
|
||||
/migrations/ @0xfullex
|
||||
/packages/shared/data/ @0xfullex
|
||||
/src/main/data/ @0xfullex
|
||||
/src/renderer/src/data/ @0xfullex
|
||||
|
||||
/packages/ui/ @MyPrototypeWhat
|
||||
|
||||
/app-upgrade-config.json @kangfenmao
|
||||
|
||||
2
.github/workflows/pr-ci.yml
vendored
2
.github/workflows/pr-ci.yml
vendored
@@ -17,7 +17,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
PRCI: true
|
||||
if: github.event.pull_request.draft == false
|
||||
if: github.event.pull_request.draft == false || github.head_ref == 'v2'
|
||||
|
||||
steps:
|
||||
- name: Check out Git repository
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"files": ["src/main/**", "resources/scripts/**", "scripts/**", "playwright.config.ts", "electron.vite.config.ts"]
|
||||
"files": ["src/main/**", "resources/scripts/**", "scripts/**", "playwright.config.ts", "electron.vite.config.ts", "packages/ui/scripts/**"]
|
||||
},
|
||||
{
|
||||
"env": {
|
||||
@@ -35,7 +35,8 @@
|
||||
"files": [
|
||||
"src/renderer/**/*.{ts,tsx}",
|
||||
"packages/aiCore/**",
|
||||
"packages/extension-table-plus/**"
|
||||
"packages/extension-table-plus/**",
|
||||
"packages/ui/**"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
8
.vscode/settings.json
vendored
8
.vscode/settings.json
vendored
@@ -31,7 +31,8 @@
|
||||
},
|
||||
"editor.formatOnSave": true,
|
||||
"files.associations": {
|
||||
"*.css": "tailwindcss"
|
||||
"*.css": "tailwindcss",
|
||||
".oxlintrc.json": "jsonc"
|
||||
},
|
||||
"files.eol": "\n",
|
||||
// "i18n-ally.displayLanguage": "zh-cn", // 界面显示语言
|
||||
@@ -50,6 +51,9 @@
|
||||
},
|
||||
"tailwindCSS.classAttributes": [
|
||||
"className",
|
||||
"classNames",
|
||||
"classNames"
|
||||
],
|
||||
"tailwindCSS.experimental.classRegex": [
|
||||
["cva\\(([^;]*)[\\);]", "[`'\"`]([^'\"`;]*)[`'\"`]"]
|
||||
]
|
||||
}
|
||||
|
||||
113
CLAUDE.md
113
CLAUDE.md
@@ -7,6 +7,7 @@ This file provides guidance to AI coding assistants when working with code in th
|
||||
- **Keep it clear**: Write code that is easy to read, maintain, and explain.
|
||||
- **Match the house style**: Reuse existing patterns, naming, and conventions.
|
||||
- **Search smart**: Prefer `ast-grep` for semantic queries; fall back to `rg`/`grep` when needed.
|
||||
- **Build with Tailwind CSS & Shadcn UI**: Use components from `@packages/ui` (Shadcn UI + Tailwind CSS) for every new UI component; never add `antd` or `styled-components`.
|
||||
- **Log centrally**: Route all logging through `loggerService` with the right context—no `console.log`.
|
||||
- **Research via subagent**: Lean on `subagent` for external docs, APIs, news, and references.
|
||||
- **Always propose before executing**: Before making any changes, clearly explain your planned approach and wait for explicit user approval to ensure alignment and prevent unwanted modifications.
|
||||
@@ -34,13 +35,113 @@ This file provides guidance to AI coding assistants when working with code in th
|
||||
- **Renderer Process** (`src/renderer/`): React UI with Redux state management
|
||||
- **Preload Scripts** (`src/preload/`): Secure IPC bridge
|
||||
|
||||
### Key Components
|
||||
- **AI Core** (`src/renderer/src/aiCore/`): Middleware pipeline for multiple AI providers.
|
||||
- **Services** (`src/main/services/`): MCPService, KnowledgeService, WindowService, etc.
|
||||
- **Build System**: Electron-Vite with experimental rolldown-vite, yarn workspaces.
|
||||
- **State Management**: Redux Toolkit (`src/renderer/src/store/`) for predictable state.
|
||||
### Key Architectural Components
|
||||
|
||||
#### Main Process Services (`src/main/services/`)
|
||||
|
||||
- **MCPService**: Model Context Protocol server management
|
||||
- **KnowledgeService**: Document processing and knowledge base management
|
||||
- **FileStorage/S3Storage/WebDav**: Multiple storage backends
|
||||
- **WindowService**: Multi-window management (main, mini, selection windows)
|
||||
- **ProxyManager**: Network proxy handling
|
||||
- **SearchService**: Full-text search capabilities
|
||||
|
||||
#### AI Core (`src/renderer/src/aiCore/`)
|
||||
|
||||
- **Middleware System**: Composable pipeline for AI request processing
|
||||
- **Client Factory**: Supports multiple AI providers (OpenAI, Anthropic, Gemini, etc.)
|
||||
- **Stream Processing**: Real-time response handling
|
||||
|
||||
#### Data Management
|
||||
|
||||
- **Cache System**: Three-layer caching (memory/shared/persist) with React hooks integration
|
||||
- **Preferences**: Type-safe configuration management with multi-window synchronization
|
||||
- **User Data**: SQLite-based storage with Drizzle ORM for business data
|
||||
|
||||
#### Knowledge Management
|
||||
|
||||
- **Embeddings**: Vector search with multiple providers (OpenAI, Voyage, etc.)
|
||||
- **OCR**: Document text extraction (system OCR, Doc2x, Mineru)
|
||||
- **Preprocessing**: Document preparation pipeline
|
||||
- **Loaders**: Support for various file formats (PDF, DOCX, EPUB, etc.)
|
||||
|
||||
### Build System
|
||||
|
||||
- **Electron-Vite**: Development and build tooling (v4.0.0)
|
||||
- **Rolldown-Vite**: Using experimental rolldown-vite instead of standard vite
|
||||
- **Workspaces**: Monorepo structure with `packages/` directory
|
||||
- **Multiple Entry Points**: Main app, mini window, selection toolbar
|
||||
- **Styled Components**: CSS-in-JS styling with SWC optimization
|
||||
|
||||
### Testing Strategy
|
||||
|
||||
- **Vitest**: Unit and integration testing
|
||||
- **Playwright**: End-to-end testing
|
||||
- **Component Testing**: React Testing Library
|
||||
- **Coverage**: Available via `yarn test:coverage`
|
||||
|
||||
### Key Patterns
|
||||
|
||||
- **IPC Communication**: Secure main-renderer communication via preload scripts
|
||||
- **Service Layer**: Clear separation between UI and business logic
|
||||
- **Plugin Architecture**: Extensible via MCP servers and middleware
|
||||
- **Multi-language Support**: i18n with dynamic loading
|
||||
- **Theme System**: Light/dark themes with custom CSS variables
|
||||
|
||||
### UI Design
|
||||
|
||||
The project is in the process of migrating from antd & styled-components to Tailwind CSS and Shadcn UI. Please use components from `@packages/ui` to build UI components. The use of antd and styled-components is prohibited.
|
||||
|
||||
UI Library: `@packages/ui`
|
||||
|
||||
### Database Architecture
|
||||
|
||||
- **Database**: SQLite (`cherrystudio.sqlite`) + libsql driver
|
||||
- **ORM**: Drizzle ORM with comprehensive migration system
|
||||
- **Schemas**: Located in `src/main/data/db/schemas/` directory
|
||||
|
||||
#### Database Standards
|
||||
|
||||
- **Table Naming**: Use singular form with snake_case (e.g., `topic`, `message`, `app_state`)
|
||||
- **Schema Exports**: Export using `xxxTable` pattern (e.g., `topicTable`, `appStateTable`)
|
||||
- **Field Definition**: Drizzle auto-infers field names, no need to add default field names
|
||||
- **JSON Fields**: For JSON support, add `{ mode: 'json' }`, refer to `preference.ts` table definition
|
||||
- **JSON Serialization**: For JSON fields, no need to manually serialize/deserialize when reading/writing to database, Drizzle handles this automatically
|
||||
- **Timestamps**: Use existing `crudTimestamps` utility
|
||||
- **Migrations**: Generate via `yarn run migrations:generate`
|
||||
|
||||
## Data Access Patterns
|
||||
|
||||
The application uses three distinct data management systems. Choose the appropriate system based on data characteristics:
|
||||
|
||||
### Cache System
|
||||
- **Purpose**: Temporary data that can be regenerated
|
||||
- **Lifecycle**: Component-level (memory), window-level (shared), or persistent (survives restart)
|
||||
- **Use Cases**: API response caching, computed results, temporary UI state
|
||||
- **APIs**: `useCache`, `useSharedCache`, `usePersistCache` hooks, or `cacheService`
|
||||
|
||||
### Preference System
|
||||
- **Purpose**: User configuration and application settings
|
||||
- **Lifecycle**: Permanent until user changes
|
||||
- **Use Cases**: Theme, language, editor settings, user preferences
|
||||
- **APIs**: `usePreference`, `usePreferences` hooks, or `preferenceService`
|
||||
|
||||
### User Data API
|
||||
- **Purpose**: Core business data (conversations, files, notes, etc.)
|
||||
- **Lifecycle**: Permanent business records
|
||||
- **Use Cases**: Topics, messages, files, knowledge base, user-generated content
|
||||
- **APIs**: `useDataApi` hook or `dataApiService` for direct calls
|
||||
|
||||
### Selection Guidelines
|
||||
|
||||
- **Use Cache** for data that can be lost without impact (computed values, API responses)
|
||||
- **Use Preferences** for user settings that affect app behavior (UI configuration, feature flags)
|
||||
- **Use User Data API** for irreplaceable business data (conversations, documents, user content)
|
||||
|
||||
## Logging Standards
|
||||
|
||||
### Usage
|
||||
|
||||
### Logging
|
||||
```typescript
|
||||
import { loggerService } from '@logger'
|
||||
const logger = loggerService.withContext('moduleName')
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
}
|
||||
},
|
||||
"enabled": true,
|
||||
"includes": ["**/*.json", "!*.json", "!**/package.json"]
|
||||
"includes": ["**/*.json", "!*.json", "!**/package.json", "!packages/**/*.json"]
|
||||
},
|
||||
"css": {
|
||||
"formatter": {
|
||||
@@ -42,6 +42,7 @@
|
||||
"!.github/**",
|
||||
"!.husky/**",
|
||||
"!.vscode/**",
|
||||
"!.claude/**",
|
||||
"!*.yaml",
|
||||
"!*.yml",
|
||||
"!*.mjs",
|
||||
|
||||
@@ -66,9 +66,10 @@ asarUnpack:
|
||||
- resources/**
|
||||
- "**/*.{metal,exp,lib}"
|
||||
- "node_modules/@img/sharp-libvips-*/**"
|
||||
|
||||
# copy from node_modules/claude-code-plugins/plugins to resources/data/claude-code-pluginso
|
||||
extraResources:
|
||||
- from: "migrations/sqlite-drizzle"
|
||||
to: "migrations/sqlite-drizzle"
|
||||
# copy from node_modules/claude-code-plugins/plugins to resources/data/claude-code-pluginso
|
||||
- from: "./node_modules/claude-code-plugins/plugins/"
|
||||
to: "claude-code-plugins"
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ export default defineConfig({
|
||||
alias: {
|
||||
'@main': resolve('src/main'),
|
||||
'@types': resolve('src/renderer/src/types'),
|
||||
'@data': resolve('src/main/data'),
|
||||
'@shared': resolve('packages/shared'),
|
||||
'@logger': resolve('src/main/services/LoggerService'),
|
||||
'@mcp-trace/trace-core': resolve('packages/mcp-trace/trace-core'),
|
||||
@@ -61,7 +62,20 @@ export default defineConfig({
|
||||
}
|
||||
},
|
||||
build: {
|
||||
sourcemap: isDev
|
||||
sourcemap: isDev,
|
||||
rollupOptions: {
|
||||
// Unlike renderer which auto-discovers entries from HTML files,
|
||||
// preload requires explicit entry point configuration for multiple scripts
|
||||
input: {
|
||||
index: resolve(__dirname, 'src/preload/index.ts'),
|
||||
simplest: resolve(__dirname, 'src/preload/simplest.ts') // Minimal preload
|
||||
},
|
||||
external: ['electron'],
|
||||
output: {
|
||||
entryFileNames: '[name].js',
|
||||
format: 'cjs'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
renderer: {
|
||||
@@ -90,13 +104,17 @@ export default defineConfig({
|
||||
'@shared': resolve('packages/shared'),
|
||||
'@types': resolve('src/renderer/src/types'),
|
||||
'@logger': resolve('src/renderer/src/services/LoggerService'),
|
||||
'@data': resolve('src/renderer/src/data'),
|
||||
'@mcp-trace/trace-core': resolve('packages/mcp-trace/trace-core'),
|
||||
'@mcp-trace/trace-web': resolve('packages/mcp-trace/trace-web'),
|
||||
'@cherrystudio/ai-core/provider': resolve('packages/aiCore/src/core/providers'),
|
||||
'@cherrystudio/ai-core/built-in/plugins': resolve('packages/aiCore/src/core/plugins/built-in'),
|
||||
'@cherrystudio/ai-core': resolve('packages/aiCore/src'),
|
||||
'@cherrystudio/extension-table-plus': resolve('packages/extension-table-plus/src'),
|
||||
'@cherrystudio/ai-sdk-provider': resolve('packages/ai-sdk-provider/src')
|
||||
'@cherrystudio/ai-sdk-provider': resolve('packages/ai-sdk-provider/src'),
|
||||
'@cherrystudio/ui/icons': resolve('packages/ui/src/components/icons'),
|
||||
'@cherrystudio/ui': resolve('packages/ui/src'),
|
||||
'@cherrystudio/catalog': resolve('packages/catalog/src')
|
||||
}
|
||||
},
|
||||
optimizeDeps: {
|
||||
@@ -116,7 +134,8 @@ export default defineConfig({
|
||||
miniWindow: resolve(__dirname, 'src/renderer/miniWindow.html'),
|
||||
selectionToolbar: resolve(__dirname, 'src/renderer/selectionToolbar.html'),
|
||||
selectionAction: resolve(__dirname, 'src/renderer/selectionAction.html'),
|
||||
traceWindow: resolve(__dirname, 'src/renderer/traceWindow.html')
|
||||
traceWindow: resolve(__dirname, 'src/renderer/traceWindow.html'),
|
||||
migrationV2: resolve(__dirname, 'src/renderer/migrationV2.html')
|
||||
},
|
||||
onwarn(warning, warn) {
|
||||
if (warning.code === 'COMMONJS_VARIABLE_IN_ESM') return
|
||||
|
||||
@@ -72,8 +72,9 @@ export default defineConfig([
|
||||
...oxlint.configs['flat/eslint'],
|
||||
...oxlint.configs['flat/typescript'],
|
||||
...oxlint.configs['flat/unicorn'],
|
||||
// Custom rules should be after oxlint to overwrite
|
||||
// LoggerService Custom Rules - only apply to src directory
|
||||
{
|
||||
// LoggerService Custom Rules - only apply to src directory
|
||||
files: ['src/**/*.{ts,tsx,js,jsx}'],
|
||||
ignores: ['src/**/__tests__/**', 'src/**/__mocks__/**', 'src/**/*.test.*', 'src/preload/**'],
|
||||
rules: {
|
||||
@@ -87,6 +88,7 @@ export default defineConfig([
|
||||
]
|
||||
}
|
||||
},
|
||||
// i18n
|
||||
{
|
||||
files: ['**/*.{ts,tsx,js,jsx}'],
|
||||
languageOptions: {
|
||||
@@ -134,4 +136,25 @@ export default defineConfig([
|
||||
'i18n/no-template-in-t': 'warn'
|
||||
}
|
||||
},
|
||||
// ui migration
|
||||
{
|
||||
// Component Rules - prevent importing antd components when migration completed
|
||||
files: ['**/*.{ts,tsx,js,jsx}'],
|
||||
ignores: [],
|
||||
rules: {
|
||||
// 'no-restricted-imports': [
|
||||
// 'error',
|
||||
// {
|
||||
// paths: [
|
||||
// {
|
||||
// name: 'antd',
|
||||
// importNames: ['Flex', 'Switch', 'message', 'Button', 'Tooltip'],
|
||||
// message:
|
||||
// '❌ Do not import this component from antd. Use our custom components instead: import { ... } from "@cherrystudio/ui"'
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
}
|
||||
},
|
||||
])
|
||||
|
||||
6
migrations/README.md
Normal file
6
migrations/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
**THIS DIRECTORY IS NOT FOR RUNTIME USE**
|
||||
|
||||
- Using `libsql` as the `sqlite3` driver, and `drizzle` as the ORM and database migration tool
|
||||
- `migrations/sqlite-drizzle` contains auto-generated migration data. Please **DO NOT** modify it.
|
||||
- If table structure changes, we should run migrations.
|
||||
- To generate migrations, use the command `yarn run migrations:generate`
|
||||
7
migrations/sqlite-drizzle.config.ts
Normal file
7
migrations/sqlite-drizzle.config.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { defineConfig } from 'drizzle-kit'
|
||||
export default defineConfig({
|
||||
out: './migrations/sqlite-drizzle',
|
||||
schema: './src/main/data/db/schemas/*',
|
||||
dialect: 'sqlite',
|
||||
casing: 'snake_case'
|
||||
})
|
||||
17
migrations/sqlite-drizzle/0000_solid_lord_hawal.sql
Normal file
17
migrations/sqlite-drizzle/0000_solid_lord_hawal.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
CREATE TABLE `app_state` (
|
||||
`key` text PRIMARY KEY NOT NULL,
|
||||
`value` text NOT NULL,
|
||||
`description` text,
|
||||
`created_at` integer,
|
||||
`updated_at` integer
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `preference` (
|
||||
`scope` text NOT NULL,
|
||||
`key` text NOT NULL,
|
||||
`value` text,
|
||||
`created_at` integer,
|
||||
`updated_at` integer
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX `scope_name_idx` ON `preference` (`scope`,`key`);
|
||||
114
migrations/sqlite-drizzle/meta/0000_snapshot.json
Normal file
114
migrations/sqlite-drizzle/meta/0000_snapshot.json
Normal file
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"_meta": {
|
||||
"columns": {},
|
||||
"schemas": {},
|
||||
"tables": {}
|
||||
},
|
||||
"dialect": "sqlite",
|
||||
"enums": {},
|
||||
"id": "de8009d7-95b9-4f99-99fa-4b8795708f21",
|
||||
"internal": {
|
||||
"indexes": {}
|
||||
},
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"tables": {
|
||||
"app_state": {
|
||||
"checkConstraints": {},
|
||||
"columns": {
|
||||
"created_at": {
|
||||
"autoincrement": false,
|
||||
"name": "created_at",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "integer"
|
||||
},
|
||||
"description": {
|
||||
"autoincrement": false,
|
||||
"name": "description",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "text"
|
||||
},
|
||||
"key": {
|
||||
"autoincrement": false,
|
||||
"name": "key",
|
||||
"notNull": true,
|
||||
"primaryKey": true,
|
||||
"type": "text"
|
||||
},
|
||||
"updated_at": {
|
||||
"autoincrement": false,
|
||||
"name": "updated_at",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "integer"
|
||||
},
|
||||
"value": {
|
||||
"autoincrement": false,
|
||||
"name": "value",
|
||||
"notNull": true,
|
||||
"primaryKey": false,
|
||||
"type": "text"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"foreignKeys": {},
|
||||
"indexes": {},
|
||||
"name": "app_state",
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"preference": {
|
||||
"checkConstraints": {},
|
||||
"columns": {
|
||||
"created_at": {
|
||||
"autoincrement": false,
|
||||
"name": "created_at",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "integer"
|
||||
},
|
||||
"key": {
|
||||
"autoincrement": false,
|
||||
"name": "key",
|
||||
"notNull": true,
|
||||
"primaryKey": false,
|
||||
"type": "text"
|
||||
},
|
||||
"scope": {
|
||||
"autoincrement": false,
|
||||
"name": "scope",
|
||||
"notNull": true,
|
||||
"primaryKey": false,
|
||||
"type": "text"
|
||||
},
|
||||
"updated_at": {
|
||||
"autoincrement": false,
|
||||
"name": "updated_at",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "integer"
|
||||
},
|
||||
"value": {
|
||||
"autoincrement": false,
|
||||
"name": "value",
|
||||
"notNull": false,
|
||||
"primaryKey": false,
|
||||
"type": "text"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"foreignKeys": {},
|
||||
"indexes": {
|
||||
"scope_name_idx": {
|
||||
"columns": ["scope", "key"],
|
||||
"isUnique": false,
|
||||
"name": "scope_name_idx"
|
||||
}
|
||||
},
|
||||
"name": "preference",
|
||||
"uniqueConstraints": {}
|
||||
}
|
||||
},
|
||||
"version": "6",
|
||||
"views": {}
|
||||
}
|
||||
13
migrations/sqlite-drizzle/meta/_journal.json
Normal file
13
migrations/sqlite-drizzle/meta/_journal.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"dialect": "sqlite",
|
||||
"entries": [
|
||||
{
|
||||
"breakpoints": true,
|
||||
"idx": 0,
|
||||
"tag": "0000_solid_lord_hawal",
|
||||
"version": "6",
|
||||
"when": 1754745234572
|
||||
}
|
||||
],
|
||||
"version": "7"
|
||||
}
|
||||
13
package.json
13
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "CherryStudio",
|
||||
"version": "1.7.0-rc.1",
|
||||
"version": "2.0.0-alpha",
|
||||
"private": true,
|
||||
"description": "A powerful AI assistant for producer.",
|
||||
"main": "./out/main/index.js",
|
||||
@@ -50,9 +50,10 @@
|
||||
"generate:icons": "electron-icon-builder --input=./build/logo.png --output=build",
|
||||
"analyze:renderer": "VISUALIZER_RENDERER=true yarn build",
|
||||
"analyze:main": "VISUALIZER_MAIN=true yarn build",
|
||||
"typecheck": "concurrently -n \"node,web\" -c \"cyan,magenta\" \"npm run typecheck:node\" \"npm run typecheck:web\"",
|
||||
"typecheck": "concurrently -n \"node,web,ui\" -c \"cyan,magenta,green\" \"npm run typecheck:node\" \"npm run typecheck:web\" \"npm run typecheck:ui\"",
|
||||
"typecheck:node": "tsgo --noEmit -p tsconfig.node.json --composite false",
|
||||
"typecheck:web": "tsgo --noEmit -p tsconfig.web.json --composite false",
|
||||
"typecheck:ui": "cd packages/ui && npm run type-check",
|
||||
"check:i18n": "dotenv -e .env -- tsx scripts/check-i18n.ts",
|
||||
"sync:i18n": "dotenv -e .env -- tsx scripts/sync-i18n.ts",
|
||||
"update:i18n": "dotenv -e .env -- tsx scripts/update-i18n.ts",
|
||||
@@ -69,11 +70,13 @@
|
||||
"test:e2e": "yarn playwright test",
|
||||
"test:lint": "oxlint --deny-warnings && eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --cache",
|
||||
"test:scripts": "vitest scripts",
|
||||
"lint": "oxlint --fix && eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --cache && yarn typecheck && yarn check:i18n && yarn format:check",
|
||||
"lint": "oxlint --fix && eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --cache && biome lint --write && biome format --write && yarn typecheck && yarn check:i18n && yarn format:check",
|
||||
"lint:ox": "oxlint --fix && biome lint --write && biome format --write",
|
||||
"format": "biome format --write && biome lint --write",
|
||||
"format:check": "biome format && biome lint",
|
||||
"prepare": "git config blame.ignoreRevsFile .git-blame-ignore-revs && husky",
|
||||
"claude": "dotenv -e .env -- claude",
|
||||
"migrations:generate": "drizzle-kit generate --config ./migrations/sqlite-drizzle.config.ts",
|
||||
"release:aicore:alpha": "yarn workspace @cherrystudio/ai-core version prerelease --preid alpha --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --tag alpha --access public",
|
||||
"release:aicore:beta": "yarn workspace @cherrystudio/ai-core version prerelease --preid beta --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --tag beta --access public",
|
||||
"release:aicore": "yarn workspace @cherrystudio/ai-core version patch --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --access public",
|
||||
@@ -100,6 +103,7 @@
|
||||
"selection-hook": "^1.0.12",
|
||||
"sharp": "^0.34.3",
|
||||
"socket.io": "^4.8.1",
|
||||
"stream-json": "^1.9.1",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.1",
|
||||
"tesseract.js": "patch:tesseract.js@npm%3A6.0.1#~/.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch",
|
||||
@@ -141,6 +145,7 @@
|
||||
"@cherrystudio/embedjs-openai": "^0.1.31",
|
||||
"@cherrystudio/extension-table-plus": "workspace:^",
|
||||
"@cherrystudio/openai": "^6.9.0",
|
||||
"@cherrystudio/ui": "workspace:*",
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/modifiers": "^9.0.0",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
@@ -155,7 +160,6 @@
|
||||
"@eslint/js": "^9.22.0",
|
||||
"@google/genai": "patch:@google/genai@npm%3A1.0.1#~/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch",
|
||||
"@hello-pangea/dnd": "^18.0.1",
|
||||
"@kangfenmao/keyv-storage": "^0.1.0",
|
||||
"@langchain/community": "^1.0.0",
|
||||
"@langchain/core": "patch:@langchain/core@npm%3A1.0.2#~/.yarn/patches/@langchain-core-npm-1.0.2-183ef83fe4.patch",
|
||||
"@langchain/openai": "patch:@langchain/openai@npm%3A1.0.0#~/.yarn/patches/@langchain-openai-npm-1.0.0-474d0ad9d4.patch",
|
||||
@@ -221,6 +225,7 @@
|
||||
"@types/react-infinite-scroll-component": "^5.0.0",
|
||||
"@types/react-transition-group": "^4.4.12",
|
||||
"@types/react-window": "^1",
|
||||
"@types/stream-json": "^1",
|
||||
"@types/swagger-jsdoc": "^6",
|
||||
"@types/swagger-ui-express": "^4.1.8",
|
||||
"@types/tinycolor2": "^1",
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { anthropic } from '@ai-sdk/anthropic'
|
||||
import { google } from '@ai-sdk/google'
|
||||
import { openai } from '@ai-sdk/openai'
|
||||
import type { InferToolInput, InferToolOutput } from 'ai'
|
||||
import { type Tool } from 'ai'
|
||||
import type { InferToolInput, InferToolOutput, Tool } from 'ai'
|
||||
|
||||
import { createOpenRouterOptions, createXaiOptions, mergeProviderOptions } from '../../../options'
|
||||
import type { ProviderOptionsMap } from '../../../options/types'
|
||||
|
||||
857
packages/catalog/PLANS.md
Normal file
857
packages/catalog/PLANS.md
Normal file
@@ -0,0 +1,857 @@
|
||||
# 模型和供应商参数化配置实现方案
|
||||
|
||||
## 📋 项目概述
|
||||
|
||||
本文档描述了在 `@packages/catalog/` 下实现模型和供应商参数化配置的方案,目标是将现有的硬编码逻辑重构为元数据驱动的配置系统。
|
||||
|
||||
## 🎯 目标
|
||||
|
||||
### 主要目标
|
||||
- 将硬编码的模型识别逻辑转换为 JSON 配置驱动
|
||||
- 解决"同一模型在不同供应商下有差异"的问题
|
||||
- 提供类型安全的配置系统(使用 Zod)
|
||||
- 支持未来通过配置更新添加新模型
|
||||
|
||||
### 痛点解决
|
||||
- **当前问题**:`src/renderer/src/config/models/` 下复杂的正则表达式和硬编码逻辑
|
||||
- **期望状态**:配置以 JSON 形式存在,代码中使用 Zod Schema 验证
|
||||
- **可维护性**:新模型发布时只需更新 JSON 配置,无需修改代码
|
||||
|
||||
## 🏗️ 架构设计
|
||||
|
||||
### 三层分离的元数据架构
|
||||
|
||||
```
|
||||
1. Base Model Catalog (models/*.json)
|
||||
├─ 模型基础信息(ID、能力、模态、限制、价格)
|
||||
└─ 官方/标准配置
|
||||
|
||||
2. Provider Catalog (providers/*.json)
|
||||
├─ 供应商特性(端点支持、API 兼容性)
|
||||
└─ 认证和定价模型
|
||||
|
||||
3. Provider Model Overrides (overrides/*.json)
|
||||
├─ 供应商对特定模型的覆盖
|
||||
└─ 解决"同一模型不同供应商差异"问题
|
||||
```
|
||||
|
||||
### 简化后的文件结构
|
||||
|
||||
```
|
||||
packages/catalog/
|
||||
├── src/
|
||||
│ ├── index.ts # 主导出文件
|
||||
│ ├── schemas/ # Schema 定义
|
||||
│ │ ├── index.ts # 统一导出
|
||||
│ │ ├── model.schema.ts # 模型配置 Schema + Zod
|
||||
│ │ ├── provider.schema.ts # 供应商配置 Schema + Zod
|
||||
│ │ └── override.schema.ts # 覆盖配置 Schema + Zod
|
||||
│ ├── data/ # 配置数据(单文件存储)
|
||||
│ │ ├── models.json # 所有模型配置
|
||||
│ │ ├── providers.json # 所有供应商配置
|
||||
│ │ └── overrides.json # 所有覆盖配置
|
||||
│ ├── services/ # 核心服务
|
||||
│ │ ├── CatalogService.ts # 统一的目录服务
|
||||
│ │ └── ConfigLoader.ts # 配置加载 + 验证
|
||||
│ ├── utils/ # 工具函数
|
||||
│ │ ├── migrate.ts # 迁移工具(从旧代码提取配置)
|
||||
│ │ └── helpers.ts # 辅助函数
|
||||
│ └── __tests__/ # 测试文件
|
||||
│ ├── fixtures/ # 测试数据
|
||||
│ ├── schemas.test.ts # Schema 测试
|
||||
│ └── catalog.test.ts # 目录服务测试
|
||||
├── scripts/
|
||||
│ └── migrate.ts # 迁移脚本 CLI
|
||||
└── package.json
|
||||
```
|
||||
|
||||
## 📝 Schema 定义
|
||||
|
||||
### 1. 模型配置 Schema
|
||||
|
||||
```typescript
|
||||
// packages/catalog/src/schemas/model.schema.ts
|
||||
|
||||
import * as z from 'zod'
|
||||
import { EndpointTypeSchema } from './provider.schema'
|
||||
|
||||
// 模态类型
|
||||
export const ModalitySchema = z.enum(['TEXT', 'VISION', 'AUDIO', 'VIDEO', 'VECTOR'])
|
||||
|
||||
// 能力类型
|
||||
export const ModelCapabilityTypeSchema = z.enum([
|
||||
'FUNCTION_CALL', // 函数调用
|
||||
'REASONING', // 推理
|
||||
'IMAGE_RECOGNITION', // 图像识别
|
||||
'IMAGE_GENERATION', // 图像生成
|
||||
'AUDIO_RECOGNITION', // 音频识别
|
||||
'AUDIO_GENERATION', // 音频生成
|
||||
'EMBEDDING', // 嵌入向量生成
|
||||
'RERANK', // 文本重排序
|
||||
'AUDIO_TRANSCRIPT', // 音频转录
|
||||
'VIDEO_RECOGNITION', // 视频识别
|
||||
'VIDEO_GENERATION', // 视频生成
|
||||
'STRUCTURED_OUTPUT', // 结构化输出
|
||||
'FILE_INPUT', // 文件输入支持
|
||||
'WEB_SEARCH', // 内置网络搜索
|
||||
'CODE_EXECUTION', // 代码执行
|
||||
'FILE_SEARCH', // 文件搜索
|
||||
'COMPUTER_USE' // 计算机使用
|
||||
])
|
||||
|
||||
// 推理配置
|
||||
export const ReasoningConfigSchema = z.object({
|
||||
supportedEfforts: z.array(z.enum(['low', 'medium', 'high'])),
|
||||
implementation: z.enum(['OPENAI_O1', 'ANTHROPIC_CLAUDE', 'DEEPSEEK_R1', 'GEMINI_THINKING']),
|
||||
reasoningMode: z.enum(['ALWAYS_ON', 'ON_DEMAND']),
|
||||
thinkingControl: z.object({
|
||||
enabled: z.boolean(),
|
||||
budget: z.object({
|
||||
min: z.number().optional(),
|
||||
max: z.number().optional()
|
||||
}).optional()
|
||||
}).optional()
|
||||
})
|
||||
|
||||
// 参数支持配置
|
||||
export const ParameterSupportSchema = z.object({
|
||||
temperature: z.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().min(0).max(2).optional(),
|
||||
max: z.number().min(0).max(2).optional(),
|
||||
default: z.number().min(0).max(2).optional()
|
||||
}).optional(),
|
||||
topP: z.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().min(0).max(1).optional(),
|
||||
max: z.number().min(0).max(1).optional(),
|
||||
default: z.number().min(0).max(1).optional()
|
||||
}).optional(),
|
||||
topK: z.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().positive().optional(),
|
||||
max: z.number().positive().optional()
|
||||
}).optional(),
|
||||
frequencyPenalty: z.boolean().optional(),
|
||||
presencePenalty: z.boolean().optional(),
|
||||
maxTokens: z.boolean().optional(),
|
||||
stopSequences: z.boolean().optional(),
|
||||
systemMessage: z.boolean().optional(),
|
||||
developerRole: z.boolean().optional()
|
||||
})
|
||||
|
||||
// 定价配置
|
||||
export const ModelPricingSchema = z.object({
|
||||
input: z.object({
|
||||
perMillionTokens: z.number(),
|
||||
currency: z.string().default('USD')
|
||||
}),
|
||||
output: z.object({
|
||||
perMillionTokens: z.number(),
|
||||
currency: z.string().default('USD')
|
||||
}),
|
||||
perImage: z.object({
|
||||
price: z.number(),
|
||||
currency: z.string().default('USD')
|
||||
}).optional(),
|
||||
perMinute: z.object({
|
||||
price: z.number(),
|
||||
currency: z.string().default('USD')
|
||||
}).optional()
|
||||
})
|
||||
|
||||
// 模型配置 Schema
|
||||
export const ModelConfigSchema = z.object({
|
||||
// 基础信息
|
||||
id: z.string(),
|
||||
name: z.string().optional(),
|
||||
ownedBy: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
|
||||
// 能力(核心)
|
||||
capabilities: z.array(ModelCapabilityTypeSchema),
|
||||
|
||||
// 模态
|
||||
inputModalities: z.array(ModalitySchema),
|
||||
outputModalities: z.array(ModalitySchema),
|
||||
|
||||
// 限制
|
||||
contextWindow: z.number(),
|
||||
maxOutputTokens: z.number(),
|
||||
maxInputTokens: z.number().optional(),
|
||||
|
||||
// 价格
|
||||
pricing: ModelPricingSchema.optional(),
|
||||
|
||||
// 推理配置
|
||||
reasoning: ReasoningConfigSchema.optional(),
|
||||
|
||||
// 参数支持
|
||||
parameters: ParameterSupportSchema.optional(),
|
||||
|
||||
// 端点类型
|
||||
endpointTypes: z.array(EndpointTypeSchema).optional(),
|
||||
|
||||
// 元数据
|
||||
releaseDate: z.string().optional(),
|
||||
deprecationDate: z.string().optional(),
|
||||
replacedBy: z.string().optional()
|
||||
})
|
||||
|
||||
export type ModelConfig = z.infer<typeof ModelConfigSchema>
|
||||
```
|
||||
|
||||
### 2. 供应商配置 Schema(简化版)
|
||||
|
||||
```typescript
|
||||
// packages/catalog/src/schemas/provider.schema.ts
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
// 端点类型
|
||||
export const EndpointTypeSchema = z.enum([
|
||||
'CHAT_COMPLETIONS',
|
||||
'COMPLETIONS',
|
||||
'EMBEDDINGS',
|
||||
'IMAGE_GENERATION',
|
||||
'AUDIO_SPEECH',
|
||||
'AUDIO_TRANSCRIPTIONS',
|
||||
'MESSAGES',
|
||||
'GENERATE_CONTENT',
|
||||
'RERANK',
|
||||
'MODERATIONS'
|
||||
])
|
||||
|
||||
// 认证方式
|
||||
export const AuthenticationSchema = z.enum([
|
||||
'API_KEY',
|
||||
'OAUTH',
|
||||
'CLOUD_CREDENTIALS'
|
||||
])
|
||||
|
||||
// 定价模型
|
||||
export const PricingModelSchema = z.enum([
|
||||
'UNIFIED', // 统一定价 (如 OpenRouter)
|
||||
'PER_MODEL', // 按模型独立定价 (如 OpenAI 官方)
|
||||
'TRANSPARENT', // 透明定价 (如 New-API)
|
||||
])
|
||||
|
||||
// 模型路由策略
|
||||
export const ModelRoutingSchema = z.enum([
|
||||
'INTELLIGENT', // 智能路由
|
||||
'DIRECT', // 直接路由
|
||||
'LOAD_BALANCED', // 负载均衡
|
||||
])
|
||||
|
||||
// API 兼容性配置
|
||||
export const ApiCompatibilitySchema = z.object({
|
||||
supportsArrayContent: z.boolean().default(true),
|
||||
supportsStreamOptions: z.boolean().default(true),
|
||||
supportsDeveloperRole: z.boolean().default(false),
|
||||
supportsThinkingControl: z.boolean().default(false),
|
||||
supportsParallelTools: z.boolean().default(false),
|
||||
supportsMultimodal: z.boolean().default(false),
|
||||
maxFileUploadSize: z.number().optional(),
|
||||
supportedFileTypes: z.array(z.string()).optional()
|
||||
})
|
||||
|
||||
// 供应商能力(简化版 - 使用数组代替多个布尔字段)
|
||||
export const ProviderCapabilitySchema = z.enum([
|
||||
'CUSTOM_MODELS', // 支持自定义模型
|
||||
'MODEL_MAPPING', // 提供模型映射
|
||||
'FALLBACK_ROUTING', // 降级路由
|
||||
'AUTO_RETRY', // 自动重试
|
||||
'REAL_TIME_METRICS', // 实时指标
|
||||
'USAGE_ANALYTICS', // 使用分析
|
||||
'STREAMING', // 流式响应
|
||||
'BATCH_PROCESSING', // 批量处理
|
||||
'RATE_LIMITING', // 速率限制
|
||||
])
|
||||
|
||||
// 供应商配置 Schema(简化版)
|
||||
export const ProviderConfigSchema = z.object({
|
||||
// 基础信息
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string().optional(),
|
||||
|
||||
// 核心配置
|
||||
authentication: AuthenticationSchema,
|
||||
pricingModel: PricingModelSchema,
|
||||
modelRouting: ModelRoutingSchema,
|
||||
|
||||
// 能力(使用数组替代多个布尔字段)
|
||||
capabilities: z.array(ProviderCapabilitySchema).default([]),
|
||||
|
||||
// 功能支持
|
||||
supportedEndpoints: z.array(EndpointTypeSchema),
|
||||
apiCompatibility: ApiCompatibilitySchema.optional(),
|
||||
|
||||
// 默认配置
|
||||
defaultApiHost: z.string().optional(),
|
||||
defaultRateLimit: z.number().optional(),
|
||||
|
||||
// 模型匹配
|
||||
modelIdPatterns: z.array(z.string()).optional(),
|
||||
aliasModelIds: z.record(z.string()).optional(),
|
||||
|
||||
// 元数据
|
||||
documentation: z.string().url().optional(),
|
||||
statusPage: z.string().url().optional(),
|
||||
|
||||
// 状态
|
||||
deprecated: z.boolean().default(false)
|
||||
})
|
||||
|
||||
export type ProviderConfig = z.infer<typeof ProviderConfigSchema>
|
||||
```
|
||||
|
||||
### 3. 覆盖配置 Schema
|
||||
|
||||
```typescript
|
||||
// packages/catalog/src/schemas/override.schema.ts
|
||||
|
||||
import * as z from 'zod'
|
||||
import { ModelCapabilityTypeSchema, ModelPricingSchema, ParameterSupportSchema } from './model.schema'
|
||||
|
||||
export const ProviderModelOverrideSchema = z.object({
|
||||
providerId: z.string(),
|
||||
modelId: z.string(),
|
||||
|
||||
// 能力覆盖
|
||||
capabilities: z.object({
|
||||
add: z.array(ModelCapabilityTypeSchema).optional(),
|
||||
remove: z.array(ModelCapabilityTypeSchema).optional()
|
||||
}).optional(),
|
||||
|
||||
// 限制覆盖
|
||||
limits: z.object({
|
||||
contextWindow: z.number().optional(),
|
||||
maxOutputTokens: z.number().optional()
|
||||
}).optional(),
|
||||
|
||||
// 价格覆盖
|
||||
pricing: ModelPricingSchema.optional(),
|
||||
|
||||
// 参数支持覆盖
|
||||
parameters: ParameterSupportSchema.optional(),
|
||||
|
||||
// 禁用模型
|
||||
disabled: z.boolean().optional(),
|
||||
|
||||
// 覆盖原因
|
||||
reason: z.string().optional()
|
||||
})
|
||||
|
||||
export type ProviderModelOverride = z.infer<typeof ProviderModelOverrideSchema>
|
||||
```
|
||||
|
||||
## 🔧 核心 API 设计
|
||||
|
||||
### 统一的目录服务
|
||||
|
||||
```typescript
|
||||
// packages/catalog/src/services/CatalogService.ts
|
||||
|
||||
export interface ModelFilters {
|
||||
capabilities?: ModelCapabilityType[]
|
||||
inputModalities?: Modality[]
|
||||
providers?: string[]
|
||||
minContextWindow?: number
|
||||
}
|
||||
|
||||
export interface ProviderFilter {
|
||||
capabilities?: ProviderCapability[]
|
||||
authentication?: AuthenticationSchema
|
||||
pricingModel?: PricingModelSchema
|
||||
notDeprecated?: boolean
|
||||
}
|
||||
|
||||
export class CatalogService {
|
||||
private models: Map<string, ModelConfig>
|
||||
private providers: Map<string, ProviderConfig>
|
||||
private overrides: Map<string, ProviderModelOverride[]>
|
||||
|
||||
// === 模型查询 ===
|
||||
|
||||
/**
|
||||
* 获取模型配置(应用供应商覆盖)
|
||||
*/
|
||||
getModel(modelId: string, providerId?: string): ModelConfig | null
|
||||
|
||||
/**
|
||||
* 检查模型能力
|
||||
*/
|
||||
hasCapability(modelId: string, capability: ModelCapabilityType, providerId?: string): boolean
|
||||
|
||||
/**
|
||||
* 获取模型的推理配置
|
||||
*/
|
||||
getReasoningConfig(modelId: string, providerId?: string): ReasoningConfig | null
|
||||
|
||||
/**
|
||||
* 获取模型参数范围
|
||||
*/
|
||||
getParameterRange(
|
||||
modelId: string,
|
||||
parameter: 'temperature' | 'topP' | 'topK',
|
||||
providerId?: string
|
||||
): { min: number, max: number, default?: number } | null
|
||||
|
||||
/**
|
||||
* 批量匹配模型
|
||||
*/
|
||||
findModels(filters?: ModelFilters): ModelConfig[]
|
||||
|
||||
// === 供应商查询 ===
|
||||
|
||||
/**
|
||||
* 获取供应商配置
|
||||
*/
|
||||
getProvider(providerId: string): ProviderConfig | null
|
||||
|
||||
/**
|
||||
* 检查供应商能力
|
||||
*/
|
||||
hasProviderCapability(providerId: string, capability: ProviderCapability): boolean
|
||||
|
||||
/**
|
||||
* 检查端点支持
|
||||
*/
|
||||
supportsEndpoint(providerId: string, endpoint: EndpointType): boolean
|
||||
|
||||
/**
|
||||
* 查找供应商
|
||||
*/
|
||||
findProviders(filter?: ProviderFilter): ProviderConfig[]
|
||||
|
||||
// === 内部方法 ===
|
||||
|
||||
/**
|
||||
* 应用覆盖配置
|
||||
*/
|
||||
private applyOverrides(model: ModelConfig, providerId: string): ModelConfig
|
||||
}
|
||||
|
||||
// 统一导出
|
||||
export const catalog = new CatalogService()
|
||||
|
||||
// 向后兼容的辅助函数
|
||||
export const isFunctionCallingModel = (model: Model): boolean =>
|
||||
catalog.hasCapability(model.id, 'FUNCTION_CALL', model.provider)
|
||||
|
||||
export const isReasoningModel = (model: Model): boolean =>
|
||||
catalog.hasCapability(model.id, 'REASONING', model.provider)
|
||||
|
||||
export const isVisionModel = (model: Model): boolean =>
|
||||
catalog.hasCapability(model.id, 'IMAGE_RECOGNITION', model.provider)
|
||||
```
|
||||
|
||||
## 📊 JSON 配置示例
|
||||
|
||||
### 模型配置示例
|
||||
|
||||
```json
|
||||
// packages/catalog/src/data/models.json
|
||||
{
|
||||
"version": "2025.11.24",
|
||||
"models": [
|
||||
{
|
||||
"id": "claude-3-5-sonnet-20241022",
|
||||
"name": "Claude 3.5 Sonnet",
|
||||
"owned_by": "anthropic",
|
||||
"capabilities": [
|
||||
"FUNCTION_CALL",
|
||||
"REASONING",
|
||||
"IMAGE_RECOGNITION",
|
||||
"STRUCTURED_OUTPUT",
|
||||
"FILE_INPUT"
|
||||
],
|
||||
"input_modalities": ["TEXT", "VISION"],
|
||||
"output_modalities": ["TEXT"],
|
||||
"context_window": 200000,
|
||||
"max_output_tokens": 8192,
|
||||
"pricing": {
|
||||
"input": { "per_million_tokens": 3.0, "currency": "USD" },
|
||||
"output": { "per_million_tokens": 15.0, "currency": "USD" }
|
||||
},
|
||||
"reasoning": {
|
||||
"type": "anthropic",
|
||||
"params": {
|
||||
"type": "enabled",
|
||||
"budgetTokens": 10000
|
||||
}
|
||||
},
|
||||
"parameters": {
|
||||
"temperature": {
|
||||
"supported": true,
|
||||
"min": 0.0,
|
||||
"max": 1.0,
|
||||
"default": 1.0
|
||||
}
|
||||
},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"id": "gpt-4-turbo",
|
||||
"name": "GPT-4 Turbo",
|
||||
"owned_by": "openai",
|
||||
"capabilities": [
|
||||
"FUNCTION_CALL",
|
||||
"IMAGE_RECOGNITION",
|
||||
"STRUCTURED_OUTPUT"
|
||||
],
|
||||
"input_modalities": ["TEXT", "VISION"],
|
||||
"output_modalities": ["TEXT"],
|
||||
"context_window": 128000,
|
||||
"max_output_tokens": 4096,
|
||||
"pricing": {
|
||||
"input": { "per_million_tokens": 10.0, "currency": "USD" },
|
||||
"output": { "per_million_tokens": 30.0, "currency": "USD" }
|
||||
},
|
||||
"metadata": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 供应商配置示例
|
||||
|
||||
```json
|
||||
// packages/catalog/src/data/providers.json
|
||||
{
|
||||
"version": "2025.11.24",
|
||||
"providers": [
|
||||
{
|
||||
"id": "anthropic",
|
||||
"name": "Anthropic",
|
||||
"authentication": "API_KEY",
|
||||
"pricing_model": "PER_MODEL",
|
||||
"model_routing": "DIRECT",
|
||||
"behaviors": {
|
||||
"supports_custom_models": false,
|
||||
"provides_model_mapping": false,
|
||||
"supports_streaming": true,
|
||||
"has_real_time_metrics": true,
|
||||
"supports_rate_limiting": true,
|
||||
"provides_usage_analytics": true,
|
||||
"requires_api_key_validation": true
|
||||
},
|
||||
"supported_endpoints": ["MESSAGES"],
|
||||
"api_compatibility": {
|
||||
"supports_stream_options": true,
|
||||
"supports_parallel_tools": true,
|
||||
"supports_multimodal": true
|
||||
},
|
||||
"default_api_host": "https://api.anthropic.com",
|
||||
"deprecated": false,
|
||||
"maintenance_mode": false,
|
||||
"config_version": "1.0.0",
|
||||
"special_config": {},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"id": "openrouter",
|
||||
"name": "OpenRouter",
|
||||
"authentication": "API_KEY",
|
||||
"pricing_model": "UNIFIED",
|
||||
"model_routing": "INTELLIGENT",
|
||||
"behaviors": {
|
||||
"supports_custom_models": true,
|
||||
"provides_model_mapping": true,
|
||||
"provides_fallback_routing": true,
|
||||
"has_auto_retry": true,
|
||||
"supports_streaming": true,
|
||||
"has_real_time_metrics": true
|
||||
},
|
||||
"supported_endpoints": ["CHAT_COMPLETIONS"],
|
||||
"default_api_host": "https://openrouter.ai/api/v1",
|
||||
"deprecated": false,
|
||||
"maintenance_mode": false,
|
||||
"config_version": "1.0.0",
|
||||
"special_config": {},
|
||||
"metadata": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 覆盖配置示例
|
||||
|
||||
```json
|
||||
// packages/catalog/src/data/overrides.json
|
||||
{
|
||||
"version": "2025.11.24",
|
||||
"overrides": [
|
||||
{
|
||||
"provider_id": "openrouter",
|
||||
"model_id": "claude-3-5-sonnet-20241022",
|
||||
"pricing": {
|
||||
"input": { "per_million_tokens": 4.5, "currency": "USD" },
|
||||
"output": { "per_million_tokens": 22.5, "currency": "USD" }
|
||||
},
|
||||
"capabilities": {
|
||||
"add": ["WEB_SEARCH"]
|
||||
},
|
||||
"reason": "OpenRouter applies markup and adds web search",
|
||||
"priority": 0
|
||||
},
|
||||
{
|
||||
"provider_id": "openrouter",
|
||||
"model_id": "gpt-4-turbo",
|
||||
"limits": {
|
||||
"context_window": 128000,
|
||||
"max_output_tokens": 16384
|
||||
},
|
||||
"reason": "OpenRouter extends output token limit",
|
||||
"priority": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 🔄 实现计划
|
||||
|
||||
### Phase 1: 基础架构 (2-3 days)
|
||||
|
||||
**目标**:建立核心架构和类型系统
|
||||
|
||||
**任务**:
|
||||
1. **Schema 定义**
|
||||
- 实现 `model.schema.ts`、`provider.schema.ts`、`override.schema.ts`
|
||||
- 所有 Schema 使用 Zod 验证
|
||||
- 导出 TypeScript 类型
|
||||
|
||||
2. **配置加载器**
|
||||
```typescript
|
||||
// packages/catalog/src/services/ConfigLoader.ts
|
||||
export class ConfigLoader {
|
||||
loadModels(): ModelConfig[]
|
||||
loadProviders(): ProviderConfig[]
|
||||
loadOverrides(): ProviderModelOverride[]
|
||||
validate(): boolean
|
||||
}
|
||||
```
|
||||
|
||||
3. **目录服务**
|
||||
```typescript
|
||||
// packages/catalog/src/services/CatalogService.ts
|
||||
export class CatalogService {
|
||||
// 实现所有查询 API
|
||||
}
|
||||
```
|
||||
|
||||
**验收标准**:
|
||||
- ✅ 所有 Schema 定义完成,通过 Zod 验证
|
||||
- ✅ ConfigLoader 可以加载和验证 JSON 文件
|
||||
- ✅ CatalogService 基础 API 实现
|
||||
- ✅ 单元测试覆盖核心功能
|
||||
|
||||
### Phase 2: 数据迁移 (1-2 days)
|
||||
|
||||
**目标**:从现有硬编码逻辑生成 JSON 配置
|
||||
|
||||
**任务**:
|
||||
1. **迁移工具**
|
||||
```typescript
|
||||
// packages/catalog/src/utils/migrate.ts
|
||||
export class MigrationTool {
|
||||
// 从 src/renderer/src/config/models/ 提取模型配置
|
||||
extractModelConfigs(): ModelConfig[]
|
||||
|
||||
// 提取供应商配置
|
||||
extractProviderConfigs(): ProviderConfig[]
|
||||
|
||||
// 写入 JSON 文件
|
||||
writeConfigs(models: ModelConfig[], providers: ProviderConfig[]): void
|
||||
|
||||
// 简单验证
|
||||
validate(): boolean
|
||||
}
|
||||
```
|
||||
|
||||
2. **迁移脚本**
|
||||
```bash
|
||||
# 运行迁移
|
||||
yarn catalog:migrate
|
||||
```
|
||||
|
||||
3. **手动审核**
|
||||
- 检查生成的配置文件
|
||||
- 补充缺失的价格和限制信息
|
||||
- 调整不准确的能力定义
|
||||
|
||||
**验收标准**:
|
||||
- ✅ 迁移工具能够提取现有配置
|
||||
- ✅ 生成的配置通过 Schema 验证
|
||||
- ✅ 手动审核完成,配置准确
|
||||
|
||||
### Phase 3: 集成替换 (2-3 days)
|
||||
|
||||
**目标**:替换现有硬编码逻辑
|
||||
|
||||
**任务**:
|
||||
1. **向后兼容层**
|
||||
```typescript
|
||||
// packages/catalog/src/index.ts
|
||||
export const isFunctionCallingModel = (model: Model): boolean =>
|
||||
catalog.hasCapability(model.id, 'FUNCTION_CALL', model.provider)
|
||||
```
|
||||
|
||||
2. **逐步替换**
|
||||
- 替换 `src/renderer/src/config/models/` 中的函数
|
||||
- 更新所有调用点
|
||||
- 确保测试通过
|
||||
|
||||
3. **集成测试**
|
||||
- 端到端测试
|
||||
- 性能测试
|
||||
- 兼容性测试
|
||||
|
||||
**验收标准**:
|
||||
- ✅ 所有现有测试通过
|
||||
- ✅ 新配置系统与旧系统行为一致
|
||||
- ✅ 性能不低于原有实现
|
||||
|
||||
### 延迟实现 ⏸️
|
||||
|
||||
以下功能在初期版本不实现,等待实际需求:
|
||||
|
||||
- ⏸️ **在线配置更新**:等到有用户需求再实现
|
||||
- ⏸️ **复杂缓存机制**:等出现性能问题再优化
|
||||
- ⏸️ **配置版本控制**:简化为文件级别的版本号
|
||||
|
||||
## 🧪 测试策略
|
||||
|
||||
### 测试覆盖
|
||||
|
||||
1. **Schema 测试**
|
||||
```typescript
|
||||
describe('ModelConfig Schema', () => {
|
||||
it('validates correct config', () => {
|
||||
expect(() => ModelConfigSchema.parse(validConfig)).not.toThrow()
|
||||
})
|
||||
|
||||
it('rejects invalid config', () => {
|
||||
expect(() => ModelConfigSchema.parse(invalidConfig)).toThrow()
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
2. **服务测试**
|
||||
```typescript
|
||||
describe('CatalogService', () => {
|
||||
it('returns model with overrides applied', () => {
|
||||
const model = catalog.getModel('claude-3-5-sonnet', 'openrouter')
|
||||
expect(model?.pricing).toEqual(expectedPricing)
|
||||
})
|
||||
|
||||
it('checks capabilities correctly', () => {
|
||||
expect(catalog.hasCapability('gpt-4', 'FUNCTION_CALL')).toBe(true)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
3. **兼容性测试**
|
||||
```typescript
|
||||
describe('Backward Compatibility', () => {
|
||||
it('produces same results as legacy', () => {
|
||||
expect(isFunctionCallingModel(testModel)).toBe(legacyResult)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
## 📖 使用指南
|
||||
|
||||
### 基本用法
|
||||
|
||||
```typescript
|
||||
import { catalog } from '@cherrystudio/catalog'
|
||||
|
||||
// 检查模型能力
|
||||
const canCallFunctions = catalog.hasCapability('gpt-4', 'FUNCTION_CALL')
|
||||
const canReason = catalog.hasCapability('o1-preview', 'REASONING')
|
||||
|
||||
// 获取模型配置
|
||||
const modelConfig = catalog.getModel('claude-3-5-sonnet', 'openrouter')
|
||||
|
||||
// 查找模型
|
||||
const visionModels = catalog.findModels({
|
||||
capabilities: ['IMAGE_RECOGNITION'],
|
||||
providers: ['anthropic', 'openai']
|
||||
})
|
||||
|
||||
// 检查供应商能力
|
||||
const hasMapping = catalog.hasProviderCapability('openrouter', 'MODEL_MAPPING')
|
||||
```
|
||||
|
||||
### 供应商查询
|
||||
|
||||
```typescript
|
||||
// 查找具有特定能力的供应商
|
||||
const providersWithFallback = catalog.findProviders({
|
||||
capabilities: ['FALLBACK_ROUTING', 'AUTO_RETRY']
|
||||
})
|
||||
|
||||
// 查找统一定价的供应商
|
||||
const unifiedPricingProviders = catalog.findProviders({
|
||||
pricingModel: 'UNIFIED'
|
||||
})
|
||||
```
|
||||
|
||||
## 📝 维护指南
|
||||
|
||||
### 添加新模型
|
||||
|
||||
1. 编辑对应的模型配置文件
|
||||
2. 添加模型信息
|
||||
3. 运行验证:`yarn catalog:validate`
|
||||
4. 提交 PR
|
||||
|
||||
### 添加新供应商
|
||||
|
||||
1. 编辑 `providers.json`
|
||||
2. 添加供应商配置
|
||||
3. 如需覆盖,添加到 `overrides.json`
|
||||
4. 验证并提交
|
||||
|
||||
## 🔧 开发工具
|
||||
|
||||
### 命令行
|
||||
|
||||
```json
|
||||
{
|
||||
"scripts": {
|
||||
"catalog:validate": "tsx scripts/validate.ts",
|
||||
"catalog:migrate": "tsx scripts/migrate.ts",
|
||||
"catalog:test": "vitest run",
|
||||
"catalog:build": "tsdown"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 📚 迁移对照表
|
||||
|
||||
| 旧函数 | 新 API |
|
||||
|--------|--------|
|
||||
| `isFunctionCallingModel(model)` | `catalog.hasCapability(model.id, 'FUNCTION_CALL', model.provider)` |
|
||||
| `isReasoningModel(model)` | `catalog.hasCapability(model.id, 'REASONING', model.provider)` |
|
||||
| `isVisionModel(model)` | `catalog.hasCapability(model.id, 'IMAGE_RECOGNITION', model.provider)` |
|
||||
| `getThinkModelType(model)` | `catalog.getReasoningConfig(model.id, model.provider)` |
|
||||
|
||||
## 📊 预期成果
|
||||
|
||||
### 时间估算
|
||||
- Phase 1: 2-3 天
|
||||
- Phase 2: 1-2 天
|
||||
- Phase 3: 2-3 天
|
||||
- **总计**: 5-8 天
|
||||
|
||||
### 性能目标
|
||||
- 配置加载时间: < 100ms
|
||||
- 模型查询时间: < 1ms
|
||||
- 内存使用: < 50MB
|
||||
|
||||
---
|
||||
|
||||
这个简化方案专注于核心功能,避免过度设计,遵循"保持简洁"的原则,为未来扩展留有空间。
|
||||
1
packages/catalog/README.md
Normal file
1
packages/catalog/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# catalog
|
||||
627
packages/catalog/api/openapi.json
Normal file
627
packages/catalog/api/openapi.json
Normal file
@@ -0,0 +1,627 @@
|
||||
{
|
||||
"openapi": "3.0.3",
|
||||
"info": {
|
||||
"title": "Cherry Studio Catalog API",
|
||||
"description": "REST API for managing AI models and providers catalog",
|
||||
"version": "1.0.0",
|
||||
"contact": {
|
||||
"name": "Cherry Studio Team"
|
||||
}
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "http://localhost:3000/api",
|
||||
"description": "Development server"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"/catalog/models": {
|
||||
"get": {
|
||||
"summary": "List models with pagination and filtering",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"default": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "search",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "capabilities",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "providers",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Paginated list of models",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/PaginatedModels"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"put": {
|
||||
"summary": "Update models configuration",
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ModelsConfig"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Models updated successfully"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/models/{modelId}": {
|
||||
"get": {
|
||||
"summary": "Get specific model details",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "modelId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Model details",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"put": {
|
||||
"summary": "Update specific model",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "modelId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Model updated successfully"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/providers": {
|
||||
"get": {
|
||||
"summary": "List providers with pagination and filtering",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"default": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "search",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "authentication",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Paginated list of providers",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/PaginatedProviders"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"put": {
|
||||
"summary": "Update providers configuration",
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProvidersConfig"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Providers updated successfully"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/providers/{providerId}": {
|
||||
"get": {
|
||||
"summary": "Get specific provider details",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "providerId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Provider details",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Provider"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/models/{modelId}/overrides": {
|
||||
"get": {
|
||||
"summary": "Get provider-specific overrides for a model",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "modelId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Provider overrides for the model",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/models/{modelId}/providers/{providerId}": {
|
||||
"get": {
|
||||
"summary": "Get model configuration as seen by specific provider",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "modelId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "providerId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Model configuration with provider-specific overrides applied",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"put": {
|
||||
"summary": "Update model configuration for specific provider (auto-detects if override is needed)",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "modelId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "providerId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Model configuration updated (override created/updated if needed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"updated": {
|
||||
"type": "string",
|
||||
"enum": ["base_model", "override", "both"]
|
||||
},
|
||||
"model": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/stats": {
|
||||
"get": {
|
||||
"summary": "Get catalog statistics",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Catalog statistics",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/CatalogStats"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/catalog/validate": {
|
||||
"post": {
|
||||
"summary": "Validate catalog configuration",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Validation results",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ValidationResult"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"Model": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"name": { "type": "string" },
|
||||
"owned_by": { "type": "string" },
|
||||
"capabilities": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"input_modalities": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"output_modalities": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"context_window": { "type": "integer" },
|
||||
"max_output_tokens": { "type": "integer" },
|
||||
"max_input_tokens": { "type": "integer" },
|
||||
"pricing": {
|
||||
"$ref": "#/components/schemas/Pricing"
|
||||
},
|
||||
"parameters": {
|
||||
"$ref": "#/components/schemas/Parameters"
|
||||
},
|
||||
"endpoint_types": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"metadata": { "type": "object" }
|
||||
}
|
||||
},
|
||||
"Provider": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"name": { "type": "string" },
|
||||
"description": { "type": "string" },
|
||||
"authentication": { "type": "string" },
|
||||
"pricing_model": { "type": "string" },
|
||||
"model_routing": { "type": "string" },
|
||||
"behaviors": { "type": "object" },
|
||||
"supported_endpoints": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"api_compatibility": { "type": "object" },
|
||||
"special_config": { "type": "object" },
|
||||
"documentation": { "type": "string" },
|
||||
"website": { "type": "string" },
|
||||
"deprecated": { "type": "boolean" },
|
||||
"maintenance_mode": { "type": "boolean" },
|
||||
"config_version": { "type": "string" },
|
||||
"metadata": { "type": "object" }
|
||||
}
|
||||
},
|
||||
"Override": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"provider_id": { "type": "string" },
|
||||
"model_id": { "type": "string" },
|
||||
"disabled": { "type": "boolean" },
|
||||
"reason": { "type": "string" },
|
||||
"last_updated": { "type": "string" },
|
||||
"updated_by": { "type": "string" },
|
||||
"priority": { "type": "integer" },
|
||||
"limits": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"context_window": { "type": "integer" },
|
||||
"max_output_tokens": { "type": "integer" }
|
||||
}
|
||||
},
|
||||
"pricing": {
|
||||
"$ref": "#/components/schemas/Pricing"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Pricing": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"per_million_tokens": { "type": "number" },
|
||||
"currency": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"output": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"per_million_tokens": { "type": "number" },
|
||||
"currency": { "type": "string" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Parameters": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"PaginatedModels": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
},
|
||||
"pagination": {
|
||||
"$ref": "#/components/schemas/Pagination"
|
||||
}
|
||||
}
|
||||
},
|
||||
"PaginatedProviders": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Provider"
|
||||
}
|
||||
},
|
||||
"pagination": {
|
||||
"$ref": "#/components/schemas/Pagination"
|
||||
}
|
||||
}
|
||||
},
|
||||
"PaginatedOverrides": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Override"
|
||||
}
|
||||
},
|
||||
"pagination": {
|
||||
"$ref": "#/components/schemas/Pagination"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Pagination": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"page": { "type": "integer" },
|
||||
"limit": { "type": "integer" },
|
||||
"total": { "type": "integer" },
|
||||
"totalPages": { "type": "integer" }
|
||||
}
|
||||
},
|
||||
"ModelsConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"version": { "type": "string" },
|
||||
"models": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"ProvidersConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"version": { "type": "string" },
|
||||
"providers": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Provider"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"OverridesConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"version": { "type": "string" },
|
||||
"overrides": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Override"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"CatalogStats": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"total_models": { "type": "integer" },
|
||||
"total_providers": { "type": "integer" },
|
||||
"total_overrides": { "type": "integer" },
|
||||
"models_by_provider": { "type": "object" },
|
||||
"overrides_by_provider": { "type": "object" },
|
||||
"last_updated": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"ValidationResult": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"valid": { "type": "boolean" },
|
||||
"errors": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"warnings": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
88
packages/catalog/data/migration-report.json
Normal file
88
packages/catalog/data/migration-report.json
Normal file
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"timestamp": "2025-11-24T06:41:03.487Z",
|
||||
"summary": {
|
||||
"total_providers": 104,
|
||||
"total_base_models": 241,
|
||||
"total_overrides": 1164,
|
||||
"provider_categories": {
|
||||
"direct": 2,
|
||||
"cloud": 6,
|
||||
"proxy": 3,
|
||||
"self_hosted": 5
|
||||
},
|
||||
"models_by_provider": {
|
||||
"openai": 79,
|
||||
"anthropic": 20,
|
||||
"dashscope": 22,
|
||||
"deepseek": 7,
|
||||
"gemini": 50,
|
||||
"mistral": 31,
|
||||
"xai": 32
|
||||
},
|
||||
"overrides_by_provider": {
|
||||
"bedrock": 152,
|
||||
"bedrock_converse": 56,
|
||||
"anyscale": 12,
|
||||
"azure": 112,
|
||||
"azure_ai": 45,
|
||||
"cerebras": 5,
|
||||
"vertex_ai-chat-models": 5,
|
||||
"nlp_cloud": 1,
|
||||
"cloudflare": 4,
|
||||
"vertex_ai-code-text-models": 1,
|
||||
"vertex_ai-code-chat-models": 6,
|
||||
"codestral": 2,
|
||||
"cohere_chat": 7,
|
||||
"databricks": 9,
|
||||
"deepinfra": 67,
|
||||
"featherless_ai": 2,
|
||||
"fireworks_ai": 27,
|
||||
"friendliai": 2,
|
||||
"openai": 8,
|
||||
"vertex_ai-language-models": 46,
|
||||
"vertex_ai-vision-models": 3,
|
||||
"gradient_ai": 13,
|
||||
"groq": 27,
|
||||
"heroku": 4,
|
||||
"hyperbolic": 16,
|
||||
"ai21": 9,
|
||||
"lambda_ai": 20,
|
||||
"lemonade": 5,
|
||||
"aleph_alpha": 3,
|
||||
"meta_llama": 4,
|
||||
"moonshot": 17,
|
||||
"morph": 2,
|
||||
"nscale": 14,
|
||||
"oci": 13,
|
||||
"ollama": 21,
|
||||
"openrouter": 92,
|
||||
"ovhcloud": 15,
|
||||
"palm": 2,
|
||||
"perplexity": 25,
|
||||
"replicate": 13,
|
||||
"sagemaker": 3,
|
||||
"sambanova": 16,
|
||||
"snowflake": 24,
|
||||
"together_ai": 36,
|
||||
"v0": 3,
|
||||
"vercel_ai_gateway": 85,
|
||||
"vertex_ai-anthropic_models": 22,
|
||||
"vertex_ai-mistral_models": 19,
|
||||
"vertex_ai-deepseek_models": 2,
|
||||
"vertex_ai": 1,
|
||||
"vertex_ai-ai21_models": 5,
|
||||
"vertex_ai-llama_models": 11,
|
||||
"vertex_ai-minimax_models": 1,
|
||||
"vertex_ai-moonshot_models": 1,
|
||||
"vertex_ai-openai_models": 2,
|
||||
"vertex_ai-qwen_models": 4,
|
||||
"wandb": 14,
|
||||
"watsonx": 28
|
||||
}
|
||||
},
|
||||
"files": {
|
||||
"providers": "providers.json",
|
||||
"models": "models.json",
|
||||
"overrides": "overrides.json"
|
||||
}
|
||||
}
|
||||
9371
packages/catalog/data/models.json
Normal file
9371
packages/catalog/data/models.json
Normal file
File diff suppressed because it is too large
Load Diff
26365
packages/catalog/data/overrides.json
Normal file
26365
packages/catalog/data/overrides.json
Normal file
File diff suppressed because it is too large
Load Diff
4949
packages/catalog/data/providers.json
Normal file
4949
packages/catalog/data/providers.json
Normal file
File diff suppressed because it is too large
Load Diff
24586
packages/catalog/model_prices_and_context_window.json
Normal file
24586
packages/catalog/model_prices_and_context_window.json
Normal file
File diff suppressed because it is too large
Load Diff
54
packages/catalog/package.json
Normal file
54
packages/catalog/package.json
Normal file
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "@cherrystudio/catalog",
|
||||
"version": "0.0.1-alpha.1",
|
||||
"description": "All Model Catalog",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"packageManager": "yarn@4.9.1",
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"dev": "tsc -w",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"author": "Cherry Studio",
|
||||
"license": "MIT",
|
||||
"files": [
|
||||
"dist/**/*"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/CherryHQ/cherry-studio.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/CherryHQ/cherry-studio/issues"
|
||||
},
|
||||
"homepage": "https://github.com/CherryHQ/cherry-studio#readme",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"react-native": "./dist/index.js",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/json-schema": "^7.0.15",
|
||||
"tsdown": "^0.16.6",
|
||||
"typescript": "^5.9.3",
|
||||
"vitest": "^4.0.13",
|
||||
"zod": "^4.1.12"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^4.1.12"
|
||||
},
|
||||
"dependencies": {
|
||||
"json-schema": "^0.4.0"
|
||||
},
|
||||
"workspaces": [
|
||||
"web"
|
||||
]
|
||||
}
|
||||
1701
packages/catalog/provider_endpoints_support.json
Normal file
1701
packages/catalog/provider_endpoints_support.json
Normal file
File diff suppressed because it is too large
Load Diff
39
packages/catalog/scripts/migrate.ts
Normal file
39
packages/catalog/scripts/migrate.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env tsx
|
||||
|
||||
/**
|
||||
* Migration Script - Phase 2 Implementation
|
||||
* Usage: npx tsx migrate.ts
|
||||
*/
|
||||
|
||||
import * as path from 'path'
|
||||
|
||||
import { MigrationTool } from '../src/utils/migration'
|
||||
|
||||
async function main() {
|
||||
const packageRoot = path.resolve(__dirname, '..')
|
||||
const sourceDir = packageRoot
|
||||
const outputDir = path.join(packageRoot, 'data')
|
||||
|
||||
console.log('🔧 Cherry Studio Catalog Migration - Phase 2')
|
||||
console.log('==========================================')
|
||||
console.log(`📁 Source: ${sourceDir}`)
|
||||
console.log(`📁 Output: ${outputDir}`)
|
||||
console.log('')
|
||||
|
||||
const tool = new MigrationTool(
|
||||
path.join(sourceDir, 'provider_endpoints_support.json'),
|
||||
path.join(sourceDir, 'model_prices_and_context_window.json'),
|
||||
outputDir
|
||||
)
|
||||
|
||||
try {
|
||||
await tool.migrate()
|
||||
console.log('')
|
||||
console.log('🎉 Migration completed! Check the src/data/ directory for results.')
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,240 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`Config & Schema > Snapshot Tests > should snapshot complete configuration structure 1`] = `
|
||||
{
|
||||
"models": Any<Array>,
|
||||
"overrides": Any<Array>,
|
||||
"providers": Any<Array>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`Config & Schema > Snapshot Tests > should snapshot model configurations 1`] = `
|
||||
[
|
||||
{
|
||||
"capabilities": [
|
||||
"FUNCTION_CALL",
|
||||
"REASONING",
|
||||
],
|
||||
"contextWindow": 128000,
|
||||
"description": "A test model for unit testing",
|
||||
"endpointTypes": [
|
||||
"CHAT_COMPLETIONS",
|
||||
],
|
||||
"id": "test-model",
|
||||
"inputModalities": [
|
||||
"TEXT",
|
||||
],
|
||||
"maxInputTokens": 124000,
|
||||
"maxOutputTokens": 4096,
|
||||
"metadata": {
|
||||
"architecture": "transformer",
|
||||
"category": "language-model",
|
||||
"documentation": "https://docs.test.com/models/test-model",
|
||||
"family": "test-family",
|
||||
"license": "mit",
|
||||
"source": "test",
|
||||
"tags": [
|
||||
"test",
|
||||
"fast",
|
||||
"reliable",
|
||||
],
|
||||
"trainingData": "synthetic",
|
||||
},
|
||||
"name": "Test Model",
|
||||
"outputModalities": [
|
||||
"TEXT",
|
||||
],
|
||||
"ownedBy": "TestProvider",
|
||||
"parameters": {
|
||||
"maxTokens": true,
|
||||
"systemMessage": true,
|
||||
"temperature": {
|
||||
"default": 1,
|
||||
"max": 2,
|
||||
"min": 0,
|
||||
"supported": true,
|
||||
},
|
||||
"topP": {
|
||||
"default": 1,
|
||||
"max": 1,
|
||||
"min": 0,
|
||||
"supported": true,
|
||||
},
|
||||
},
|
||||
"pricing": {
|
||||
"input": {
|
||||
"currency": "USD",
|
||||
"perMillionTokens": 1,
|
||||
},
|
||||
"output": {
|
||||
"currency": "USD",
|
||||
"perMillionTokens": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`Config & Schema > Snapshot Tests > should snapshot override configurations 1`] = `
|
||||
[
|
||||
{
|
||||
"capabilities": {
|
||||
"add": [
|
||||
"FUNCTION_CALL",
|
||||
],
|
||||
"remove": [
|
||||
"REASONING",
|
||||
],
|
||||
},
|
||||
"disabled": false,
|
||||
"lastUpdated": "2025-11-24T07:08:00Z",
|
||||
"limits": {
|
||||
"contextWindow": 256000,
|
||||
"maxOutputTokens": 8192,
|
||||
},
|
||||
"modelId": "test-model",
|
||||
"pricing": {
|
||||
"input": {
|
||||
"currency": "USD",
|
||||
"perMillionTokens": 0.5,
|
||||
},
|
||||
},
|
||||
"priority": 100,
|
||||
"providerId": "test-provider",
|
||||
"reason": "Test override for enhanced capabilities and limits",
|
||||
"updatedBy": "test-suite",
|
||||
},
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`Config & Schema > Snapshot Tests > should snapshot provider configurations 1`] = `
|
||||
[
|
||||
{
|
||||
"apiCompatibility": {
|
||||
"supportsApiVersion": false,
|
||||
"supportsArrayContent": true,
|
||||
"supportsDeveloperRole": false,
|
||||
"supportsMultimodal": false,
|
||||
"supportsParallelTools": false,
|
||||
"supportsServiceTier": false,
|
||||
"supportsStreamOptions": false,
|
||||
"supportsThinkingControl": false,
|
||||
},
|
||||
"authentication": "API_KEY",
|
||||
"behaviors": {
|
||||
"hasAutoRetry": false,
|
||||
"hasRealTimeMetrics": false,
|
||||
"providesFallbackRouting": false,
|
||||
"providesModelMapping": false,
|
||||
"providesUsageAnalytics": false,
|
||||
"providesUsageLimits": false,
|
||||
"requiresApiKeyValidation": true,
|
||||
"supportsBatchProcessing": false,
|
||||
"supportsCustomModels": false,
|
||||
"supportsHealthCheck": false,
|
||||
"supportsModelFineTuning": false,
|
||||
"supportsModelVersioning": false,
|
||||
"supportsRateLimiting": false,
|
||||
"supportsStreaming": true,
|
||||
"supportsWebhookEvents": false,
|
||||
},
|
||||
"configVersion": "1.0.0",
|
||||
"deprecated": false,
|
||||
"description": "A test provider for unit testing",
|
||||
"documentation": "https://docs.test.com",
|
||||
"id": "test-provider",
|
||||
"maintenanceMode": false,
|
||||
"metadata": {
|
||||
"category": "ai-provider",
|
||||
"reliability": "high",
|
||||
"source": "test",
|
||||
"supportedLanguages": [
|
||||
"en",
|
||||
],
|
||||
"tags": [
|
||||
"test",
|
||||
],
|
||||
},
|
||||
"modelRouting": "DIRECT",
|
||||
"name": "Test Provider",
|
||||
"pricingModel": "PER_MODEL",
|
||||
"specialConfig": {},
|
||||
"supportedEndpoints": [
|
||||
"CHAT_COMPLETIONS",
|
||||
],
|
||||
"website": "https://test.com",
|
||||
},
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`Config & Schema > Snapshot Tests > should snapshot validation results 1`] = `
|
||||
{
|
||||
"data": {
|
||||
"capabilities": [
|
||||
"FUNCTION_CALL",
|
||||
"REASONING",
|
||||
],
|
||||
"contextWindow": 128000,
|
||||
"description": "A test model for unit testing",
|
||||
"endpointTypes": [
|
||||
"CHAT_COMPLETIONS",
|
||||
],
|
||||
"id": "test-model",
|
||||
"inputModalities": [
|
||||
"TEXT",
|
||||
],
|
||||
"maxInputTokens": 124000,
|
||||
"maxOutputTokens": 4096,
|
||||
"metadata": {
|
||||
"architecture": "transformer",
|
||||
"category": "language-model",
|
||||
"documentation": "https://docs.test.com/models/test-model",
|
||||
"family": "test-family",
|
||||
"license": "mit",
|
||||
"source": "test",
|
||||
"tags": [
|
||||
"test",
|
||||
"fast",
|
||||
"reliable",
|
||||
],
|
||||
"trainingData": "synthetic",
|
||||
},
|
||||
"name": "Test Model",
|
||||
"outputModalities": [
|
||||
"TEXT",
|
||||
],
|
||||
"ownedBy": "TestProvider",
|
||||
"parameters": {
|
||||
"maxTokens": true,
|
||||
"systemMessage": true,
|
||||
"temperature": {
|
||||
"default": 1,
|
||||
"max": 2,
|
||||
"min": 0,
|
||||
"supported": true,
|
||||
},
|
||||
"topP": {
|
||||
"default": 1,
|
||||
"max": 1,
|
||||
"min": 0,
|
||||
"supported": true,
|
||||
},
|
||||
},
|
||||
"pricing": {
|
||||
"input": {
|
||||
"currency": "USD",
|
||||
"perMillionTokens": 1,
|
||||
},
|
||||
"output": {
|
||||
"currency": "USD",
|
||||
"perMillionTokens": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
"success": true,
|
||||
"warnings": [
|
||||
"Model has REASONING capability but no reasoning configuration",
|
||||
"Custom validation warning for snapshot",
|
||||
],
|
||||
}
|
||||
`;
|
||||
381
packages/catalog/src/__tests__/catalog.test.ts
Normal file
381
packages/catalog/src/__tests__/catalog.test.ts
Normal file
@@ -0,0 +1,381 @@
|
||||
import * as path from 'path'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { ConfigLoader } from '../loader/ConfigLoader'
|
||||
import { SchemaValidator } from '../validator/SchemaValidator'
|
||||
|
||||
// Use fixtures directory for test data
|
||||
const fixturesPath = path.join(__dirname, 'fixtures')
|
||||
|
||||
describe('Config & Schema', () => {
|
||||
describe('ConfigLoader', () => {
|
||||
it('should load models with complete validation', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const models = await loader.loadModels('test-models.json')
|
||||
expect(models).toBeDefined()
|
||||
expect(Array.isArray(models)).toBe(true)
|
||||
expect(models).toHaveLength(1)
|
||||
|
||||
const model = models[0]
|
||||
expect(model).toStrictEqual({
|
||||
id: 'test-model',
|
||||
name: 'Test Model',
|
||||
ownedBy: 'TestProvider',
|
||||
description: 'A test model for unit testing',
|
||||
capabilities: ['FUNCTION_CALL', 'REASONING'],
|
||||
inputModalities: ['TEXT'],
|
||||
outputModalities: ['TEXT'],
|
||||
contextWindow: 128000,
|
||||
maxOutputTokens: 4096,
|
||||
maxInputTokens: 124000,
|
||||
pricing: {
|
||||
input: { perMillionTokens: 1, currency: 'USD' },
|
||||
output: { perMillionTokens: 2, currency: 'USD' }
|
||||
},
|
||||
parameters: {
|
||||
temperature: { supported: true, min: 0, max: 2, default: 1 },
|
||||
maxTokens: true,
|
||||
systemMessage: true,
|
||||
topP: { supported: true, min: 0, max: 1, default: 1 }
|
||||
},
|
||||
endpointTypes: ['CHAT_COMPLETIONS'],
|
||||
metadata: {
|
||||
tags: ['test', 'fast', 'reliable'],
|
||||
category: 'language-model',
|
||||
source: 'test',
|
||||
license: 'mit',
|
||||
documentation: 'https://docs.test.com/models/test-model',
|
||||
family: 'test-family',
|
||||
architecture: 'transformer',
|
||||
trainingData: 'synthetic'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('should load providers with complete validation', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const providers = await loader.loadProviders('test-providers.json')
|
||||
expect(providers).toBeDefined()
|
||||
expect(Array.isArray(providers)).toBe(true)
|
||||
expect(providers).toHaveLength(1)
|
||||
|
||||
const provider = providers[0]
|
||||
expect(provider).toStrictEqual({
|
||||
id: 'test-provider',
|
||||
name: 'Test Provider',
|
||||
description: 'A test provider for unit testing',
|
||||
authentication: 'API_KEY',
|
||||
pricingModel: 'PER_MODEL',
|
||||
modelRouting: 'DIRECT',
|
||||
behaviors: {
|
||||
supportsCustomModels: false,
|
||||
providesModelMapping: false,
|
||||
supportsModelVersioning: false,
|
||||
providesFallbackRouting: false,
|
||||
hasAutoRetry: false,
|
||||
supportsHealthCheck: false,
|
||||
hasRealTimeMetrics: false,
|
||||
providesUsageAnalytics: false,
|
||||
supportsWebhookEvents: false,
|
||||
requiresApiKeyValidation: true,
|
||||
supportsRateLimiting: false,
|
||||
providesUsageLimits: false,
|
||||
supportsStreaming: true,
|
||||
supportsBatchProcessing: false,
|
||||
supportsModelFineTuning: false
|
||||
},
|
||||
supportedEndpoints: ['CHAT_COMPLETIONS'],
|
||||
apiCompatibility: {
|
||||
supportsArrayContent: true,
|
||||
supportsStreamOptions: false,
|
||||
supportsDeveloperRole: false,
|
||||
supportsThinkingControl: false,
|
||||
supportsApiVersion: false,
|
||||
supportsParallelTools: false,
|
||||
supportsMultimodal: false,
|
||||
supportsServiceTier: false
|
||||
},
|
||||
specialConfig: {},
|
||||
documentation: 'https://docs.test.com',
|
||||
website: 'https://test.com',
|
||||
deprecated: false,
|
||||
maintenanceMode: false,
|
||||
configVersion: '1.0.0',
|
||||
metadata: {
|
||||
tags: ['test'],
|
||||
category: 'ai-provider',
|
||||
source: 'test',
|
||||
reliability: 'high',
|
||||
supportedLanguages: ['en']
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('should load overrides with complete validation', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const overrides = await loader.loadOverrides('test-overrides.json')
|
||||
expect(overrides).toBeDefined()
|
||||
expect(Array.isArray(overrides)).toBe(true)
|
||||
expect(overrides).toHaveLength(1)
|
||||
|
||||
const override = overrides[0]
|
||||
expect(override).toMatchObject({
|
||||
providerId: 'test-provider',
|
||||
modelId: 'test-model',
|
||||
disabled: false,
|
||||
reason: 'Test override for enhanced capabilities and limits',
|
||||
priority: 100
|
||||
})
|
||||
|
||||
expect(override.capabilities?.add).toContain('FUNCTION_CALL')
|
||||
expect(override.capabilities?.remove).toContain('REASONING')
|
||||
expect(override.limits?.contextWindow).toBe(256000)
|
||||
expect(override.limits?.maxOutputTokens).toBe(8192)
|
||||
})
|
||||
|
||||
it('should load all configs simultaneously', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const configs = await loader.loadAllConfigs({
|
||||
modelsFile: 'test-models.json',
|
||||
providersFile: 'test-providers.json',
|
||||
overridesFile: 'test-overrides.json'
|
||||
})
|
||||
|
||||
expect(configs).toHaveProperty('models')
|
||||
expect(configs).toHaveProperty('providers')
|
||||
expect(configs).toHaveProperty('overrides')
|
||||
expect(configs.models).toHaveLength(1)
|
||||
expect(configs.providers).toHaveLength(1)
|
||||
expect(configs.overrides).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should handle missing files gracefully', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: '/nonexistent/path'
|
||||
})
|
||||
|
||||
await expect(loader.loadModels('nonexistent.json')).rejects.toThrow('Failed to load models')
|
||||
})
|
||||
})
|
||||
|
||||
describe('SchemaValidator', () => {
|
||||
it('should validate valid model configuration', async () => {
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
const validModel = {
|
||||
id: 'test-model',
|
||||
capabilities: ['FUNCTION_CALL', 'REASONING'],
|
||||
inputModalities: ['TEXT'],
|
||||
outputModalities: ['TEXT'],
|
||||
contextWindow: 128000,
|
||||
maxOutputTokens: 4096,
|
||||
metadata: {
|
||||
tags: ['test'],
|
||||
category: 'language-model',
|
||||
source: 'test'
|
||||
}
|
||||
}
|
||||
|
||||
const result = await validator.validateModel(validModel)
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.data).toBeDefined()
|
||||
expect(result.data!.id).toBe('test-model')
|
||||
})
|
||||
|
||||
it('should reject invalid model configuration', async () => {
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
const invalidModel = {
|
||||
id: 123, // Should be string
|
||||
capabilities: 'not-array', // Should be array
|
||||
contextWindow: -1000 // Should be positive
|
||||
}
|
||||
|
||||
const result = await validator.validateModel(invalidModel)
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.errors).toBeDefined()
|
||||
expect(result.errors!.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should provide warnings for model configuration issues', async () => {
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
const modelWithIssues = {
|
||||
id: 'test-model',
|
||||
capabilities: [], // Empty capabilities
|
||||
inputModalities: ['TEXT'],
|
||||
outputModalities: ['TEXT'],
|
||||
contextWindow: 200000, // Large context window
|
||||
maxOutputTokens: 4096,
|
||||
// Missing pricing and description
|
||||
metadata: {
|
||||
tags: ['test'],
|
||||
category: 'language-model',
|
||||
source: 'test'
|
||||
}
|
||||
}
|
||||
|
||||
const result = await validator.validateModel(modelWithIssues)
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.warnings).toBeDefined()
|
||||
expect(result.warnings!.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should accept custom validation warnings', async () => {
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
const model = {
|
||||
id: 'test-model',
|
||||
capabilities: ['FUNCTION_CALL'],
|
||||
inputModalities: ['TEXT'],
|
||||
outputModalities: ['TEXT'],
|
||||
contextWindow: 1000,
|
||||
maxOutputTokens: 500,
|
||||
metadata: {
|
||||
tags: ['test'],
|
||||
category: 'language-model',
|
||||
source: 'test'
|
||||
}
|
||||
}
|
||||
|
||||
const result = await validator.validateModel(model, {
|
||||
includeWarnings: true,
|
||||
customValidation: () => ['Custom warning message']
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.warnings).toContain('Custom warning message')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Integration Tests', () => {
|
||||
it('should load and validate models end-to-end', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
// Load models
|
||||
const models = await loader.loadModels('test-models.json')
|
||||
expect(models.length).toBeGreaterThan(0)
|
||||
|
||||
// Validate first model
|
||||
const validationResult = await validator.validateModel(models[0])
|
||||
expect(validationResult.success).toBe(true)
|
||||
expect(validationResult.data).toBeDefined()
|
||||
expect(validationResult.data!.id).toBe(models[0].id)
|
||||
})
|
||||
|
||||
it('should work with caching enabled', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: true
|
||||
})
|
||||
|
||||
// Test that caching doesn't break basic functionality
|
||||
const models1 = await loader.loadModels('test-models.json')
|
||||
expect(models1.length).toBeGreaterThan(0)
|
||||
expect(models1[0]).toHaveProperty('id', 'test-model')
|
||||
|
||||
// Test cache clear functionality
|
||||
loader.clearCache()
|
||||
expect(true).toBe(true) // Cache clear should not throw
|
||||
})
|
||||
})
|
||||
|
||||
describe('Snapshot Tests', () => {
|
||||
it('should snapshot model configurations', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const models = await loader.loadModels('test-models.json')
|
||||
expect(models).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('should snapshot provider configurations', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const providers = await loader.loadProviders('test-providers.json')
|
||||
expect(providers).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('should snapshot override configurations', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const overrides = await loader.loadOverrides('test-overrides.json')
|
||||
expect(overrides).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('should snapshot complete configuration structure', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
|
||||
const configs = await loader.loadAllConfigs({
|
||||
modelsFile: 'test-models.json',
|
||||
providersFile: 'test-providers.json',
|
||||
overridesFile: 'test-overrides.json'
|
||||
})
|
||||
|
||||
expect(configs).toMatchSnapshot({
|
||||
models: expect.any(Array),
|
||||
providers: expect.any(Array),
|
||||
overrides: expect.any(Array)
|
||||
})
|
||||
})
|
||||
|
||||
it('should snapshot validation results', async () => {
|
||||
const loader = new ConfigLoader({
|
||||
basePath: fixturesPath,
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: false
|
||||
})
|
||||
const validator = new SchemaValidator()
|
||||
|
||||
const model = await loader.loadModels('test-models.json')
|
||||
const validationResult = await validator.validateModel(model[0], {
|
||||
includeWarnings: true,
|
||||
customValidation: () => ['Custom validation warning for snapshot']
|
||||
})
|
||||
|
||||
expect(validationResult).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
})
|
||||
54
packages/catalog/src/__tests__/fixtures/test-models.json
Normal file
54
packages/catalog/src/__tests__/fixtures/test-models.json
Normal file
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"models": [
|
||||
{
|
||||
"id": "test-model",
|
||||
"name": "Test Model",
|
||||
"ownedBy": "TestProvider",
|
||||
"description": "A test model for unit testing",
|
||||
"capabilities": ["FUNCTION_CALL", "REASONING"],
|
||||
"inputModalities": ["TEXT"],
|
||||
"outputModalities": ["TEXT"],
|
||||
"contextWindow": 128000,
|
||||
"maxOutputTokens": 4096,
|
||||
"maxInputTokens": 124000,
|
||||
"pricing": {
|
||||
"input": {
|
||||
"perMillionTokens": 1,
|
||||
"currency": "USD"
|
||||
},
|
||||
"output": {
|
||||
"perMillionTokens": 2,
|
||||
"currency": "USD"
|
||||
}
|
||||
},
|
||||
"parameters": {
|
||||
"temperature": {
|
||||
"supported": true,
|
||||
"min": 0,
|
||||
"max": 2,
|
||||
"default": 1
|
||||
},
|
||||
"maxTokens": true,
|
||||
"systemMessage": true,
|
||||
"topP": {
|
||||
"supported": true,
|
||||
"min": 0,
|
||||
"max": 1,
|
||||
"default": 1
|
||||
}
|
||||
},
|
||||
"endpointTypes": ["CHAT_COMPLETIONS"],
|
||||
"metadata": {
|
||||
"tags": ["test", "fast", "reliable"],
|
||||
"category": "language-model",
|
||||
"source": "test",
|
||||
"license": "mit",
|
||||
"documentation": "https://docs.test.com/models/test-model",
|
||||
"family": "test-family",
|
||||
"architecture": "transformer",
|
||||
"trainingData": "synthetic"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
28
packages/catalog/src/__tests__/fixtures/test-overrides.json
Normal file
28
packages/catalog/src/__tests__/fixtures/test-overrides.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"overrides": [
|
||||
{
|
||||
"providerId": "test-provider",
|
||||
"modelId": "test-model",
|
||||
"capabilities": {
|
||||
"add": ["FUNCTION_CALL"],
|
||||
"remove": ["REASONING"]
|
||||
},
|
||||
"limits": {
|
||||
"contextWindow": 256000,
|
||||
"maxOutputTokens": 8192
|
||||
},
|
||||
"pricing": {
|
||||
"input": {
|
||||
"perMillionTokens": 0.5,
|
||||
"currency": "USD"
|
||||
}
|
||||
},
|
||||
"disabled": false,
|
||||
"reason": "Test override for enhanced capabilities and limits",
|
||||
"lastUpdated": "2025-11-24T07:08:00Z",
|
||||
"updatedBy": "test-suite",
|
||||
"priority": 100
|
||||
}
|
||||
]
|
||||
}
|
||||
53
packages/catalog/src/__tests__/fixtures/test-providers.json
Normal file
53
packages/catalog/src/__tests__/fixtures/test-providers.json
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"providers": [
|
||||
{
|
||||
"id": "test-provider",
|
||||
"name": "Test Provider",
|
||||
"description": "A test provider for unit testing",
|
||||
"authentication": "API_KEY",
|
||||
"pricingModel": "PER_MODEL",
|
||||
"modelRouting": "DIRECT",
|
||||
"behaviors": {
|
||||
"supportsCustomModels": false,
|
||||
"providesModelMapping": false,
|
||||
"supportsModelVersioning": false,
|
||||
"providesFallbackRouting": false,
|
||||
"hasAutoRetry": false,
|
||||
"supportsHealthCheck": false,
|
||||
"hasRealTimeMetrics": false,
|
||||
"providesUsageAnalytics": false,
|
||||
"supportsWebhookEvents": false,
|
||||
"requiresApiKeyValidation": true,
|
||||
"supportsRateLimiting": false,
|
||||
"providesUsageLimits": false,
|
||||
"supportsStreaming": true,
|
||||
"supportsBatchProcessing": false,
|
||||
"supportsModelFineTuning": false
|
||||
},
|
||||
"supportedEndpoints": ["CHAT_COMPLETIONS"],
|
||||
"apiCompatibility": {
|
||||
"supportsArrayContent": true,
|
||||
"supportsStreamOptions": false,
|
||||
"supportsDeveloperRole": false,
|
||||
"supportsThinkingControl": false,
|
||||
"supportsApiVersion": false,
|
||||
"supportsParallelTools": false,
|
||||
"supportsMultimodal": false
|
||||
},
|
||||
"specialConfig": {},
|
||||
"documentation": "https://docs.test.com",
|
||||
"website": "https://test.com",
|
||||
"deprecated": false,
|
||||
"maintenanceMode": false,
|
||||
"configVersion": "1.0.0",
|
||||
"metadata": {
|
||||
"tags": ["test"],
|
||||
"category": "ai-provider",
|
||||
"source": "test",
|
||||
"reliability": "high",
|
||||
"supportedLanguages": ["en"]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
21
packages/catalog/src/index.ts
Normal file
21
packages/catalog/src/index.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Cherry Studio Catalog
|
||||
* Main entry point for the model and provider catalog system
|
||||
*/
|
||||
|
||||
// Export all schemas
|
||||
export * from './schemas'
|
||||
|
||||
// Export core functionality
|
||||
export type {
|
||||
ConfigLoadOptions,
|
||||
ModelConfig,
|
||||
ProviderConfig,
|
||||
ProviderModelOverride
|
||||
} from './loader/ConfigLoader'
|
||||
export { ConfigLoader } from './loader/ConfigLoader'
|
||||
export type {
|
||||
ValidationOptions,
|
||||
ValidationResult
|
||||
} from './validator/SchemaValidator'
|
||||
export { SchemaValidator } from './validator/SchemaValidator'
|
||||
244
packages/catalog/src/loader/ConfigLoader.ts
Normal file
244
packages/catalog/src/loader/ConfigLoader.ts
Normal file
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* Configuration Loader
|
||||
* Responsible for loading and parsing JSON configuration files
|
||||
*/
|
||||
|
||||
import * as fs from 'fs/promises'
|
||||
import * as path from 'path'
|
||||
import type * as z from 'zod'
|
||||
|
||||
import { ModelListSchema, OverrideListSchema, ProviderListSchema } from '../schemas'
|
||||
import { safeParseJSON } from '../utils/parse-json/parse-json'
|
||||
import { zod4Schema } from '../utils/schema'
|
||||
|
||||
export type ModelConfig = z.infer<typeof ModelListSchema>['models'][0]
|
||||
export type ProviderConfig = z.infer<typeof ProviderListSchema>['providers'][0]
|
||||
export type ProviderModelOverride = z.infer<typeof OverrideListSchema>['overrides'][0]
|
||||
|
||||
export interface ConfigLoadOptions {
|
||||
basePath?: string
|
||||
validateOnLoad?: boolean
|
||||
cacheEnabled?: boolean
|
||||
}
|
||||
|
||||
export class ConfigLoader {
|
||||
private cache = new Map<string, any>()
|
||||
private options: ConfigLoadOptions
|
||||
|
||||
constructor(options: ConfigLoadOptions = {}) {
|
||||
this.options = {
|
||||
basePath: path.join(__dirname, '../data'),
|
||||
validateOnLoad: true,
|
||||
cacheEnabled: true,
|
||||
...options
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load model configurations from JSON file
|
||||
*/
|
||||
async loadModels(filename = 'models.json'): Promise<ModelConfig[]> {
|
||||
const filePath = path.join(this.options.basePath!, filename)
|
||||
|
||||
if (this.options.cacheEnabled && this.cache.has(filePath)) {
|
||||
return this.cache.get(filePath)
|
||||
}
|
||||
|
||||
try {
|
||||
const rawData = await fs.readFile(filePath, 'utf-8')
|
||||
|
||||
let validatedData: any
|
||||
if (this.options.validateOnLoad) {
|
||||
const schema = zod4Schema(ModelListSchema)
|
||||
const parseResult = await safeParseJSON({ text: rawData, schema })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Validation failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
} else {
|
||||
const parseResult = await safeParseJSON({ text: rawData })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Parse failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
}
|
||||
|
||||
const models = validatedData.models
|
||||
const version = validatedData.version
|
||||
|
||||
if (this.options.cacheEnabled) {
|
||||
this.cache.set(filePath, { models, version })
|
||||
}
|
||||
|
||||
return models
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to load models from ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load provider configurations from JSON file
|
||||
*/
|
||||
async loadProviders(filename = 'providers.json'): Promise<ProviderConfig[]> {
|
||||
const filePath = path.join(this.options.basePath!, filename)
|
||||
|
||||
if (this.options.cacheEnabled && this.cache.has(filePath)) {
|
||||
return this.cache.get(filePath)
|
||||
}
|
||||
|
||||
try {
|
||||
const rawData = await fs.readFile(filePath, 'utf-8')
|
||||
let validatedData: any
|
||||
if (this.options.validateOnLoad) {
|
||||
const schema = zod4Schema(ProviderListSchema)
|
||||
const parseResult = await safeParseJSON({ text: rawData, schema })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Validation failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
} else {
|
||||
const parseResult = await safeParseJSON({ text: rawData })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Parse failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
}
|
||||
|
||||
const providers = validatedData.providers
|
||||
const version = validatedData.version
|
||||
|
||||
if (this.options.cacheEnabled) {
|
||||
this.cache.set(filePath, { providers, version })
|
||||
}
|
||||
|
||||
return providers
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to load providers from ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load override configurations from JSON file
|
||||
*/
|
||||
async loadOverrides(filename = 'overrides.json'): Promise<ProviderModelOverride[]> {
|
||||
const filePath = path.join(this.options.basePath!, filename)
|
||||
|
||||
if (this.options.cacheEnabled && this.cache.has(filePath)) {
|
||||
return this.cache.get(filePath)
|
||||
}
|
||||
|
||||
try {
|
||||
const rawData = await fs.readFile(filePath, 'utf-8')
|
||||
let validatedData: any
|
||||
if (this.options.validateOnLoad) {
|
||||
const schema = zod4Schema(OverrideListSchema)
|
||||
const parseResult = await safeParseJSON({ text: rawData, schema })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Validation failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
} else {
|
||||
const parseResult = await safeParseJSON({ text: rawData })
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new Error(`Parse failed: ${parseResult.error.message}`)
|
||||
}
|
||||
validatedData = parseResult.value
|
||||
}
|
||||
|
||||
const overrides = validatedData.overrides
|
||||
const version = validatedData.version
|
||||
|
||||
if (this.options.cacheEnabled) {
|
||||
this.cache.set(filePath, { overrides, version })
|
||||
}
|
||||
|
||||
return overrides
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to load overrides from ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all configuration files
|
||||
*/
|
||||
async loadAllConfigs(options: { modelsFile?: string; providersFile?: string; overridesFile?: string } = {}): Promise<{
|
||||
models: ModelConfig[]
|
||||
providers: ProviderConfig[]
|
||||
overrides: ProviderModelOverride[]
|
||||
}> {
|
||||
const [models, providers, overrides] = await Promise.all([
|
||||
this.loadModels(options.modelsFile),
|
||||
this.loadProviders(options.providersFile),
|
||||
this.loadOverrides(options.overridesFile)
|
||||
])
|
||||
|
||||
return { models, providers, overrides }
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache
|
||||
*/
|
||||
clearCache(): void {
|
||||
this.cache.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file exists
|
||||
*/
|
||||
private async fileExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await fs.access(filePath)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration file version
|
||||
*/
|
||||
async getConfigVersion(filename: string): Promise<string | null> {
|
||||
const filePath = path.join(this.options.basePath!, filename)
|
||||
|
||||
if (!(await this.fileExists(filePath))) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const rawData = await fs.readFile(filePath, 'utf-8')
|
||||
const jsonData = JSON.parse(rawData)
|
||||
return jsonData.version || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all configuration versions
|
||||
*/
|
||||
async getAllConfigVersions(): Promise<{
|
||||
models: string | null
|
||||
providers: string | null
|
||||
overrides: string | null
|
||||
}> {
|
||||
const [models, providers, overrides] = await Promise.all([
|
||||
this.getConfigVersion('models.json'),
|
||||
this.getConfigVersion('providers.json'),
|
||||
this.getConfigVersion('overrides.json')
|
||||
])
|
||||
|
||||
return { models, providers, overrides }
|
||||
}
|
||||
}
|
||||
69
packages/catalog/src/schemas/common.ts
Normal file
69
packages/catalog/src/schemas/common.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Common type definitions for the catalog system
|
||||
* Shared across model, provider, and override schemas
|
||||
*/
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
// Common string types for reuse
|
||||
export const ModelIdSchema = z.string()
|
||||
export const ProviderIdSchema = z.string()
|
||||
export const VersionSchema = z.string()
|
||||
|
||||
// Currency codes
|
||||
export const CurrencySchema = z.enum(['USD', 'EUR', 'CNY', 'JPY', 'GBP'])
|
||||
|
||||
// Common file size units
|
||||
export const FileSizeUnitSchema = z.enum(['B', 'KB', 'MB', 'GB'])
|
||||
|
||||
// Common status types
|
||||
export const StatusSchema = z.enum(['active', 'inactive', 'deprecated', 'maintenance'])
|
||||
|
||||
// Timestamp schema for date fields
|
||||
export const TimestampSchema = z.iso.datetime()
|
||||
|
||||
// Range helper schemas
|
||||
export const NumericRangeSchema = z.object({
|
||||
min: z.number(),
|
||||
max: z.number()
|
||||
})
|
||||
|
||||
export const StringRangeSchema = z.object({
|
||||
min: z.string(),
|
||||
max: z.string()
|
||||
})
|
||||
|
||||
// Price per token schema
|
||||
export const PricePerTokenSchema = z.object({
|
||||
perMillionTokens: z.number().nonnegative(),
|
||||
currency: CurrencySchema.default('USD')
|
||||
})
|
||||
|
||||
// Generic metadata schema
|
||||
export const MetadataSchema = z.record(z.string(), z.any()).optional()
|
||||
|
||||
// Type exports
|
||||
export type ModelId = z.infer<typeof ModelIdSchema>
|
||||
export type ProviderId = z.infer<typeof ProviderIdSchema>
|
||||
export type Version = z.infer<typeof VersionSchema>
|
||||
export type Currency = z.infer<typeof CurrencySchema>
|
||||
export type FileSizeUnit = z.infer<typeof FileSizeUnitSchema>
|
||||
export type Status = z.infer<typeof StatusSchema>
|
||||
export type Timestamp = z.infer<typeof TimestampSchema>
|
||||
export type NumericRange = z.infer<typeof NumericRangeSchema>
|
||||
export type StringRange = z.infer<typeof StringRangeSchema>
|
||||
export type PricePerToken = z.infer<typeof PricePerTokenSchema>
|
||||
export type Metadata = z.infer<typeof MetadataSchema>
|
||||
|
||||
// Common validation utilities
|
||||
export const validateRange = (min: number, max: number): boolean => {
|
||||
return min <= max
|
||||
}
|
||||
|
||||
export const validatePositiveNumber = (value: number): boolean => {
|
||||
return value >= 0
|
||||
}
|
||||
|
||||
export const validateNonEmptyString = (value: string): boolean => {
|
||||
return value.trim().length > 0
|
||||
}
|
||||
49
packages/catalog/src/schemas/index.ts
Normal file
49
packages/catalog/src/schemas/index.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Unified export of all catalog schemas and types
|
||||
* This file provides a single entry point for all schema definitions
|
||||
*/
|
||||
|
||||
// Export all schemas from common types
|
||||
export * from './common'
|
||||
|
||||
// Export model schemas
|
||||
export * from './model'
|
||||
|
||||
// Export provider schemas
|
||||
export * from './provider'
|
||||
|
||||
// Export override schemas
|
||||
export * from './override'
|
||||
|
||||
// Re-export commonly used combined types for convenience
|
||||
export type {
|
||||
Modality,
|
||||
ModelCapabilityType,
|
||||
ModelConfig,
|
||||
ModelPricing,
|
||||
ParameterSupport,
|
||||
Reasoning
|
||||
} from './model'
|
||||
export type {
|
||||
OverrideResult,
|
||||
OverrideValidation,
|
||||
ProviderModelOverride
|
||||
} from './override'
|
||||
export type {
|
||||
Authentication,
|
||||
EndpointType,
|
||||
McpSupport,
|
||||
PricingModel,
|
||||
ProviderBehaviors,
|
||||
ProviderConfig
|
||||
} from './provider'
|
||||
|
||||
// Export common types
|
||||
export type {
|
||||
Currency,
|
||||
Metadata,
|
||||
ModelId,
|
||||
ProviderId,
|
||||
Timestamp,
|
||||
Version
|
||||
} from './common'
|
||||
254
packages/catalog/src/schemas/model.ts
Normal file
254
packages/catalog/src/schemas/model.ts
Normal file
@@ -0,0 +1,254 @@
|
||||
/**
|
||||
* Model configuration schema definitions
|
||||
* Defines the structure for model metadata, capabilities, and configurations
|
||||
*/
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
import {
|
||||
CurrencySchema,
|
||||
MetadataSchema,
|
||||
ModelIdSchema,
|
||||
PricePerTokenSchema,
|
||||
TimestampSchema,
|
||||
VersionSchema
|
||||
} from './common'
|
||||
|
||||
// Modality types - supported input/output modalities
|
||||
export const ModalitySchema = z.enum(['TEXT', 'VISION', 'AUDIO', 'VIDEO', 'VECTOR'])
|
||||
|
||||
// Model capability types
|
||||
export const ModelCapabilityTypeSchema = z.enum([
|
||||
'FUNCTION_CALL', // Function calling
|
||||
'REASONING', // Reasoning/thinking
|
||||
'IMAGE_RECOGNITION', // Image recognition
|
||||
'IMAGE_GENERATION', // Image generation
|
||||
'AUDIO_RECOGNITION', // Audio recognition
|
||||
'AUDIO_GENERATION', // Audio generation
|
||||
'EMBEDDING', // Embedding vector generation
|
||||
'RERANK', // Text reranking
|
||||
'AUDIO_TRANSCRIPT', // Audio transcription
|
||||
'VIDEO_RECOGNITION', // Video recognition
|
||||
'VIDEO_GENERATION', // Video generation
|
||||
'STRUCTURED_OUTPUT', // Structured output
|
||||
'FILE_INPUT', // File input support
|
||||
'WEB_SEARCH', // Built-in web search
|
||||
'CODE_EXECUTION', // Code execution
|
||||
'FILE_SEARCH', // File search
|
||||
'COMPUTER_USE' // Computer use
|
||||
])
|
||||
|
||||
// Reasoning configuration
|
||||
export const ReasoningSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('openai-chat'),
|
||||
params: z.object({
|
||||
reasoning_effort: z.enum(['none', 'minimal', 'low', 'medium', 'high']).optional()
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('openai-responses'),
|
||||
params: z.object({
|
||||
reasoning: z.object({
|
||||
effort: z.enum(['none', 'minimal', 'low', 'medium', 'high']).optional(),
|
||||
summary: z.enum(['auto', 'concise', 'detailed']).optional()
|
||||
})
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('anthropic'),
|
||||
params: z.object({
|
||||
type: z.union([z.literal('enabled'), z.literal('disabled')]),
|
||||
budgetTokens: z.number().optional()
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('gemini'),
|
||||
params: z.union([
|
||||
z
|
||||
.object({
|
||||
thinking_config: z.object({
|
||||
include_thoughts: z.boolean().optional(),
|
||||
thinking_budget: z.number().optional()
|
||||
})
|
||||
})
|
||||
.optional(),
|
||||
z
|
||||
.object({
|
||||
thinking_level: z.enum(['low', 'medium', 'high']).optional()
|
||||
})
|
||||
.optional()
|
||||
])
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('openrouter'),
|
||||
params: z.object({
|
||||
reasoning: z
|
||||
.object({
|
||||
effort: z
|
||||
.union([z.literal('none'), z.literal('minimal'), z.literal('low'), z.literal('medium'), z.literal('high')])
|
||||
.optional(),
|
||||
max_tokens: z.number().optional(),
|
||||
exclude: z.boolean().optional()
|
||||
})
|
||||
.refine((v) => {
|
||||
v.effort == null || v.max_tokens == null
|
||||
}, 'One of the following (not both)')
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('qwen'),
|
||||
params: z.object({
|
||||
enable_thinking: z.boolean(),
|
||||
thinking_budget: z.number().optional()
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('doubao'),
|
||||
params: z.object({
|
||||
thinking: z.object({
|
||||
type: z.union([z.literal('enabled'), z.literal('disabled'), z.literal('auto')])
|
||||
})
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('dashscope'),
|
||||
params: z.object({
|
||||
enable_thinking: z.boolean(),
|
||||
incremental_output: z.boolean().optional()
|
||||
})
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('self-hosted'),
|
||||
params: z.object({
|
||||
chat_template_kwargs: z.object({
|
||||
enable_thinking: z.boolean().optional(),
|
||||
thinking: z.boolean().optional()
|
||||
})
|
||||
})
|
||||
})
|
||||
])
|
||||
|
||||
// Parameter support configuration
|
||||
export const ParameterSupportSchema = z.object({
|
||||
temperature: z
|
||||
.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().min(0).max(2).optional(),
|
||||
max: z.number().min(0).max(2).optional(),
|
||||
default: z.number().min(0).max(2).optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
topP: z
|
||||
.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().min(0).max(1).optional(),
|
||||
max: z.number().min(0).max(1).optional(),
|
||||
default: z.number().min(0).max(1).optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
topK: z
|
||||
.object({
|
||||
supported: z.boolean(),
|
||||
min: z.number().positive().optional(),
|
||||
max: z.number().positive().optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
frequencyPenalty: z.boolean().optional(),
|
||||
presencePenalty: z.boolean().optional(),
|
||||
maxTokens: z.boolean().optional(),
|
||||
stopSequences: z.boolean().optional(),
|
||||
systemMessage: z.boolean().optional(),
|
||||
developerRole: z.boolean().optional()
|
||||
})
|
||||
|
||||
// Model pricing configuration
|
||||
export const ModelPricingSchema = z.object({
|
||||
input: PricePerTokenSchema,
|
||||
output: PricePerTokenSchema,
|
||||
|
||||
// Image pricing (optional)
|
||||
per_image: z
|
||||
.object({
|
||||
price: z.number(),
|
||||
currency: CurrencySchema.default('USD'),
|
||||
unit: z.enum(['image', 'pixel']).optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
// Audio/video pricing (optional)
|
||||
per_minute: z
|
||||
.object({
|
||||
price: z.number(),
|
||||
currency: CurrencySchema.default('USD')
|
||||
})
|
||||
.optional()
|
||||
})
|
||||
|
||||
// Model configuration schema
|
||||
export const ModelConfigSchema = z.object({
|
||||
// Basic information
|
||||
id: ModelIdSchema,
|
||||
name: z.string().optional(),
|
||||
owned_by: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
|
||||
// Capabilities (core)
|
||||
capabilities: z.array(ModelCapabilityTypeSchema),
|
||||
|
||||
// Modalities
|
||||
input_modalities: z.array(ModalitySchema),
|
||||
output_modalities: z.array(ModalitySchema),
|
||||
|
||||
// Limits
|
||||
context_window: z.number(),
|
||||
max_output_tokens: z.number(),
|
||||
max_input_tokens: z.number().optional(),
|
||||
|
||||
// Pricing
|
||||
pricing: ModelPricingSchema.optional(),
|
||||
|
||||
// Reasoning configuration
|
||||
reasoning: ReasoningSchema.optional(),
|
||||
|
||||
// Parameter support
|
||||
parameters: ParameterSupportSchema.optional(),
|
||||
|
||||
// Endpoint types (will reference provider schema)
|
||||
endpoint_types: z.array(z.string()).optional(),
|
||||
|
||||
// Metadata
|
||||
release_date: TimestampSchema.optional(),
|
||||
deprecation_date: TimestampSchema.optional(),
|
||||
replaced_by: ModelIdSchema.optional(),
|
||||
|
||||
// Version control
|
||||
version: VersionSchema.optional(),
|
||||
compatibility: z
|
||||
.object({
|
||||
min_version: VersionSchema.optional(),
|
||||
max_version: VersionSchema.optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
// Additional metadata
|
||||
metadata: MetadataSchema
|
||||
})
|
||||
|
||||
// Model list container schema for JSON files
|
||||
export const ModelListSchema = z.object({
|
||||
version: VersionSchema,
|
||||
models: z.array(ModelConfigSchema)
|
||||
})
|
||||
|
||||
// Type exports
|
||||
export type Modality = z.infer<typeof ModalitySchema>
|
||||
export type ModelCapabilityType = z.infer<typeof ModelCapabilityTypeSchema>
|
||||
export type Reasoning = z.infer<typeof ReasoningSchema>
|
||||
export type ParameterSupport = z.infer<typeof ParameterSupportSchema>
|
||||
export type ModelPricing = z.infer<typeof ModelPricingSchema>
|
||||
export type ModelConfig = z.infer<typeof ModelConfigSchema>
|
||||
export type ModelList = z.infer<typeof ModelListSchema>
|
||||
147
packages/catalog/src/schemas/override.ts
Normal file
147
packages/catalog/src/schemas/override.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* Provider model override schema definitions
|
||||
* Defines how providers can override specific model configurations
|
||||
*/
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
import { MetadataSchema, ModelIdSchema, ProviderIdSchema, VersionSchema } from './common'
|
||||
import { ModelCapabilityTypeSchema, ModelPricingSchema, ParameterSupportSchema, ReasoningSchema } from './model'
|
||||
import { EndpointTypeSchema } from './provider'
|
||||
|
||||
// Capability override operations
|
||||
export const CapabilityOverrideSchema = z.object({
|
||||
add: z.array(ModelCapabilityTypeSchema).optional(), // Add capabilities
|
||||
remove: z.array(ModelCapabilityTypeSchema).optional(), // Remove capabilities
|
||||
force: z.array(ModelCapabilityTypeSchema).optional() // Force set capabilities (ignore base config)
|
||||
})
|
||||
|
||||
// Limits override configuration
|
||||
export const LimitsOverrideSchema = z.object({
|
||||
context_window: z.number().optional(),
|
||||
max_output_tokens: z.number().optional(),
|
||||
max_input_tokens: z.number().optional()
|
||||
})
|
||||
|
||||
// Pricing override configuration
|
||||
export const PricingOverrideSchema = ModelPricingSchema.partial().optional()
|
||||
|
||||
// Endpoint types override
|
||||
export const EndpointTypesOverrideSchema = z.array(EndpointTypeSchema).optional()
|
||||
|
||||
// Reasoning configuration override - allows partial override of reasoning configs
|
||||
export const ReasoningOverrideSchema = ReasoningSchema.optional()
|
||||
|
||||
// Parameter support override
|
||||
export const ParameterSupportOverrideSchema = ParameterSupportSchema.partial().optional()
|
||||
|
||||
// Model metadata override
|
||||
export const MetadataOverrideSchema = z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
deprecation_date: z.iso.datetime().optional(),
|
||||
replaced_by: ModelIdSchema.optional(),
|
||||
metadata: MetadataSchema
|
||||
})
|
||||
.optional()
|
||||
|
||||
// Main provider model override schema
|
||||
export const ProviderModelOverrideSchema = z.object({
|
||||
// Identification
|
||||
provider_id: ProviderIdSchema,
|
||||
model_id: ModelIdSchema,
|
||||
|
||||
// Capability overrides
|
||||
capabilities: CapabilityOverrideSchema.optional(),
|
||||
|
||||
// Limits overrides
|
||||
limits: LimitsOverrideSchema.optional(),
|
||||
|
||||
// Pricing overrides
|
||||
pricing: PricingOverrideSchema,
|
||||
|
||||
// Reasoning configuration overrides
|
||||
reasoning: ReasoningOverrideSchema.optional(),
|
||||
|
||||
// Parameter support overrides
|
||||
parameters: ParameterSupportOverrideSchema.optional(),
|
||||
|
||||
// Endpoint type overrides
|
||||
endpoint_types: EndpointTypesOverrideSchema.optional(),
|
||||
|
||||
// Model metadata overrides
|
||||
metadata: MetadataOverrideSchema.optional(),
|
||||
|
||||
// Status overrides
|
||||
disabled: z.boolean().optional(), // Disable this model for this provider
|
||||
replace_with: ModelIdSchema.optional(), // Replace with alternative model
|
||||
|
||||
// Override tracking
|
||||
reason: z.string().optional(), // Reason for override
|
||||
last_updated: z.iso.datetime().optional(),
|
||||
updated_by: z.string().optional(), // Who made the override
|
||||
|
||||
// Override priority (higher number = higher priority)
|
||||
priority: z.number().default(0),
|
||||
|
||||
// Override conditions
|
||||
conditions: z
|
||||
.object({
|
||||
// Apply override only for specific regions
|
||||
regions: z.array(z.string()).optional(),
|
||||
|
||||
// Apply override only for specific user tiers
|
||||
user_tiers: z.array(z.string()).optional(),
|
||||
|
||||
// Apply override only in specific environments
|
||||
environments: z.array(z.enum(['development', 'staging', 'production'])).optional(),
|
||||
|
||||
// Time-based conditions
|
||||
valid_from: z.iso.datetime().optional(),
|
||||
valid_until: z.iso.datetime().optional()
|
||||
})
|
||||
.optional(),
|
||||
|
||||
// Additional override metadata
|
||||
override_metadata: MetadataSchema.optional()
|
||||
})
|
||||
|
||||
// Override container schema for JSON files
|
||||
export const OverrideListSchema = z.object({
|
||||
version: VersionSchema,
|
||||
overrides: z.array(ProviderModelOverrideSchema)
|
||||
})
|
||||
|
||||
// Override application result schema
|
||||
export const OverrideResultSchema = z.object({
|
||||
model_id: ModelIdSchema,
|
||||
provider_id: ProviderIdSchema,
|
||||
applied: z.boolean(),
|
||||
applied_overrides: z.array(z.string()), // List of applied override fields
|
||||
original_values: z.record(z.string(), z.unknown()), // Original values before override
|
||||
new_values: z.record(z.string(), z.unknown()), // New values after override
|
||||
override_reason: z.string().optional(),
|
||||
applied_at: z.iso.datetime().optional()
|
||||
})
|
||||
|
||||
// Override validation result
|
||||
export const OverrideValidationSchema = z.object({
|
||||
valid: z.boolean(),
|
||||
errors: z.array(z.string()),
|
||||
warnings: z.array(z.string()),
|
||||
recommendations: z.array(z.string())
|
||||
})
|
||||
|
||||
// Type exports
|
||||
export type CapabilityOverride = z.infer<typeof CapabilityOverrideSchema>
|
||||
export type LimitsOverride = z.infer<typeof LimitsOverrideSchema>
|
||||
export type PricingOverride = z.infer<typeof PricingOverrideSchema>
|
||||
export type EndpointTypesOverride = z.infer<typeof EndpointTypesOverrideSchema>
|
||||
export type ReasoningOverride = z.infer<typeof ReasoningOverrideSchema>
|
||||
export type ParameterSupportOverride = z.infer<typeof ParameterSupportOverrideSchema>
|
||||
export type MetadataOverride = z.infer<typeof MetadataOverrideSchema>
|
||||
export type ProviderModelOverride = z.infer<typeof ProviderModelOverrideSchema>
|
||||
export type OverrideList = z.infer<typeof OverrideListSchema>
|
||||
export type OverrideResult = z.infer<typeof OverrideResultSchema>
|
||||
export type OverrideValidation = z.infer<typeof OverrideValidationSchema>
|
||||
171
packages/catalog/src/schemas/provider.ts
Normal file
171
packages/catalog/src/schemas/provider.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* Provider configuration schema definitions
|
||||
* Defines the structure for AI service provider metadata and capabilities
|
||||
*/
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
import { MetadataSchema, ProviderIdSchema, VersionSchema } from './common'
|
||||
|
||||
// Endpoint types supported by providers
|
||||
export const EndpointTypeSchema = z.enum([
|
||||
'CHAT_COMPLETIONS', // /chat/completions
|
||||
'COMPLETIONS', // /completions
|
||||
'EMBEDDINGS', // /embeddings
|
||||
'IMAGE_GENERATION', // /images/generations
|
||||
'IMAGE_EDIT', // /images/edits
|
||||
'AUDIO_SPEECH', // /audio/speech (TTS)
|
||||
'AUDIO_TRANSCRIPTIONS', // /audio/transcriptions (STT)
|
||||
'MESSAGES', // /messages
|
||||
'RESPONSES', // /responses
|
||||
'GENERATE_CONTENT', // :generateContent
|
||||
'STREAM_GENERATE_CONTENT', // :streamGenerateContent
|
||||
'RERANK', // /rerank
|
||||
'MODERATIONS' // /moderations
|
||||
])
|
||||
|
||||
// Authentication methods
|
||||
export const AuthenticationSchema = z.enum([
|
||||
'API_KEY', // Standard API Key authentication
|
||||
'OAUTH', // OAuth 2.0 authentication
|
||||
'CLOUD_CREDENTIALS' // Cloud service credentials (AWS, GCP, Azure)
|
||||
])
|
||||
|
||||
// Pricing models that affect UI and behavior
|
||||
export const PricingModelSchema = z.enum([
|
||||
'UNIFIED', // Unified pricing (like OpenRouter)
|
||||
'PER_MODEL', // Per-model independent pricing (like OpenAI official)
|
||||
'TRANSPARENT', // Transparent pricing (like New-API)
|
||||
'USAGE_BASED', // Dynamic usage-based pricing
|
||||
'SUBSCRIPTION' // Subscription-based pricing
|
||||
])
|
||||
|
||||
// Model routing strategies affecting performance and reliability
|
||||
export const ModelRoutingSchema = z.enum([
|
||||
'INTELLIGENT', // Intelligent routing, auto-select optimal instance
|
||||
'DIRECT', // Direct routing to specified model
|
||||
'LOAD_BALANCED', // Load balanced across multiple instances
|
||||
'GEO_ROUTED', // Geographic location routing
|
||||
'COST_OPTIMIZED' // Cost-optimized routing
|
||||
])
|
||||
|
||||
// Server-side MCP support configuration
|
||||
export const McpSupportSchema = z.object({
|
||||
supported: z.boolean().default(false),
|
||||
configuration: z
|
||||
.object({
|
||||
supports_url_pass_through: z.boolean().default(false),
|
||||
supported_servers: z.array(z.string()).optional(),
|
||||
max_concurrent_servers: z.number().optional()
|
||||
})
|
||||
.optional()
|
||||
})
|
||||
|
||||
// API compatibility configuration
|
||||
export const ApiCompatibilitySchema = z.object({
|
||||
supports_array_content: z.boolean().default(true),
|
||||
supports_stream_options: z.boolean().default(true),
|
||||
supports_developer_role: z.boolean().default(false),
|
||||
supports_service_tier: z.boolean().default(false),
|
||||
supports_thinking_control: z.boolean().default(false),
|
||||
supports_api_version: z.boolean().default(false),
|
||||
supports_parallel_tools: z.boolean().default(false),
|
||||
supports_multimodal: z.boolean().default(false),
|
||||
max_file_upload_size: z.number().optional(), // bytes
|
||||
supported_file_types: z.array(z.string()).optional()
|
||||
})
|
||||
|
||||
// Behavior characteristics configuration - replaces categorization, describes actual behavior
|
||||
export const ProviderBehaviorsSchema = z.object({
|
||||
// Model management
|
||||
supports_custom_models: z.boolean().default(false), // Supports user custom models
|
||||
provides_model_mapping: z.boolean().default(false), // Provides model name mapping
|
||||
supports_model_versioning: z.boolean().default(false), // Supports model version control
|
||||
|
||||
// Reliability and fault tolerance
|
||||
provides_fallback_routing: z.boolean().default(false), // Provides fallback routing
|
||||
has_auto_retry: z.boolean().default(false), // Has automatic retry mechanism
|
||||
supports_health_check: z.boolean().default(false), // Supports health checks
|
||||
|
||||
// Monitoring and metrics
|
||||
has_real_time_metrics: z.boolean().default(false), // Has real-time metrics
|
||||
provides_usage_analytics: z.boolean().default(false), // Provides usage analytics
|
||||
supports_webhook_events: z.boolean().default(false), // Supports webhook events
|
||||
|
||||
// Configuration and management
|
||||
requires_api_key_validation: z.boolean().default(true), // Requires API key validation
|
||||
supports_rate_limiting: z.boolean().default(false), // Supports rate limiting
|
||||
provides_usage_limits: z.boolean().default(false), // Provides usage limit configuration
|
||||
|
||||
// Advanced features
|
||||
supports_streaming: z.boolean().default(true), // Supports streaming responses
|
||||
supports_batch_processing: z.boolean().default(false), // Supports batch processing
|
||||
supports_model_fine_tuning: z.boolean().default(false) // Provides model fine-tuning
|
||||
})
|
||||
|
||||
// Provider configuration schema
|
||||
export const ProviderConfigSchema = z.object({
|
||||
// Basic information
|
||||
id: ProviderIdSchema,
|
||||
name: z.string(),
|
||||
description: z.string().optional(),
|
||||
|
||||
// Behavior-related configuration
|
||||
authentication: AuthenticationSchema,
|
||||
pricing_model: PricingModelSchema,
|
||||
model_routing: ModelRoutingSchema,
|
||||
behaviors: ProviderBehaviorsSchema,
|
||||
|
||||
// Feature support
|
||||
supported_endpoints: z.array(EndpointTypeSchema),
|
||||
mcp_support: McpSupportSchema.optional(),
|
||||
api_compatibility: ApiCompatibilitySchema.optional(),
|
||||
|
||||
// Default configuration
|
||||
default_api_host: z.string().optional(),
|
||||
default_rate_limit: z.number().optional(), // requests per minute
|
||||
|
||||
// Model matching assistance
|
||||
model_id_patterns: z.array(z.string()).optional(),
|
||||
alias_model_ids: z.record(z.string(), z.string()).optional(), // Model alias mapping
|
||||
|
||||
// Special configuration
|
||||
special_config: MetadataSchema,
|
||||
|
||||
// Metadata and links
|
||||
documentation: z.string().url().optional(),
|
||||
status_page: z.string().url().optional(),
|
||||
pricing_page: z.string().url().optional(),
|
||||
support_email: z.string().email().optional(),
|
||||
website: z.string().url().optional(),
|
||||
|
||||
// Status management
|
||||
deprecated: z.boolean().default(false),
|
||||
deprecation_date: z.iso.datetime().optional(),
|
||||
maintenance_mode: z.boolean().default(false),
|
||||
|
||||
// Version and compatibility
|
||||
min_app_version: VersionSchema.optional(), // Minimum supported app version
|
||||
max_app_version: VersionSchema.optional(), // Maximum supported app version
|
||||
config_version: VersionSchema.default('1.0.0'), // Configuration file version
|
||||
|
||||
// Additional metadata
|
||||
metadata: MetadataSchema
|
||||
})
|
||||
|
||||
// Provider list container schema for JSON files
|
||||
export const ProviderListSchema = z.object({
|
||||
version: VersionSchema,
|
||||
providers: z.array(ProviderConfigSchema)
|
||||
})
|
||||
|
||||
// Type exports
|
||||
export type EndpointType = z.infer<typeof EndpointTypeSchema>
|
||||
export type Authentication = z.infer<typeof AuthenticationSchema>
|
||||
export type PricingModel = z.infer<typeof PricingModelSchema>
|
||||
export type ModelRouting = z.infer<typeof ModelRoutingSchema>
|
||||
export type McpSupport = z.infer<typeof McpSupportSchema>
|
||||
export type ApiCompatibility = z.infer<typeof ApiCompatibilitySchema>
|
||||
export type ProviderBehaviors = z.infer<typeof ProviderBehaviorsSchema>
|
||||
export type ProviderConfig = z.infer<typeof ProviderConfigSchema>
|
||||
export type ProviderList = z.infer<typeof ProviderListSchema>
|
||||
2
packages/catalog/src/utils/json-value/index.ts
Normal file
2
packages/catalog/src/utils/json-value/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { isJSONArray, isJSONObject, isJSONValue } from './is-json'
|
||||
export type { JSONArray, JSONObject, JSONValue } from './json-value'
|
||||
32
packages/catalog/src/utils/json-value/is-json.ts
Normal file
32
packages/catalog/src/utils/json-value/is-json.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// https://github.com/vercel/ai/blob/4c44a5bea002ef0db0e1b86a1e223cd9f4837d62/packages/provider/src/json-value/is-json.ts
|
||||
import type { JSONArray, JSONObject, JSONValue } from './json-value'
|
||||
|
||||
export function isJSONValue(value: unknown): value is JSONValue {
|
||||
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
||||
return true
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.every(isJSONValue)
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
return Object.entries(value).every(
|
||||
([key, val]) => typeof key === 'string' && (val === undefined || isJSONValue(val))
|
||||
)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export function isJSONArray(value: unknown): value is JSONArray {
|
||||
return Array.isArray(value) && value.every(isJSONValue)
|
||||
}
|
||||
|
||||
export function isJSONObject(value: unknown): value is JSONObject {
|
||||
return (
|
||||
value != null &&
|
||||
typeof value === 'object' &&
|
||||
Object.entries(value).every(([key, val]) => typeof key === 'string' && (val === undefined || isJSONValue(val)))
|
||||
)
|
||||
}
|
||||
13
packages/catalog/src/utils/json-value/json-value.ts
Normal file
13
packages/catalog/src/utils/json-value/json-value.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
// https://github.com/vercel/ai/blob/4c44a5bea002ef0db0e1b86a1e223cd9f4837d62/packages/provider/src/json-value/json-value.ts
|
||||
|
||||
/**
|
||||
A JSON value can be a string, number, boolean, object, array, or null.
|
||||
JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
|
||||
*/
|
||||
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray
|
||||
|
||||
export type JSONObject = {
|
||||
[key: string]: JSONValue | undefined
|
||||
}
|
||||
|
||||
export type JSONArray = JSONValue[]
|
||||
543
packages/catalog/src/utils/migration.ts
Normal file
543
packages/catalog/src/utils/migration.ts
Normal file
@@ -0,0 +1,543 @@
|
||||
/**
|
||||
* Migration Tool - Phase 2 Implementation
|
||||
* Migrates existing JSON data to new schema-based catalog system
|
||||
*/
|
||||
|
||||
import * as fs from 'fs/promises'
|
||||
import * as path from 'path'
|
||||
|
||||
interface ProviderEndpointsData {
|
||||
providers: Record<
|
||||
string,
|
||||
{
|
||||
display_name: string
|
||||
endpoints: Record<string, boolean>
|
||||
url: string
|
||||
}
|
||||
>
|
||||
}
|
||||
|
||||
interface ModelPricesData {
|
||||
[modelId: string]: {
|
||||
litellm_provider: string
|
||||
mode: string
|
||||
input_cost_per_token?: number
|
||||
output_cost_per_token?: number
|
||||
input_cost_per_pixel?: number
|
||||
output_cost_per_pixel?: number
|
||||
output_cost_per_image?: number
|
||||
max_input_tokens?: number
|
||||
max_output_tokens?: number
|
||||
max_tokens?: number
|
||||
supports_function_calling?: boolean
|
||||
supports_vision?: boolean
|
||||
supports_parallel_function_calling?: boolean
|
||||
supports_response_schema?: boolean
|
||||
supports_tool_choice?: boolean
|
||||
supports_system_messages?: boolean
|
||||
supports_assistant_prefill?: boolean
|
||||
supports_pdf_input?: boolean
|
||||
supports_prompt_caching?: boolean
|
||||
cache_creation_input_token_cost?: number
|
||||
cache_read_input_token_cost?: number
|
||||
metadata?: {
|
||||
notes?: string
|
||||
}
|
||||
source?: string
|
||||
supported_endpoints?: string[]
|
||||
deprecation_date?: string
|
||||
}
|
||||
}
|
||||
|
||||
interface ModelConfig {
|
||||
id: string
|
||||
name?: string
|
||||
owned_by?: string
|
||||
description?: string
|
||||
capabilities: string[]
|
||||
input_modalities: string[]
|
||||
output_modalities: string[]
|
||||
context_window: number
|
||||
max_output_tokens: number
|
||||
max_input_tokens?: number
|
||||
pricing?: {
|
||||
input: { per_million_tokens: number; currency: string }
|
||||
output: { per_million_tokens: number; currency: string }
|
||||
}
|
||||
parameters?: Record<string, any>
|
||||
endpoint_types?: string[]
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
interface ProviderConfig {
|
||||
id: string
|
||||
name: string
|
||||
description?: string
|
||||
authentication: string
|
||||
supported_endpoints: string[]
|
||||
api_compatibility?: Record<string, boolean>
|
||||
special_config?: Record<string, any>
|
||||
documentation?: string
|
||||
website?: string
|
||||
deprecated: boolean
|
||||
maintenance_mode: boolean
|
||||
config_version: string
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
interface OverrideConfig {
|
||||
provider_id: string
|
||||
model_id: string
|
||||
capabilities?: {
|
||||
add?: string[]
|
||||
remove?: string[]
|
||||
force?: string[]
|
||||
}
|
||||
limits?: {
|
||||
context_window?: number
|
||||
max_output_tokens?: number
|
||||
max_input_tokens?: number
|
||||
}
|
||||
pricing?: {
|
||||
input: { per_million_tokens: number; currency: string }
|
||||
output: { per_million_tokens: number; currency: string }
|
||||
}
|
||||
disabled?: boolean
|
||||
reason?: string
|
||||
last_updated?: string
|
||||
updated_by?: string
|
||||
priority?: number
|
||||
}
|
||||
|
||||
export class MigrationTool {
|
||||
private providerEndpointsData: ProviderEndpointsData
|
||||
private modelPricesData: ModelPricesData
|
||||
|
||||
constructor(
|
||||
private providerEndpointsPath: string,
|
||||
private modelPricesPath: string,
|
||||
private outputDir: string
|
||||
) {
|
||||
// Initialize with empty objects to satisfy TypeScript
|
||||
this.providerEndpointsData = { providers: {} }
|
||||
this.modelPricesData = {}
|
||||
}
|
||||
|
||||
async loadData(): Promise<void> {
|
||||
console.log('📖 Loading existing data...')
|
||||
|
||||
const providerEndpointsContent = await fs.readFile(this.providerEndpointsPath, 'utf-8')
|
||||
this.providerEndpointsData = JSON.parse(providerEndpointsContent)
|
||||
|
||||
const modelPricesContent = await fs.readFile(this.modelPricesPath, 'utf-8')
|
||||
this.modelPricesData = JSON.parse(modelPricesContent)
|
||||
|
||||
console.log(`✅ Loaded ${Object.keys(this.providerEndpointsData.providers).length} providers`)
|
||||
console.log(`✅ Loaded ${Object.keys(this.modelPricesData).length} model configurations`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract base model identifier from provider-specific model ID
|
||||
*/
|
||||
private extractBaseModelId(providerModelId: string): string {
|
||||
// Remove provider prefixes
|
||||
const prefixes = [
|
||||
'azure/',
|
||||
'bedrock/',
|
||||
'openrouter/',
|
||||
'vertex_ai/',
|
||||
'sagemaker/',
|
||||
'watsonx/',
|
||||
'litellm_proxy/',
|
||||
'custom/',
|
||||
'aiml/',
|
||||
'together_ai/',
|
||||
'deepinfra/',
|
||||
'hyperbolic/',
|
||||
'fireworks_ai/',
|
||||
'replicate/',
|
||||
'novita/',
|
||||
'anyscale/',
|
||||
'runpod/',
|
||||
'triton/',
|
||||
'vllm/',
|
||||
'ollama/',
|
||||
'lm_studio/'
|
||||
]
|
||||
|
||||
let baseId = providerModelId
|
||||
for (const prefix of prefixes) {
|
||||
if (baseId.startsWith(prefix)) {
|
||||
baseId = baseId.substring(prefix.length)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Handle AWS Bedrock specific naming
|
||||
if (baseId.includes(':')) {
|
||||
baseId = baseId.split(':')[0]
|
||||
}
|
||||
|
||||
// Handle version suffixes
|
||||
baseId = baseId.replace(/\/v\d+$/, '').replace(/:v\d+$/, '')
|
||||
|
||||
return baseId
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a model is a base model or provider-specific override
|
||||
*/
|
||||
private isBaseModel(modelId: string, provider: string): boolean {
|
||||
const baseId = this.extractBaseModelId(modelId)
|
||||
|
||||
// Official provider models are base models
|
||||
const officialProviders = [
|
||||
'anthropic',
|
||||
'openai',
|
||||
'gemini',
|
||||
'deepseek',
|
||||
'dashscope',
|
||||
'volceengine',
|
||||
'minimax',
|
||||
'moonshotai',
|
||||
'zai',
|
||||
'meta',
|
||||
'mistral',
|
||||
'cohere',
|
||||
'xai'
|
||||
]
|
||||
|
||||
if (officialProviders.includes(provider)) {
|
||||
return modelId === baseId || modelId.startsWith(provider + '/')
|
||||
}
|
||||
|
||||
// Third-party providers selling access to official models are overrides
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert endpoint support to provider capabilities
|
||||
*/
|
||||
private privateConvertEndpointsToCapabilities(endpoints: Record<string, boolean>): string[] {
|
||||
const endpointCapabilityMap: Record<string, string> = {
|
||||
chat_completions: 'CHAT_COMPLETIONS',
|
||||
messages: 'MESSAGES',
|
||||
responses: 'RESPONSES',
|
||||
completions: 'COMPLETIONS',
|
||||
embeddings: 'EMBEDDINGS',
|
||||
image_generations: 'IMAGE_GENERATION',
|
||||
image_edit: 'IMAGE_EDIT',
|
||||
audio_speech: 'AUDIO_GENERATION',
|
||||
audio_transcriptions: 'AUDIO_TRANSCRIPT',
|
||||
rerank: 'RERANK',
|
||||
moderations: 'MODERATIONS',
|
||||
ocr: 'OCR',
|
||||
search: 'WEB_SEARCH'
|
||||
}
|
||||
|
||||
const capabilities: string[] = []
|
||||
for (const [endpoint, supported] of Object.entries(endpoints)) {
|
||||
if (supported && endpointCapabilityMap[endpoint]) {
|
||||
capabilities.push(endpointCapabilityMap[endpoint])
|
||||
}
|
||||
}
|
||||
|
||||
return capabilities
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate provider configurations
|
||||
*/
|
||||
private generateProviderConfigs(): ProviderConfig[] {
|
||||
const providers: ProviderConfig[] = []
|
||||
|
||||
for (const [providerId, providerData] of Object.entries(this.providerEndpointsData.providers)) {
|
||||
const supported_endpoints = this.privateConvertEndpointsToCapabilities(providerData.endpoints)
|
||||
|
||||
const provider: ProviderConfig = {
|
||||
id: providerId,
|
||||
name: providerData.display_name,
|
||||
description: `Provider: ${providerData.display_name}`,
|
||||
authentication: 'API_KEY',
|
||||
supported_endpoints,
|
||||
api_compatibility: {
|
||||
supports_array_content: providerData.endpoints.chat_completions || false,
|
||||
supports_stream_options: providerData.endpoints.chat_completions || false,
|
||||
supports_developer_role: providerId === 'openai',
|
||||
supports_service_tier: providerId === 'openai',
|
||||
supports_thinking_control: false,
|
||||
supports_api_version: providerId === 'openai',
|
||||
supports_parallel_tools: providerData.endpoints.chat_completions || false,
|
||||
supports_multimodal: providerData.endpoints.chat_completions || false
|
||||
},
|
||||
special_config: {},
|
||||
documentation: providerData.url,
|
||||
website: providerData.url,
|
||||
deprecated: false,
|
||||
maintenance_mode: false,
|
||||
config_version: '1.0.0'
|
||||
}
|
||||
|
||||
providers.push(provider)
|
||||
}
|
||||
|
||||
return providers
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate base model configurations
|
||||
*/
|
||||
private generateBaseModels(): ModelConfig[] {
|
||||
const baseModels = new Map<string, ModelConfig>()
|
||||
|
||||
for (const [modelId, modelData] of Object.entries(this.modelPricesData)) {
|
||||
if (modelData.mode !== 'chat') continue // Skip non-chat models for now
|
||||
|
||||
const baseId = this.extractBaseModelId(modelId)
|
||||
const isBase = this.isBaseModel(modelId, modelData.litellm_provider)
|
||||
|
||||
if (!isBase) continue // Only process base models
|
||||
|
||||
// Extract capabilities from model data
|
||||
const capabilities: string[] = []
|
||||
if (modelData.supports_function_calling) capabilities.push('FUNCTION_CALL')
|
||||
if (modelData.supports_vision) capabilities.push('IMAGE_RECOGNITION')
|
||||
if (modelData.supports_response_schema) capabilities.push('STRUCTURED_OUTPUT')
|
||||
if (modelData.supports_pdf_input) capabilities.push('FILE_INPUT')
|
||||
if (modelData.supports_tool_choice) capabilities.push('FUNCTION_CALL')
|
||||
|
||||
// Determine modalities
|
||||
const input_modalities = ['TEXT']
|
||||
const output_modalities = ['TEXT']
|
||||
if (modelData.supports_vision) {
|
||||
input_modalities.push('VISION')
|
||||
}
|
||||
|
||||
// Convert pricing
|
||||
let pricing
|
||||
if (modelData.input_cost_per_token && modelData.output_cost_per_token) {
|
||||
pricing = {
|
||||
input: {
|
||||
per_million_tokens: Math.round(modelData.input_cost_per_token * 1000000 * 1000) / 1000,
|
||||
currency: 'USD'
|
||||
},
|
||||
output: {
|
||||
per_million_tokens: Math.round(modelData.output_cost_per_token * 1000000 * 1000) / 1000,
|
||||
currency: 'USD'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const baseModel: ModelConfig = {
|
||||
id: baseId,
|
||||
name: baseId,
|
||||
owned_by: modelData.litellm_provider,
|
||||
capabilities,
|
||||
input_modalities,
|
||||
output_modalities,
|
||||
context_window: modelData.max_input_tokens || 4096,
|
||||
max_output_tokens: modelData.max_output_tokens || modelData.max_tokens || 2048,
|
||||
max_input_tokens: modelData.max_input_tokens,
|
||||
pricing,
|
||||
parameters: {
|
||||
temperature: { supported: true, min: 0, max: 1, default: 1 },
|
||||
max_tokens: true,
|
||||
system_message: modelData.supports_system_messages || false,
|
||||
top_p: { supported: false }
|
||||
},
|
||||
endpoint_types: ['CHAT_COMPLETIONS'],
|
||||
metadata: {
|
||||
source: 'migration',
|
||||
original_provider: modelData.litellm_provider,
|
||||
supports_caching: !!modelData.supports_prompt_caching
|
||||
}
|
||||
}
|
||||
|
||||
baseModels.set(baseId, baseModel)
|
||||
}
|
||||
|
||||
return Array.from(baseModels.values())
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate override configurations
|
||||
*/
|
||||
private generateOverrides(): OverrideConfig[] {
|
||||
const overrides: OverrideConfig[] = []
|
||||
|
||||
for (const [modelId, modelData] of Object.entries(this.modelPricesData)) {
|
||||
if (modelData.mode !== 'chat') continue
|
||||
|
||||
const baseId = this.extractBaseModelId(modelId)
|
||||
const isBase = this.isBaseModel(modelId, modelData.litellm_provider)
|
||||
|
||||
if (isBase) continue // Only generate overrides for non-base models
|
||||
|
||||
const override: OverrideConfig = {
|
||||
provider_id: modelData.litellm_provider,
|
||||
model_id: baseId,
|
||||
disabled: false,
|
||||
reason: `Provider-specific implementation of ${baseId}`,
|
||||
last_updated: new Date().toISOString().split('T')[0],
|
||||
updated_by: 'migration-tool',
|
||||
priority: 100
|
||||
}
|
||||
|
||||
// Add capability differences
|
||||
const capabilities = modelData.supports_function_calling ? ['FUNCTION_CALL'] : []
|
||||
if (modelData.supports_vision) capabilities.push('IMAGE_RECOGNITION')
|
||||
|
||||
if (capabilities.length > 0) {
|
||||
override.capabilities = { add: capabilities }
|
||||
}
|
||||
|
||||
// Add limit differences
|
||||
const limits: any = {}
|
||||
if (modelData.max_input_tokens && modelData.max_input_tokens !== 128000) {
|
||||
limits.context_window = modelData.max_input_tokens
|
||||
}
|
||||
if (modelData.max_output_tokens && modelData.max_output_tokens !== 4096) {
|
||||
limits.max_output_tokens = modelData.max_output_tokens
|
||||
}
|
||||
|
||||
if (Object.keys(limits).length > 0) {
|
||||
override.limits = limits
|
||||
}
|
||||
|
||||
// Add pricing differences
|
||||
if (modelData.input_cost_per_token && modelData.output_cost_per_token) {
|
||||
override.pricing = {
|
||||
input: {
|
||||
per_million_tokens: Math.round(modelData.input_cost_per_token * 1000000 * 1000) / 1000,
|
||||
currency: 'USD'
|
||||
},
|
||||
output: {
|
||||
per_million_tokens: Math.round(modelData.output_cost_per_token * 1000000 * 1000) / 1000,
|
||||
currency: 'USD'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
overrides.push(override)
|
||||
}
|
||||
|
||||
return overrides
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the full migration
|
||||
*/
|
||||
async migrate(): Promise<void> {
|
||||
console.log('🚀 Starting Phase 2 Migration...')
|
||||
|
||||
await this.loadData()
|
||||
|
||||
// Create output directory
|
||||
await fs.mkdir(this.outputDir, { recursive: true })
|
||||
|
||||
// Generate configurations
|
||||
console.log('📦 Generating provider configurations...')
|
||||
const providers = this.generateProviderConfigs()
|
||||
|
||||
console.log('📦 Generating base model configurations...')
|
||||
const models = this.generateBaseModels()
|
||||
|
||||
console.log('📦 Generating override configurations...')
|
||||
const overrides = this.generateOverrides()
|
||||
|
||||
// Write single file for all providers
|
||||
console.log('💾 Writing providers.json...')
|
||||
await this.writeJsonFile('providers.json', {
|
||||
version: '2025.11.24',
|
||||
providers
|
||||
})
|
||||
|
||||
// Write single file for all models
|
||||
console.log('💾 Writing models.json...')
|
||||
await this.writeJsonFile('models.json', {
|
||||
version: '2025.11.24',
|
||||
models
|
||||
})
|
||||
|
||||
// Write single file for all overrides
|
||||
console.log('💾 Writing overrides.json...')
|
||||
await this.writeJsonFile('overrides.json', {
|
||||
version: '2025.11.24',
|
||||
overrides
|
||||
})
|
||||
|
||||
// Generate migration report
|
||||
const providersByType = {
|
||||
direct: providers.filter((p) => ['anthropic', 'openai', 'google'].includes(p.id)).length,
|
||||
cloud: providers.filter((p) => ['azure', 'bedrock', 'vertex_ai'].some((c) => p.id.includes(c))).length,
|
||||
proxy: providers.filter((p) => ['openrouter', 'litellm_proxy', 'together_ai'].some((c) => p.id.includes(c)))
|
||||
.length,
|
||||
self_hosted: providers.filter((p) => ['ollama', 'lm_studio', 'vllm'].some((c) => p.id.includes(c))).length
|
||||
}
|
||||
|
||||
const modelsByProvider = models.reduce(
|
||||
(acc, model) => {
|
||||
const provider = model.owned_by || 'unknown'
|
||||
acc[provider] = (acc[provider] || 0) + 1
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, number>
|
||||
)
|
||||
|
||||
const overridesByProvider = overrides.reduce(
|
||||
(acc, override) => {
|
||||
acc[override.provider_id] = (acc[override.provider_id] || 0) + 1
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, number>
|
||||
)
|
||||
|
||||
const report = {
|
||||
timestamp: new Date().toISOString(),
|
||||
summary: {
|
||||
total_providers: providers.length,
|
||||
total_base_models: models.length,
|
||||
total_overrides: overrides.length,
|
||||
provider_categories: providersByType,
|
||||
models_by_provider: modelsByProvider,
|
||||
overrides_by_provider: overridesByProvider
|
||||
},
|
||||
files: {
|
||||
providers: 'providers.json',
|
||||
models: 'models.json',
|
||||
overrides: 'overrides.json'
|
||||
}
|
||||
}
|
||||
|
||||
await this.writeJsonFile('migration-report.json', report)
|
||||
|
||||
console.log('\n✅ Migration completed successfully!')
|
||||
console.log(`📊 Migration Summary:`)
|
||||
console.log(
|
||||
` Providers: ${providers.length} (${providersByType.direct} direct, ${providersByType.cloud} cloud, ${providersByType.proxy} proxy, ${providersByType.self_hosted} self-hosted)`
|
||||
)
|
||||
console.log(` Base Models: ${models.length}`)
|
||||
console.log(` Overrides: ${overrides.length}`)
|
||||
console.log(`\n📁 Output Files:`)
|
||||
console.log(` ${this.outputDir}/providers.json`)
|
||||
console.log(` ${this.outputDir}/models.json`)
|
||||
console.log(` ${this.outputDir}/overrides.json`)
|
||||
console.log(` ${this.outputDir}/migration-report.json`)
|
||||
}
|
||||
|
||||
private async writeJsonFile(filename: string, data: any): Promise<void> {
|
||||
const filePath = path.join(this.outputDir, filename)
|
||||
await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8')
|
||||
}
|
||||
}
|
||||
|
||||
// CLI execution
|
||||
if (require.main === module) {
|
||||
const tool = new MigrationTool(
|
||||
'./provider_endpoints_support.json',
|
||||
'./model_prices_and_context_window.json',
|
||||
'./migrated-data'
|
||||
)
|
||||
|
||||
tool.migrate().catch(console.error)
|
||||
}
|
||||
88
packages/catalog/src/utils/parse-json/parse-json.ts
Normal file
88
packages/catalog/src/utils/parse-json/parse-json.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
// https://github.com/vercel/ai/blob/6306603220f9f023fcdbeb9768d1c3fc2ca6bc80/packages/provider-utils/src/parse-json.ts
|
||||
import type { JSONValue } from '../json-value'
|
||||
import type { Schema } from '../schema'
|
||||
import { safeValidateTypes, validateTypes } from '../validate-type'
|
||||
import { secureJsonParse } from './secure-json-parse'
|
||||
|
||||
/**
|
||||
* Parses a JSON string into an unknown object.
|
||||
*
|
||||
* @param text - The JSON string to parse.
|
||||
* @returns {JSONValue} - The parsed JSON object.
|
||||
*/
|
||||
export async function parseJSON(options: { text: string; schema?: undefined }): Promise<JSONValue>
|
||||
/**
|
||||
* Parses a JSON string into a strongly-typed object using the provided schema.
|
||||
*
|
||||
* @template T - The type of the object to parse the JSON into.
|
||||
* @param {string} text - The JSON string to parse.
|
||||
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
||||
* @returns {Promise<T>} - The parsed object.
|
||||
*/
|
||||
export async function parseJSON<T>(options: { text: string; schema: Schema<T> }): Promise<T>
|
||||
export async function parseJSON<T>({ text, schema }: { text: string; schema?: Schema<T> }): Promise<T> {
|
||||
const value = secureJsonParse(text)
|
||||
|
||||
if (schema == null) {
|
||||
return value
|
||||
}
|
||||
|
||||
return validateTypes<T>({ value, schema })
|
||||
}
|
||||
|
||||
export type ParseResult<T> =
|
||||
| { success: true; value: T; rawValue: unknown }
|
||||
| {
|
||||
success: false
|
||||
error: Error
|
||||
rawValue: unknown
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely parses a JSON string and returns the result as an object of type `unknown`.
|
||||
*
|
||||
* @param text - The JSON string to parse.
|
||||
* @returns {Promise<object>} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
|
||||
*/
|
||||
export async function safeParseJSON(options: { text: string; schema?: undefined }): Promise<ParseResult<JSONValue>>
|
||||
/**
|
||||
* Safely parses a JSON string into a strongly-typed object, using a provided schema to validate the object.
|
||||
*
|
||||
* @template T - The type of the object to parse the JSON into.
|
||||
* @param {string} text - The JSON string to parse.
|
||||
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
||||
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
||||
*/
|
||||
export async function safeParseJSON<T>(options: { text: string; schema: Schema<T> }): Promise<ParseResult<T>>
|
||||
export async function safeParseJSON<T>({
|
||||
text,
|
||||
schema
|
||||
}: {
|
||||
text: string
|
||||
schema?: Schema<T>
|
||||
}): Promise<ParseResult<T>> {
|
||||
try {
|
||||
const value = secureJsonParse(text)
|
||||
|
||||
if (schema == null) {
|
||||
return { success: true, value: value as T, rawValue: value }
|
||||
}
|
||||
|
||||
return await safeValidateTypes<T>({ value, schema })
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error : new Error('Unknown parsing error'),
|
||||
rawValue: undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isParsableJson(input: string): boolean {
|
||||
try {
|
||||
secureJsonParse(input)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
90
packages/catalog/src/utils/parse-json/secure-json-parse.ts
Normal file
90
packages/catalog/src/utils/parse-json/secure-json-parse.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
// https://github.com/vercel/ai/blob/32d8dbbebdb7831467c702094cc903cf93ee15ef/packages/provider-utils/src/secure-json-parse.ts
|
||||
// Licensed under BSD-3-Clause (this file only)
|
||||
// Code adapted from https://github.com/fastify/secure-json-parse/blob/783fcb1b5434709466759847cec974381939673a/index.js
|
||||
//
|
||||
// Copyright (c) Vercel, Inc. (https://vercel.com)
|
||||
// Copyright (c) 2019 The Fastify Team
|
||||
// Copyright (c) 2019, Sideway Inc, and project contributors
|
||||
// All rights reserved.
|
||||
//
|
||||
// The complete list of contributors can be found at:
|
||||
// - https://github.com/hapijs/bourne/graphs/contributors
|
||||
// - https://github.com/fastify/secure-json-parse/graphs/contributors
|
||||
// - https://github.com/vercel/ai/commits/main/packages/provider-utils/src/secure-parse-json.ts
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
const suspectProtoRx = /"__proto__"\s*:/
|
||||
const suspectConstructorRx = /"constructor"\s*:/
|
||||
|
||||
function _parse(text: string) {
|
||||
// Parse normally
|
||||
const obj = JSON.parse(text)
|
||||
|
||||
// Ignore null and non-objects
|
||||
if (obj === null || typeof obj !== 'object') {
|
||||
return obj
|
||||
}
|
||||
|
||||
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
||||
return obj
|
||||
}
|
||||
|
||||
// Scan result for proto keys
|
||||
return filter(obj)
|
||||
}
|
||||
|
||||
function filter(obj: any) {
|
||||
let next = [obj]
|
||||
|
||||
while (next.length) {
|
||||
const nodes = next
|
||||
next = []
|
||||
|
||||
for (const node of nodes) {
|
||||
if (Object.prototype.hasOwnProperty.call(node, '__proto__')) {
|
||||
throw new SyntaxError('Object contains forbidden prototype property')
|
||||
}
|
||||
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(node, 'constructor') &&
|
||||
Object.prototype.hasOwnProperty.call(node.constructor, 'prototype')
|
||||
) {
|
||||
throw new SyntaxError('Object contains forbidden prototype property')
|
||||
}
|
||||
|
||||
for (const key in node) {
|
||||
const value = node[key]
|
||||
if (value && typeof value === 'object') {
|
||||
next.push(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
export function secureJsonParse(text: string) {
|
||||
const { stackTraceLimit } = Error
|
||||
try {
|
||||
// Performance optimization, see https://github.com/fastify/secure-json-parse/pull/90
|
||||
Error.stackTraceLimit = 0
|
||||
} catch (e) {
|
||||
// Fallback in case Error is immutable (v8 readonly)
|
||||
return _parse(text)
|
||||
}
|
||||
|
||||
try {
|
||||
return _parse(text)
|
||||
} finally {
|
||||
Error.stackTraceLimit = stackTraceLimit
|
||||
}
|
||||
}
|
||||
92
packages/catalog/src/utils/schema.ts
Normal file
92
packages/catalog/src/utils/schema.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
// https://github.com/vercel/ai/blob/6306603220f9f023fcdbeb9768d1c3fc2ca6bc80/packages/provider-utils/src/schema.ts
|
||||
import type { JSONSchema7 } from 'json-schema'
|
||||
import * as z4 from 'zod/v4'
|
||||
|
||||
export type ValidationResult<OBJECT> = { success: true; value: OBJECT } | { success: false; error: Error }
|
||||
|
||||
const schemaSymbol = Symbol.for('schema')
|
||||
|
||||
export type Schema<OBJECT = unknown> = {
|
||||
/**
|
||||
* Used to mark schemas so we can support both Zod and custom schemas.
|
||||
*/
|
||||
[schemaSymbol]: true
|
||||
|
||||
/**
|
||||
* Schema type for inference.
|
||||
*/
|
||||
_type: OBJECT
|
||||
|
||||
/**
|
||||
* Optional. Validates that the structure of a value matches this schema,
|
||||
* and returns a typed version of the value if it does.
|
||||
*/
|
||||
readonly validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>
|
||||
|
||||
/**
|
||||
* The JSON Schema for the schema.
|
||||
*/
|
||||
readonly jsonSchema: JSONSchema7 | PromiseLike<JSONSchema7>
|
||||
}
|
||||
|
||||
export function asSchema<OBJECT>(schema: Schema<OBJECT> | undefined): Schema<OBJECT> {
|
||||
return schema == null
|
||||
? jsonSchema({
|
||||
properties: {},
|
||||
additionalProperties: false
|
||||
})
|
||||
: schema
|
||||
}
|
||||
|
||||
export function jsonSchema<OBJECT = unknown>(
|
||||
jsonSchema: JSONSchema7 | PromiseLike<JSONSchema7> | (() => JSONSchema7 | PromiseLike<JSONSchema7>),
|
||||
{
|
||||
validate
|
||||
}: {
|
||||
validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>
|
||||
} = {}
|
||||
): Schema<OBJECT> {
|
||||
return {
|
||||
[schemaSymbol]: true,
|
||||
_type: undefined as OBJECT, // should never be used directly
|
||||
get jsonSchema() {
|
||||
if (typeof jsonSchema === 'function') {
|
||||
jsonSchema = jsonSchema() // cache the function results
|
||||
}
|
||||
return jsonSchema
|
||||
},
|
||||
validate
|
||||
}
|
||||
}
|
||||
|
||||
export function zod4Schema<OBJECT>(
|
||||
zodSchema: z4.core.$ZodType<OBJECT, any>,
|
||||
options?: {
|
||||
/**
|
||||
* Enables support for references in the schema.
|
||||
* This is required for recursive schemas, e.g. with `z.lazy`.
|
||||
* However, not all language models and providers support such references.
|
||||
* Defaults to `false`.
|
||||
*/
|
||||
useReferences?: boolean
|
||||
}
|
||||
): Schema<OBJECT> {
|
||||
// default to no references (to support openapi conversion for google)
|
||||
const useReferences = options?.useReferences ?? false
|
||||
|
||||
return jsonSchema(
|
||||
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
||||
() =>
|
||||
z4.toJSONSchema(zodSchema, {
|
||||
target: 'draft-7',
|
||||
io: 'output',
|
||||
reused: useReferences ? 'ref' : 'inline'
|
||||
}) as JSONSchema7,
|
||||
{
|
||||
validate: async (value) => {
|
||||
const result = await z4.safeParseAsync(zodSchema, value)
|
||||
return result.success ? { success: true, value: result.data } : { success: false, error: result.error }
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
75
packages/catalog/src/utils/validate-type.ts
Normal file
75
packages/catalog/src/utils/validate-type.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
// https://github.com/vercel/ai/blob/6306603220f9f023fcdbeb9768d1c3fc2ca6bc80/packages/provider-utils/src/validate-types.ts
|
||||
import { asSchema, type Schema } from './schema'
|
||||
|
||||
/**
|
||||
* Validates the types of an unknown object using a schema and
|
||||
* return a strongly-typed object.
|
||||
*
|
||||
* @template T - The type of the object to validate.
|
||||
* @param {string} options.value - The object to validate.
|
||||
* @param {Validator<T>} options.schema - The schema to use for validating the JSON.
|
||||
* @returns {Promise<T>} - The typed object.
|
||||
*/
|
||||
export async function validateTypes<OBJECT>({
|
||||
value,
|
||||
schema
|
||||
}: {
|
||||
value: unknown
|
||||
schema: Schema<OBJECT>
|
||||
}): Promise<OBJECT> {
|
||||
const result = await safeValidateTypes({ value, schema })
|
||||
|
||||
if (!result.success) {
|
||||
throw Error(`Validation failed: ${result.error.message}`)
|
||||
}
|
||||
|
||||
return result.value
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely validates the types of an unknown object using a schema and
|
||||
* return a strongly-typed object.
|
||||
*
|
||||
* @template T - The type of the object to validate.
|
||||
* @param {string} options.value - The JSON object to validate.
|
||||
* @param {Validator<T>} options.schema - The schema to use for validating the JSON.
|
||||
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
||||
*/
|
||||
export async function safeValidateTypes<OBJECT>({ value, schema }: { value: unknown; schema: Schema<OBJECT> }): Promise<
|
||||
| {
|
||||
success: true
|
||||
value: OBJECT
|
||||
rawValue: unknown
|
||||
}
|
||||
| {
|
||||
success: false
|
||||
error: Error
|
||||
rawValue: unknown
|
||||
}
|
||||
> {
|
||||
const actualSchema = asSchema(schema)
|
||||
|
||||
try {
|
||||
if (actualSchema.validate == null) {
|
||||
return { success: true, value: value as OBJECT, rawValue: value }
|
||||
}
|
||||
|
||||
const result = await actualSchema.validate(value)
|
||||
|
||||
if (result.success) {
|
||||
return { success: true, value: result.value, rawValue: value }
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: Error(`Validation failed: ${result.error.message}`),
|
||||
rawValue: value
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error : new Error('Unknown validation error'),
|
||||
rawValue: value
|
||||
}
|
||||
}
|
||||
}
|
||||
299
packages/catalog/src/validator/SchemaValidator.ts
Normal file
299
packages/catalog/src/validator/SchemaValidator.ts
Normal file
@@ -0,0 +1,299 @@
|
||||
/**
|
||||
* Schema Validator
|
||||
* Provides validation functionality for all configuration schemas
|
||||
*/
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
import { ModelConfigSchema, OverrideListSchema, ProviderConfigSchema } from '../schemas'
|
||||
import { zod4Schema } from '../utils/schema'
|
||||
import { safeValidateTypes } from '../utils/validate-type'
|
||||
|
||||
export type ModelConfig = z.infer<typeof ModelConfigSchema>
|
||||
export type ProviderConfig = z.infer<typeof ProviderConfigSchema>
|
||||
export type OverrideConfig = z.infer<typeof OverrideListSchema>
|
||||
|
||||
export interface ValidationResult<T = any> {
|
||||
success: boolean
|
||||
data?: T
|
||||
errors?: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[]
|
||||
warnings?: string[]
|
||||
}
|
||||
|
||||
export interface ValidationOptions {
|
||||
strict?: boolean
|
||||
includeWarnings?: boolean
|
||||
customValidation?: (data: any) => string[]
|
||||
}
|
||||
|
||||
export class SchemaValidator {
|
||||
/**
|
||||
* Validate model configuration
|
||||
*/
|
||||
async validateModel(config: any, options: ValidationOptions = {}): Promise<ValidationResult<ModelConfig>> {
|
||||
const { includeWarnings = true, customValidation } = options
|
||||
|
||||
const schema = zod4Schema(ModelConfigSchema)
|
||||
|
||||
const validation = await safeValidateTypes({ value: config, schema })
|
||||
|
||||
if (!validation.success) {
|
||||
return {
|
||||
success: false,
|
||||
errors: [{ code: 'custom' as const, message: validation.error.message, path: [] }]
|
||||
}
|
||||
}
|
||||
|
||||
const model = validation.value
|
||||
|
||||
const warnings: string[] = []
|
||||
|
||||
// Basic warnings
|
||||
if (includeWarnings) {
|
||||
if (!model.pricing) {
|
||||
warnings.push('No pricing information provided')
|
||||
}
|
||||
|
||||
if (!model.description) {
|
||||
warnings.push('No model description provided')
|
||||
}
|
||||
|
||||
if (model.capabilities?.includes('REASONING') && !model.reasoning) {
|
||||
warnings.push('Model has REASONING capability but no reasoning configuration')
|
||||
}
|
||||
|
||||
if (model.contextWindow && model.contextWindow > 128000) {
|
||||
warnings.push('Large context window may impact performance')
|
||||
}
|
||||
|
||||
if (model.capabilities?.length === 0) {
|
||||
warnings.push('No capabilities specified for model')
|
||||
}
|
||||
}
|
||||
|
||||
// Custom validation warnings
|
||||
if (includeWarnings && customValidation) {
|
||||
warnings.push(...customValidation(config))
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: model,
|
||||
warnings: warnings.length > 0 ? warnings : undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate provider configuration
|
||||
*/
|
||||
validateProvider(config: any, options: ValidationOptions = {}): ValidationResult<ProviderConfig> {
|
||||
const { includeWarnings = true, customValidation } = options
|
||||
|
||||
try {
|
||||
const result = ProviderConfigSchema.parse(config)
|
||||
|
||||
const warnings: string[] = []
|
||||
|
||||
if (includeWarnings && customValidation) {
|
||||
warnings.push(...customValidation(config))
|
||||
}
|
||||
|
||||
if (includeWarnings) {
|
||||
if (!config.behaviors.requiresApiKeyValidation) {
|
||||
warnings.push('Provider does not require API key validation - ensure this is intentional')
|
||||
}
|
||||
|
||||
if (config.endpoints.length === 0) {
|
||||
warnings.push('No endpoints defined for provider')
|
||||
}
|
||||
|
||||
if (config.pricingModel === 'UNIFIED' && !config.behaviors.providesModelMapping) {
|
||||
warnings.push('Unified pricing model without model mapping may cause confusion')
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: result,
|
||||
warnings: warnings.length > 0 ? warnings : undefined
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return {
|
||||
success: false,
|
||||
errors: error.issues
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
errors: [{ code: 'custom' as const, message: 'Unknown validation error', path: [] }]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate override configuration
|
||||
*/
|
||||
validateOverride(config: any, options: ValidationOptions = {}): ValidationResult<OverrideConfig> {
|
||||
const { includeWarnings = true, customValidation } = options
|
||||
|
||||
try {
|
||||
const result = OverrideListSchema.parse(config)
|
||||
|
||||
const warnings: string[] = []
|
||||
|
||||
if (includeWarnings && customValidation) {
|
||||
warnings.push(...customValidation(config))
|
||||
}
|
||||
|
||||
if (includeWarnings) {
|
||||
if (result.overrides.some((override) => !override.reason)) {
|
||||
warnings.push('Some overrides lack reason documentation')
|
||||
}
|
||||
|
||||
if (result.overrides.some((override) => override.priority > 1000)) {
|
||||
warnings.push('Very high priority values may indicate configuration issues')
|
||||
}
|
||||
|
||||
// Check for potential conflicts
|
||||
const modelProviderPairs = result.overrides.map((o) => `${o.modelId}:${o.providerId}`)
|
||||
const duplicates = modelProviderPairs.filter((pair, index) => modelProviderPairs.indexOf(pair) !== index)
|
||||
if (duplicates.length > 0) {
|
||||
warnings.push(`Duplicate override entries detected: ${duplicates.join(', ')}`)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: result,
|
||||
warnings: warnings.length > 0 ? warnings : undefined
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return {
|
||||
success: false,
|
||||
errors: error.issues
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
errors: [{ code: 'custom' as const, message: 'Unknown validation error', path: [] }]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate array of configurations
|
||||
*/
|
||||
async validateModelArray(
|
||||
configs: any[],
|
||||
options: ValidationOptions = {}
|
||||
): Promise<{
|
||||
valid: ModelConfig[]
|
||||
invalid: { config: any; errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[] }[]
|
||||
warnings: string[]
|
||||
}> {
|
||||
const valid: ModelConfig[] = []
|
||||
const invalid: {
|
||||
config: any
|
||||
errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[]
|
||||
}[] = []
|
||||
const allWarnings: string[] = []
|
||||
|
||||
configs.forEach(async (config, index) => {
|
||||
const result = await this.validateModel(config, options)
|
||||
|
||||
if (result.success) {
|
||||
valid.push(result.data!)
|
||||
if (result.warnings) {
|
||||
allWarnings.push(...result.warnings.map((w) => `Model ${index}: ${w}`))
|
||||
}
|
||||
} else {
|
||||
invalid.push({ config, errors: result.errors! })
|
||||
}
|
||||
})
|
||||
|
||||
return { valid, invalid, warnings: allWarnings }
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate provider array
|
||||
*/
|
||||
validateProviderArray(
|
||||
configs: any[],
|
||||
options: ValidationOptions = {}
|
||||
): {
|
||||
valid: ProviderConfig[]
|
||||
invalid: { config: any; errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[] }[]
|
||||
warnings: string[]
|
||||
} {
|
||||
const valid: ProviderConfig[] = []
|
||||
const invalid: {
|
||||
config: any
|
||||
errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[]
|
||||
}[] = []
|
||||
const allWarnings: string[] = []
|
||||
|
||||
configs.forEach((config, index) => {
|
||||
const result = this.validateProvider(config, options)
|
||||
|
||||
if (result.success) {
|
||||
valid.push(result.data!)
|
||||
if (result.warnings) {
|
||||
allWarnings.push(...result.warnings.map((w) => `Provider ${index}: ${w}`))
|
||||
}
|
||||
} else {
|
||||
invalid.push({ config, errors: result.errors! })
|
||||
}
|
||||
})
|
||||
|
||||
return { valid, invalid, warnings: allWarnings }
|
||||
}
|
||||
|
||||
/**
|
||||
* Format validation errors for display
|
||||
*/
|
||||
formatErrors(errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[]): string[] {
|
||||
return errors.map((error) => {
|
||||
const path = error.path.length > 0 ? `${error.path.join('.')}: ` : ''
|
||||
return `${path}${error.message}`
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate validation summary
|
||||
*/
|
||||
generateSummary(results: {
|
||||
models: {
|
||||
valid: ModelConfig[]
|
||||
invalid: { config: any; errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[] }[]
|
||||
warnings: string[]
|
||||
}
|
||||
providers: {
|
||||
valid: ProviderConfig[]
|
||||
invalid: { config: any; errors: z.ZodIssue['path'] extends (string | number)[] ? z.ZodIssue : z.ZodIssue[] }[]
|
||||
warnings: string[]
|
||||
}
|
||||
overrides: ValidationResult<OverrideConfig>
|
||||
}): {
|
||||
totalModels: number
|
||||
validModels: number
|
||||
totalProviders: number
|
||||
validProviders: number
|
||||
overridesValid: boolean
|
||||
allWarnings: string[]
|
||||
} {
|
||||
const { models, providers, overrides } = results
|
||||
|
||||
return {
|
||||
totalModels: models.valid.length + models.invalid.length,
|
||||
validModels: models.valid.length,
|
||||
totalProviders: providers.valid.length + providers.invalid.length,
|
||||
validProviders: providers.valid.length,
|
||||
overridesValid: overrides.success || false,
|
||||
allWarnings: [...models.warnings, ...providers.warnings, ...(overrides.warnings || [])]
|
||||
}
|
||||
}
|
||||
}
|
||||
21
packages/catalog/tsconfig.json
Normal file
21
packages/catalog/tsconfig.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"declaration": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"noEmitOnError": false,
|
||||
"outDir": "./dist",
|
||||
"resolveJsonModule": true,
|
||||
"rootDir": "./src",
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"target": "ES2020"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src/**/*", "scripts"]
|
||||
}
|
||||
12
packages/catalog/tsdown.config.ts
Normal file
12
packages/catalog/tsdown.config.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { defineConfig } from 'tsdown'
|
||||
|
||||
export default defineConfig({
|
||||
entry: {
|
||||
index: 'src/index.ts'
|
||||
},
|
||||
outDir: 'dist',
|
||||
format: ['esm', 'cjs'],
|
||||
clean: true,
|
||||
dts: true,
|
||||
tsconfig: 'tsconfig.json'
|
||||
})
|
||||
0
packages/catalog/vitest.config.ts
Normal file
0
packages/catalog/vitest.config.ts
Normal file
41
packages/catalog/web/.gitignore
vendored
Normal file
41
packages/catalog/web/.gitignore
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.*
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/versions
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
/.next/
|
||||
/out/
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# env files (can opt-in for committing if needed)
|
||||
.env*
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
36
packages/catalog/web/README.md
Normal file
36
packages/catalog/web/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
||||
|
||||
## Getting Started
|
||||
|
||||
First, run the development server:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
# or
|
||||
yarn dev
|
||||
# or
|
||||
pnpm dev
|
||||
# or
|
||||
bun dev
|
||||
```
|
||||
|
||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||
|
||||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
||||
|
||||
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||
|
||||
## Learn More
|
||||
|
||||
To learn more about Next.js, take a look at the following resources:
|
||||
|
||||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
||||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
||||
|
||||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
||||
|
||||
## Deploy on Vercel
|
||||
|
||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||
|
||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
||||
@@ -0,0 +1,285 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
import { z } from 'zod'
|
||||
|
||||
import type { Model, ProviderModelOverride, OverridesDataFile } from '@/lib/catalog-types'
|
||||
import {
|
||||
ModelSchema,
|
||||
ModelsDataFileSchema,
|
||||
ProvidersDataFileSchema,
|
||||
OverridesDataFileSchema
|
||||
} from '@/lib/catalog-types'
|
||||
import { safeParseWithValidation, validateString, ValidationError, createErrorResponse } from '@/lib/validation'
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
// Type-safe helper function to apply overrides to base model
|
||||
function applyOverrides(baseModel: Model, override: ProviderModelOverride | null): Model {
|
||||
if (!override) return baseModel
|
||||
|
||||
return {
|
||||
...baseModel,
|
||||
...(override.limits && {
|
||||
context_window: override.limits.context_window ?? baseModel.context_window,
|
||||
max_output_tokens: override.limits.max_output_tokens ?? baseModel.max_output_tokens
|
||||
}),
|
||||
...(override.pricing && { pricing: override.pricing })
|
||||
}
|
||||
}
|
||||
|
||||
// Type-safe helper function to detect model modifications
|
||||
function detectModifications(
|
||||
baseModel: Model,
|
||||
updatedModel: Partial<Model>
|
||||
): {
|
||||
pricing: Model['pricing'] | undefined
|
||||
limits:
|
||||
| {
|
||||
context_window?: number
|
||||
max_output_tokens?: number
|
||||
}
|
||||
| undefined
|
||||
} | null {
|
||||
const modifications: {
|
||||
pricing: Model['pricing'] | undefined
|
||||
limits:
|
||||
| {
|
||||
context_window?: number
|
||||
max_output_tokens?: number
|
||||
}
|
||||
| undefined
|
||||
} = {
|
||||
pricing: undefined,
|
||||
limits: undefined
|
||||
}
|
||||
|
||||
// Check for differences in pricing
|
||||
if (JSON.stringify(baseModel.pricing) !== JSON.stringify(updatedModel.pricing)) {
|
||||
modifications.pricing = updatedModel.pricing
|
||||
}
|
||||
|
||||
// Check for differences in limits
|
||||
if (
|
||||
baseModel.context_window !== updatedModel.context_window ||
|
||||
baseModel.max_output_tokens !== updatedModel.max_output_tokens
|
||||
) {
|
||||
modifications.limits = {}
|
||||
if (baseModel.context_window !== updatedModel.context_window) {
|
||||
modifications.limits.context_window = updatedModel.context_window
|
||||
}
|
||||
if (baseModel.max_output_tokens !== updatedModel.max_output_tokens) {
|
||||
modifications.limits.max_output_tokens = updatedModel.max_output_tokens
|
||||
}
|
||||
}
|
||||
|
||||
return modifications.pricing || modifications.limits ? modifications : null
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: { modelId: string; providerId: string } }) {
|
||||
try {
|
||||
const { modelId, providerId } = params
|
||||
|
||||
// Validate parameters
|
||||
const validModelId = validateString(modelId, 'modelId')
|
||||
const validProviderId = validateString(providerId, 'providerId')
|
||||
|
||||
// Read and validate all data files
|
||||
const [modelsDataRaw, providersDataRaw, overridesDataRaw] = await Promise.all([
|
||||
fs.readFile(path.join(DATA_DIR, 'models.json'), 'utf-8'),
|
||||
fs.readFile(path.join(DATA_DIR, 'providers.json'), 'utf-8'),
|
||||
fs.readFile(path.join(DATA_DIR, 'overrides.json'), 'utf-8')
|
||||
])
|
||||
|
||||
const modelsData = await safeParseWithValidation(
|
||||
modelsDataRaw,
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in file'
|
||||
)
|
||||
const providersData = await safeParseWithValidation(
|
||||
providersDataRaw,
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in file'
|
||||
)
|
||||
const overridesData = await safeParseWithValidation(
|
||||
overridesDataRaw,
|
||||
OverridesDataFileSchema,
|
||||
'Invalid overrides data format in file'
|
||||
)
|
||||
|
||||
// Find base model
|
||||
const baseModel = modelsData.models.find((m) => m.id === validModelId)
|
||||
if (!baseModel) {
|
||||
return NextResponse.json(createErrorResponse('Model not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
// Find provider override for this model
|
||||
const override = overridesData.overrides.find(
|
||||
(o) => o.model_id === validModelId && o.provider_id === validProviderId
|
||||
)
|
||||
|
||||
// Apply override if exists
|
||||
const finalModel = applyOverrides(baseModel, override || null)
|
||||
|
||||
return NextResponse.json(ModelSchema.parse(finalModel))
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error fetching provider model:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse(
|
||||
'Failed to fetch model configuration',
|
||||
500,
|
||||
error instanceof Error ? error.message : 'Unknown error'
|
||||
),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Response schema for provider model updates
|
||||
const ProviderModelUpdateResponseSchema = z.object({
|
||||
updated: z.enum(['base_model', 'override', 'override_updated', 'override_removed']),
|
||||
model: ModelSchema
|
||||
})
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { modelId: string; providerId: string } }) {
|
||||
try {
|
||||
const { modelId, providerId } = params
|
||||
|
||||
// Validate parameters
|
||||
const validModelId = validateString(modelId, 'modelId')
|
||||
const validProviderId = validateString(providerId, 'providerId')
|
||||
|
||||
// Validate request body
|
||||
const requestBody = await request.json()
|
||||
const updatedModel = await safeParseWithValidation(
|
||||
JSON.stringify(requestBody),
|
||||
ModelSchema.partial(),
|
||||
'Invalid model data in request body'
|
||||
)
|
||||
|
||||
// Read and validate current data
|
||||
const [modelsDataRaw, providersDataRaw, overridesDataRaw] = await Promise.all([
|
||||
fs.readFile(path.join(DATA_DIR, 'models.json'), 'utf-8'),
|
||||
fs.readFile(path.join(DATA_DIR, 'providers.json'), 'utf-8'),
|
||||
fs.readFile(path.join(DATA_DIR, 'overrides.json'), 'utf-8')
|
||||
])
|
||||
|
||||
const modelsData = await safeParseWithValidation(
|
||||
modelsDataRaw,
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in file'
|
||||
)
|
||||
const providersData = await safeParseWithValidation(
|
||||
providersDataRaw,
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in file'
|
||||
)
|
||||
const overridesData = await safeParseWithValidation(
|
||||
overridesDataRaw,
|
||||
OverridesDataFileSchema,
|
||||
'Invalid overrides data format in file'
|
||||
)
|
||||
|
||||
// Find base model and existing override
|
||||
const baseModelIndex = modelsData.models.findIndex((m) => m.id === validModelId)
|
||||
const existingOverrideIndex = overridesData.overrides.findIndex(
|
||||
(o) => o.model_id === validModelId && o.provider_id === validProviderId
|
||||
)
|
||||
|
||||
if (baseModelIndex === -1) {
|
||||
return NextResponse.json(createErrorResponse('Base model not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
const baseModel = modelsData.models[baseModelIndex]
|
||||
|
||||
// Detect what needs to be overridden
|
||||
const modifications = detectModifications(baseModel, updatedModel)
|
||||
|
||||
let updated: 'base_model' | 'override' | 'override_updated' | 'override_removed' = 'base_model'
|
||||
let overrideCreated = false
|
||||
|
||||
if (modifications) {
|
||||
// Create or update override
|
||||
const override: ProviderModelOverride = {
|
||||
provider_id: validProviderId,
|
||||
model_id: validModelId,
|
||||
disabled: false,
|
||||
reason: 'Manual configuration update',
|
||||
last_updated: new Date().toISOString().split('T')[0],
|
||||
updated_by: 'web-interface',
|
||||
priority: 100,
|
||||
...modifications
|
||||
}
|
||||
|
||||
const updatedOverrides = [...overridesData.overrides]
|
||||
|
||||
if (existingOverrideIndex >= 0) {
|
||||
updatedOverrides[existingOverrideIndex] = {
|
||||
...updatedOverrides[existingOverrideIndex],
|
||||
...override,
|
||||
last_updated: new Date().toISOString().split('T')[0]
|
||||
}
|
||||
} else {
|
||||
updatedOverrides.push(override)
|
||||
overrideCreated = true
|
||||
}
|
||||
|
||||
const updatedOverridesData: OverridesDataFile = {
|
||||
...overridesData,
|
||||
overrides: updatedOverrides
|
||||
}
|
||||
|
||||
updated = overrideCreated ? 'override' : 'override_updated'
|
||||
|
||||
// Save changes to overrides file
|
||||
await fs.writeFile(path.join(DATA_DIR, 'overrides.json'), JSON.stringify(updatedOverridesData, null, 2), 'utf-8')
|
||||
} else if (existingOverrideIndex >= 0) {
|
||||
// Remove override if no differences exist
|
||||
const updatedOverrides = overridesData.overrides.filter((_, index) => index !== existingOverrideIndex)
|
||||
|
||||
const updatedOverridesData: OverridesDataFile = {
|
||||
...overridesData,
|
||||
overrides: updatedOverrides
|
||||
}
|
||||
|
||||
updated = 'override_removed'
|
||||
|
||||
// Save changes to overrides file
|
||||
await fs.writeFile(path.join(DATA_DIR, 'overrides.json'), JSON.stringify(updatedOverridesData, null, 2), 'utf-8')
|
||||
}
|
||||
|
||||
// Return the final model configuration
|
||||
const finalOverride = overridesData.overrides.find(
|
||||
(o) => o.model_id === validModelId && o.provider_id === validProviderId
|
||||
)
|
||||
const finalModel = applyOverrides(baseModel, finalOverride || null)
|
||||
|
||||
const response = ProviderModelUpdateResponseSchema.parse({
|
||||
updated,
|
||||
model: finalModel
|
||||
})
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error updating provider model:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse(
|
||||
'Failed to update model configuration',
|
||||
500,
|
||||
error instanceof Error ? error.message : 'Unknown error'
|
||||
),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
113
packages/catalog/web/app/api/catalog/models/[modelId]/route.ts
Normal file
113
packages/catalog/web/app/api/catalog/models/[modelId]/route.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
|
||||
import type { ModelsDataFile } from '@/lib/catalog-types'
|
||||
import { ModelSchema, ModelsDataFileSchema, ModelUpdateResponseSchema } from '@/lib/catalog-types'
|
||||
import { createErrorResponse, safeParseWithValidation, ValidationError } from '@/lib/validation'
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: { modelId: string } }) {
|
||||
try {
|
||||
const { modelId } = params
|
||||
|
||||
// Read and validate models data using Zod
|
||||
const modelsDataPath = path.join(DATA_DIR, 'models.json')
|
||||
const modelsDataRaw = await fs.readFile(modelsDataPath, 'utf-8')
|
||||
const modelsData = await safeParseWithValidation(
|
||||
modelsDataRaw,
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in file'
|
||||
)
|
||||
|
||||
// Find the model with type safety
|
||||
const model = modelsData.models.find((m) => m.id === modelId)
|
||||
if (!model) {
|
||||
return NextResponse.json(createErrorResponse('Model not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(model)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error fetching model:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to fetch model', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { modelId: string } }) {
|
||||
try {
|
||||
const { modelId } = params
|
||||
|
||||
// Read and validate request body using Zod
|
||||
const requestBody = await request.json()
|
||||
const updatedModel = await safeParseWithValidation(
|
||||
JSON.stringify(requestBody),
|
||||
ModelSchema,
|
||||
'Invalid model data in request body'
|
||||
)
|
||||
|
||||
// Validate that the model ID matches
|
||||
if (updatedModel.id !== modelId) {
|
||||
return NextResponse.json(createErrorResponse('Model ID in request body must match URL parameter', 400), {
|
||||
status: 400
|
||||
})
|
||||
}
|
||||
|
||||
// Read current models data using Zod
|
||||
const modelsDataPath = path.join(DATA_DIR, 'models.json')
|
||||
const modelsDataRaw = await fs.readFile(modelsDataPath, 'utf-8')
|
||||
const modelsData = await safeParseWithValidation(
|
||||
modelsDataRaw,
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in file'
|
||||
)
|
||||
|
||||
// Find and update the model
|
||||
const modelIndex = modelsData.models.findIndex((m) => m.id === modelId)
|
||||
if (modelIndex === -1) {
|
||||
return NextResponse.json(createErrorResponse('Model not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
// Create updated models array (immutability)
|
||||
const updatedModels = [
|
||||
...modelsData.models.slice(0, modelIndex),
|
||||
updatedModel,
|
||||
...modelsData.models.slice(modelIndex + 1)
|
||||
]
|
||||
|
||||
const updatedModelsData: ModelsDataFile = {
|
||||
...modelsData,
|
||||
models: updatedModels
|
||||
}
|
||||
|
||||
// Write back to file
|
||||
await fs.writeFile(modelsDataPath, JSON.stringify(updatedModelsData, null, 2), 'utf-8')
|
||||
|
||||
const response = ModelUpdateResponseSchema.parse({
|
||||
success: true,
|
||||
model: updatedModel
|
||||
})
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error updating model:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to update model', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
156
packages/catalog/web/app/api/catalog/models/route.ts
Normal file
156
packages/catalog/web/app/api/catalog/models/route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
|
||||
import type { Model } from '@/lib/catalog-types'
|
||||
import {
|
||||
ModelSchema,
|
||||
ModelsDataFileSchema
|
||||
} from '@/lib/catalog-types'
|
||||
import {
|
||||
createErrorResponse,
|
||||
safeParseWithValidation,
|
||||
validatePaginatedResponse,
|
||||
validateQueryParams,
|
||||
ValidationError
|
||||
} from '@/lib/validation'
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
function filterModels(
|
||||
models: readonly Model[],
|
||||
search?: string,
|
||||
capabilities?: string[],
|
||||
providers?: string[]
|
||||
): Model[] {
|
||||
let filtered = [...models]
|
||||
|
||||
if (search) {
|
||||
const searchLower = search.toLowerCase()
|
||||
filtered = filtered.filter(
|
||||
(model) =>
|
||||
model.id.toLowerCase().includes(searchLower) ||
|
||||
model.name?.toLowerCase().includes(searchLower) ||
|
||||
model.owned_by?.toLowerCase().includes(searchLower)
|
||||
)
|
||||
}
|
||||
|
||||
if (capabilities && capabilities.length > 0) {
|
||||
filtered = filtered.filter((model) => capabilities.some((cap) => model.capabilities.includes(cap)))
|
||||
}
|
||||
|
||||
if (providers && providers.length > 0) {
|
||||
filtered = filtered.filter((model) => model.owned_by && providers.includes(model.owned_by))
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
||||
|
||||
function paginateItems<T>(
|
||||
items: readonly T[],
|
||||
page: number,
|
||||
limit: number
|
||||
): {
|
||||
items: T[]
|
||||
pagination: {
|
||||
page: number
|
||||
limit: number
|
||||
total: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrev: boolean
|
||||
}
|
||||
} {
|
||||
const total = items.length
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
const offset = (page - 1) * limit
|
||||
const paginatedItems = items.slice(offset, offset + limit)
|
||||
|
||||
return {
|
||||
items: paginatedItems,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
|
||||
// Validate query parameters using Zod
|
||||
const validatedParams = validateQueryParams(searchParams)
|
||||
|
||||
// Read and validate models data using Zod
|
||||
const modelsDataPath = path.join(DATA_DIR, 'models.json')
|
||||
const modelsDataRaw = await fs.readFile(modelsDataPath, 'utf-8')
|
||||
const modelsData = await safeParseWithValidation(
|
||||
modelsDataRaw,
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in file'
|
||||
)
|
||||
|
||||
// Filter models with type safety
|
||||
const filteredModels = filterModels(
|
||||
modelsData.models,
|
||||
validatedParams.search,
|
||||
validatedParams.capabilities,
|
||||
validatedParams.providers
|
||||
)
|
||||
|
||||
// Paginate results
|
||||
const { items, pagination } = paginateItems(filteredModels, validatedParams.page, validatedParams.limit)
|
||||
|
||||
// Create paginated response using Zod schema
|
||||
const response = validatePaginatedResponse({ data: items, pagination }, ModelSchema)
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error fetching models:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to fetch models', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
|
||||
// Validate the data structure using Zod
|
||||
const validatedData = await safeParseWithValidation(
|
||||
JSON.stringify(body),
|
||||
ModelsDataFileSchema,
|
||||
'Invalid models data format in request body'
|
||||
)
|
||||
|
||||
// Write validated data back to file
|
||||
const modelsDataPath = path.join(DATA_DIR, 'models.json')
|
||||
await fs.writeFile(modelsDataPath, JSON.stringify(validatedData, null, 2), 'utf-8')
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error updating models:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to update models', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
|
||||
import type { ProvidersDataFile } from '@/lib/catalog-types'
|
||||
import { ProviderSchema, ProvidersDataFileSchema, ProviderUpdateResponseSchema } from '@/lib/catalog-types'
|
||||
import { createErrorResponse, safeParseWithValidation, ValidationError } from '@/lib/validation'
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: { providerId: string } }) {
|
||||
try {
|
||||
const { providerId } = params
|
||||
|
||||
// Read and validate providers data using Zod
|
||||
const providersDataPath = path.join(DATA_DIR, 'providers.json')
|
||||
const providersDataRaw = await fs.readFile(providersDataPath, 'utf-8')
|
||||
const providersData = await safeParseWithValidation(
|
||||
providersDataRaw,
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in file'
|
||||
)
|
||||
|
||||
// Find the provider with type safety
|
||||
const provider = providersData.providers.find((p) => p.id === providerId)
|
||||
if (!provider) {
|
||||
return NextResponse.json(createErrorResponse('Provider not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(provider)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error fetching provider:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to fetch provider', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { providerId: string } }) {
|
||||
try {
|
||||
const { providerId } = params
|
||||
|
||||
// Read and validate request body using Zod
|
||||
const requestBody = await request.json()
|
||||
const updatedProvider = await safeParseWithValidation(
|
||||
JSON.stringify(requestBody),
|
||||
ProviderSchema,
|
||||
'Invalid provider data in request body'
|
||||
)
|
||||
|
||||
// Validate that the provider ID matches
|
||||
if (updatedProvider.id !== providerId) {
|
||||
return NextResponse.json(createErrorResponse('Provider ID in request body must match URL parameter', 400), {
|
||||
status: 400
|
||||
})
|
||||
}
|
||||
|
||||
// Read current providers data using Zod
|
||||
const providersDataPath = path.join(DATA_DIR, 'providers.json')
|
||||
const providersDataRaw = await fs.readFile(providersDataPath, 'utf-8')
|
||||
const providersData = await safeParseWithValidation(
|
||||
providersDataRaw,
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in file'
|
||||
)
|
||||
|
||||
// Find and update the provider
|
||||
const providerIndex = providersData.providers.findIndex((p) => p.id === providerId)
|
||||
if (providerIndex === -1) {
|
||||
return NextResponse.json(createErrorResponse('Provider not found', 404), { status: 404 })
|
||||
}
|
||||
|
||||
// Create updated providers array (immutability)
|
||||
const updatedProviders = [
|
||||
...providersData.providers.slice(0, providerIndex),
|
||||
updatedProvider,
|
||||
...providersData.providers.slice(providerIndex + 1)
|
||||
]
|
||||
|
||||
const updatedProvidersData: ProvidersDataFile = {
|
||||
...providersData,
|
||||
providers: updatedProviders
|
||||
}
|
||||
|
||||
// Write back to file
|
||||
await fs.writeFile(providersDataPath, JSON.stringify(updatedProvidersData, null, 2), 'utf-8')
|
||||
|
||||
const response = ProviderUpdateResponseSchema.parse({
|
||||
success: true,
|
||||
provider: updatedProvider
|
||||
})
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error updating provider:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to update provider', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
146
packages/catalog/web/app/api/catalog/providers/route.ts
Normal file
146
packages/catalog/web/app/api/catalog/providers/route.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
|
||||
import type { Provider } from '@/lib/catalog-types'
|
||||
import {
|
||||
ProviderSchema,
|
||||
ProvidersDataFileSchema
|
||||
} from '@/lib/catalog-types'
|
||||
import {
|
||||
createErrorResponse,
|
||||
safeParseWithValidation,
|
||||
validatePaginatedResponse,
|
||||
validateQueryParams,
|
||||
ValidationError
|
||||
} from '@/lib/validation'
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
function filterProviders(providers: readonly Provider[], search?: string, authentication?: string[]): Provider[] {
|
||||
let filtered = [...providers]
|
||||
|
||||
if (search) {
|
||||
const searchLower = search.toLowerCase()
|
||||
filtered = filtered.filter(
|
||||
(provider) =>
|
||||
provider.id.toLowerCase().includes(searchLower) ||
|
||||
provider.name.toLowerCase().includes(searchLower) ||
|
||||
provider.description?.toLowerCase().includes(searchLower)
|
||||
)
|
||||
}
|
||||
|
||||
if (authentication && authentication.length > 0) {
|
||||
filtered = filtered.filter((provider) => authentication.includes(provider.authentication))
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
||||
|
||||
function paginateItems<T>(
|
||||
items: readonly T[],
|
||||
page: number,
|
||||
limit: number
|
||||
): {
|
||||
items: T[]
|
||||
pagination: {
|
||||
page: number
|
||||
limit: number
|
||||
total: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrev: boolean
|
||||
}
|
||||
} {
|
||||
const total = items.length
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
const offset = (page - 1) * limit
|
||||
const paginatedItems = items.slice(offset, offset + limit)
|
||||
|
||||
return {
|
||||
items: paginatedItems,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
|
||||
// Validate query parameters using Zod
|
||||
const validatedParams = validateQueryParams(searchParams)
|
||||
|
||||
// Read and validate providers data using Zod
|
||||
const providersDataPath = path.join(DATA_DIR, 'providers.json')
|
||||
const providersDataRaw = await fs.readFile(providersDataPath, 'utf-8')
|
||||
const providersData = await safeParseWithValidation(
|
||||
providersDataRaw,
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in file'
|
||||
)
|
||||
|
||||
// Filter providers with type safety
|
||||
const filteredProviders = filterProviders(
|
||||
providersData.providers,
|
||||
validatedParams.search,
|
||||
validatedParams.authentication
|
||||
)
|
||||
|
||||
// Paginate results
|
||||
const { items, pagination } = paginateItems(filteredProviders, validatedParams.page, validatedParams.limit)
|
||||
|
||||
// Create paginated response using Zod schema
|
||||
const response = validatePaginatedResponse({ data: items, pagination }, ProviderSchema)
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error fetching providers:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to fetch providers', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
|
||||
// Validate the data structure using Zod
|
||||
const validatedData = await safeParseWithValidation(
|
||||
JSON.stringify(body),
|
||||
ProvidersDataFileSchema,
|
||||
'Invalid providers data format in request body'
|
||||
)
|
||||
|
||||
// Write validated data back to file
|
||||
const providersDataPath = path.join(DATA_DIR, 'providers.json')
|
||||
await fs.writeFile(providersDataPath, JSON.stringify(validatedData, null, 2), 'utf-8')
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
console.error('Validation error:', error.message, error.details)
|
||||
return NextResponse.json(createErrorResponse(error.message, 400, error.details), { status: 400 })
|
||||
}
|
||||
|
||||
console.error('Error updating providers:', error)
|
||||
return NextResponse.json(
|
||||
createErrorResponse('Failed to update providers', 500, error instanceof Error ? error.message : 'Unknown error'),
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
70
packages/catalog/web/app/api/catalog/stats/route.ts
Normal file
70
packages/catalog/web/app/api/catalog/stats/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import { NextResponse } from 'next/server'
|
||||
import path from 'path'
|
||||
import { z } from 'zod'
|
||||
|
||||
// Define schema for stats response
|
||||
const StatsResponseSchema = z.object({
|
||||
total_models: z.number(),
|
||||
total_providers: z.number(),
|
||||
total_overrides: z.number(),
|
||||
last_updated: z.string().optional(),
|
||||
migration_status: z.enum(['completed', 'in_progress', 'failed']).optional()
|
||||
})
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), '../data')
|
||||
|
||||
// Define schema for migration report
|
||||
const MigrationReportSchema = z.object({
|
||||
summary: z.object({
|
||||
total_base_models: z.number(),
|
||||
total_providers: z.number(),
|
||||
total_overrides: z.number()
|
||||
})
|
||||
})
|
||||
|
||||
const ModelsDataSchema = z.object({
|
||||
version: z.string(),
|
||||
models: z.array(z.any())
|
||||
})
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Read migration report for stats with Zod validation
|
||||
const reportData = await fs.readFile(path.join(DATA_DIR, 'migration-report.json'), 'utf-8')
|
||||
const report = MigrationReportSchema.parse(JSON.parse(reportData))
|
||||
|
||||
// Read actual data for last updated timestamp with Zod validation
|
||||
const modelsData = await fs.readFile(path.join(DATA_DIR, 'models.json'), 'utf-8')
|
||||
const models = ModelsDataSchema.parse(JSON.parse(modelsData))
|
||||
|
||||
const stats = {
|
||||
total_models: report.summary.total_base_models,
|
||||
total_providers: report.summary.total_providers,
|
||||
total_overrides: report.summary.total_overrides,
|
||||
last_updated: new Date().toISOString(),
|
||||
version: models.version
|
||||
}
|
||||
|
||||
// Validate response with Zod schema
|
||||
const validatedStats = StatsResponseSchema.parse(stats)
|
||||
|
||||
return NextResponse.json(validatedStats)
|
||||
} catch (error) {
|
||||
console.error('Error fetching stats:', error)
|
||||
|
||||
// Try to provide a minimal fallback response
|
||||
const fallbackStats = {
|
||||
total_models: 0,
|
||||
total_providers: 0,
|
||||
total_overrides: 0
|
||||
}
|
||||
|
||||
try {
|
||||
const validatedFallback = StatsResponseSchema.parse(fallbackStats)
|
||||
return NextResponse.json(validatedFallback)
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
packages/catalog/web/app/favicon.ico
Normal file
BIN
packages/catalog/web/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
26
packages/catalog/web/app/globals.css
Normal file
26
packages/catalog/web/app/globals.css
Normal file
@@ -0,0 +1,26 @@
|
||||
@import 'tailwindcss';
|
||||
|
||||
:root {
|
||||
--background: #ffffff;
|
||||
--foreground: #171717;
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--font-sans: var(--font-geist-sans);
|
||||
--font-mono: var(--font-geist-mono);
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: #0a0a0a;
|
||||
--foreground: #ededed;
|
||||
}
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
}
|
||||
31
packages/catalog/web/app/layout.tsx
Normal file
31
packages/catalog/web/app/layout.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import './globals.css'
|
||||
|
||||
import type { Metadata } from 'next'
|
||||
import { Geist, Geist_Mono } from 'next/font/google'
|
||||
|
||||
const geistSans = Geist({
|
||||
variable: '--font-geist-sans',
|
||||
subsets: ['latin']
|
||||
})
|
||||
|
||||
const geistMono = Geist_Mono({
|
||||
variable: '--font-geist-mono',
|
||||
subsets: ['latin']
|
||||
})
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Create Next App',
|
||||
description: 'Generated by create next app'
|
||||
}
|
||||
|
||||
export default function RootLayout({
|
||||
children
|
||||
}: Readonly<{
|
||||
children: React.ReactNode
|
||||
}>) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>{children}</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
348
packages/catalog/web/app/page.tsx
Normal file
348
packages/catalog/web/app/page.tsx
Normal file
@@ -0,0 +1,348 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
|
||||
import { Navigation } from '@/components/navigation'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger
|
||||
} from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
// Import SWR hooks and utilities
|
||||
import { getErrorMessage, useDebounce, useModels, useUpdateModel } from '@/lib/api-client'
|
||||
import type { CapabilityType, Model } from '@/lib/catalog-types'
|
||||
|
||||
// Type-safe capabilities list
|
||||
const CAPABILITIES: readonly CapabilityType[] = [
|
||||
'FUNCTION_CALL',
|
||||
'REASONING',
|
||||
'IMAGE_RECOGNITION',
|
||||
'IMAGE_GENERATION',
|
||||
'AUDIO_RECOGNITION',
|
||||
'AUDIO_GENERATION',
|
||||
'EMBEDDING',
|
||||
'RERANK',
|
||||
'AUDIO_TRANSCRIPT',
|
||||
'VIDEO_RECOGNITION',
|
||||
'VIDEO_GENERATION',
|
||||
'STRUCTURED_OUTPUT',
|
||||
'FILE_INPUT',
|
||||
'WEB_SEARCH',
|
||||
'CODE_EXECUTION',
|
||||
'FILE_SEARCH',
|
||||
'COMPUTER_USE'
|
||||
] as const
|
||||
|
||||
// Simple Pagination Component
|
||||
function SimplePagination({
|
||||
currentPage,
|
||||
totalPages,
|
||||
onPageChange
|
||||
}: {
|
||||
currentPage: number
|
||||
totalPages: number
|
||||
onPageChange: (page: number) => void
|
||||
}) {
|
||||
const pages = Array.from({ length: Math.min(5, totalPages) }, (_, i) => {
|
||||
if (totalPages <= 5) return i + 1
|
||||
if (currentPage <= 3) return i + 1
|
||||
if (currentPage >= totalPages - 2) return totalPages - 4 + i
|
||||
return currentPage - 2 + i
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="outline" size="sm" onClick={() => onPageChange(currentPage - 1)} disabled={currentPage <= 1}>
|
||||
Previous
|
||||
</Button>
|
||||
{pages.map((page) => (
|
||||
<Button
|
||||
key={page}
|
||||
variant={currentPage === page ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
onClick={() => onPageChange(page)}>
|
||||
{page}
|
||||
</Button>
|
||||
))}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onPageChange(currentPage + 1)}
|
||||
disabled={currentPage >= totalPages}>
|
||||
Next
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function CatalogReview() {
|
||||
// Form state
|
||||
const [search, setSearch] = useState('')
|
||||
const [selectedCapabilities, setSelectedCapabilities] = useState<string[]>([])
|
||||
const [selectedProviders, setSelectedProviders] = useState<string[]>([])
|
||||
const [currentPage, setCurrentPage] = useState(1)
|
||||
const [editingModel, setEditingModel] = useState<Model | null>(null)
|
||||
const [jsonContent, setJsonContent] = useState('')
|
||||
|
||||
// Debounce search to avoid excessive API calls
|
||||
const debouncedSearch = useDebounce(search, 300)
|
||||
|
||||
// SWR hook for fetching models
|
||||
const {
|
||||
data: modelsData,
|
||||
error,
|
||||
isLoading
|
||||
} = useModels({
|
||||
page: currentPage,
|
||||
limit: 20,
|
||||
search: debouncedSearch,
|
||||
capabilities: selectedCapabilities.length > 0 ? selectedCapabilities : undefined,
|
||||
providers: selectedProviders.length > 0 ? selectedProviders : undefined
|
||||
})
|
||||
|
||||
// SWR mutation for updating models
|
||||
const { trigger: updateModel, isMutating: isUpdating } = useUpdateModel()
|
||||
|
||||
// Extract data from SWR response
|
||||
const models = modelsData?.data || []
|
||||
const pagination = modelsData?.pagination || {
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: 0,
|
||||
totalPages: 0,
|
||||
hasNext: false,
|
||||
hasPrev: false
|
||||
}
|
||||
|
||||
const handleEdit = (model: Model) => {
|
||||
setEditingModel(model)
|
||||
setJsonContent(JSON.stringify(model, null, 2))
|
||||
}
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!editingModel) return
|
||||
|
||||
try {
|
||||
// Validate JSON before sending
|
||||
const updatedModel = JSON.parse(jsonContent) as unknown
|
||||
|
||||
// Basic validation - the API will do thorough validation
|
||||
if (!updatedModel || typeof updatedModel !== 'object') {
|
||||
throw new Error('Invalid JSON format')
|
||||
}
|
||||
|
||||
// Use SWR mutation for optimistic update
|
||||
await updateModel({
|
||||
id: editingModel.id,
|
||||
data: updatedModel as Partial<Model>
|
||||
})
|
||||
|
||||
// Close dialog and reset form
|
||||
setEditingModel(null)
|
||||
setJsonContent('')
|
||||
} catch (error) {
|
||||
console.error('Error saving model:', error)
|
||||
// Error will be handled by SWR and displayed in UI
|
||||
}
|
||||
}
|
||||
|
||||
// Type-safe function to extract unique providers
|
||||
const getUniqueProviders = (): string[] => {
|
||||
return [
|
||||
...new Set(models.map((model) => model.owned_by).filter((provider): provider is string => Boolean(provider)))
|
||||
]
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-6 space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Catalog Review</h1>
|
||||
<p className="text-muted-foreground">Review and validate model configurations after migration</p>
|
||||
</div>
|
||||
<Navigation />
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Filters</CardTitle>
|
||||
<CardDescription>Filter models to review specific configurations</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex gap-4">
|
||||
<Input
|
||||
placeholder="Search models..."
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
className="max-w-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm font-medium mb-2 block">Capabilities</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{CAPABILITIES.map((capability) => (
|
||||
<Badge
|
||||
key={capability}
|
||||
variant={selectedCapabilities.includes(capability) ? 'default' : 'outline'}
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
setSelectedCapabilities((prev) =>
|
||||
prev.includes(capability) ? prev.filter((c) => c !== capability) : [...prev, capability]
|
||||
)
|
||||
}}>
|
||||
{capability.replace('_', ' ')}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm font-medium mb-2 block">Providers</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{getUniqueProviders().map((provider) => (
|
||||
<Badge
|
||||
key={provider}
|
||||
variant={selectedProviders.includes(provider) ? 'default' : 'outline'}
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
setSelectedProviders((prev) =>
|
||||
prev.includes(provider) ? prev.filter((p) => p !== provider) : [...prev, provider]
|
||||
)
|
||||
}}>
|
||||
{provider}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Error Display */}
|
||||
{error && (
|
||||
<Alert variant="destructive">
|
||||
<AlertDescription>{getErrorMessage(error)}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Models ({pagination.total})</CardTitle>
|
||||
<CardDescription>Review migrated model configurations</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="text-center py-8">
|
||||
<div className="animate-pulse">Loading models...</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>ID</TableHead>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead>Provider</TableHead>
|
||||
<TableHead>Capabilities</TableHead>
|
||||
<TableHead>Context Window</TableHead>
|
||||
<TableHead>Modalities</TableHead>
|
||||
<TableHead>Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{models.map((model) => (
|
||||
<TableRow key={model.id}>
|
||||
<TableCell className="font-mono text-sm">{model.id}</TableCell>
|
||||
<TableCell>{model.name || model.id}</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="outline">{model.owned_by}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1 max-w-xs">
|
||||
{model.capabilities.slice(0, 3).map((cap) => (
|
||||
<Badge key={cap} variant="secondary" className="text-xs">
|
||||
{cap.replace('_', ' ')}
|
||||
</Badge>
|
||||
))}
|
||||
{model.capabilities.length > 3 && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
+{model.capabilities.length - 3}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>{model.context_window.toLocaleString()}</TableCell>
|
||||
<TableCell>
|
||||
<div className="text-sm">
|
||||
<div>In: {model.input_modalities?.join(', ')}</div>
|
||||
<div>Out: {model.output_modalities?.join(', ')}</div>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="sm" onClick={() => handleEdit(model)}>
|
||||
Edit
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-4xl max-h-[80vh] overflow-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Edit Model Configuration</DialogTitle>
|
||||
<DialogDescription>
|
||||
Modify the JSON configuration for {model.name || model.id}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4">
|
||||
<Textarea
|
||||
value={jsonContent}
|
||||
onChange={(e) => setJsonContent(e.target.value)}
|
||||
className="min-h-[400px] font-mono text-sm"
|
||||
/>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<Button variant="outline" onClick={() => setEditingModel(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} disabled={isUpdating}>
|
||||
{isUpdating ? 'Saving...' : 'Save Changes'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
|
||||
<Separator className="my-4" />
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Showing {(pagination.page - 1) * pagination.limit + 1} to{' '}
|
||||
{Math.min(pagination.page * pagination.limit, pagination.total)} of {pagination.total} models
|
||||
</div>
|
||||
<SimplePagination
|
||||
currentPage={pagination.page}
|
||||
totalPages={pagination.totalPages}
|
||||
onPageChange={setCurrentPage}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
323
packages/catalog/web/app/providers/page.tsx
Normal file
323
packages/catalog/web/app/providers/page.tsx
Normal file
@@ -0,0 +1,323 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
|
||||
import { Navigation } from '@/components/navigation'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger
|
||||
} from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
// Import SWR hooks and utilities
|
||||
import { getErrorMessage, useDebounce, useProviders, useUpdateProvider } from '@/lib/api-client'
|
||||
import type { Provider } from '@/lib/catalog-types'
|
||||
|
||||
// Simple Pagination Component
|
||||
function SimplePagination({
|
||||
currentPage,
|
||||
totalPages,
|
||||
onPageChange
|
||||
}: {
|
||||
currentPage: number
|
||||
totalPages: number
|
||||
onPageChange: (page: number) => void
|
||||
}) {
|
||||
const pages = Array.from({ length: Math.min(5, totalPages) }, (_, i) => {
|
||||
if (totalPages <= 5) return i + 1
|
||||
if (currentPage <= 3) return i + 1
|
||||
if (currentPage >= totalPages - 2) return totalPages - 4 + i
|
||||
return currentPage - 2 + i
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="outline" size="sm" onClick={() => onPageChange(currentPage - 1)} disabled={currentPage <= 1}>
|
||||
Previous
|
||||
</Button>
|
||||
{pages.map((page) => (
|
||||
<Button
|
||||
key={page}
|
||||
variant={currentPage === page ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
onClick={() => onPageChange(page)}>
|
||||
{page}
|
||||
</Button>
|
||||
))}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onPageChange(currentPage + 1)}
|
||||
disabled={currentPage >= totalPages}>
|
||||
Next
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function ProvidersPage() {
|
||||
// Form state
|
||||
const [search, setSearch] = useState('')
|
||||
const [currentPage, setCurrentPage] = useState(1)
|
||||
const [editingProvider, setEditingProvider] = useState<Provider | null>(null)
|
||||
const [jsonContent, setJsonContent] = useState('')
|
||||
|
||||
// Debounce search to avoid excessive API calls
|
||||
const debouncedSearch = useDebounce(search, 300)
|
||||
|
||||
// SWR hook for fetching providers
|
||||
const {
|
||||
data: providersData,
|
||||
error,
|
||||
isLoading,
|
||||
mutate: refetchProviders
|
||||
} = useProviders({
|
||||
page: currentPage,
|
||||
limit: 20,
|
||||
search: debouncedSearch
|
||||
})
|
||||
|
||||
// SWR mutation for updating providers
|
||||
const { trigger: updateProvider, isMutating: isUpdating } = useUpdateProvider()
|
||||
|
||||
// Extract data from SWR response
|
||||
const providers = providersData?.data || []
|
||||
const pagination = providersData?.pagination || {
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: 0,
|
||||
totalPages: 0,
|
||||
hasNext: false,
|
||||
hasPrev: false
|
||||
}
|
||||
|
||||
const handleEdit = (provider: Provider) => {
|
||||
setEditingProvider(provider)
|
||||
setJsonContent(JSON.stringify(provider, null, 2))
|
||||
}
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!editingProvider) return
|
||||
|
||||
try {
|
||||
// Validate JSON before sending
|
||||
const updatedProvider = JSON.parse(jsonContent) as unknown
|
||||
|
||||
// Basic validation - the API will do thorough validation
|
||||
if (!updatedProvider || typeof updatedProvider !== 'object') {
|
||||
throw new Error('Invalid JSON format')
|
||||
}
|
||||
|
||||
// Use SWR mutation for optimistic update
|
||||
await updateProvider({
|
||||
id: editingProvider.id,
|
||||
data: updatedProvider as Partial<Provider>
|
||||
})
|
||||
|
||||
// Close dialog and reset form
|
||||
setEditingProvider(null)
|
||||
setJsonContent('')
|
||||
} catch (error) {
|
||||
console.error('Error saving provider:', error)
|
||||
// Error will be handled by SWR and displayed in UI
|
||||
}
|
||||
}
|
||||
|
||||
// Type-safe function to extract provider capabilities
|
||||
const getCapabilities = (behaviors: Record<string, unknown>): string[] => {
|
||||
return Object.entries(behaviors)
|
||||
.filter(([_, value]) => value === true)
|
||||
.map(([key, _]) => key.replace(/_/g, ' ').replace(/\b\w/g, (letter) => letter.toUpperCase()))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-6 space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Provider Management</h1>
|
||||
<p className="text-muted-foreground">Review and validate provider configurations</p>
|
||||
</div>
|
||||
<Navigation />
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Filters</CardTitle>
|
||||
<CardDescription>Filter providers to review specific configurations</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex gap-4">
|
||||
<Input
|
||||
placeholder="Search providers..."
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
className="max-w-sm"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Error Display */}
|
||||
{error && (
|
||||
<Alert variant="destructive">
|
||||
<AlertDescription>{getErrorMessage(error)}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Providers ({pagination.total})</CardTitle>
|
||||
<CardDescription>Review provider configurations and capabilities</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="text-center py-8">
|
||||
<div className="animate-pulse">Loading providers...</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>ID</TableHead>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead>Authentication</TableHead>
|
||||
<TableHead>Pricing Model</TableHead>
|
||||
<TableHead>Endpoints</TableHead>
|
||||
<TableHead>Capabilities</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{providers.map((provider) => (
|
||||
<TableRow key={provider.id}>
|
||||
<TableCell className="font-mono text-sm">{provider.id}</TableCell>
|
||||
<TableCell>
|
||||
<div>
|
||||
<div className="font-medium">{provider.name}</div>
|
||||
{provider.description && (
|
||||
<div className="text-sm text-muted-foreground">{provider.description}</div>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="outline">{provider.authentication}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="secondary">{provider.pricing_model}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1 max-w-xs">
|
||||
{provider.supported_endpoints.slice(0, 2).map((endpoint) => (
|
||||
<Badge key={endpoint} variant="outline" className="text-xs">
|
||||
{endpoint}
|
||||
</Badge>
|
||||
))}
|
||||
{provider.supported_endpoints.length > 2 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{provider.supported_endpoints.length - 2}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1 max-w-xs">
|
||||
{getCapabilities(provider.behaviors)
|
||||
.slice(0, 2)
|
||||
.map((capability) => (
|
||||
<Badge key={capability} variant="secondary" className="text-xs">
|
||||
{capability}
|
||||
</Badge>
|
||||
))}
|
||||
{getCapabilities(provider.behaviors).length > 2 && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
+{getCapabilities(provider.behaviors).length - 2}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="space-y-1">
|
||||
{provider.deprecated && (
|
||||
<Badge variant="destructive" className="text-xs">
|
||||
Deprecated
|
||||
</Badge>
|
||||
)}
|
||||
{provider.maintenance_mode && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
Maintenance
|
||||
</Badge>
|
||||
)}
|
||||
{!provider.deprecated && !provider.maintenance_mode && (
|
||||
<Badge variant="default" className="text-xs">
|
||||
Active
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="sm" onClick={() => handleEdit(provider)}>
|
||||
Edit
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-4xl max-h-[80vh] overflow-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Edit Provider Configuration</DialogTitle>
|
||||
<DialogDescription>Modify the JSON configuration for {provider.name}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4">
|
||||
<Textarea
|
||||
value={jsonContent}
|
||||
onChange={(e) => setJsonContent(e.target.value)}
|
||||
className="min-h-[400px] font-mono text-sm"
|
||||
/>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<Button variant="outline" onClick={() => setEditingProvider(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} disabled={isUpdating}>
|
||||
{isUpdating ? 'Saving...' : 'Save Changes'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
|
||||
<Separator className="my-4" />
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Showing {(pagination.page - 1) * pagination.limit + 1} to{' '}
|
||||
{Math.min(pagination.page * pagination.limit, pagination.total)} of {pagination.total} providers
|
||||
</div>
|
||||
<SimplePagination
|
||||
currentPage={pagination.page}
|
||||
totalPages={pagination.totalPages}
|
||||
onPageChange={setCurrentPage}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
20
packages/catalog/web/components.json
Normal file
20
packages/catalog/web/components.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"$schema": "https://ui.shadcn.com/schema.json",
|
||||
"style": "new-york",
|
||||
"rsc": true,
|
||||
"tsx": true,
|
||||
"tailwind": {
|
||||
"config": "tailwind.config.ts",
|
||||
"css": "app/globals.css",
|
||||
"baseColor": "slate",
|
||||
"cssVariables": true,
|
||||
"prefix": ""
|
||||
},
|
||||
"aliases": {
|
||||
"components": "@/components",
|
||||
"utils": "@/lib/utils",
|
||||
"ui": "@/components/ui",
|
||||
"lib": "@/lib",
|
||||
"hooks": "@/hooks"
|
||||
}
|
||||
}
|
||||
32
packages/catalog/web/components/navigation.tsx
Normal file
32
packages/catalog/web/components/navigation.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { usePathname } from 'next/navigation'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const navigation = [
|
||||
{ name: 'Models', href: '/' },
|
||||
{ name: 'Providers', href: '/providers' },
|
||||
{ name: 'Overrides', href: '/overrides' }
|
||||
]
|
||||
|
||||
export function Navigation() {
|
||||
const pathname = usePathname()
|
||||
|
||||
return (
|
||||
<nav className="flex space-x-8">
|
||||
{navigation.map((item) => (
|
||||
<Link
|
||||
key={item.name}
|
||||
href={item.href}
|
||||
className={cn(
|
||||
'text-sm font-medium transition-colors hover:text-primary',
|
||||
pathname === item.href ? 'text-foreground' : 'text-muted-foreground'
|
||||
)}>
|
||||
{item.name}
|
||||
</Link>
|
||||
))}
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
59
packages/catalog/web/components/ui/alert.tsx
Normal file
59
packages/catalog/web/components/ui/alert.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const alertVariants = cva(
|
||||
"relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-background text-foreground",
|
||||
destructive:
|
||||
"border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const Alert = React.forwardRef<
|
||||
HTMLDivElement,
|
||||
React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
|
||||
>(({ className, variant, ...props }, ref) => (
|
||||
<div
|
||||
ref={ref}
|
||||
role="alert"
|
||||
className={cn(alertVariants({ variant }), className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
Alert.displayName = "Alert"
|
||||
|
||||
const AlertTitle = React.forwardRef<
|
||||
HTMLParagraphElement,
|
||||
React.HTMLAttributes<HTMLHeadingElement>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<h5
|
||||
ref={ref}
|
||||
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
AlertTitle.displayName = "AlertTitle"
|
||||
|
||||
const AlertDescription = React.forwardRef<
|
||||
HTMLParagraphElement,
|
||||
React.HTMLAttributes<HTMLParagraphElement>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<div
|
||||
ref={ref}
|
||||
className={cn("text-sm [&_p]:leading-relaxed", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
AlertDescription.displayName = "AlertDescription"
|
||||
|
||||
export { Alert, AlertTitle, AlertDescription }
|
||||
29
packages/catalog/web/components/ui/badge.tsx
Normal file
29
packages/catalog/web/components/ui/badge.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const badgeVariants = cva(
|
||||
'inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: 'border-transparent bg-primary text-primary-foreground shadow hover:bg-primary/80',
|
||||
secondary: 'border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80',
|
||||
destructive: 'border-transparent bg-destructive text-destructive-foreground shadow hover:bg-destructive/80',
|
||||
outline: 'text-foreground'
|
||||
}
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: 'default'
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export interface BadgeProps extends React.HTMLAttributes<HTMLDivElement>, VariantProps<typeof badgeVariants> {}
|
||||
|
||||
function Badge({ className, variant, ...props }: BadgeProps) {
|
||||
return <div className={cn(badgeVariants({ variant }), className)} {...props} />
|
||||
}
|
||||
|
||||
export { Badge, badgeVariants }
|
||||
52
packages/catalog/web/components/ui/button.tsx
Normal file
52
packages/catalog/web/components/ui/button.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Slot } from '@radix-ui/react-slot'
|
||||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const buttonVariants = cva(
|
||||
'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: 'bg-primary text-primary-foreground shadow hover:bg-primary/90',
|
||||
destructive: 'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
|
||||
outline: 'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground',
|
||||
secondary: 'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80',
|
||||
ghost: 'hover:bg-accent hover:text-accent-foreground',
|
||||
link: 'text-primary underline-offset-4 hover:underline'
|
||||
},
|
||||
size: {
|
||||
default: 'h-9 px-4 py-2',
|
||||
sm: 'h-8 rounded-md px-3 text-xs',
|
||||
lg: 'h-10 rounded-md px-8',
|
||||
icon: 'h-9 w-9'
|
||||
}
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: 'default',
|
||||
size: 'default'
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export interface ButtonProps
|
||||
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||
VariantProps<typeof buttonVariants> {
|
||||
asChild?: boolean
|
||||
}
|
||||
|
||||
const Button = ({
|
||||
ref,
|
||||
className,
|
||||
variant,
|
||||
size,
|
||||
asChild = false,
|
||||
...props
|
||||
}: ButtonProps & { ref?: React.RefObject<HTMLButtonElement | null> }) => {
|
||||
const Comp = asChild ? Slot : 'button'
|
||||
return <Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props} />
|
||||
}
|
||||
Button.displayName = 'Button'
|
||||
|
||||
export { Button, buttonVariants }
|
||||
59
packages/catalog/web/components/ui/card.tsx
Normal file
59
packages/catalog/web/components/ui/card.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Card = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('rounded-xl border bg-card text-card-foreground shadow', className)} {...props} />
|
||||
)
|
||||
Card.displayName = 'Card'
|
||||
|
||||
const CardHeader = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('flex flex-col space-y-1.5 p-6', className)} {...props} />
|
||||
)
|
||||
CardHeader.displayName = 'CardHeader'
|
||||
|
||||
const CardTitle = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('font-semibold leading-none tracking-tight', className)} {...props} />
|
||||
)
|
||||
CardTitle.displayName = 'CardTitle'
|
||||
|
||||
const CardDescription = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('text-sm text-muted-foreground', className)} {...props} />
|
||||
)
|
||||
CardDescription.displayName = 'CardDescription'
|
||||
|
||||
const CardContent = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('p-6 pt-0', className)} {...props} />
|
||||
)
|
||||
CardContent.displayName = 'CardContent'
|
||||
|
||||
const CardFooter = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.RefObject<HTMLDivElement | null> }) => (
|
||||
<div ref={ref} className={cn('flex items-center p-6 pt-0', className)} {...props} />
|
||||
)
|
||||
CardFooter.displayName = 'CardFooter'
|
||||
|
||||
export { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
|
||||
107
packages/catalog/web/components/ui/dialog.tsx
Normal file
107
packages/catalog/web/components/ui/dialog.tsx
Normal file
@@ -0,0 +1,107 @@
|
||||
'use client'
|
||||
|
||||
import * as DialogPrimitive from '@radix-ui/react-dialog'
|
||||
import { Cross2Icon } from '@radix-ui/react-icons'
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Dialog = DialogPrimitive.Root
|
||||
|
||||
const DialogTrigger = DialogPrimitive.Trigger
|
||||
|
||||
const DialogPortal = DialogPrimitive.Portal
|
||||
|
||||
const DialogClose = DialogPrimitive.Close
|
||||
|
||||
const DialogOverlay = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> & {
|
||||
ref?: React.RefObject<React.ElementRef<typeof DialogPrimitive.Overlay> | null>
|
||||
}) => (
|
||||
<DialogPrimitive.Overlay
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
|
||||
|
||||
const DialogContent = ({
|
||||
ref,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
|
||||
ref?: React.RefObject<React.ElementRef<typeof DialogPrimitive.Content> | null>
|
||||
}) => (
|
||||
<DialogPortal>
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg',
|
||||
className
|
||||
)}
|
||||
{...props}>
|
||||
{children}
|
||||
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
|
||||
<Cross2Icon className="h-4 w-4" />
|
||||
<span className="sr-only">Close</span>
|
||||
</DialogPrimitive.Close>
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPortal>
|
||||
)
|
||||
DialogContent.displayName = DialogPrimitive.Content.displayName
|
||||
|
||||
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div className={cn('flex flex-col space-y-1.5 text-center sm:text-left', className)} {...props} />
|
||||
)
|
||||
DialogHeader.displayName = 'DialogHeader'
|
||||
|
||||
const DialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', className)} {...props} />
|
||||
)
|
||||
DialogFooter.displayName = 'DialogFooter'
|
||||
|
||||
const DialogTitle = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title> & {
|
||||
ref?: React.RefObject<React.ElementRef<typeof DialogPrimitive.Title> | null>
|
||||
}) => (
|
||||
<DialogPrimitive.Title
|
||||
ref={ref}
|
||||
className={cn('text-lg font-semibold leading-none tracking-tight', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
DialogTitle.displayName = DialogPrimitive.Title.displayName
|
||||
|
||||
const DialogDescription = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description> & {
|
||||
ref?: React.RefObject<React.ElementRef<typeof DialogPrimitive.Description> | null>
|
||||
}) => <DialogPrimitive.Description ref={ref} className={cn('text-sm text-muted-foreground', className)} {...props} />
|
||||
DialogDescription.displayName = DialogPrimitive.Description.displayName
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogPortal,
|
||||
DialogTitle,
|
||||
DialogTrigger
|
||||
}
|
||||
25
packages/catalog/web/components/ui/input.tsx
Normal file
25
packages/catalog/web/components/ui/input.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Input = ({
|
||||
ref,
|
||||
className,
|
||||
type,
|
||||
...props
|
||||
}: React.ComponentProps<'input'> & { ref?: React.RefObject<HTMLInputElement | null> }) => {
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
className={cn(
|
||||
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm',
|
||||
className
|
||||
)}
|
||||
ref={ref}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Input.displayName = 'Input'
|
||||
|
||||
export { Input }
|
||||
88
packages/catalog/web/components/ui/pagination.tsx
Normal file
88
packages/catalog/web/components/ui/pagination.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
import { ChevronLeftIcon, ChevronRightIcon, DotsHorizontalIcon } from '@radix-ui/react-icons'
|
||||
import * as React from 'react'
|
||||
|
||||
import type { ButtonProps } from '@/components/ui/button'
|
||||
import { buttonVariants } from '@/components/ui/button'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => (
|
||||
<nav
|
||||
role="navigation"
|
||||
aria-label="pagination"
|
||||
className={cn('mx-auto flex w-full justify-center', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
Pagination.displayName = 'Pagination'
|
||||
|
||||
const PaginationContent = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<'ul'> & { ref?: React.RefObject<HTMLUListElement | null> }) => (
|
||||
<ul ref={ref} className={cn('flex flex-row items-center gap-1', className)} {...props} />
|
||||
)
|
||||
PaginationContent.displayName = 'PaginationContent'
|
||||
|
||||
const PaginationItem = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<'li'> & { ref?: React.RefObject<HTMLLIElement | null> }) => (
|
||||
<li ref={ref} className={cn('', className)} {...props} />
|
||||
)
|
||||
PaginationItem.displayName = 'PaginationItem'
|
||||
|
||||
type PaginationLinkProps = {
|
||||
isActive?: boolean
|
||||
} & Pick<ButtonProps, 'size'> &
|
||||
React.ComponentProps<'a'>
|
||||
|
||||
const PaginationLink = ({ className, isActive, size = 'icon', ...props }: PaginationLinkProps) => (
|
||||
<a
|
||||
aria-current={isActive ? 'page' : undefined}
|
||||
className={cn(
|
||||
buttonVariants({
|
||||
variant: isActive ? 'outline' : 'ghost',
|
||||
size
|
||||
}),
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
PaginationLink.displayName = 'PaginationLink'
|
||||
|
||||
const PaginationPrevious = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => (
|
||||
<PaginationLink aria-label="Go to previous page" size="default" className={cn('gap-1 pl-2.5', className)} {...props}>
|
||||
<ChevronLeftIcon className="h-4 w-4" />
|
||||
<span>Previous</span>
|
||||
</PaginationLink>
|
||||
)
|
||||
PaginationPrevious.displayName = 'PaginationPrevious'
|
||||
|
||||
const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => (
|
||||
<PaginationLink aria-label="Go to next page" size="default" className={cn('gap-1 pr-2.5', className)} {...props}>
|
||||
<span>Next</span>
|
||||
<ChevronRightIcon className="h-4 w-4" />
|
||||
</PaginationLink>
|
||||
)
|
||||
PaginationNext.displayName = 'PaginationNext'
|
||||
|
||||
const PaginationEllipsis = ({ className, ...props }: React.ComponentProps<'span'>) => (
|
||||
<span aria-hidden className={cn('flex h-9 w-9 items-center justify-center', className)} {...props}>
|
||||
<DotsHorizontalIcon className="h-4 w-4" />
|
||||
<span className="sr-only">More pages</span>
|
||||
</span>
|
||||
)
|
||||
PaginationEllipsis.displayName = 'PaginationEllipsis'
|
||||
|
||||
export {
|
||||
Pagination,
|
||||
PaginationContent,
|
||||
PaginationEllipsis,
|
||||
PaginationItem,
|
||||
PaginationLink,
|
||||
PaginationNext,
|
||||
PaginationPrevious
|
||||
}
|
||||
27
packages/catalog/web/components/ui/separator.tsx
Normal file
27
packages/catalog/web/components/ui/separator.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
'use client'
|
||||
|
||||
import * as SeparatorPrimitive from '@radix-ui/react-separator'
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Separator = ({
|
||||
ref,
|
||||
className,
|
||||
orientation = 'horizontal',
|
||||
decorative = true,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<typeof SeparatorPrimitive.Root> & {
|
||||
ref?: React.RefObject<React.ElementRef<typeof SeparatorPrimitive.Root> | null>
|
||||
}) => (
|
||||
<SeparatorPrimitive.Root
|
||||
ref={ref}
|
||||
decorative={decorative}
|
||||
orientation={orientation}
|
||||
className={cn('shrink-0 bg-border', orientation === 'horizontal' ? 'h-[1px] w-full' : 'h-full w-[1px]', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
Separator.displayName = SeparatorPrimitive.Root.displayName
|
||||
|
||||
export { Separator }
|
||||
94
packages/catalog/web/components/ui/table.tsx
Normal file
94
packages/catalog/web/components/ui/table.tsx
Normal file
@@ -0,0 +1,94 @@
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Table = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableElement> & { ref?: React.RefObject<HTMLTableElement | null> }) => (
|
||||
<div className="relative w-full overflow-auto">
|
||||
<table ref={ref} className={cn('w-full caption-bottom text-sm', className)} {...props} />
|
||||
</div>
|
||||
)
|
||||
Table.displayName = 'Table'
|
||||
|
||||
const TableHeader = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableSectionElement> & { ref?: React.RefObject<HTMLTableSectionElement | null> }) => (
|
||||
<thead ref={ref} className={cn('[&_tr]:border-b', className)} {...props} />
|
||||
)
|
||||
TableHeader.displayName = 'TableHeader'
|
||||
|
||||
const TableBody = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableSectionElement> & { ref?: React.RefObject<HTMLTableSectionElement | null> }) => (
|
||||
<tbody ref={ref} className={cn('[&_tr:last-child]:border-0', className)} {...props} />
|
||||
)
|
||||
TableBody.displayName = 'TableBody'
|
||||
|
||||
const TableFooter = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableSectionElement> & { ref?: React.RefObject<HTMLTableSectionElement | null> }) => (
|
||||
<tfoot ref={ref} className={cn('border-t bg-muted/50 font-medium [&>tr]:last:border-b-0', className)} {...props} />
|
||||
)
|
||||
TableFooter.displayName = 'TableFooter'
|
||||
|
||||
const TableRow = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableRowElement> & { ref?: React.RefObject<HTMLTableRowElement | null> }) => (
|
||||
<tr
|
||||
ref={ref}
|
||||
className={cn('border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
TableRow.displayName = 'TableRow'
|
||||
|
||||
const TableHead = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ThHTMLAttributes<HTMLTableCellElement> & { ref?: React.RefObject<HTMLTableCellElement | null> }) => (
|
||||
<th
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'h-10 px-2 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
TableHead.displayName = 'TableHead'
|
||||
|
||||
const TableCell = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.TdHTMLAttributes<HTMLTableCellElement> & { ref?: React.RefObject<HTMLTableCellElement | null> }) => (
|
||||
<td
|
||||
ref={ref}
|
||||
className={cn('p-2 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
TableCell.displayName = 'TableCell'
|
||||
|
||||
const TableCaption = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLTableCaptionElement> & { ref?: React.RefObject<HTMLTableCaptionElement | null> }) => (
|
||||
<caption ref={ref} className={cn('mt-4 text-sm text-muted-foreground', className)} {...props} />
|
||||
)
|
||||
TableCaption.displayName = 'TableCaption'
|
||||
|
||||
export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow }
|
||||
23
packages/catalog/web/components/ui/textarea.tsx
Normal file
23
packages/catalog/web/components/ui/textarea.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import * as React from 'react'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const Textarea = ({
|
||||
ref,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<'textarea'> & { ref?: React.RefObject<HTMLTextAreaElement | null> }) => {
|
||||
return (
|
||||
<textarea
|
||||
className={cn(
|
||||
'flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm',
|
||||
className
|
||||
)}
|
||||
ref={ref}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Textarea.displayName = 'Textarea'
|
||||
|
||||
export { Textarea }
|
||||
18
packages/catalog/web/eslint.config.mjs
Normal file
18
packages/catalog/web/eslint.config.mjs
Normal file
@@ -0,0 +1,18 @@
|
||||
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||
import nextVitals from 'eslint-config-next/core-web-vitals'
|
||||
import nextTs from 'eslint-config-next/typescript'
|
||||
|
||||
const eslintConfig = defineConfig([
|
||||
...nextVitals,
|
||||
...nextTs,
|
||||
// Override default ignores of eslint-config-next.
|
||||
globalIgnores([
|
||||
// Default ignores of eslint-config-next:
|
||||
'.next/**',
|
||||
'out/**',
|
||||
'build/**',
|
||||
'next-env.d.ts'
|
||||
])
|
||||
])
|
||||
|
||||
export default eslintConfig
|
||||
299
packages/catalog/web/lib/api-client.ts
Normal file
299
packages/catalog/web/lib/api-client.ts
Normal file
@@ -0,0 +1,299 @@
|
||||
/**
|
||||
* API Client with SWR integration for catalog management
|
||||
*
|
||||
* This file provides:
|
||||
* - Custom SWR fetchers with Zod validation
|
||||
* - Mutations for CRUD operations with optimistic updates
|
||||
* - Error handling utilities
|
||||
* - Type-safe API interactions
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import type { SWRConfiguration, SWRResponse } from 'swr'
|
||||
import useSWR from 'swr'
|
||||
import useSWRMutation from 'swr/mutation'
|
||||
import type { z } from 'zod'
|
||||
|
||||
// Import catalog types and schemas
|
||||
import type { Model, PaginatedResponse, Provider } from './catalog-types'
|
||||
import {
|
||||
ModelSchema,
|
||||
ModelUpdateResponseSchema,
|
||||
PaginatedResponseSchema,
|
||||
ProviderSchema,
|
||||
ProviderUpdateResponseSchema
|
||||
} from './catalog-types'
|
||||
|
||||
// API base configuration
|
||||
const API_BASE = '/api/catalog'
|
||||
|
||||
// Extended error interface for better error handling
|
||||
export interface ExtendedApiError {
|
||||
error: string
|
||||
status?: number
|
||||
info?: unknown
|
||||
}
|
||||
|
||||
// Generic API fetcher with Zod validation
|
||||
async function apiFetcher<T extends z.ZodType>(url: string, schema: T, options?: RequestInit): Promise<z.infer<T>> {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options?.headers
|
||||
},
|
||||
...options
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = response.headers.get('content-type')?.includes('application/json')
|
||||
? await response.json()
|
||||
: { error: response.statusText }
|
||||
|
||||
const error: ExtendedApiError = {
|
||||
error: errorData.error || `HTTP ${response.status}`,
|
||||
status: response.status,
|
||||
info: errorData
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return schema.parse(data)
|
||||
}
|
||||
|
||||
// API Client class for organized endpoint management
|
||||
export class ApiClient {
|
||||
// Models endpoints
|
||||
static models = {
|
||||
// Get models with pagination and filtering
|
||||
list: (
|
||||
params: { page?: number; limit?: number; search?: string; capabilities?: string[]; providers?: string[] } = {}
|
||||
) => {
|
||||
const searchParams = new URLSearchParams()
|
||||
|
||||
if (params.page) searchParams.set('page', params.page.toString())
|
||||
if (params.limit) searchParams.set('limit', params.limit.toString())
|
||||
if (params.search) searchParams.set('search', params.search)
|
||||
if (params.capabilities?.length) searchParams.set('capabilities', params.capabilities.join(','))
|
||||
if (params.providers?.length) searchParams.set('providers', params.providers.join(','))
|
||||
|
||||
return `${API_BASE}/models?${searchParams.toString()}`
|
||||
},
|
||||
|
||||
// Update a model
|
||||
update: (id: string, data: Partial<Model>) => ({
|
||||
url: `${API_BASE}/models/${id}`,
|
||||
method: 'PUT',
|
||||
body: data
|
||||
}),
|
||||
|
||||
// Delete a model (if implemented)
|
||||
delete: (id: string) => ({
|
||||
url: `${API_BASE}/models/${id}`,
|
||||
method: 'DELETE'
|
||||
})
|
||||
}
|
||||
|
||||
// Providers endpoints
|
||||
static providers = {
|
||||
// Get providers with pagination and filtering
|
||||
list: (params: { page?: number; limit?: number; search?: string } = {}) => {
|
||||
const searchParams = new URLSearchParams()
|
||||
|
||||
if (params.page) searchParams.set('page', params.page.toString())
|
||||
if (params.limit) searchParams.set('limit', params.limit.toString())
|
||||
if (params.search) searchParams.set('search', params.search)
|
||||
|
||||
return `${API_BASE}/providers?${searchParams.toString()}`
|
||||
},
|
||||
|
||||
// Update a provider
|
||||
update: (id: string, data: Partial<Provider>) => ({
|
||||
url: `${API_BASE}/providers/${id}`,
|
||||
method: 'PUT',
|
||||
body: data
|
||||
}),
|
||||
|
||||
// Delete a provider (if implemented)
|
||||
delete: (id: string) => ({
|
||||
url: `${API_BASE}/providers/${id}`,
|
||||
method: 'DELETE'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// SWR Hooks for Models
|
||||
export function useModels(
|
||||
params: {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
capabilities?: string[]
|
||||
providers?: string[]
|
||||
} = {},
|
||||
config?: SWRConfiguration<PaginatedResponse<Model>, ExtendedApiError>
|
||||
): SWRResponse<PaginatedResponse<Model>, ExtendedApiError> {
|
||||
const url = ApiClient.models.list(params)
|
||||
|
||||
return useSWR<PaginatedResponse<Model>, ExtendedApiError>(
|
||||
url,
|
||||
(url) => apiFetcher(url, PaginatedResponseSchema(ModelSchema)),
|
||||
{
|
||||
revalidateOnFocus: true,
|
||||
revalidateOnReconnect: true,
|
||||
dedupingInterval: 5000,
|
||||
errorRetryCount: 3,
|
||||
errorRetryInterval: 1000,
|
||||
...config
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// SWR Hooks for Providers
|
||||
export function useProviders(
|
||||
params: {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
} = {},
|
||||
config?: SWRConfiguration<PaginatedResponse<Provider>, ExtendedApiError>
|
||||
): SWRResponse<PaginatedResponse<Provider>, ExtendedApiError> {
|
||||
const url = ApiClient.providers.list(params)
|
||||
|
||||
return useSWR<PaginatedResponse<Provider>, ExtendedApiError>(
|
||||
url,
|
||||
(url) => apiFetcher(url, PaginatedResponseSchema(ProviderSchema)),
|
||||
{
|
||||
revalidateOnFocus: true,
|
||||
revalidateOnReconnect: true,
|
||||
dedupingInterval: 5000,
|
||||
errorRetryCount: 3,
|
||||
errorRetryInterval: 1000,
|
||||
...config
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Mutation for updating models
|
||||
export function useUpdateModel() {
|
||||
return useSWRMutation(
|
||||
'/api/catalog/models',
|
||||
async (url: string, { arg }: { arg: { id: string; data: Partial<Model> } }) => {
|
||||
const response = await fetch(`${url}/${arg.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(arg.data)
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
const error: ExtendedApiError = {
|
||||
error: errorData.error || 'Failed to update model',
|
||||
status: response.status,
|
||||
info: errorData
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return ModelUpdateResponseSchema.parse(data)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Mutation for updating providers
|
||||
export function useUpdateProvider() {
|
||||
return useSWRMutation(
|
||||
'/api/catalog/providers',
|
||||
async (url: string, { arg }: { arg: { id: string; data: Partial<Provider> } }) => {
|
||||
const response = await fetch(`${url}/${arg.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(arg.data)
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
const error: ExtendedApiError = {
|
||||
error: errorData.error || 'Failed to update provider',
|
||||
status: response.status,
|
||||
info: errorData
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return ProviderUpdateResponseSchema.parse(data)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Utility function for global error handling
|
||||
export function handleApiError(error: unknown): ExtendedApiError {
|
||||
if (error && typeof error === 'object' && 'error' in error) {
|
||||
return error as ExtendedApiError
|
||||
}
|
||||
|
||||
return {
|
||||
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function to get user-friendly error messages
|
||||
export function getErrorMessage(error: unknown): string {
|
||||
const apiError = handleApiError(error)
|
||||
|
||||
// Map common error codes to user-friendly messages
|
||||
switch (apiError.status) {
|
||||
case 400:
|
||||
return 'Invalid request. Please check your input and try again.'
|
||||
case 401:
|
||||
return 'Authentication required. Please log in and try again.'
|
||||
case 403:
|
||||
return 'You do not have permission to perform this action.'
|
||||
case 404:
|
||||
return 'The requested resource was not found.'
|
||||
case 429:
|
||||
return 'Too many requests. Please wait a moment and try again.'
|
||||
case 500:
|
||||
return 'Server error. Please try again later.'
|
||||
default:
|
||||
return apiError.error || 'An unexpected error occurred.'
|
||||
}
|
||||
}
|
||||
|
||||
// Custom hook for debounced search
|
||||
export function useDebounce<T>(value: T, delay: number): T {
|
||||
const [debouncedValue, setDebouncedValue] = useState(value)
|
||||
|
||||
useEffect(() => {
|
||||
const handler = setTimeout(() => {
|
||||
setDebouncedValue(value)
|
||||
}, delay)
|
||||
|
||||
return () => {
|
||||
clearTimeout(handler)
|
||||
}
|
||||
}, [value, delay])
|
||||
|
||||
return debouncedValue
|
||||
}
|
||||
|
||||
// Export all types for use in components
|
||||
export type { SWRResponse }
|
||||
|
||||
// Re-export SWR types for convenience
|
||||
export type { SWRConfiguration } from 'swr'
|
||||
|
||||
// Legacy API Error class for backward compatibility
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status: number,
|
||||
public details?: unknown
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'ApiError'
|
||||
}
|
||||
}
|
||||
86
packages/catalog/web/lib/catalog-types.ts
Normal file
86
packages/catalog/web/lib/catalog-types.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Type definitions for catalog management system
|
||||
* Now using Zod inferred types for complete type safety
|
||||
*
|
||||
* This file serves as the main export point for all types and schemas.
|
||||
* Types are now inferred from Zod schemas to ensure compile-time and runtime consistency.
|
||||
*/
|
||||
|
||||
// Import all types from Zod validation schemas
|
||||
export type {
|
||||
// Response and error types
|
||||
ApiError,
|
||||
AuthenticationType,
|
||||
// Utility enum types
|
||||
CapabilityType,
|
||||
EndpointType,
|
||||
ModalityType,
|
||||
// Core data types (inferred from Zod schemas)
|
||||
Model,
|
||||
ModelListRequest,
|
||||
ModelsDataFile,
|
||||
ModelUpdateResponse,
|
||||
OverridesDataFile,
|
||||
PaginatedResponse,
|
||||
// Pagination and response types
|
||||
PaginationInfo,
|
||||
Provider,
|
||||
ProviderListRequest,
|
||||
ProviderModelOverride,
|
||||
ProvidersDataFile,
|
||||
ProviderUpdateResponse,
|
||||
SuccessResponse
|
||||
} from './validation'
|
||||
|
||||
// Import Zod schemas for direct use if needed
|
||||
export {
|
||||
ApiErrorSchema,
|
||||
AuthenticationTypeSchema,
|
||||
// Utility schemas
|
||||
CapabilityTypeSchema,
|
||||
EndpointTypeSchema,
|
||||
ModalityTypeSchema,
|
||||
ModelListRequestSchema,
|
||||
// Core schemas
|
||||
ModelSchema,
|
||||
ModelsDataFileSchema,
|
||||
ModelUpdateResponseSchema,
|
||||
OverridesDataFileSchema,
|
||||
PaginatedResponseSchema,
|
||||
ProviderModelOverrideSchema,
|
||||
// Response schemas
|
||||
PaginationInfoSchema,
|
||||
ProviderListRequestSchema,
|
||||
ProviderSchema,
|
||||
ProvidersDataFileSchema,
|
||||
ProviderUpdateResponseSchema,
|
||||
QueryParamsSchema,
|
||||
SuccessResponseSchema
|
||||
} from './validation'
|
||||
|
||||
// Import validation utilities for easy access
|
||||
export {
|
||||
createErrorResponse,
|
||||
formatZodError,
|
||||
// Type guard functions (powered by Zod)
|
||||
isModel,
|
||||
isModelsDataFile,
|
||||
isProvider,
|
||||
isProvidersDataFile,
|
||||
safeParseWithValidation,
|
||||
safeTypeCast,
|
||||
validatePaginatedResponse,
|
||||
validateQueryParams,
|
||||
validateString,
|
||||
// Validation functions
|
||||
ValidationError
|
||||
} from './validation'
|
||||
|
||||
// Legacy convenience types (for backward compatibility)
|
||||
// These are now re-exports of the Zod-inferred types above
|
||||
export type {
|
||||
// Re-export core types with legacy names for compatibility
|
||||
Model as CatalogModel,
|
||||
Provider as CatalogProvider,
|
||||
PaginatedResponse as CatalogResponse
|
||||
} from './validation'
|
||||
6
packages/catalog/web/lib/utils.ts
Normal file
6
packages/catalog/web/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { type ClassValue, clsx } from 'clsx'
|
||||
import { twMerge } from 'tailwind-merge'
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
431
packages/catalog/web/lib/validation.ts
Normal file
431
packages/catalog/web/lib/validation.ts
Normal file
@@ -0,0 +1,431 @@
|
||||
/**
|
||||
* Zod v4 schemas for comprehensive runtime type validation
|
||||
* Replaces manual validation with strict type-safe schemas
|
||||
*/
|
||||
|
||||
//TODO: 从catalog导入
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
// Base parameter schemas
|
||||
const ParameterRangeSchema = z.object({
|
||||
supported: z.literal(true),
|
||||
min: z.number().positive(),
|
||||
max: z.number().positive(),
|
||||
default: z.number().positive()
|
||||
})
|
||||
|
||||
const ParameterBooleanSchema = z.object({
|
||||
supported: z.boolean()
|
||||
})
|
||||
|
||||
const ParameterUnsupportedSchema = z.object({
|
||||
supported: z.literal(false)
|
||||
})
|
||||
|
||||
const ParameterValueSchema = z.union([ParameterRangeSchema, ParameterBooleanSchema, ParameterUnsupportedSchema])
|
||||
|
||||
// Model parameters schema
|
||||
const ModelParametersSchema = z
|
||||
.object({
|
||||
temperature: ParameterValueSchema.optional(),
|
||||
max_tokens: z.union([
|
||||
z.boolean(), // Simple boolean support indicator
|
||||
z.object({
|
||||
supported: z.literal(true),
|
||||
default: z.number().positive().optional()
|
||||
})
|
||||
]).optional(),
|
||||
system_message: z.boolean().optional(), // Simple boolean support indicator
|
||||
top_p: z.union([ParameterValueSchema, ParameterUnsupportedSchema]).optional()
|
||||
})
|
||||
.loose() // Allow additional parameter types
|
||||
|
||||
// Pricing schema
|
||||
const PricingInfoSchema = z.object({
|
||||
input: z.object({
|
||||
per_million_tokens: z.number().nonnegative(),
|
||||
currency: z.string().length(3) // ISO 4217 currency codes
|
||||
}),
|
||||
output: z.object({
|
||||
per_million_tokens: z.number().nonnegative(),
|
||||
currency: z.string().length(3)
|
||||
})
|
||||
})
|
||||
|
||||
// Model metadata schema
|
||||
const ModelMetadataSchema = z
|
||||
.object({
|
||||
source: z.string().optional(),
|
||||
original_provider: z.string().optional(),
|
||||
supports_caching: z.boolean().optional()
|
||||
})
|
||||
.loose() // Allow additional metadata
|
||||
|
||||
// Complete Model schema
|
||||
export const ModelSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().optional(),
|
||||
owned_by: z.string().optional(),
|
||||
capabilities: z.array(z.string()),
|
||||
input_modalities: z.array(z.string()),
|
||||
output_modalities: z.array(z.string()),
|
||||
context_window: z.number().positive(),
|
||||
max_output_tokens: z.number().positive(),
|
||||
max_input_tokens: z.number().positive().optional(),
|
||||
pricing: PricingInfoSchema.optional(),
|
||||
parameters: ModelParametersSchema.optional(),
|
||||
endpoint_types: z.array(z.string()).optional(),
|
||||
metadata: ModelMetadataSchema.optional()
|
||||
})
|
||||
|
||||
// Provider behaviors schema
|
||||
const ProviderBehaviorsSchema = z
|
||||
.object({
|
||||
supports_custom_models: z.boolean(),
|
||||
provides_model_mapping: z.boolean(),
|
||||
supports_model_versioning: z.boolean(),
|
||||
provides_fallback_routing: z.boolean(),
|
||||
has_auto_retry: z.boolean(),
|
||||
supports_health_check: z.boolean(),
|
||||
has_real_time_metrics: z.boolean(),
|
||||
provides_usage_analytics: z.boolean(),
|
||||
supports_webhook_events: z.boolean(),
|
||||
requires_api_key_validation: z.boolean(),
|
||||
supports_rate_limiting: z.boolean(),
|
||||
provides_usage_limits: z.boolean(),
|
||||
supports_streaming: z.boolean(),
|
||||
supports_batch_processing: z.boolean(),
|
||||
supports_model_fine_tuning: z.boolean()
|
||||
})
|
||||
.loose() // Allow extensions
|
||||
|
||||
// API compatibility schema
|
||||
const ApiCompatibilitySchema = z
|
||||
.object({
|
||||
supports_array_content: z.boolean().optional(),
|
||||
supports_stream_options: z.boolean().optional(),
|
||||
supports_developer_role: z.boolean().optional(),
|
||||
supports_service_tier: z.boolean().optional(),
|
||||
supports_thinking_control: z.boolean().optional(),
|
||||
supports_api_version: z.boolean().optional(),
|
||||
supports_parallel_tools: z.boolean().optional(),
|
||||
supports_multimodal: z.boolean().optional()
|
||||
})
|
||||
.loose()
|
||||
|
||||
// Special configuration schema (flexible)
|
||||
const SpecialConfigSchema = z.record(z.string(), z.unknown())
|
||||
|
||||
// Provider metadata schema
|
||||
const ProviderMetadataSchema = z
|
||||
.object({
|
||||
source: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
reliability: z.enum(['low', 'medium', 'high']).optional()
|
||||
})
|
||||
.loose()
|
||||
|
||||
// Complete Provider schema
|
||||
export const ProviderSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
description: z.string().optional(),
|
||||
authentication: z.string().min(1),
|
||||
pricing_model: z.string().min(1),
|
||||
model_routing: z.string().min(1),
|
||||
behaviors: ProviderBehaviorsSchema,
|
||||
supported_endpoints: z.array(z.string()),
|
||||
api_compatibility: ApiCompatibilitySchema.optional(),
|
||||
default_api_host: z.url().optional(),
|
||||
default_rate_limit: z.number().positive().optional(),
|
||||
model_id_patterns: z.array(z.string()).optional(),
|
||||
alias_model_ids: z.record(z.string(), z.string()).optional(),
|
||||
documentation: z.string().url().optional(),
|
||||
website: z.string().url().optional(),
|
||||
deprecated: z.boolean(),
|
||||
maintenance_mode: z.boolean(),
|
||||
config_version: z.string().min(1),
|
||||
special_config: SpecialConfigSchema.optional(),
|
||||
metadata: ProviderMetadataSchema.optional()
|
||||
})
|
||||
|
||||
// Data file schemas
|
||||
export const ModelsDataFileSchema = z.object({
|
||||
version: z.string().min(1),
|
||||
models: z.array(ModelSchema)
|
||||
})
|
||||
|
||||
export const ProvidersDataFileSchema = z.object({
|
||||
version: z.string().min(1),
|
||||
providers: z.array(ProviderSchema)
|
||||
})
|
||||
|
||||
// Override schemas
|
||||
const OverrideLimitsSchema = z.object({
|
||||
context_window: z.number().positive().optional(),
|
||||
max_output_tokens: z.number().positive().optional()
|
||||
})
|
||||
|
||||
export const ProviderModelOverrideSchema = z.object({
|
||||
provider_id: z.string().min(1),
|
||||
model_id: z.string().min(1),
|
||||
disabled: z.boolean().default(false),
|
||||
reason: z.string().optional(),
|
||||
last_updated: z.string().optional(),
|
||||
updated_by: z.string().optional(),
|
||||
priority: z.number().default(100),
|
||||
limits: OverrideLimitsSchema.optional(),
|
||||
pricing: PricingInfoSchema.optional()
|
||||
})
|
||||
|
||||
export const OverridesDataFileSchema = z.object({
|
||||
version: z.string().min(1),
|
||||
overrides: z.array(ProviderModelOverrideSchema)
|
||||
})
|
||||
|
||||
// Pagination schemas
|
||||
export const PaginationInfoSchema = z.object({
|
||||
page: z.number().positive(),
|
||||
limit: z.number().positive().max(100),
|
||||
total: z.number().nonnegative(),
|
||||
totalPages: z.number().nonnegative(),
|
||||
hasNext: z.boolean(),
|
||||
hasPrev: z.boolean()
|
||||
})
|
||||
|
||||
export const PaginatedResponseSchema = <T extends z.ZodType>(itemSchema: T) =>
|
||||
z.object({
|
||||
data: z.array(itemSchema),
|
||||
pagination: PaginationInfoSchema
|
||||
})
|
||||
|
||||
// Query parameter schemas
|
||||
export const QueryParamsSchema = z.object({
|
||||
page: z.coerce.number().positive().default(1),
|
||||
limit: z.coerce.number().positive().max(100).default(20),
|
||||
search: z.string().trim().optional(),
|
||||
capabilities: z.array(z.string()).optional(),
|
||||
providers: z.array(z.string()).optional(),
|
||||
authentication: z.array(z.string()).optional()
|
||||
})
|
||||
|
||||
// Request schemas for API endpoints
|
||||
export const ModelListRequestSchema = QueryParamsSchema.extend({
|
||||
capabilities: z.array(z.string()).optional(),
|
||||
providers: z.array(z.string()).optional()
|
||||
})
|
||||
|
||||
export const ProviderListRequestSchema = QueryParamsSchema.extend({
|
||||
authentication: z.array(z.string()).optional()
|
||||
})
|
||||
|
||||
// Response schemas
|
||||
export const ApiErrorSchema = z.object({
|
||||
error: z.string(),
|
||||
details: z.unknown().optional()
|
||||
})
|
||||
|
||||
export const SuccessResponseSchema = z.object({
|
||||
success: z.literal(true)
|
||||
})
|
||||
|
||||
export const ModelUpdateResponseSchema = SuccessResponseSchema.extend({
|
||||
model: ModelSchema
|
||||
})
|
||||
|
||||
export const ProviderUpdateResponseSchema = SuccessResponseSchema.extend({
|
||||
provider: ProviderSchema
|
||||
})
|
||||
|
||||
// Utility types for strict typing
|
||||
export const CapabilityTypeSchema = z.enum([
|
||||
'FUNCTION_CALL',
|
||||
'REASONING',
|
||||
'IMAGE_RECOGNITION',
|
||||
'IMAGE_GENERATION',
|
||||
'AUDIO_RECOGNITION',
|
||||
'AUDIO_GENERATION',
|
||||
'EMBEDDING',
|
||||
'RERANK',
|
||||
'AUDIO_TRANSCRIPT',
|
||||
'VIDEO_RECOGNITION',
|
||||
'VIDEO_GENERATION',
|
||||
'STRUCTURED_OUTPUT',
|
||||
'FILE_INPUT',
|
||||
'WEB_SEARCH',
|
||||
'CODE_EXECUTION',
|
||||
'FILE_SEARCH',
|
||||
'COMPUTER_USE'
|
||||
])
|
||||
|
||||
export const ModalityTypeSchema = z.enum(['TEXT', 'VISION', 'AUDIO', 'VIDEO'])
|
||||
|
||||
export const AuthenticationTypeSchema = z.enum(['API_KEY', 'OAUTH', 'NONE', 'CUSTOM'])
|
||||
|
||||
export const EndpointTypeSchema = z.enum(['CHAT_COMPLETIONS', 'MESSAGES', 'RESPONSES', 'EMBEDDINGS', 'RERANK'])
|
||||
|
||||
// Validation utilities using Zod
|
||||
|
||||
// Custom error class for Zod validation errors
|
||||
export class ValidationError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public details?: unknown,
|
||||
public zodError?: z.ZodError
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'ValidationError'
|
||||
}
|
||||
}
|
||||
|
||||
// String validation function
|
||||
export function validateString(value: string, fieldName: string): string {
|
||||
if (typeof value !== 'string' || value.trim().length === 0) {
|
||||
throw new ValidationError(`${fieldName} must be a non-empty string`)
|
||||
}
|
||||
return value.trim()
|
||||
}
|
||||
|
||||
// Safe JSON parsing with Zod validation
|
||||
export async function safeParseWithValidation<T>(
|
||||
jsonString: string,
|
||||
schema: z.ZodType<T>,
|
||||
errorMessage: string
|
||||
): Promise<T> {
|
||||
try {
|
||||
const parsed = JSON.parse(jsonString)
|
||||
const result = schema.safeParse(parsed)
|
||||
|
||||
if (!result.success) {
|
||||
throw new ValidationError(`${errorMessage}: ${result.error.message}`, result.error.issues, result.error)
|
||||
}
|
||||
|
||||
return result.data
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
throw new ValidationError('Invalid JSON format', { originalError: error.message })
|
||||
}
|
||||
if (error instanceof ValidationError) {
|
||||
throw error
|
||||
}
|
||||
throw new ValidationError(
|
||||
`Unexpected error during validation: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate API response structure using Zod
|
||||
export function validatePaginatedResponse<T>(
|
||||
data: unknown,
|
||||
itemSchema: z.ZodType<T>
|
||||
): z.infer<ReturnType<typeof PaginatedResponseSchema<typeof itemSchema>>> {
|
||||
const schema = PaginatedResponseSchema(itemSchema)
|
||||
const result = schema.safeParse(data)
|
||||
|
||||
if (!result.success) {
|
||||
throw new ValidationError(`Invalid response format: ${result.error.message}`, result.error.issues, result.error)
|
||||
}
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
// Validate and sanitize query parameters using Zod
|
||||
export function validateQueryParams(params: URLSearchParams): z.infer<typeof QueryParamsSchema> {
|
||||
const queryParams: Record<string, string | string[]> = {}
|
||||
|
||||
// Handle all parameters - Array.from() for compatibility
|
||||
Array.from(params.entries()).forEach(([key, value]) => {
|
||||
if (['capabilities', 'providers', 'authentication'].includes(key)) {
|
||||
if (!queryParams[key]) {
|
||||
queryParams[key] = []
|
||||
}
|
||||
;(queryParams[key] as string[]).push(value)
|
||||
} else {
|
||||
queryParams[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
const result = QueryParamsSchema.safeParse(queryParams)
|
||||
|
||||
if (!result.success) {
|
||||
throw new ValidationError(`Invalid query parameters: ${result.error.message}`, result.error.issues, result.error)
|
||||
}
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
// Type-safe error response creation
|
||||
export function createErrorResponse(
|
||||
message: string,
|
||||
status: number = 500,
|
||||
details?: unknown
|
||||
): z.infer<typeof ApiErrorSchema> {
|
||||
const error: z.infer<typeof ApiErrorSchema> = { error: message }
|
||||
if (details !== undefined) {
|
||||
;(error as any).details = details
|
||||
}
|
||||
return error
|
||||
}
|
||||
|
||||
// Safe type casting utility using Zod
|
||||
export function safeTypeCast<T>(value: unknown, schema: z.ZodType<T>, typeName?: string): T {
|
||||
const result = schema.safeParse(value)
|
||||
if (!result.success) {
|
||||
throw new ValidationError(
|
||||
`Expected ${typeName || schema.description || 'valid type'}, but validation failed: ${result.error.message}`,
|
||||
result.error.issues,
|
||||
result.error
|
||||
)
|
||||
}
|
||||
return result.data
|
||||
}
|
||||
|
||||
// Utility function to extract validation error details
|
||||
export function formatZodError(error: z.ZodError): string {
|
||||
return error.issues
|
||||
.map((issue) => {
|
||||
const path = issue.path.join('.')
|
||||
return `${path ? `${path}: ` : ''}${issue.message}`
|
||||
})
|
||||
.join('; ')
|
||||
}
|
||||
|
||||
// Export inferred types
|
||||
export type Model = z.infer<typeof ModelSchema>
|
||||
export type Provider = z.infer<typeof ProviderSchema>
|
||||
export type ProviderModelOverride = z.infer<typeof ProviderModelOverrideSchema>
|
||||
export type ModelsDataFile = z.infer<typeof ModelsDataFileSchema>
|
||||
export type ProvidersDataFile = z.infer<typeof ProvidersDataFileSchema>
|
||||
export type OverridesDataFile = z.infer<typeof OverridesDataFileSchema>
|
||||
export type PaginationInfo = z.infer<typeof PaginationInfoSchema>
|
||||
export type PaginatedResponse<T> = z.infer<ReturnType<typeof PaginatedResponseSchema<z.ZodType<T>>>>
|
||||
export type ModelListRequest = z.infer<typeof ModelListRequestSchema>
|
||||
export type ProviderListRequest = z.infer<typeof ProviderListRequestSchema>
|
||||
export type ApiError = z.infer<typeof ApiErrorSchema>
|
||||
export type SuccessResponse = z.infer<typeof SuccessResponseSchema>
|
||||
export type ModelUpdateResponse = z.infer<typeof ModelUpdateResponseSchema>
|
||||
export type ProviderUpdateResponse = z.infer<typeof ProviderUpdateResponseSchema>
|
||||
|
||||
// Export enum types for convenience
|
||||
export type CapabilityType = z.infer<typeof CapabilityTypeSchema>
|
||||
export type ModalityType = z.infer<typeof ModalityTypeSchema>
|
||||
export type AuthenticationType = z.infer<typeof AuthenticationTypeSchema>
|
||||
export type EndpointType = z.infer<typeof EndpointTypeSchema>
|
||||
|
||||
// Legacy compatibility type guards (now using Zod internally)
|
||||
export function isModel(obj: unknown): obj is Model {
|
||||
return ModelSchema.safeParse(obj).success
|
||||
}
|
||||
|
||||
export function isProvider(obj: unknown): obj is Provider {
|
||||
return ProviderSchema.safeParse(obj).success
|
||||
}
|
||||
|
||||
export function isModelsDataFile(obj: unknown): obj is ModelsDataFile {
|
||||
return ModelsDataFileSchema.safeParse(obj).success
|
||||
}
|
||||
|
||||
export function isProvidersDataFile(obj: unknown): obj is ProvidersDataFile {
|
||||
return ProvidersDataFileSchema.safeParse(obj).success
|
||||
}
|
||||
40
packages/catalog/web/next.config.ts
Normal file
40
packages/catalog/web/next.config.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { NextConfig } from 'next'
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
// Configure static file serving from external directory
|
||||
async rewrites() {
|
||||
return [
|
||||
// Proxy API requests to the catalog API
|
||||
{
|
||||
source: '/api/catalog/:path*',
|
||||
destination: 'http://localhost:3001/api/catalog/:path*'
|
||||
}
|
||||
]
|
||||
},
|
||||
// Add custom headers for static files
|
||||
async headers() {
|
||||
return [
|
||||
{
|
||||
source: '/data/:path*',
|
||||
headers: [
|
||||
{
|
||||
key: 'Cache-Control',
|
||||
value: 'public, max-age=3600, must-revalidate'
|
||||
},
|
||||
{
|
||||
key: 'Access-Control-Allow-Origin',
|
||||
value: '*'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
// Configure serving static files from outside public directory
|
||||
outputFileTracingExcludes: {
|
||||
'*': ['./**/__tests__/**/*']
|
||||
},
|
||||
// Basic Turbopack configuration to silence warning
|
||||
turbopack: {}
|
||||
}
|
||||
|
||||
export default nextConfig
|
||||
32
packages/catalog/web/package.json
Normal file
32
packages/catalog/web/package.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "@cherrystudio/catalog-web",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "eslint"
|
||||
},
|
||||
"dependencies": {
|
||||
"@radix-ui/react-dialog": "^1.1.15",
|
||||
"@radix-ui/react-icons": "^1.3.2",
|
||||
"@radix-ui/react-separator": "^1.1.8",
|
||||
"@radix-ui/react-slot": "^1.2.4",
|
||||
"next": "16.0.6",
|
||||
"react": "19.2.0",
|
||||
"react-dom": "19.2.0",
|
||||
"swr": "^2.3.7",
|
||||
"zod": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tailwindcss/postcss": "^4",
|
||||
"@types/node": "^20",
|
||||
"@types/react": "^19",
|
||||
"@types/react-dom": "^19",
|
||||
"eslint": "^9",
|
||||
"eslint-config-next": "16.0.6",
|
||||
"tailwindcss": "^4",
|
||||
"typescript": "^5"
|
||||
}
|
||||
}
|
||||
7
packages/catalog/web/postcss.config.mjs
Normal file
7
packages/catalog/web/postcss.config.mjs
Normal file
@@ -0,0 +1,7 @@
|
||||
const config = {
|
||||
plugins: {
|
||||
'@tailwindcss/postcss': {}
|
||||
}
|
||||
}
|
||||
|
||||
export default config
|
||||
1
packages/catalog/web/public/file.svg
Normal file
1
packages/catalog/web/public/file.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 391 B |
1
packages/catalog/web/public/globe.svg
Normal file
1
packages/catalog/web/public/globe.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>
|
||||
|
After Width: | Height: | Size: 1.0 KiB |
1
packages/catalog/web/public/next.svg
Normal file
1
packages/catalog/web/public/next.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
1
packages/catalog/web/public/vercel.svg
Normal file
1
packages/catalog/web/public/vercel.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>
|
||||
|
After Width: | Height: | Size: 128 B |
1
packages/catalog/web/public/window.svg
Normal file
1
packages/catalog/web/public/window.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>
|
||||
|
After Width: | Height: | Size: 385 B |
27
packages/catalog/web/tsconfig.json
Normal file
27
packages/catalog/web/tsconfig.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "react-jsx",
|
||||
"incremental": true,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", ".next/dev/types/**/*.ts", "**/*.mts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -2,7 +2,7 @@ export enum IpcChannel {
|
||||
App_GetCacheSize = 'app:get-cache-size',
|
||||
App_ClearCache = 'app:clear-cache',
|
||||
App_SetLaunchOnBoot = 'app:set-launch-on-boot',
|
||||
App_SetLanguage = 'app:set-language',
|
||||
// App_SetLanguage = 'app:set-language',
|
||||
App_SetEnableSpellCheck = 'app:set-enable-spell-check',
|
||||
App_SetSpellCheckLanguages = 'app:set-spell-check-languages',
|
||||
App_CheckForUpdate = 'app:check-for-update',
|
||||
@@ -14,7 +14,7 @@ export enum IpcChannel {
|
||||
App_SetLaunchToTray = 'app:set-launch-to-tray',
|
||||
App_SetTray = 'app:set-tray',
|
||||
App_SetTrayOnClose = 'app:set-tray-on-close',
|
||||
App_SetTheme = 'app:set-theme',
|
||||
// App_SetTheme = 'app:set-theme',
|
||||
App_SetAutoUpdate = 'app:set-auto-update',
|
||||
App_SetTestPlan = 'app:set-test-plan',
|
||||
App_SetTestChannel = 'app:set-test-channel',
|
||||
@@ -47,7 +47,7 @@ export enum IpcChannel {
|
||||
App_MacRequestProcessTrust = 'app:mac-request-process-trust',
|
||||
|
||||
App_QuoteToMain = 'app:quote-to-main',
|
||||
App_SetDisableHardwareAcceleration = 'app:set-disable-hardware-acceleration',
|
||||
// App_SetDisableHardwareAcceleration = 'app:set-disable-hardware-acceleration',
|
||||
|
||||
Notification_Send = 'notification:send',
|
||||
Notification_OnClick = 'notification:on-click',
|
||||
@@ -227,6 +227,22 @@ export enum IpcChannel {
|
||||
Backup_DeleteS3File = 'backup:deleteS3File',
|
||||
Backup_CheckS3Connection = 'backup:checkS3Connection',
|
||||
|
||||
// data migration
|
||||
DataMigrate_CheckNeeded = 'data-migrate:check-needed',
|
||||
DataMigrate_GetProgress = 'data-migrate:get-progress',
|
||||
DataMigrate_Cancel = 'data-migrate:cancel',
|
||||
DataMigrate_RequireBackup = 'data-migrate:require-backup',
|
||||
DataMigrate_BackupCompleted = 'data-migrate:backup-completed',
|
||||
DataMigrate_ShowBackupDialog = 'data-migrate:show-backup-dialog',
|
||||
DataMigrate_StartFlow = 'data-migrate:start-flow',
|
||||
DataMigrate_ProceedToBackup = 'data-migrate:proceed-to-backup',
|
||||
DataMigrate_StartMigration = 'data-migrate:start-migration',
|
||||
DataMigrate_RetryMigration = 'data-migrate:retry-migration',
|
||||
DataMigrate_RestartApp = 'data-migrate:restart-app',
|
||||
DataMigrate_CloseWindow = 'data-migrate:close-window',
|
||||
DataMigrate_SendReduxData = 'data-migrate:send-redux-data',
|
||||
DataMigrate_GetReduxData = 'data-migrate:get-redux-data',
|
||||
|
||||
// zip
|
||||
Zip_Compress = 'zip:compress',
|
||||
Zip_Decompress = 'zip:decompress',
|
||||
@@ -242,7 +258,8 @@ export enum IpcChannel {
|
||||
|
||||
// events
|
||||
BackupProgress = 'backup-progress',
|
||||
ThemeUpdated = 'theme:updated',
|
||||
DataMigrateProgress = 'data-migrate-progress',
|
||||
NativeThemeUpdated = 'native-theme:updated',
|
||||
RestoreProgress = 'restore-progress',
|
||||
UpdateError = 'update-error',
|
||||
UpdateAvailable = 'update-available',
|
||||
@@ -281,12 +298,6 @@ export enum IpcChannel {
|
||||
Selection_ToolbarVisibilityChange = 'selection:toolbar-visibility-change',
|
||||
Selection_ToolbarDetermineSize = 'selection:toolbar-determine-size',
|
||||
Selection_WriteToClipboard = 'selection:write-to-clipboard',
|
||||
Selection_SetEnabled = 'selection:set-enabled',
|
||||
Selection_SetTriggerMode = 'selection:set-trigger-mode',
|
||||
Selection_SetFilterMode = 'selection:set-filter-mode',
|
||||
Selection_SetFilterList = 'selection:set-filter-list',
|
||||
Selection_SetFollowToolbar = 'selection:set-follow-toolbar',
|
||||
Selection_SetRemeberWinSize = 'selection:set-remeber-win-size',
|
||||
Selection_ActionWindowClose = 'selection:action-window-close',
|
||||
Selection_ActionWindowMinimize = 'selection:action-window-minimize',
|
||||
Selection_ActionWindowPin = 'selection:action-window-pin',
|
||||
@@ -305,6 +316,27 @@ export enum IpcChannel {
|
||||
Memory_DeleteAllMemoriesForUser = 'memory:delete-all-memories-for-user',
|
||||
Memory_GetUsersList = 'memory:get-users-list',
|
||||
|
||||
// Data: Preference
|
||||
Preference_Get = 'preference:get',
|
||||
Preference_Set = 'preference:set',
|
||||
Preference_GetMultiple = 'preference:get-multiple',
|
||||
Preference_SetMultiple = 'preference:set-multiple',
|
||||
Preference_GetAll = 'preference:get-all',
|
||||
Preference_Subscribe = 'preference:subscribe',
|
||||
Preference_Changed = 'preference:changed',
|
||||
|
||||
// Data: Cache
|
||||
Cache_Sync = 'cache:sync',
|
||||
Cache_SyncBatch = 'cache:sync-batch',
|
||||
|
||||
// Data: API Channels
|
||||
DataApi_Request = 'data-api:request',
|
||||
DataApi_Batch = 'data-api:batch',
|
||||
DataApi_Transaction = 'data-api:transaction',
|
||||
DataApi_Subscribe = 'data-api:subscribe',
|
||||
DataApi_Unsubscribe = 'data-api:unsubscribe',
|
||||
DataApi_Stream = 'data-api:stream',
|
||||
|
||||
// TRACE
|
||||
TRACE_SAVE_DATA = 'trace:saveData',
|
||||
TRACE_GET_DATA = 'trace:getData',
|
||||
|
||||
@@ -202,11 +202,11 @@ export enum UpdateConfigUrl {
|
||||
GITCODE = 'https://raw.gitcode.com/CherryHQ/cherry-studio/raw/x-files%2Fapp-upgrade-config/app-upgrade-config.json'
|
||||
}
|
||||
|
||||
export enum UpgradeChannel {
|
||||
LATEST = 'latest', // 最新稳定版本
|
||||
RC = 'rc', // 公测版本
|
||||
BETA = 'beta' // 预览版本
|
||||
}
|
||||
// export enum UpgradeChannel {
|
||||
// LATEST = 'latest', // 最新稳定版本
|
||||
// RC = 'rc', // 公测版本
|
||||
// BETA = 'beta' // 预览版本
|
||||
// }
|
||||
|
||||
export enum UpdateMirror {
|
||||
GITHUB = 'github',
|
||||
|
||||
106
packages/shared/data/README.md
Normal file
106
packages/shared/data/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# Cherry Studio Shared Data
|
||||
|
||||
This directory contains shared type definitions and schemas for the Cherry Studio data management systems. These files provide type safety and consistency across the entire application.
|
||||
|
||||
## 📁 Directory Structure
|
||||
|
||||
```
|
||||
packages/shared/data/
|
||||
├── api/ # Data API type system
|
||||
│ ├── index.ts # Barrel exports for clean imports
|
||||
│ ├── apiSchemas.ts # API endpoint definitions and mappings
|
||||
│ ├── apiTypes.ts # Core request/response infrastructure types
|
||||
│ ├── apiModels.ts # Business entity types and DTOs
|
||||
│ ├── apiPaths.ts # API path definitions and utilities
|
||||
│ └── errorCodes.ts # Standardized error handling
|
||||
├── cache/ # Cache system type definitions
|
||||
│ ├── cacheTypes.ts # Core cache infrastructure types
|
||||
│ ├── cacheSchemas.ts # Cache key schemas and type mappings
|
||||
│ └── cacheValueTypes.ts # Cache value type definitions
|
||||
├── preference/ # Preference system type definitions
|
||||
│ ├── preferenceTypes.ts # Core preference system types
|
||||
│ └── preferenceSchemas.ts # Preference schemas and default values
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
## 🏗️ System Overview
|
||||
|
||||
This directory provides type definitions for three main data management systems:
|
||||
|
||||
### API System (`api/`)
|
||||
- **Purpose**: Type-safe IPC communication between Main and Renderer processes
|
||||
- **Features**: RESTful patterns, error handling, business entity definitions
|
||||
- **Usage**: Ensures type safety for all data API operations
|
||||
|
||||
### Cache System (`cache/`)
|
||||
- **Purpose**: Type definitions for three-layer caching architecture
|
||||
- **Features**: Memory/shared/persist cache schemas, TTL support, hook integration
|
||||
- **Usage**: Type-safe caching operations across the application
|
||||
|
||||
### Preference System (`preference/`)
|
||||
- **Purpose**: User configuration and settings management
|
||||
- **Features**: 158 configuration items, default values, nested key support
|
||||
- **Usage**: Type-safe preference access and synchronization
|
||||
|
||||
## 📋 File Categories
|
||||
|
||||
**Framework Infrastructure** - These are TypeScript type definitions that:
|
||||
- ✅ Exist only at compile time
|
||||
- ✅ Provide type safety and IntelliSense support
|
||||
- ✅ Define contracts between application layers
|
||||
- ✅ Enable static analysis and error detection
|
||||
|
||||
## 📖 Usage Examples
|
||||
|
||||
### API Types
|
||||
```typescript
|
||||
// Import API types
|
||||
import type { DataRequest, DataResponse, ApiSchemas } from '@shared/data/api'
|
||||
```
|
||||
|
||||
### Cache Types
|
||||
```typescript
|
||||
// Import cache types
|
||||
import type { UseCacheKey, UseSharedCacheKey } from '@shared/data/cache'
|
||||
```
|
||||
|
||||
### Preference Types
|
||||
```typescript
|
||||
// Import preference types
|
||||
import type { PreferenceKeyType, PreferenceDefaultScopeType } from '@shared/data/preference'
|
||||
```
|
||||
|
||||
## 🔧 Development Guidelines
|
||||
|
||||
### Adding Cache Types
|
||||
1. Add cache key to `cache/cacheSchemas.ts`
|
||||
2. Define value type in `cache/cacheValueTypes.ts`
|
||||
3. Update type mappings for type safety
|
||||
|
||||
### Adding Preference Types
|
||||
1. Add preference key to `preference/preferenceSchemas.ts`
|
||||
2. Define default value and type
|
||||
3. Preference system automatically picks up new keys
|
||||
|
||||
### Adding API Types
|
||||
1. Define business entities in `api/apiModels.ts`
|
||||
2. Add endpoint definitions to `api/apiSchemas.ts`
|
||||
3. Export types from `api/index.ts`
|
||||
|
||||
### Best Practices
|
||||
- Use `import type` for type-only imports
|
||||
- Follow existing naming conventions
|
||||
- Document complex types with JSDoc
|
||||
- Maintain type safety across all imports
|
||||
|
||||
## 🔗 Related Implementation
|
||||
|
||||
### Main Process Services
|
||||
- `src/main/data/CacheService.ts` - Main process cache management
|
||||
- `src/main/data/PreferenceService.ts` - Preference management service
|
||||
- `src/main/data/DataApiService.ts` - Data API coordination service
|
||||
|
||||
### Renderer Process Services
|
||||
- `src/renderer/src/data/CacheService.ts` - Renderer cache service
|
||||
- `src/renderer/src/data/PreferenceService.ts` - Renderer preference service
|
||||
- `src/renderer/src/data/DataApiService.ts` - Renderer API client
|
||||
107
packages/shared/data/api/apiModels.ts
Normal file
107
packages/shared/data/api/apiModels.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Generic test model definitions
|
||||
* Contains flexible types for comprehensive API testing
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic test item entity - flexible structure for testing various scenarios
|
||||
*/
|
||||
export interface TestItem {
|
||||
/** Unique identifier */
|
||||
id: string
|
||||
/** Item title */
|
||||
title: string
|
||||
/** Optional description */
|
||||
description?: string
|
||||
/** Type category */
|
||||
type: string
|
||||
/** Current status */
|
||||
status: string
|
||||
/** Priority level */
|
||||
priority: string
|
||||
/** Associated tags */
|
||||
tags: string[]
|
||||
/** Creation timestamp */
|
||||
createdAt: string
|
||||
/** Last update timestamp */
|
||||
updatedAt: string
|
||||
/** Additional metadata */
|
||||
metadata: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Data Transfer Objects (DTOs) for test operations
|
||||
*/
|
||||
|
||||
/**
|
||||
* DTO for creating a new test item
|
||||
*/
|
||||
export interface CreateTestItemDto {
|
||||
/** Item title */
|
||||
title: string
|
||||
/** Optional description */
|
||||
description?: string
|
||||
/** Type category */
|
||||
type?: string
|
||||
/** Current status */
|
||||
status?: string
|
||||
/** Priority level */
|
||||
priority?: string
|
||||
/** Associated tags */
|
||||
tags?: string[]
|
||||
/** Additional metadata */
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* DTO for updating an existing test item
|
||||
*/
|
||||
export interface UpdateTestItemDto {
|
||||
/** Updated title */
|
||||
title?: string
|
||||
/** Updated description */
|
||||
description?: string
|
||||
/** Updated type */
|
||||
type?: string
|
||||
/** Updated status */
|
||||
status?: string
|
||||
/** Updated priority */
|
||||
priority?: string
|
||||
/** Updated tags */
|
||||
tags?: string[]
|
||||
/** Updated metadata */
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk operation types for batch processing
|
||||
*/
|
||||
|
||||
/**
|
||||
* Request for bulk operations on multiple items
|
||||
*/
|
||||
export interface BulkOperationRequest<TData = any> {
|
||||
/** Type of bulk operation to perform */
|
||||
operation: 'create' | 'update' | 'delete' | 'archive' | 'restore'
|
||||
/** Array of data items to process */
|
||||
data: TData[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from a bulk operation
|
||||
*/
|
||||
export interface BulkOperationResponse {
|
||||
/** Number of successfully processed items */
|
||||
successful: number
|
||||
/** Number of items that failed processing */
|
||||
failed: number
|
||||
/** Array of errors that occurred during processing */
|
||||
errors: Array<{
|
||||
/** Index of the item that failed */
|
||||
index: number
|
||||
/** Error message */
|
||||
error: string
|
||||
/** Optional additional error data */
|
||||
data?: any
|
||||
}>
|
||||
}
|
||||
60
packages/shared/data/api/apiPaths.ts
Normal file
60
packages/shared/data/api/apiPaths.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import type { ApiSchemas } from './apiSchemas'
|
||||
|
||||
/**
|
||||
* Template literal type utilities for converting parameterized paths to concrete paths
|
||||
* This enables type-safe API calls with actual paths like '/test/items/123' instead of '/test/items/:id'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convert parameterized path templates to concrete path types
|
||||
* @example '/test/items/:id' -> '/test/items/${string}'
|
||||
* @example '/topics/:id/messages' -> '/topics/${string}/messages'
|
||||
*/
|
||||
export type ResolvedPath<T extends string> = T extends `${infer Prefix}/:${string}/${infer Suffix}`
|
||||
? `${Prefix}/${string}/${ResolvedPath<Suffix>}`
|
||||
: T extends `${infer Prefix}/:${string}`
|
||||
? `${Prefix}/${string}`
|
||||
: T
|
||||
|
||||
/**
|
||||
* Generate all possible concrete paths from ApiSchemas
|
||||
* This creates a union type of all valid API paths
|
||||
*/
|
||||
export type ConcreteApiPaths = {
|
||||
[K in keyof ApiSchemas]: ResolvedPath<K & string>
|
||||
}[keyof ApiSchemas]
|
||||
|
||||
/**
|
||||
* Reverse lookup: from concrete path back to original template path
|
||||
* Used to determine which ApiSchema entry matches a concrete path
|
||||
*/
|
||||
export type MatchApiPath<Path extends string> = {
|
||||
[K in keyof ApiSchemas]: Path extends ResolvedPath<K & string> ? K : never
|
||||
}[keyof ApiSchemas]
|
||||
|
||||
/**
|
||||
* Extract query parameters type for a given concrete path
|
||||
*/
|
||||
export type QueryParamsForPath<Path extends string> = MatchApiPath<Path> extends keyof ApiSchemas
|
||||
? ApiSchemas[MatchApiPath<Path>] extends { GET: { query?: infer Q } }
|
||||
? Q
|
||||
: Record<string, any>
|
||||
: Record<string, any>
|
||||
|
||||
/**
|
||||
* Extract request body type for a given concrete path and HTTP method
|
||||
*/
|
||||
export type BodyForPath<Path extends string, Method extends string> = MatchApiPath<Path> extends keyof ApiSchemas
|
||||
? ApiSchemas[MatchApiPath<Path>] extends { [M in Method]: { body: infer B } }
|
||||
? B
|
||||
: any
|
||||
: any
|
||||
|
||||
/**
|
||||
* Extract response type for a given concrete path and HTTP method
|
||||
*/
|
||||
export type ResponseForPath<Path extends string, Method extends string> = MatchApiPath<Path> extends keyof ApiSchemas
|
||||
? ApiSchemas[MatchApiPath<Path>] extends { [M in Method]: { response: infer R } }
|
||||
? R
|
||||
: any
|
||||
: any
|
||||
487
packages/shared/data/api/apiSchemas.ts
Normal file
487
packages/shared/data/api/apiSchemas.ts
Normal file
@@ -0,0 +1,487 @@
|
||||
// NOTE: Types are defined inline in the schema for simplicity
|
||||
// If needed, specific types can be imported from './apiModels'
|
||||
import type { BodyForPath, ConcreteApiPaths, QueryParamsForPath, ResponseForPath } from './apiPaths'
|
||||
import type { HttpMethod, PaginatedResponse, PaginationParams } from './apiTypes'
|
||||
|
||||
// Re-export for external use
|
||||
export type { ConcreteApiPaths } from './apiPaths'
|
||||
|
||||
/**
|
||||
* Complete API Schema definitions for Test API
|
||||
*
|
||||
* Each path defines the supported HTTP methods with their:
|
||||
* - Request parameters (params, query, body)
|
||||
* - Response types
|
||||
* - Type safety guarantees
|
||||
*
|
||||
* This schema serves as the contract between renderer and main processes,
|
||||
* enabling full TypeScript type checking across IPC boundaries.
|
||||
*/
|
||||
export interface ApiSchemas {
|
||||
/**
|
||||
* Test items collection endpoint
|
||||
* @example GET /test/items?page=1&limit=10&search=hello
|
||||
* @example POST /test/items { "title": "New Test Item" }
|
||||
*/
|
||||
'/test/items': {
|
||||
/** List all test items with optional filtering and pagination */
|
||||
GET: {
|
||||
query?: PaginationParams & {
|
||||
/** Search items by title or description */
|
||||
search?: string
|
||||
/** Filter by item type */
|
||||
type?: string
|
||||
/** Filter by status */
|
||||
status?: string
|
||||
}
|
||||
response: PaginatedResponse<any>
|
||||
}
|
||||
/** Create a new test item */
|
||||
POST: {
|
||||
body: {
|
||||
title: string
|
||||
description?: string
|
||||
type?: string
|
||||
status?: string
|
||||
priority?: string
|
||||
tags?: string[]
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
response: any
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Individual test item endpoint
|
||||
* @example GET /test/items/123
|
||||
* @example PUT /test/items/123 { "title": "Updated Title" }
|
||||
* @example DELETE /test/items/123
|
||||
*/
|
||||
'/test/items/:id': {
|
||||
/** Get a specific test item by ID */
|
||||
GET: {
|
||||
params: { id: string }
|
||||
response: any
|
||||
}
|
||||
/** Update a specific test item */
|
||||
PUT: {
|
||||
params: { id: string }
|
||||
body: {
|
||||
title?: string
|
||||
description?: string
|
||||
type?: string
|
||||
status?: string
|
||||
priority?: string
|
||||
tags?: string[]
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
response: any
|
||||
}
|
||||
/** Delete a specific test item */
|
||||
DELETE: {
|
||||
params: { id: string }
|
||||
response: void
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test search endpoint
|
||||
* @example GET /test/search?query=hello&page=1&limit=20
|
||||
*/
|
||||
'/test/search': {
|
||||
/** Search test items */
|
||||
GET: {
|
||||
query: {
|
||||
/** Search query string */
|
||||
query: string
|
||||
/** Page number for pagination */
|
||||
page?: number
|
||||
/** Number of results per page */
|
||||
limit?: number
|
||||
/** Additional filters */
|
||||
type?: string
|
||||
status?: string
|
||||
}
|
||||
response: PaginatedResponse<any>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test statistics endpoint
|
||||
* @example GET /test/stats
|
||||
*/
|
||||
'/test/stats': {
|
||||
/** Get comprehensive test statistics */
|
||||
GET: {
|
||||
response: {
|
||||
/** Total number of items */
|
||||
total: number
|
||||
/** Item count grouped by type */
|
||||
byType: Record<string, number>
|
||||
/** Item count grouped by status */
|
||||
byStatus: Record<string, number>
|
||||
/** Item count grouped by priority */
|
||||
byPriority: Record<string, number>
|
||||
/** Recent activity timeline */
|
||||
recentActivity: Array<{
|
||||
/** Date of activity */
|
||||
date: string
|
||||
/** Number of items on that date */
|
||||
count: number
|
||||
}>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test bulk operations endpoint
|
||||
* @example POST /test/bulk { "operation": "create", "data": [...] }
|
||||
*/
|
||||
'/test/bulk': {
|
||||
/** Perform bulk operations on test items */
|
||||
POST: {
|
||||
body: {
|
||||
/** Operation type */
|
||||
operation: 'create' | 'update' | 'delete'
|
||||
/** Array of data items to process */
|
||||
data: any[]
|
||||
}
|
||||
response: {
|
||||
successful: number
|
||||
failed: number
|
||||
errors: string[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test error simulation endpoint
|
||||
* @example POST /test/error { "errorType": "timeout" }
|
||||
*/
|
||||
'/test/error': {
|
||||
/** Simulate various error scenarios for testing */
|
||||
POST: {
|
||||
body: {
|
||||
/** Type of error to simulate */
|
||||
errorType:
|
||||
| 'timeout'
|
||||
| 'network'
|
||||
| 'server'
|
||||
| 'notfound'
|
||||
| 'validation'
|
||||
| 'unauthorized'
|
||||
| 'ratelimit'
|
||||
| 'generic'
|
||||
}
|
||||
response: never
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test slow response endpoint
|
||||
* @example POST /test/slow { "delay": 2000 }
|
||||
*/
|
||||
'/test/slow': {
|
||||
/** Test slow response for performance testing */
|
||||
POST: {
|
||||
body: {
|
||||
/** Delay in milliseconds */
|
||||
delay: number
|
||||
}
|
||||
response: {
|
||||
message: string
|
||||
delay: number
|
||||
timestamp: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test data reset endpoint
|
||||
* @example POST /test/reset
|
||||
*/
|
||||
'/test/reset': {
|
||||
/** Reset all test data to initial state */
|
||||
POST: {
|
||||
response: {
|
||||
message: string
|
||||
timestamp: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test config endpoint
|
||||
* @example GET /test/config
|
||||
* @example PUT /test/config { "setting": "value" }
|
||||
*/
|
||||
'/test/config': {
|
||||
/** Get test configuration */
|
||||
GET: {
|
||||
response: Record<string, any>
|
||||
}
|
||||
/** Update test configuration */
|
||||
PUT: {
|
||||
body: Record<string, any>
|
||||
response: Record<string, any>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test status endpoint
|
||||
* @example GET /test/status
|
||||
*/
|
||||
'/test/status': {
|
||||
/** Get system test status */
|
||||
GET: {
|
||||
response: {
|
||||
status: string
|
||||
timestamp: string
|
||||
version: string
|
||||
uptime: number
|
||||
environment: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test performance endpoint
|
||||
* @example GET /test/performance
|
||||
*/
|
||||
'/test/performance': {
|
||||
/** Get performance metrics */
|
||||
GET: {
|
||||
response: {
|
||||
requestsPerSecond: number
|
||||
averageLatency: number
|
||||
memoryUsage: number
|
||||
cpuUsage: number
|
||||
uptime: number
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch execution of multiple requests
|
||||
* @example POST /batch { "requests": [...], "parallel": true }
|
||||
*/
|
||||
'/batch': {
|
||||
/** Execute multiple API requests in a single call */
|
||||
POST: {
|
||||
body: {
|
||||
/** Array of requests to execute */
|
||||
requests: Array<{
|
||||
/** HTTP method for the request */
|
||||
method: HttpMethod
|
||||
/** API path for the request */
|
||||
path: string
|
||||
/** URL parameters */
|
||||
params?: any
|
||||
/** Request body */
|
||||
body?: any
|
||||
}>
|
||||
/** Execute requests in parallel vs sequential */
|
||||
parallel?: boolean
|
||||
}
|
||||
response: {
|
||||
/** Results array matching input order */
|
||||
results: Array<{
|
||||
/** HTTP status code */
|
||||
status: number
|
||||
/** Response data if successful */
|
||||
data?: any
|
||||
/** Error information if failed */
|
||||
error?: any
|
||||
}>
|
||||
/** Batch execution metadata */
|
||||
metadata: {
|
||||
/** Total execution duration in ms */
|
||||
duration: number
|
||||
/** Number of successful requests */
|
||||
successCount: number
|
||||
/** Number of failed requests */
|
||||
errorCount: number
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomic transaction of multiple operations
|
||||
* @example POST /transaction { "operations": [...], "options": { "rollbackOnError": true } }
|
||||
*/
|
||||
'/transaction': {
|
||||
/** Execute multiple operations in a database transaction */
|
||||
POST: {
|
||||
body: {
|
||||
/** Array of operations to execute atomically */
|
||||
operations: Array<{
|
||||
/** HTTP method for the operation */
|
||||
method: HttpMethod
|
||||
/** API path for the operation */
|
||||
path: string
|
||||
/** URL parameters */
|
||||
params?: any
|
||||
/** Request body */
|
||||
body?: any
|
||||
}>
|
||||
/** Transaction configuration options */
|
||||
options?: {
|
||||
/** Database isolation level */
|
||||
isolation?: 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable'
|
||||
/** Rollback all operations on any error */
|
||||
rollbackOnError?: boolean
|
||||
/** Transaction timeout in milliseconds */
|
||||
timeout?: number
|
||||
}
|
||||
}
|
||||
response: Array<{
|
||||
/** HTTP status code */
|
||||
status: number
|
||||
/** Response data if successful */
|
||||
data?: any
|
||||
/** Error information if failed */
|
||||
error?: any
|
||||
}>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified type extraction helpers
|
||||
*/
|
||||
export type ApiPaths = keyof ApiSchemas
|
||||
export type ApiMethods<TPath extends ApiPaths> = keyof ApiSchemas[TPath] & HttpMethod
|
||||
export type ApiResponse<TPath extends ApiPaths, TMethod extends string> = TPath extends keyof ApiSchemas
|
||||
? TMethod extends keyof ApiSchemas[TPath]
|
||||
? ApiSchemas[TPath][TMethod] extends { response: infer R }
|
||||
? R
|
||||
: never
|
||||
: never
|
||||
: never
|
||||
|
||||
export type ApiParams<TPath extends ApiPaths, TMethod extends string> = TPath extends keyof ApiSchemas
|
||||
? TMethod extends keyof ApiSchemas[TPath]
|
||||
? ApiSchemas[TPath][TMethod] extends { params: infer P }
|
||||
? P
|
||||
: never
|
||||
: never
|
||||
: never
|
||||
|
||||
export type ApiQuery<TPath extends ApiPaths, TMethod extends string> = TPath extends keyof ApiSchemas
|
||||
? TMethod extends keyof ApiSchemas[TPath]
|
||||
? ApiSchemas[TPath][TMethod] extends { query: infer Q }
|
||||
? Q
|
||||
: never
|
||||
: never
|
||||
: never
|
||||
|
||||
export type ApiBody<TPath extends ApiPaths, TMethod extends string> = TPath extends keyof ApiSchemas
|
||||
? TMethod extends keyof ApiSchemas[TPath]
|
||||
? ApiSchemas[TPath][TMethod] extends { body: infer B }
|
||||
? B
|
||||
: never
|
||||
: never
|
||||
: never
|
||||
|
||||
/**
|
||||
* Type-safe API client interface using concrete paths
|
||||
* Accepts actual paths like '/test/items/123' instead of '/test/items/:id'
|
||||
* Automatically infers query, body, and response types from ApiSchemas
|
||||
*/
|
||||
export interface ApiClient {
|
||||
get<TPath extends ConcreteApiPaths>(
|
||||
path: TPath,
|
||||
options?: {
|
||||
query?: QueryParamsForPath<TPath>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
): Promise<ResponseForPath<TPath, 'GET'>>
|
||||
|
||||
post<TPath extends ConcreteApiPaths>(
|
||||
path: TPath,
|
||||
options: {
|
||||
body?: BodyForPath<TPath, 'POST'>
|
||||
query?: Record<string, any>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
): Promise<ResponseForPath<TPath, 'POST'>>
|
||||
|
||||
put<TPath extends ConcreteApiPaths>(
|
||||
path: TPath,
|
||||
options: {
|
||||
body: BodyForPath<TPath, 'PUT'>
|
||||
query?: Record<string, any>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
): Promise<ResponseForPath<TPath, 'PUT'>>
|
||||
|
||||
delete<TPath extends ConcreteApiPaths>(
|
||||
path: TPath,
|
||||
options?: {
|
||||
query?: Record<string, any>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
): Promise<ResponseForPath<TPath, 'DELETE'>>
|
||||
|
||||
patch<TPath extends ConcreteApiPaths>(
|
||||
path: TPath,
|
||||
options: {
|
||||
body?: BodyForPath<TPath, 'PATCH'>
|
||||
query?: Record<string, any>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
): Promise<ResponseForPath<TPath, 'PATCH'>>
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper types to determine if parameters are required based on schema
|
||||
*/
|
||||
type HasRequiredQuery<Path extends ApiPaths, Method extends ApiMethods<Path>> = Path extends keyof ApiSchemas
|
||||
? Method extends keyof ApiSchemas[Path]
|
||||
? ApiSchemas[Path][Method] extends { query: any }
|
||||
? true
|
||||
: false
|
||||
: false
|
||||
: false
|
||||
|
||||
type HasRequiredBody<Path extends ApiPaths, Method extends ApiMethods<Path>> = Path extends keyof ApiSchemas
|
||||
? Method extends keyof ApiSchemas[Path]
|
||||
? ApiSchemas[Path][Method] extends { body: any }
|
||||
? true
|
||||
: false
|
||||
: false
|
||||
: false
|
||||
|
||||
type HasRequiredParams<Path extends ApiPaths, Method extends ApiMethods<Path>> = Path extends keyof ApiSchemas
|
||||
? Method extends keyof ApiSchemas[Path]
|
||||
? ApiSchemas[Path][Method] extends { params: any }
|
||||
? true
|
||||
: false
|
||||
: false
|
||||
: false
|
||||
|
||||
/**
|
||||
* Handler function for a specific API endpoint
|
||||
* Provides type-safe parameter extraction based on ApiSchemas
|
||||
* Parameters are required or optional based on the schema definition
|
||||
*/
|
||||
export type ApiHandler<Path extends ApiPaths, Method extends ApiMethods<Path>> = (
|
||||
params: (HasRequiredParams<Path, Method> extends true
|
||||
? { params: ApiParams<Path, Method> }
|
||||
: { params?: ApiParams<Path, Method> }) &
|
||||
(HasRequiredQuery<Path, Method> extends true
|
||||
? { query: ApiQuery<Path, Method> }
|
||||
: { query?: ApiQuery<Path, Method> }) &
|
||||
(HasRequiredBody<Path, Method> extends true ? { body: ApiBody<Path, Method> } : { body?: ApiBody<Path, Method> })
|
||||
) => Promise<ApiResponse<Path, Method>>
|
||||
|
||||
/**
|
||||
* Complete API implementation that must match ApiSchemas structure
|
||||
* TypeScript will error if any endpoint is missing - this ensures exhaustive coverage
|
||||
*/
|
||||
export type ApiImplementation = {
|
||||
[Path in ApiPaths]: {
|
||||
[Method in ApiMethods<Path>]: ApiHandler<Path, Method>
|
||||
}
|
||||
}
|
||||
289
packages/shared/data/api/apiTypes.ts
Normal file
289
packages/shared/data/api/apiTypes.ts
Normal file
@@ -0,0 +1,289 @@
|
||||
/**
|
||||
* Core types for the Data API system
|
||||
* Provides type definitions for request/response handling across renderer-main IPC communication
|
||||
*/
|
||||
|
||||
/**
|
||||
* Standard HTTP methods supported by the Data API
|
||||
*/
|
||||
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
|
||||
|
||||
/**
|
||||
* Request object structure for Data API calls
|
||||
*/
|
||||
export interface DataRequest<T = any> {
|
||||
/** Unique request identifier for tracking and correlation */
|
||||
id: string
|
||||
/** HTTP method for the request */
|
||||
method: HttpMethod
|
||||
/** API path (e.g., '/topics', '/topics/123') */
|
||||
path: string
|
||||
/** URL parameters for the request */
|
||||
params?: Record<string, any>
|
||||
/** Request body data */
|
||||
body?: T
|
||||
/** Request headers */
|
||||
headers?: Record<string, string>
|
||||
/** Additional metadata for request processing */
|
||||
metadata?: {
|
||||
/** Request timestamp */
|
||||
timestamp: number
|
||||
/** OpenTelemetry span context for tracing */
|
||||
spanContext?: any
|
||||
/** Cache options for this specific request */
|
||||
cache?: CacheOptions
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Response object structure for Data API calls
|
||||
*/
|
||||
export interface DataResponse<T = any> {
|
||||
/** Request ID that this response corresponds to */
|
||||
id: string
|
||||
/** HTTP status code */
|
||||
status: number
|
||||
/** Response data if successful */
|
||||
data?: T
|
||||
/** Error information if request failed */
|
||||
error?: DataApiError
|
||||
/** Response metadata */
|
||||
metadata?: {
|
||||
/** Request processing duration in milliseconds */
|
||||
duration: number
|
||||
/** Whether response was served from cache */
|
||||
cached?: boolean
|
||||
/** Cache TTL if applicable */
|
||||
cacheTtl?: number
|
||||
/** Response timestamp */
|
||||
timestamp: number
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Standardized error structure for Data API
|
||||
*/
|
||||
export interface DataApiError {
|
||||
/** Error code for programmatic handling */
|
||||
code: string
|
||||
/** Human-readable error message */
|
||||
message: string
|
||||
/** HTTP status code */
|
||||
status: number
|
||||
/** Additional error details */
|
||||
details?: any
|
||||
/** Error stack trace (development mode only) */
|
||||
stack?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard error codes for Data API
|
||||
*/
|
||||
export enum ErrorCode {
|
||||
// Client errors (4xx)
|
||||
BAD_REQUEST = 'BAD_REQUEST',
|
||||
UNAUTHORIZED = 'UNAUTHORIZED',
|
||||
FORBIDDEN = 'FORBIDDEN',
|
||||
NOT_FOUND = 'NOT_FOUND',
|
||||
METHOD_NOT_ALLOWED = 'METHOD_NOT_ALLOWED',
|
||||
VALIDATION_ERROR = 'VALIDATION_ERROR',
|
||||
RATE_LIMIT_EXCEEDED = 'RATE_LIMIT_EXCEEDED',
|
||||
|
||||
// Server errors (5xx)
|
||||
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
|
||||
DATABASE_ERROR = 'DATABASE_ERROR',
|
||||
SERVICE_UNAVAILABLE = 'SERVICE_UNAVAILABLE',
|
||||
|
||||
// Custom application errors
|
||||
MIGRATION_ERROR = 'MIGRATION_ERROR',
|
||||
PERMISSION_DENIED = 'PERMISSION_DENIED',
|
||||
RESOURCE_LOCKED = 'RESOURCE_LOCKED',
|
||||
CONCURRENT_MODIFICATION = 'CONCURRENT_MODIFICATION'
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache configuration options
|
||||
*/
|
||||
export interface CacheOptions {
|
||||
/** Cache TTL in seconds */
|
||||
ttl?: number
|
||||
/** Return stale data while revalidating in background */
|
||||
staleWhileRevalidate?: boolean
|
||||
/** Custom cache key override */
|
||||
cacheKey?: string
|
||||
/** Operations that should invalidate this cache entry */
|
||||
invalidateOn?: string[]
|
||||
/** Whether to bypass cache entirely */
|
||||
noCache?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Transaction request wrapper for atomic operations
|
||||
*/
|
||||
export interface TransactionRequest {
|
||||
/** List of operations to execute in transaction */
|
||||
operations: DataRequest[]
|
||||
/** Transaction options */
|
||||
options?: {
|
||||
/** Database isolation level */
|
||||
isolation?: 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable'
|
||||
/** Whether to rollback entire transaction on any error */
|
||||
rollbackOnError?: boolean
|
||||
/** Transaction timeout in milliseconds */
|
||||
timeout?: number
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch request for multiple operations
|
||||
*/
|
||||
export interface BatchRequest {
|
||||
/** List of requests to execute */
|
||||
requests: DataRequest[]
|
||||
/** Whether to execute requests in parallel */
|
||||
parallel?: boolean
|
||||
/** Stop on first error */
|
||||
stopOnError?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch response containing results for all requests
|
||||
*/
|
||||
export interface BatchResponse {
|
||||
/** Individual response for each request */
|
||||
results: DataResponse[]
|
||||
/** Overall batch execution metadata */
|
||||
metadata: {
|
||||
/** Total execution time */
|
||||
duration: number
|
||||
/** Number of successful operations */
|
||||
successCount: number
|
||||
/** Number of failed operations */
|
||||
errorCount: number
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination parameters for list operations
|
||||
*/
|
||||
export interface PaginationParams {
|
||||
/** Page number (1-based) */
|
||||
page?: number
|
||||
/** Items per page */
|
||||
limit?: number
|
||||
/** Cursor for cursor-based pagination */
|
||||
cursor?: string
|
||||
/** Sort field and direction */
|
||||
sort?: {
|
||||
field: string
|
||||
order: 'asc' | 'desc'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Paginated response wrapper
|
||||
*/
|
||||
export interface PaginatedResponse<T> {
|
||||
/** Items for current page */
|
||||
items: T[]
|
||||
/** Total number of items */
|
||||
total: number
|
||||
/** Current page number */
|
||||
page: number
|
||||
/** Total number of pages */
|
||||
pageCount: number
|
||||
/** Whether there are more pages */
|
||||
hasNext: boolean
|
||||
/** Whether there are previous pages */
|
||||
hasPrev: boolean
|
||||
/** Next cursor for cursor-based pagination */
|
||||
nextCursor?: string
|
||||
/** Previous cursor for cursor-based pagination */
|
||||
prevCursor?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscription options for real-time data updates
|
||||
*/
|
||||
export interface SubscriptionOptions {
|
||||
/** Path pattern to subscribe to */
|
||||
path: string
|
||||
/** Filters to apply to subscription */
|
||||
filters?: Record<string, any>
|
||||
/** Whether to receive initial data */
|
||||
includeInitial?: boolean
|
||||
/** Custom subscription metadata */
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscription callback function
|
||||
*/
|
||||
export type SubscriptionCallback<T = any> = (data: T, event: SubscriptionEvent) => void
|
||||
|
||||
/**
|
||||
* Subscription event types
|
||||
*/
|
||||
export enum SubscriptionEvent {
|
||||
CREATED = 'created',
|
||||
UPDATED = 'updated',
|
||||
DELETED = 'deleted',
|
||||
INITIAL = 'initial',
|
||||
ERROR = 'error'
|
||||
}
|
||||
|
||||
/**
|
||||
* Middleware interface
|
||||
*/
|
||||
export interface Middleware {
|
||||
/** Middleware name */
|
||||
name: string
|
||||
/** Execution priority (lower = earlier) */
|
||||
priority?: number
|
||||
/** Middleware execution function */
|
||||
execute(req: DataRequest, res: DataResponse, next: () => Promise<void>): Promise<void>
|
||||
}
|
||||
|
||||
/**
|
||||
* Request context passed through middleware chain
|
||||
*/
|
||||
export interface RequestContext {
|
||||
/** Original request */
|
||||
request: DataRequest
|
||||
/** Response being built */
|
||||
response: DataResponse
|
||||
/** Path that matched this request */
|
||||
path?: string
|
||||
/** HTTP method */
|
||||
method?: HttpMethod
|
||||
/** Authenticated user (if any) */
|
||||
user?: any
|
||||
/** Additional context data */
|
||||
data: Map<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Base options for service operations
|
||||
*/
|
||||
export interface ServiceOptions {
|
||||
/** Database transaction to use */
|
||||
transaction?: any
|
||||
/** User context for authorization */
|
||||
user?: any
|
||||
/** Additional service-specific options */
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard service response wrapper
|
||||
*/
|
||||
export interface ServiceResult<T = any> {
|
||||
/** Whether operation was successful */
|
||||
success: boolean
|
||||
/** Result data if successful */
|
||||
data?: T
|
||||
/** Error information if failed */
|
||||
error?: DataApiError
|
||||
/** Additional metadata */
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
194
packages/shared/data/api/errorCodes.ts
Normal file
194
packages/shared/data/api/errorCodes.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Centralized error code definitions for the Data API system
|
||||
* Provides consistent error handling across renderer and main processes
|
||||
*/
|
||||
|
||||
import type { DataApiError } from './apiTypes'
|
||||
import { ErrorCode } from './apiTypes'
|
||||
|
||||
// Re-export ErrorCode for convenience
|
||||
export { ErrorCode } from './apiTypes'
|
||||
|
||||
/**
|
||||
* Error code to HTTP status mapping
|
||||
*/
|
||||
export const ERROR_STATUS_MAP: Record<ErrorCode, number> = {
|
||||
// Client errors (4xx)
|
||||
[ErrorCode.BAD_REQUEST]: 400,
|
||||
[ErrorCode.UNAUTHORIZED]: 401,
|
||||
[ErrorCode.FORBIDDEN]: 403,
|
||||
[ErrorCode.NOT_FOUND]: 404,
|
||||
[ErrorCode.METHOD_NOT_ALLOWED]: 405,
|
||||
[ErrorCode.VALIDATION_ERROR]: 422,
|
||||
[ErrorCode.RATE_LIMIT_EXCEEDED]: 429,
|
||||
|
||||
// Server errors (5xx)
|
||||
[ErrorCode.INTERNAL_SERVER_ERROR]: 500,
|
||||
[ErrorCode.DATABASE_ERROR]: 500,
|
||||
[ErrorCode.SERVICE_UNAVAILABLE]: 503,
|
||||
|
||||
// Custom application errors (5xx)
|
||||
[ErrorCode.MIGRATION_ERROR]: 500,
|
||||
[ErrorCode.PERMISSION_DENIED]: 403,
|
||||
[ErrorCode.RESOURCE_LOCKED]: 423,
|
||||
[ErrorCode.CONCURRENT_MODIFICATION]: 409
|
||||
}
|
||||
|
||||
/**
|
||||
* Default error messages for each error code
|
||||
*/
|
||||
export const ERROR_MESSAGES: Record<ErrorCode, string> = {
|
||||
[ErrorCode.BAD_REQUEST]: 'Bad request: Invalid request format or parameters',
|
||||
[ErrorCode.UNAUTHORIZED]: 'Unauthorized: Authentication required',
|
||||
[ErrorCode.FORBIDDEN]: 'Forbidden: Insufficient permissions',
|
||||
[ErrorCode.NOT_FOUND]: 'Not found: Requested resource does not exist',
|
||||
[ErrorCode.METHOD_NOT_ALLOWED]: 'Method not allowed: HTTP method not supported for this endpoint',
|
||||
[ErrorCode.VALIDATION_ERROR]: 'Validation error: Request data does not meet requirements',
|
||||
[ErrorCode.RATE_LIMIT_EXCEEDED]: 'Rate limit exceeded: Too many requests',
|
||||
|
||||
[ErrorCode.INTERNAL_SERVER_ERROR]: 'Internal server error: An unexpected error occurred',
|
||||
[ErrorCode.DATABASE_ERROR]: 'Database error: Failed to access or modify data',
|
||||
[ErrorCode.SERVICE_UNAVAILABLE]: 'Service unavailable: The service is temporarily unavailable',
|
||||
|
||||
[ErrorCode.MIGRATION_ERROR]: 'Migration error: Failed to migrate data',
|
||||
[ErrorCode.PERMISSION_DENIED]: 'Permission denied: Operation not allowed for current user',
|
||||
[ErrorCode.RESOURCE_LOCKED]: 'Resource locked: Resource is currently locked by another operation',
|
||||
[ErrorCode.CONCURRENT_MODIFICATION]: 'Concurrent modification: Resource was modified by another user'
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class for creating standardized Data API errors
|
||||
*/
|
||||
export class DataApiErrorFactory {
|
||||
/**
|
||||
* Create a DataApiError with standard properties
|
||||
*/
|
||||
static create(code: ErrorCode, customMessage?: string, details?: any, stack?: string): DataApiError {
|
||||
return {
|
||||
code,
|
||||
message: customMessage || ERROR_MESSAGES[code],
|
||||
status: ERROR_STATUS_MAP[code],
|
||||
details,
|
||||
stack: stack || undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a validation error with field-specific details
|
||||
*/
|
||||
static validation(fieldErrors: Record<string, string[]>, message?: string): DataApiError {
|
||||
return this.create(ErrorCode.VALIDATION_ERROR, message || 'Request validation failed', { fieldErrors })
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a not found error for specific resource
|
||||
*/
|
||||
static notFound(resource: string, id?: string): DataApiError {
|
||||
const message = id ? `${resource} with id '${id}' not found` : `${resource} not found`
|
||||
|
||||
return this.create(ErrorCode.NOT_FOUND, message, { resource, id })
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a database error with query details
|
||||
*/
|
||||
static database(originalError: Error, operation?: string): DataApiError {
|
||||
return this.create(
|
||||
ErrorCode.DATABASE_ERROR,
|
||||
`Database operation failed${operation ? `: ${operation}` : ''}`,
|
||||
{
|
||||
originalError: originalError.message,
|
||||
operation
|
||||
},
|
||||
originalError.stack
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a permission denied error
|
||||
*/
|
||||
static permissionDenied(action: string, resource?: string): DataApiError {
|
||||
const message = resource ? `Permission denied: Cannot ${action} ${resource}` : `Permission denied: Cannot ${action}`
|
||||
|
||||
return this.create(ErrorCode.PERMISSION_DENIED, message, { action, resource })
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an internal server error from an unexpected error
|
||||
*/
|
||||
static internal(originalError: Error, context?: string): DataApiError {
|
||||
const message = context
|
||||
? `Internal error in ${context}: ${originalError.message}`
|
||||
: `Internal error: ${originalError.message}`
|
||||
|
||||
return this.create(
|
||||
ErrorCode.INTERNAL_SERVER_ERROR,
|
||||
message,
|
||||
{ originalError: originalError.message, context },
|
||||
originalError.stack
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a rate limit exceeded error
|
||||
*/
|
||||
static rateLimit(limit: number, windowMs: number): DataApiError {
|
||||
return this.create(ErrorCode.RATE_LIMIT_EXCEEDED, `Rate limit exceeded: ${limit} requests per ${windowMs}ms`, {
|
||||
limit,
|
||||
windowMs
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a resource locked error
|
||||
*/
|
||||
static resourceLocked(resource: string, id: string, lockedBy?: string): DataApiError {
|
||||
const message = lockedBy
|
||||
? `${resource} '${id}' is locked by ${lockedBy}`
|
||||
: `${resource} '${id}' is currently locked`
|
||||
|
||||
return this.create(ErrorCode.RESOURCE_LOCKED, message, { resource, id, lockedBy })
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a concurrent modification error
|
||||
*/
|
||||
static concurrentModification(resource: string, id: string): DataApiError {
|
||||
return this.create(ErrorCode.CONCURRENT_MODIFICATION, `${resource} '${id}' was modified by another user`, {
|
||||
resource,
|
||||
id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error is a Data API error
|
||||
*/
|
||||
export function isDataApiError(error: any): error is DataApiError {
|
||||
return (
|
||||
error &&
|
||||
typeof error === 'object' &&
|
||||
typeof error.code === 'string' &&
|
||||
typeof error.message === 'string' &&
|
||||
typeof error.status === 'number'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a generic error to a DataApiError
|
||||
*/
|
||||
export function toDataApiError(error: unknown, context?: string): DataApiError {
|
||||
if (isDataApiError(error)) {
|
||||
return error
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return DataApiErrorFactory.internal(error, context)
|
||||
}
|
||||
|
||||
return DataApiErrorFactory.create(
|
||||
ErrorCode.INTERNAL_SERVER_ERROR,
|
||||
`Unknown error${context ? ` in ${context}` : ''}: ${String(error)}`,
|
||||
{ originalError: error, context }
|
||||
)
|
||||
}
|
||||
121
packages/shared/data/api/index.ts
Normal file
121
packages/shared/data/api/index.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Cherry Studio Data API - Barrel Exports
|
||||
*
|
||||
* This file provides a centralized entry point for all data API types,
|
||||
* schemas, and utilities. Import everything you need from this single location.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Topic, CreateTopicDto, ApiSchemas, DataRequest, ErrorCode } from '@/shared/data'
|
||||
* ```
|
||||
*/
|
||||
|
||||
// Core data API types and infrastructure
|
||||
export type {
|
||||
BatchRequest,
|
||||
BatchResponse,
|
||||
CacheOptions,
|
||||
DataApiError,
|
||||
DataRequest,
|
||||
DataResponse,
|
||||
HttpMethod,
|
||||
Middleware,
|
||||
PaginatedResponse,
|
||||
PaginationParams,
|
||||
RequestContext,
|
||||
ServiceOptions,
|
||||
ServiceResult,
|
||||
SubscriptionCallback,
|
||||
SubscriptionOptions,
|
||||
TransactionRequest
|
||||
} from './apiTypes'
|
||||
export { ErrorCode, SubscriptionEvent } from './apiTypes'
|
||||
|
||||
// Domain models and DTOs
|
||||
export type {
|
||||
BulkOperationRequest,
|
||||
BulkOperationResponse,
|
||||
CreateTestItemDto,
|
||||
TestItem,
|
||||
UpdateTestItemDto
|
||||
} from './apiModels'
|
||||
|
||||
// API schema definitions and type helpers
|
||||
export type {
|
||||
ApiBody,
|
||||
ApiClient,
|
||||
ApiMethods,
|
||||
ApiParams,
|
||||
ApiPaths,
|
||||
ApiQuery,
|
||||
ApiResponse,
|
||||
ApiSchemas
|
||||
} from './apiSchemas'
|
||||
|
||||
// Path type utilities for template literal types
|
||||
export type {
|
||||
BodyForPath,
|
||||
ConcreteApiPaths,
|
||||
MatchApiPath,
|
||||
QueryParamsForPath,
|
||||
ResolvedPath,
|
||||
ResponseForPath
|
||||
} from './apiPaths'
|
||||
|
||||
// Error handling utilities
|
||||
export {
|
||||
ErrorCode as DataApiErrorCode,
|
||||
DataApiErrorFactory,
|
||||
ERROR_MESSAGES,
|
||||
ERROR_STATUS_MAP,
|
||||
isDataApiError,
|
||||
toDataApiError
|
||||
} from './errorCodes'
|
||||
|
||||
/**
|
||||
* Re-export commonly used type combinations for convenience
|
||||
*/
|
||||
|
||||
// Import types for re-export convenience types
|
||||
import type { CreateTestItemDto, TestItem, UpdateTestItemDto } from './apiModels'
|
||||
import type {
|
||||
BatchRequest,
|
||||
BatchResponse,
|
||||
DataApiError,
|
||||
DataRequest,
|
||||
DataResponse,
|
||||
ErrorCode,
|
||||
PaginatedResponse,
|
||||
PaginationParams,
|
||||
TransactionRequest
|
||||
} from './apiTypes'
|
||||
import type { DataApiErrorFactory } from './errorCodes'
|
||||
|
||||
/** All test item-related types */
|
||||
export type TestItemTypes = {
|
||||
TestItem: TestItem
|
||||
CreateTestItemDto: CreateTestItemDto
|
||||
UpdateTestItemDto: UpdateTestItemDto
|
||||
}
|
||||
|
||||
/** All error-related types and utilities */
|
||||
export type ErrorTypes = {
|
||||
DataApiError: DataApiError
|
||||
ErrorCode: ErrorCode
|
||||
ErrorFactory: typeof DataApiErrorFactory
|
||||
}
|
||||
|
||||
/** All request/response types */
|
||||
export type RequestTypes = {
|
||||
DataRequest: DataRequest
|
||||
DataResponse: DataResponse
|
||||
BatchRequest: BatchRequest
|
||||
BatchResponse: BatchResponse
|
||||
TransactionRequest: TransactionRequest
|
||||
}
|
||||
|
||||
/** All pagination-related types */
|
||||
export type PaginationTypes = {
|
||||
PaginationParams: PaginationParams
|
||||
PaginatedResponse: PaginatedResponse<any>
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user