@veltra/compositions 1.10.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentSize } from "@veltra/utils";
|
|
1
|
+
import { AiProviderConfig, ComponentSize } from "@veltra/utils";
|
|
2
2
|
//#region src/use-config/index.d.ts
|
|
3
3
|
interface State {
|
|
4
4
|
/** 是否开启动画,机器老可以关闭动画来获得性能 */
|
|
@@ -14,6 +14,8 @@ interface State {
|
|
|
14
14
|
pageSize: number;
|
|
15
15
|
pageSizeOptions: number[];
|
|
16
16
|
};
|
|
17
|
+
/** AI 服务商全局兜底配置,与组件实例 prop 字段级合并 */
|
|
18
|
+
ai: Partial<AiProviderConfig>;
|
|
17
19
|
}
|
|
18
20
|
export declare function setDocumentSize(size: ComponentSize, oldSize?: ComponentSize): void;
|
|
19
21
|
export declare function useConfig(): {
|
package/dist/use-config/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/use-config/index.ts"],"sourcesContent":["import { isObj } from '@cat-kit/core'\nimport type { ComponentSize } from '@veltra/utils'\nimport { reactive, readonly, watch } from 'vue'\n\ninterface State {\n /** 是否开启动画,机器老可以关闭动画来获得性能 */\n animation: boolean\n /** 组件尺寸大小 */\n size: ComponentSize\n /** 表单 */\n form: {\n /** 标签宽度 */\n labelWidth?: number | string\n }\n paginator: { pageSize: number; pageSizeOptions: number[] }\n}\n\nconst state = reactive<State>({\n animation: true,\n size: 'default',\n form: { labelWidth: 100 },\n paginator: { pageSize: 40, pageSizeOptions: [40, 100, 200, 500, 1000] }\n})\n\nexport function setDocumentSize(size: ComponentSize, oldSize?: ComponentSize): void {\n if (typeof document === 'undefined') return\n if (oldSize) {\n document.documentElement.classList.remove(oldSize)\n }\n document.documentElement.classList.add(size)\n}\n\nlet stopDocumentSizeSync: (() => void) | null = null\n\nfunction ensureDocumentSizeSync(): void {\n if (stopDocumentSizeSync) return\n if (typeof document === 'undefined') return\n\n stopDocumentSizeSync = watch(\n () => state.size,\n (size, oldSize) => setDocumentSize(size, oldSize)\n )\n}\n\nfunction deepSet(original: Record<string, any>, extend: Record<string, any>) {\n Object.keys(extend).forEach((key) => {\n const val = original[key]\n const targetVal = extend[key]\n if (isObj(val)) {\n if (isObj(targetVal)) {\n deepSet(val, targetVal)\n } else {\n console.warn(`extend['${key}']应该是一个对象`)\n }\n } else {\n original[key] = targetVal\n }\n })\n}\n\nexport function useConfig(): {\n config: Readonly<State>\n setConfig: (conf: Partial<State>) => void\n} {\n ensureDocumentSizeSync()\n return {\n /** 全局配置 */\n config: readonly(state) as Readonly<State>,\n /**\n * 设置全局配置项\n * @param conf\n */\n setConfig(conf: Partial<State>) {\n deepSet(state, conf)\n }\n }\n}\n"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/use-config/index.ts"],"sourcesContent":["import { isObj } from '@cat-kit/core'\nimport type { AiProviderConfig, ComponentSize } from '@veltra/utils'\nimport { reactive, readonly, watch } from 'vue'\n\ninterface State {\n /** 是否开启动画,机器老可以关闭动画来获得性能 */\n animation: boolean\n /** 组件尺寸大小 */\n size: ComponentSize\n /** 表单 */\n form: {\n /** 标签宽度 */\n labelWidth?: number | string\n }\n paginator: { pageSize: number; pageSizeOptions: number[] }\n /** AI 服务商全局兜底配置,与组件实例 prop 字段级合并 */\n ai: Partial<AiProviderConfig>\n}\n\nconst state = reactive<State>({\n animation: true,\n size: 'default',\n form: { labelWidth: 100 },\n paginator: { pageSize: 40, pageSizeOptions: [40, 100, 200, 500, 1000] },\n ai: {}\n})\n\nexport function setDocumentSize(size: ComponentSize, oldSize?: ComponentSize): void {\n if (typeof document === 'undefined') return\n if (oldSize) {\n document.documentElement.classList.remove(oldSize)\n }\n document.documentElement.classList.add(size)\n}\n\nlet stopDocumentSizeSync: (() => void) | null = null\n\nfunction ensureDocumentSizeSync(): void {\n if (stopDocumentSizeSync) return\n if (typeof document === 'undefined') return\n\n stopDocumentSizeSync = watch(\n () => state.size,\n (size, oldSize) => setDocumentSize(size, oldSize)\n )\n}\n\nfunction deepSet(original: Record<string, any>, extend: Record<string, any>) {\n Object.keys(extend).forEach((key) => {\n const val = original[key]\n const targetVal = extend[key]\n if (isObj(val)) {\n if (isObj(targetVal)) {\n deepSet(val, targetVal)\n } else {\n console.warn(`extend['${key}']应该是一个对象`)\n }\n } else {\n original[key] = targetVal\n }\n })\n}\n\nexport function useConfig(): {\n config: Readonly<State>\n setConfig: (conf: Partial<State>) => void\n} {\n ensureDocumentSizeSync()\n return {\n /** 全局配置 */\n config: readonly(state) as Readonly<State>,\n /**\n * 设置全局配置项\n * @param conf\n */\n setConfig(conf: Partial<State>) {\n deepSet(state, conf)\n }\n }\n}\n"],"mappings":";;;AAmBA,MAAM,QAAQ,SAAgB;CAC5B,WAAW;CACX,MAAM;CACN,MAAM,EAAE,YAAY,IAAI;CACxB,WAAW;EAAE,UAAU;EAAI,iBAAiB;GAAC;GAAI;GAAK;GAAK;GAAK;EAAI;CAAE;CACtE,IAAI,CAAC;AACP,CAAC;AAED,SAAgB,gBAAgB,MAAqB,SAA+B;CAClF,IAAI,OAAO,aAAa,aAAa;CACrC,IAAI,SACF,SAAS,gBAAgB,UAAU,OAAO,OAAO;CAEnD,SAAS,gBAAgB,UAAU,IAAI,IAAI;AAC7C;AAEA,IAAI,uBAA4C;AAEhD,SAAS,yBAA+B;CACtC,IAAI,sBAAsB;CAC1B,IAAI,OAAO,aAAa,aAAa;CAErC,uBAAuB,YACf,MAAM,OACX,MAAM,YAAY,gBAAgB,MAAM,OAAO,CAClD;AACF;AAEA,SAAS,QAAQ,UAA+B,QAA6B;CAC3E,OAAO,KAAK,MAAM,CAAC,CAAC,SAAS,QAAQ;EACnC,MAAM,MAAM,SAAS;EACrB,MAAM,YAAY,OAAO;EACzB,IAAI,MAAM,GAAG,GAAG;GACd,IAAI,MAAM,SAAS,GACjB,QAAQ,KAAK,SAAS;QAEtB,QAAQ,KAAK,WAAW,IAAI,UAAU;EAE1C,OACE,SAAS,OAAO;CAEpB,CAAC;AACH;AAEA,SAAgB,YAGd;CACA,uBAAuB;CACvB,OAAO;;EAEL,QAAQ,SAAS,KAAK;;;;;EAKtB,UAAU,MAAsB;GAC9B,QAAQ,OAAO,IAAI;EACrB;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vite-plus/test'
|
|
2
|
+
|
|
3
|
+
import { useConfig } from '../index'
|
|
4
|
+
|
|
5
|
+
describe('useConfig', () => {
|
|
6
|
+
it('setConfig 可设置全局 ai 配置,config.ai 可读', () => {
|
|
7
|
+
const { config, setConfig } = useConfig()
|
|
8
|
+
setConfig({
|
|
9
|
+
ai: { url: 'https://api.example.com/v1/chat/completions', apiKey: 'sk-1', model: 'gpt-4o' }
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
expect(config.ai).toMatchObject({
|
|
13
|
+
url: 'https://api.example.com/v1/chat/completions',
|
|
14
|
+
apiKey: 'sk-1',
|
|
15
|
+
model: 'gpt-4o'
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('多次 setConfig 部分字段按字段级合并,不互相覆盖', () => {
|
|
20
|
+
const { config, setConfig } = useConfig()
|
|
21
|
+
setConfig({ ai: { url: 'https://api.example.com/v1/chat/completions' } })
|
|
22
|
+
setConfig({ ai: { model: 'gpt-4o-mini' } })
|
|
23
|
+
setConfig({ ai: { stream: true } })
|
|
24
|
+
|
|
25
|
+
expect(config.ai).toMatchObject({
|
|
26
|
+
url: 'https://api.example.com/v1/chat/completions',
|
|
27
|
+
model: 'gpt-4o-mini',
|
|
28
|
+
stream: true
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('ai 更新不影响其它全局配置键', () => {
|
|
33
|
+
const { config, setConfig } = useConfig()
|
|
34
|
+
setConfig({ animation: false, ai: { reasoningLevel: 'high' } })
|
|
35
|
+
|
|
36
|
+
expect(config.animation).toBe(false)
|
|
37
|
+
expect(config.ai.reasoningLevel).toBe('high')
|
|
38
|
+
expect(config.form).toEqual({ labelWidth: 100 })
|
|
39
|
+
})
|
|
40
|
+
})
|
package/src/use-config/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isObj } from '@cat-kit/core'
|
|
2
|
-
import type { ComponentSize } from '@veltra/utils'
|
|
2
|
+
import type { AiProviderConfig, ComponentSize } from '@veltra/utils'
|
|
3
3
|
import { reactive, readonly, watch } from 'vue'
|
|
4
4
|
|
|
5
5
|
interface State {
|
|
@@ -13,13 +13,16 @@ interface State {
|
|
|
13
13
|
labelWidth?: number | string
|
|
14
14
|
}
|
|
15
15
|
paginator: { pageSize: number; pageSizeOptions: number[] }
|
|
16
|
+
/** AI 服务商全局兜底配置,与组件实例 prop 字段级合并 */
|
|
17
|
+
ai: Partial<AiProviderConfig>
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
const state = reactive<State>({
|
|
19
21
|
animation: true,
|
|
20
22
|
size: 'default',
|
|
21
23
|
form: { labelWidth: 100 },
|
|
22
|
-
paginator: { pageSize: 40, pageSizeOptions: [40, 100, 200, 500, 1000] }
|
|
24
|
+
paginator: { pageSize: 40, pageSizeOptions: [40, 100, 200, 500, 1000] },
|
|
25
|
+
ai: {}
|
|
23
26
|
})
|
|
24
27
|
|
|
25
28
|
export function setDocumentSize(size: ComponentSize, oldSize?: ComponentSize): void {
|