Skip to content

Commit eb07868

Browse files
authored
Merge pull request #1277 from mnfst/codebase-optim
refactor: create packages/shared to DRY up duplicated code
2 parents d673edc + 2ad5997 commit eb07868

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+603
-226
lines changed

.changeset/dry-shared-package.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"manifest": patch
3+
---
4+
5+
Create packages/shared to centralize duplicated constants, types, and utilities across backend, frontend, and plugin. Moves TIERS, TIER_LABELS, AUTH_TYPES, API_KEY_PREFIX, MODEL_PREFIX_MAP, inferProviderFromModel, ResolveResponse, and subscription-capabilities into a single source of truth.

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
node-version: 22
5353
cache: npm
5454
- run: npm ci
55+
- run: npm run build --workspace=packages/shared
5556
- run: npx tsc --noEmit
5657
working-directory: packages/backend
5758
- run: npm run build --workspace=packages/backend
@@ -87,6 +88,7 @@ jobs:
8788
node-version: 22
8889
cache: npm
8990
- run: npm ci
91+
- run: npm run build --workspace=packages/shared
9092
- run: npm run build --workspace=packages/backend
9193
- name: Unit tests (sql.js dialect)
9294
run: npm test --workspace=packages/backend
@@ -102,6 +104,7 @@ jobs:
102104
node-version: 22
103105
cache: npm
104106
- run: npm ci
107+
- run: npm run build --workspace=packages/shared
105108
- run: npx tsc --noEmit
106109
working-directory: packages/frontend
107110
- run: npm test --workspace=packages/frontend -- --coverage
@@ -118,6 +121,26 @@ jobs:
118121
directory: packages/frontend/coverage
119122
fail_ci_if_error: false
120123

