@cloudbase/ai 2.28.6 → 2.30.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/ai",
3
- "version": "2.28.6",
3
+ "version": "2.30.0",
4
4
  "description": "cloudbase js sdk ai module",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -30,8 +30,8 @@
30
30
  "author": "",
31
31
  "license": "Apache-2.0",
32
32
  "dependencies": {
33
- "@cloudbase/types": "2.28.6",
34
- "@cloudbase/utilities": "2.28.6",
33
+ "@cloudbase/types": "2.30.0",
34
+ "@cloudbase/utilities": "2.30.0",
35
35
  "@mattiasbuelens/web-streams-adapter": "^0.1.0",
36
36
  "text-encoding-shim": "^1.0.5",
37
37
  "web-streams-polyfill": "^4.0.0"
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "@cloudbase/adapter-interface": "^0.7.1"
41
41
  },
42
- "gitHead": "fb1d36651d8e9b0e54dda40a3f269be0540076c1"
42
+ "gitHead": "859557576897d4778857c12fb8d7fe8ca1f3a052"
43
43
  }
package/src/errors.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 参数校验错误类
3
+ * 用于统一参数校验错误的格式
4
+ */
5
+ export class InvalidParamsError extends Error {
6
+ public code: string
7
+ public field?: string
8
+
9
+ constructor(message: string, field?: string) {
10
+ super(message)
11
+ this.name = 'InvalidParamsError'
12
+ this.code = 'INVALID_PARAMS'
13
+ this.field = field
14
+ }
15
+
16
+ toJSON() {
17
+ return {
18
+ code: this.code,
19
+ message: this.message,
20
+ field: this.field,
21
+ }
22
+ }
23
+ }
package/src/index.ts CHANGED
@@ -150,6 +150,7 @@ function registerAi(app: ICloudbase) {
150
150
 
151
151
  export { AI, Bot, createAi, createAI, restModels as models, registerAi, utils }
152
152
  export * from './type'
153
+ export * from './errors'
153
154
 
154
155
  try {
155
156
  (window as any).registerAi = registerAi
@@ -8,6 +8,7 @@ import type {
8
8
  // HunyuanEditImageInput,
9
9
  // HunyuanEditImageOutput,
10
10
  } from '../type'
11
+ import { InvalidParamsError } from '../errors'
11
12
 
12
13
  interface HunyuanImageGenerateFn {
13
14
  (input: HunyuanARGenerateImageInput): Promise<HunyuanARGenerateImageOutput>
@@ -19,28 +20,52 @@ type GenerateImageFn<TProvider> = TProvider extends 'hunyuan-image'
19
20
  : (input: unknown) => Promise<unknown>
20
21
 
21
22
  export class DefaultImageModel<TProvider extends string> {
22
- public defaultGenerateImageSubUrl = 'images/generations'
23
+ public defaultGenerateImageSubUrl = 'images/ar/generations'
23
24
 
24
- public generateImageSubUrlConfig: Record<string, Record<string, string>> = {
25
- 'hunyuan-image': {
26
- 'hunyuan-image-v3.0-v1.0.4': 'images/ar/generations',
27
- 'hunyuan-image-v3.0-v1.0.1': 'images/ar/generations',
28
- },
25
+ public generateImageSubUrlConfig: Record<string, Array<[RegExp, string]>> = {
26
+ 'hunyuan-image': [[/^hunyuan-image(-v[12](\..*)?)?$/, 'images/generations']],
29
27
  }
30
28
 
31
29
  /** 生成图片 */
32
30
  public generateImage: GenerateImageFn<TProvider>
33
31
 
34
- constructor(private req: ModelReq, public baseUrl: string, public provider: TProvider) {
32
+ constructor(
33
+ private req: ModelReq,
34
+ public baseUrl: string,
35
+ public provider: TProvider,
36
+ ) {
35
37
  this.generateImage = this.generateImageImpl.bind(this) as GenerateImageFn<TProvider>
36
38
  }
37
39
 
38
40
  /** generateImage 的实际实现 */
39
41
  private async generateImageImpl(input: HunyuanARGenerateImageInput | HunyuanGenerateImageInput,): Promise<HunyuanARGenerateImageOutput | HunyuanGenerateImageOutput> {
42
+ // 当模型为 HY-Image-v3.0-I2I-ToB-v1.0.1 且 provider 为空或为 hunyuan-image 时校验参数
43
+ if (
44
+ typeof input === 'object' && input && 'model' in input && input.model === 'HY-Image-v3.0-I2I-ToB-v1.0.1'
45
+ && (this.provider === '' || this.provider === 'hunyuan-image')
46
+ ) {
47
+ const typedInput = input as HunyuanARGenerateImageInput
48
+ const imageUrls = typedInput.image_urls as string[] | undefined
49
+ const images = typedInput.images as string[] | undefined
50
+
51
+ if (imageUrls && imageUrls.length > 1) {
52
+ throw new InvalidParamsError(
53
+ '当模型为 HY-Image-v3.0-I2I-ToB-v1.0.1 时,image_urls 最多只能有 1 个元素',
54
+ 'image_urls',
55
+ )
56
+ }
57
+ if (images && images.length > 1) {
58
+ throw new InvalidParamsError(
59
+ '当模型为 HY-Image-v3.0-I2I-ToB-v1.0.1 时,images 最多只能有 1 个元素',
60
+ 'images',
61
+ )
62
+ }
63
+ }
64
+
40
65
  let subUrl = this.defaultGenerateImageSubUrl
41
66
 
42
67
  if (typeof input === 'object' && input && 'model' in input && typeof input.model === 'string') {
43
- const newSubUrl = this.generateImageSubUrlConfig[this.provider]?.[input.model]
68
+ const newSubUrl = this.getGenerateImageSubUrl(this.provider, input.model)
44
69
  if (newSubUrl) subUrl = newSubUrl
45
70
  }
46
71
 
@@ -56,4 +81,19 @@ export class DefaultImageModel<TProvider extends string> {
56
81
  private getGenerateUrl(subUrl: string) {
57
82
  return `${this.baseUrl}/${this.provider}/${subUrl}`
58
83
  }
84
+
85
+ /**
86
+ * 根据 provider 与 model 解析生成图片的子路径。
87
+ * 按配置顺序用 RegExp 匹配,命中即返回对应 subUrl。
88
+ */
89
+ private getGenerateImageSubUrl(provider: string, model: string): string | undefined {
90
+ const rules = this.generateImageSubUrlConfig[provider]
91
+ if (!rules) return undefined
92
+
93
+ for (const [pattern, subUrl] of rules) {
94
+ if (pattern.test(model)) return subUrl
95
+ }
96
+
97
+ return undefined
98
+ }
59
99
  }
package/src/type.ts CHANGED
@@ -230,8 +230,8 @@ export interface HunyuanGenerateImageInput {
230
230
  }
231
231
 
232
232
  export interface HunyuanARGenerateImageInput {
233
- /** 模型名称:hunyuan-image-v3.0-v1.0.4(推荐)或 hunyuan-image-v3.0-v1.0.1 */
234
- model: 'hunyuan-image-v3.0-v1.0.4' | 'hunyuan-image-v3.0-v1.0.1'
233
+ /** 模型名称:hunyuan-image-v3.0-v1.0.4(推荐)、hunyuan-image-v3.0-v1.0.1 或 HY-Image-v3.0-I2I-ToB-v1.0.1 */
234
+ model: 'hunyuan-image-v3.0-v1.0.4' | 'hunyuan-image-v3.0-v1.0.1' | 'HY-Image-v3.0-I2I-ToB-v1.0.1' | 'HY-Image-3.0-Plus-4090-Tob-v1.0'
235
235
  /** 生成图片使用的文本,不超过 8192 字符 */
236
236
  prompt: string
237
237
  /**
@@ -248,6 +248,11 @@ export interface HunyuanARGenerateImageInput {
248
248
  revise?: { value: boolean }
249
249
  /** 改写是否开启 thinking 模式,默认开启。开启后效果提升但耗时增加(最大 60s) */
250
250
  enable_thinking?: { value: boolean }
251
+ /**
252
+ * 改写图片地址
253
+ */
254
+ image_urls?: string[]
255
+ images?: string[]
251
256
  }
252
257
 
253
258
  export interface HunyuanARGenerateImageOutput {