124+
shared:
125+
runs-on: ubuntu-latest
126+
steps:
127+
- uses: actions/checkout@v4
128+
- uses: actions/setup-node@v4
129+
with:
130+
node-version: 22
131+
cache: npm
132+
- run: npm ci
133+
- run: npm run build --workspace=packages/shared
134+
- run: npm test --workspace=packages/shared -- --coverage --coverageReporters=lcov
135+
- name: Upload shared coverage to Codecov
136+
if: always()
137+
uses: codecov/codecov-action@v5
138+
with:
139+
token: ${{ secrets.CODECOV_TOKEN }}
140+
flags: shared
141+
directory: packages/shared/coverage
142+
fail_ci_if_error: false
143+
121144
plugin:
122145
runs-on: ubuntu-latest
123146
steps:
@@ -127,6 +150,7 @@ jobs:
127150
node-version: 22
128151
cache: npm
129152
- run: npm ci
153+
- run: npm run build --workspace=packages/shared
130154
- run: npm test --workspace=packages/openclaw-plugin -- --coverage --coverageReporters=lcov
131155
- name: Upload plugin coverage to Codecov
132156
if: always()

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ flags:
2020
plugin:
2121
paths:
2222
- packages/openclaw-plugin/src/
23+
shared:
24+
paths:
25+
- packages/shared/src/
2326
bundle_analysis:
2427
warning_threshold: "5%"
2528
status: "informational"

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default [
77
ignores: ['**/dist/**', '**/node_modules/**', '**/coverage/**', '**/*.js', '**/*.mjs'],
88
},
99
{
10-
files: ['packages/backend/src/**/*.ts', 'packages/frontend/src/**/*.ts', 'packages/frontend/src/**/*.tsx', 'packages/openclaw-plugin/src/**/*.ts'],
10+
files: ['packages/shared/src/**/*.ts', 'packages/backend/src/**/*.ts', 'packages/frontend/src/**/*.ts', 'packages/frontend/src/**/*.tsx', 'packages/openclaw-plugin/src/**/*.ts'],
1111
languageOptions: {
1212
parser: tsParser,
1313
parserOptions: {

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
],
2828
"packageManager": "npm@10.9.2",
2929
"workspaces": [
30+
"packages/shared",
3031
"packages/frontend",
3132
"packages/backend",
3233
"packages/openclaw-plugin"
@@ -39,7 +40,7 @@
3940
"build:plugin": "npm run build --workspace=packages/openclaw-plugin",
4041
"publish:plugin": "npm publish --access public --workspace=packages/openclaw-plugin",
4142
"check:api-prefix": "node scripts/check-api-prefix.js",
42-
"lint": "eslint packages/backend/src packages/frontend/src packages/openclaw-plugin/src",
43+
"lint": "eslint packages/shared/src packages/backend/src packages/frontend/src packages/openclaw-plugin/src",
4344
"format": "prettier --write 'packages/*/src/**/*.{ts,tsx,css}'",
4445
"changeset": "changeset",
4546
"version-packages": "changeset version && node scripts/sync-plugin-version.js",

packages/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"migration:create": "typeorm-ts-node-commonjs migration:create"
1919
},
2020
"dependencies": {
21+
"manifest-shared": "*",
2122
"@nestjs/cache-manager": "^3.1.0",
2223
"@nestjs/common": "^11.0.0",
2324
"@nestjs/config": "^4.0.0",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const API_KEY_PREFIX = 'mnfst_';
1+
export { API_KEY_PREFIX } from 'manifest-shared';
Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1 @@
1-
/**
2-
* Infer a provider ID from a model name string.
3-
* Mirrors the frontend logic in services/routing-utils.ts.
4-
*/
5-
const MODEL_PREFIX_MAP: [RegExp, string][] = [
6-
[/^openrouter\//, 'openrouter'],
7-
[/^claude-/, 'anthropic'],
8-
[/^gpt-|^o[134]-|^o[134] |^chatgpt-/, 'openai'],
9-
[/^gemini-/, 'gemini'],
10-
[/^deepseek-/, 'deepseek'],
11-
[/^grok-/, 'xai'],
12-
[/^mistral-|^codestral|^pixtral|^open-mistral/, 'mistral'],
13-
[/^kimi-|^moonshot-/, 'moonshot'],
14-
[/^minimax-/i, 'minimax'],
15-
[/^glm-/, 'zai'],
16-
[/^qwen[23]|^qwq-/, 'qwen'],
17-
[/^[a-z][\w-]*\//, 'openrouter'],
18-
];
19-
20-
export function inferProviderFromModel(model: string): string | undefined {
21-
if (model.startsWith('custom:')) return 'custom';
22-
if (/:/.test(model) && !model.endsWith(':free')) return 'ollama';
23-
const lower = model.toLowerCase();
24-
for (const [re, id] of MODEL_PREFIX_MAP) {
25-
if (re.test(lower)) return id;
26-
}
27-
return undefined;
28-
}
1+
export { MODEL_PREFIX_MAP, inferProviderFromModel } from 'manifest-shared';

packages/backend/src/routing/dto/resolve-request.dto.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import {
2-
IsArray,
3-
IsIn,
4-
IsNotEmpty,
5-
IsNumber,
6-
IsOptional,
7-
ValidateNested,
8-
} from 'class-validator';
1+
import { IsArray, IsIn, IsNotEmpty, IsNumber, IsOptional, ValidateNested } from 'class-validator';
92
import { Type } from 'class-transformer';
103

11-
const VALID_TIERS = ['simple', 'standard', 'complex', 'reasoning'] as const;
4+
import { TIERS } from 'manifest-shared';
125

136
class MessageDto {
147
@IsNotEmpty()
@@ -38,6 +31,6 @@ export class ResolveRequestDto {
3831

3932
@IsOptional()
4033
@IsArray()
41-
@IsIn(VALID_TIERS, { each: true })
42-
recentTiers?: Array<(typeof VALID_TIERS)[number]>;
34+
@IsIn(TIERS, { each: true })
35+
recentTiers?: Array<(typeof TIERS)[number]>;
4336
}

0 commit comments

Comments
 (0)