@cloudbase/ai 3.0.0 → 3.0.1
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/dist/cjs/AI.d.ts +1 -0
- package/dist/cjs/AI.js +4 -1
- package/dist/cjs/bot/index.js +1 -1
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.js +10 -4
- package/dist/cjs/models/Ark/index.js +1 -1
- package/dist/cjs/models/DashScope/index.js +1 -1
- package/dist/cjs/models/DeepSeek/index.js +1 -1
- package/dist/cjs/models/Default/index.js +1 -1
- package/dist/cjs/models/HunYuan/index.js +1 -1
- package/dist/cjs/models/HunYuan/util.js +1 -1
- package/dist/cjs/models/HunYuanBeta/index.js +1 -1
- package/dist/cjs/models/HunYuanExp/index.js +1 -1
- package/dist/cjs/models/HunYuanOpen/index.js +1 -1
- package/dist/cjs/models/Moonshot/index.js +1 -1
- package/dist/cjs/models/Yi/index.js +1 -1
- package/dist/cjs/models/ZhiPu/index.js +1 -1
- package/dist/cjs/models/image-model.d.ts +18 -0
- package/dist/cjs/models/image-model.js +85 -0
- package/dist/cjs/models/index.d.ts +2 -1
- package/dist/cjs/models/index.js +4 -2
- package/dist/cjs/models/model.js +1 -1
- package/dist/cjs/type.d.ts +79 -0
- package/dist/cjs/type.js +1 -1
- package/dist/cjs/utils.js +1 -1
- package/dist/esm/AI.d.ts +1 -0
- package/dist/esm/AI.js +4 -1
- package/dist/esm/bot/index.js +1 -1
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.js +9 -4
- package/dist/esm/models/Ark/index.js +1 -1
- package/dist/esm/models/DashScope/index.js +1 -1
- package/dist/esm/models/DeepSeek/index.js +1 -1
- package/dist/esm/models/Default/index.js +1 -1
- package/dist/esm/models/HunYuan/index.js +1 -1
- package/dist/esm/models/HunYuan/util.js +1 -1
- package/dist/esm/models/HunYuanBeta/index.js +1 -1
- package/dist/esm/models/HunYuanExp/index.js +1 -1
- package/dist/esm/models/HunYuanOpen/index.js +1 -1
- package/dist/esm/models/Moonshot/index.js +1 -1
- package/dist/esm/models/Yi/index.js +1 -1
- package/dist/esm/models/ZhiPu/index.js +1 -1
- package/dist/esm/models/image-model.d.ts +18 -0
- package/dist/esm/models/image-model.js +82 -0
- package/dist/esm/models/index.d.ts +2 -1
- package/dist/esm/models/index.js +3 -2
- package/dist/esm/models/model.js +1 -1
- package/dist/esm/type.d.ts +79 -0
- package/dist/esm/type.js +1 -1
- package/dist/esm/utils.js +1 -1
- package/dist/miniprogram/index.js +1 -1
- package/package.json +4 -4
- package/src/AI.ts +11 -1
- package/src/bot/index.ts +2 -0
- package/src/index.ts +16 -5
- package/src/models/Ark/index.ts +2 -0
- package/src/models/DashScope/index.ts +2 -0
- package/src/models/DeepSeek/index.ts +2 -0
- package/src/models/Default/index.ts +2 -0
- package/src/models/HunYuan/index.ts +2 -0
- package/src/models/HunYuan/util.ts +2 -0
- package/src/models/HunYuanBeta/index.ts +2 -0
- package/src/models/HunYuanExp/index.ts +2 -0
- package/src/models/HunYuanOpen/index.ts +2 -0
- package/src/models/Moonshot/index.ts +3 -0
- package/src/models/Yi/index.ts +2 -0
- package/src/models/ZhiPu/index.ts +2 -0
- package/src/models/image-model.ts +59 -0
- package/src/models/index.ts +2 -0
- package/src/models/model.ts +2 -0
- package/src/type.ts +203 -1
- package/src/utils.ts +3 -1
- package/tsconfig.json +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/ai",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
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": "3.0.
|
|
34
|
-
"@cloudbase/utilities": "3.0.
|
|
33
|
+
"@cloudbase/types": "3.0.1",
|
|
34
|
+
"@cloudbase/utilities": "3.0.1",
|
|
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": "
|
|
42
|
+
"gitHead": "a29767afc9a2b16cd632074c7c751abcf81746ed"
|
|
43
43
|
}
|
package/src/AI.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
2
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
1
3
|
import type { SDKRequestInterface } from '@cloudbase/adapter-interface'
|
|
2
4
|
import { Bot } from './bot'
|
|
3
5
|
import * as models from './models'
|
|
@@ -15,7 +17,11 @@ class AI {
|
|
|
15
17
|
public i18n: ICloudbaseConfig['i18n']
|
|
16
18
|
defaultHeaders: { [x: string]: any }
|
|
17
19
|
|
|
18
|
-
constructor(
|
|
20
|
+
constructor(
|
|
21
|
+
private req: SDKRequestInterface,
|
|
22
|
+
public baseUrl: string,
|
|
23
|
+
i18n: ICloudbaseConfig['i18n'],
|
|
24
|
+
) {
|
|
19
25
|
this.aiBaseUrl = `${baseUrl}/ai`
|
|
20
26
|
this.aiBotBaseUrl = `${baseUrl}/aibot`
|
|
21
27
|
this.bot = new Bot(this.botRequest, this.aiBotBaseUrl)
|
|
@@ -81,6 +87,10 @@ class AI {
|
|
|
81
87
|
MODELS[name] = model
|
|
82
88
|
}
|
|
83
89
|
|
|
90
|
+
createImageModel<T extends 'hunyuan-image' | (string & {})>(provider: T) {
|
|
91
|
+
return new models.DefaultImageModel<T>(this.modelRequest, this.aiBaseUrl, provider)
|
|
92
|
+
}
|
|
93
|
+
|
|
84
94
|
modelRequest: types.ModelReq = async ({ url, data, headers, stream, timeout }) => {
|
|
85
95
|
const fetchHeaders = {
|
|
86
96
|
'Content-Type': 'application/json',
|
package/src/bot/index.ts
CHANGED
|
@@ -34,6 +34,7 @@ export class Bot {
|
|
|
34
34
|
req: BotReq
|
|
35
35
|
|
|
36
36
|
constructor(req: BotReq, public baseUrl: string) {
|
|
37
|
+
// eslint-disable-next-line prefer-rest-params
|
|
37
38
|
const token = arguments[2]
|
|
38
39
|
if (typeof token === 'string') {
|
|
39
40
|
this.req = ({ headers = {}, ...rest }) => req({ ...rest, headers: { ...headers, Authorization: `Bearer ${token}` } })
|
|
@@ -230,6 +231,7 @@ export class Bot {
|
|
|
230
231
|
type BotEventStreamData = { content: string }
|
|
231
232
|
|
|
232
233
|
class StreamResult {
|
|
234
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
233
235
|
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
234
236
|
|
|
235
237
|
constructor(_stream: ReadableStream<Uint8Array>) {
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
1
2
|
import type { IFetchOptions, SDKRequestInterface } from '@cloudbase/adapter-interface'
|
|
2
3
|
import type { ICloudbase, ICloudbaseConfig } from '@cloudbase/types'
|
|
3
4
|
import type { ICloudbaseComponent } from '@cloudbase/types/component'
|
|
@@ -54,7 +55,7 @@ interface AIInitOption {
|
|
|
54
55
|
baseUrl?: string
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
interface
|
|
58
|
+
interface ICreateAI {
|
|
58
59
|
req: SDKRequestInterface
|
|
59
60
|
baseUrl?: string
|
|
60
61
|
env?: string
|
|
@@ -73,7 +74,7 @@ interface ICreateAi {
|
|
|
73
74
|
*
|
|
74
75
|
* 其他参数可按需传入覆盖函数逻辑。
|
|
75
76
|
*/
|
|
76
|
-
function
|
|
77
|
+
function createAI({ env, baseUrl, req, getAccessToken, handleReqInstance, i18n }: ICreateAI) {
|
|
77
78
|
const getBaseUrl = () => {
|
|
78
79
|
if (baseUrl != null) {
|
|
79
80
|
return baseUrl
|
|
@@ -97,10 +98,16 @@ function createAi({ env, baseUrl, req, getAccessToken, handleReqInstance, i18n }
|
|
|
97
98
|
return new AI(getReq(), getBaseUrl(), i18n)
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
// 兼容
|
|
102
|
+
/**
|
|
103
|
+
* @deprecated use `createAI`
|
|
104
|
+
*/
|
|
105
|
+
const createAi = createAI
|
|
106
|
+
|
|
100
107
|
/**
|
|
101
108
|
* 创建 `AI` 实例。
|
|
102
109
|
* 该函数挂载在 js sdk 的 Cloudbase 实例上。
|
|
103
|
-
* 该函数调用了本模块暴露的 `
|
|
110
|
+
* 该函数调用了本模块暴露的 `createAI` 函数,但调用时通过传入参数做了逻辑覆盖:
|
|
104
111
|
*
|
|
105
112
|
* - 传入从 Cloudbase 实例上的中心的 `req`
|
|
106
113
|
* - 传入从 Cloudbase 实例上拿到的 `baseUrl`
|
|
@@ -125,7 +132,7 @@ function ai(
|
|
|
125
132
|
|
|
126
133
|
const baseUrl = options?.baseUrl ?? getUrlFromCloud()
|
|
127
134
|
|
|
128
|
-
return
|
|
135
|
+
return createAI({ req, baseUrl, handleReqInstance: ({ req }) => req, i18n: this.config.i18n })
|
|
129
136
|
}
|
|
130
137
|
|
|
131
138
|
const component: ICloudbaseComponent = {
|
|
@@ -141,5 +148,9 @@ function registerAi(app: ICloudbase) {
|
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
export { AI, Bot, createAi, restModels as models, registerAi, utils }
|
|
151
|
+
export { AI, Bot, createAi, createAI, restModels as models, registerAi, utils }
|
|
145
152
|
export * from './type'
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
(window as any).registerAi = registerAi
|
|
156
|
+
} catch (e) {}
|
package/src/models/Ark/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
1
2
|
import {
|
|
2
3
|
createAsyncIterable,
|
|
3
4
|
TransformStream,
|
|
@@ -42,6 +43,7 @@ export class DeepSeekSimpleModel implements SimpleChatModel {
|
|
|
42
43
|
|
|
43
44
|
public async doStream(data: BaseChatModelInput, options?: ReqOptions): Promise<DoStreamOutput> {
|
|
44
45
|
let isToolCall: null | boolean = null
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
45
47
|
const _stream = await this.req({
|
|
46
48
|
url: this.url,
|
|
47
49
|
data: {
|
|
@@ -33,6 +33,7 @@ export class DefaultSimpleModel implements SimpleChatModel {
|
|
|
33
33
|
public async doStream(data: BaseChatModelInput): Promise<DoStreamOutput> {
|
|
34
34
|
let isToolCall: null | boolean = null
|
|
35
35
|
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
36
37
|
const _stream = await this.req({
|
|
37
38
|
url: this.url,
|
|
38
39
|
data: {
|
|
@@ -48,6 +49,7 @@ export class DefaultSimpleModel implements SimpleChatModel {
|
|
|
48
49
|
transform(chunk, controller) {
|
|
49
50
|
const newChoices = chunk.choices.map((choice) => {
|
|
50
51
|
const message = choice.delta
|
|
52
|
+
// eslint-disable-next-line eqeqeq
|
|
51
53
|
if (isToolCall == null) isToolCall = isToolCallAssistantMessage(message)
|
|
52
54
|
if (isToolCall) {
|
|
53
55
|
return {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
1
2
|
import {
|
|
2
3
|
createAsyncIterable,
|
|
3
4
|
TransformStream,
|
|
@@ -52,6 +53,7 @@ export class HunYuanSimpleModel implements SimpleChatModel {
|
|
|
52
53
|
public async doStream(data: BaseChatModelInput, options?: ReqOptions): Promise<DoStreamOutput> {
|
|
53
54
|
let isToolCall: null | boolean = null
|
|
54
55
|
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
55
57
|
const _stream = await this.req({
|
|
56
58
|
url: this.url,
|
|
57
59
|
headers: {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
1
2
|
import {
|
|
2
3
|
createAsyncIterable,
|
|
3
4
|
TransformStream,
|
|
@@ -45,6 +46,7 @@ export class HunYuanOpenSimpleModel implements SimpleChatModel {
|
|
|
45
46
|
public async doStream(data: BaseChatModelInput, options?: ReqOptions): Promise<DoStreamOutput> {
|
|
46
47
|
let isToolCall: null | boolean = null
|
|
47
48
|
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
48
50
|
const _stream = await this.req({
|
|
49
51
|
url: this.url,
|
|
50
52
|
data: {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable eqeqeq */
|
|
1
2
|
import {
|
|
2
3
|
createAsyncIterable,
|
|
3
4
|
TransformStream,
|
|
@@ -17,6 +18,7 @@ import type {
|
|
|
17
18
|
} from '../../type'
|
|
18
19
|
|
|
19
20
|
function processInput(input: BaseChatModelInput): MoonshotInputData {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
20
22
|
const { messages, model, temperature, tools, top_p } = input
|
|
21
23
|
|
|
22
24
|
return {
|
|
@@ -56,6 +58,7 @@ export class MoonshotSimpleModel implements SimpleChatModel {
|
|
|
56
58
|
|
|
57
59
|
public async doStream(data: BaseChatModelInput, options?: ReqOptions): Promise<DoStreamOutput> {
|
|
58
60
|
let isToolCall: null | boolean = null
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
59
62
|
const _stream = await this.req({
|
|
60
63
|
url: this.url,
|
|
61
64
|
data: {
|
package/src/models/Yi/index.ts
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModelReq,
|
|
3
|
+
// ImageEditInput,
|
|
4
|
+
HunyuanGenerateImageInput,
|
|
5
|
+
HunyuanGenerateImageOutput,
|
|
6
|
+
HunyuanARGenerateImageInput,
|
|
7
|
+
HunyuanARGenerateImageOutput,
|
|
8
|
+
// HunyuanEditImageInput,
|
|
9
|
+
// HunyuanEditImageOutput,
|
|
10
|
+
} from '../type'
|
|
11
|
+
|
|
12
|
+
interface HunyuanImageGenerateFn {
|
|
13
|
+
(input: HunyuanARGenerateImageInput): Promise<HunyuanARGenerateImageOutput>
|
|
14
|
+
(input: HunyuanGenerateImageInput): Promise<HunyuanGenerateImageOutput>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type GenerateImageFn<TProvider> = TProvider extends 'hunyuan-image'
|
|
18
|
+
? HunyuanImageGenerateFn
|
|
19
|
+
: (input: unknown) => Promise<unknown>
|
|
20
|
+
|
|
21
|
+
export class DefaultImageModel<TProvider extends string> {
|
|
22
|
+
public defaultGenerateImageSubUrl = 'images/generations'
|
|
23
|
+
|
|
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
|
+
},
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 生成图片 */
|
|
32
|
+
public generateImage: GenerateImageFn<TProvider>
|
|
33
|
+
|
|
34
|
+
constructor(private req: ModelReq, public baseUrl: string, public provider: TProvider) {
|
|
35
|
+
this.generateImage = this.generateImageImpl.bind(this) as GenerateImageFn<TProvider>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** generateImage 的实际实现 */
|
|
39
|
+
private async generateImageImpl(input: HunyuanARGenerateImageInput | HunyuanGenerateImageInput,): Promise<HunyuanARGenerateImageOutput | HunyuanGenerateImageOutput> {
|
|
40
|
+
let subUrl = this.defaultGenerateImageSubUrl
|
|
41
|
+
|
|
42
|
+
if (typeof input === 'object' && input && 'model' in input && typeof input.model === 'string') {
|
|
43
|
+
const newSubUrl = this.generateImageSubUrlConfig[this.provider]?.[input.model]
|
|
44
|
+
if (newSubUrl) subUrl = newSubUrl
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const res = (await this.req({
|
|
48
|
+
url: this.getGenerateUrl(subUrl),
|
|
49
|
+
data: input,
|
|
50
|
+
stream: false,
|
|
51
|
+
})) as HunyuanARGenerateImageOutput | HunyuanGenerateImageOutput
|
|
52
|
+
|
|
53
|
+
return res
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private getGenerateUrl(subUrl: string) {
|
|
57
|
+
return `${this.baseUrl}/${this.provider}/${subUrl}`
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/models/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { HunYuanExpSimpleModel } from './HunYuanExp/index'
|
|
|
9
9
|
import { HunYuanOpenSimpleModel } from './HunYuanOpen/index'
|
|
10
10
|
import { DeepSeekSimpleModel } from './DeepSeek'
|
|
11
11
|
import { DefaultSimpleModel } from './Default'
|
|
12
|
+
import { DefaultImageModel } from './image-model'
|
|
12
13
|
|
|
13
14
|
export const MODELS = {
|
|
14
15
|
hunyuan: HunYuanSimpleModel,
|
|
@@ -34,6 +35,7 @@ export {
|
|
|
34
35
|
HunYuanExpSimpleModel,
|
|
35
36
|
HunYuanOpenSimpleModel,
|
|
36
37
|
DefaultSimpleModel,
|
|
38
|
+
DefaultImageModel,
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
export { ReactModel, toolMap } from './model'
|
package/src/models/model.ts
CHANGED
package/src/type.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface IModelReqInput {
|
|
|
20
20
|
url: string
|
|
21
21
|
headers?: Record<string, string>
|
|
22
22
|
data?: Object
|
|
23
|
-
stream?: boolean
|
|
23
|
+
stream?: boolean
|
|
24
24
|
timeout?: number
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -145,3 +145,205 @@ export interface BaseDoStreamOutputChunk {
|
|
|
145
145
|
usage?: Usage
|
|
146
146
|
}
|
|
147
147
|
// #endregion
|
|
148
|
+
|
|
149
|
+
// #region Image Model Types
|
|
150
|
+
/**
|
|
151
|
+
* 图片生成输入参数
|
|
152
|
+
*/
|
|
153
|
+
export interface ImageGenerateInput {
|
|
154
|
+
model: string // 模型名称,例如 'hunyuan-image'、'dall-e-3'
|
|
155
|
+
prompt: string // 用于描述要生成的图片内容
|
|
156
|
+
size?: string // 尺寸
|
|
157
|
+
n?: number // 张数
|
|
158
|
+
[key: string]: unknown // 允许额外的厂商特定参数
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 图片编辑输入参数
|
|
163
|
+
*/
|
|
164
|
+
export interface ImageEditInput {
|
|
165
|
+
model: string
|
|
166
|
+
prompt: string
|
|
167
|
+
size?: string // 尺寸
|
|
168
|
+
n?: number // 张数
|
|
169
|
+
[key: string]: unknown // 允许额外的厂商特定参数
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface HunyuanGenerateImageInput {
|
|
173
|
+
model: 'hunyuan-image'
|
|
174
|
+
/** 用来生成图像的文本描述 */
|
|
175
|
+
prompt: string
|
|
176
|
+
/**
|
|
177
|
+
* 模型版本,支持 v1.8.1 和 v1.9,默认版本 v1.8.1
|
|
178
|
+
* - v1.8.1: 采用二阶段模型支持文生图任务,针对图像细节、稳定性等要求高的任务较为合适
|
|
179
|
+
* - v1.9: 采用模型矩阵能力支持文生图任务,后端由人像、游戏、通用模型,以及各类功能插件组成,灵活适配多种文生图任务
|
|
180
|
+
*/
|
|
181
|
+
version?: 'v1.8.1' | 'v1.9' | (string & {})
|
|
182
|
+
/**
|
|
183
|
+
* 图片尺寸,默认 "1024x1024"
|
|
184
|
+
* v1.9 仅支持以下几个尺寸:
|
|
185
|
+
* - 1024x1024
|
|
186
|
+
* - 1024x768, 1152x864
|
|
187
|
+
* - 768x1024, 864x1152
|
|
188
|
+
* - 1280x768 (16:9)
|
|
189
|
+
* - 768x1280 (9:16)
|
|
190
|
+
* - 1280x720 (16:9)
|
|
191
|
+
* - 720x1280 (9:16)
|
|
192
|
+
*
|
|
193
|
+
* 支持用户自定义尺寸,但输入尺寸只支持 768-1280 之间且为 64 的倍数的数字
|
|
194
|
+
*/
|
|
195
|
+
size?: string
|
|
196
|
+
/**
|
|
197
|
+
* 仅 v1.9 支持,负向词
|
|
198
|
+
* 如果不传则使用各个模型内部的默认负向词
|
|
199
|
+
*/
|
|
200
|
+
negative_prompt?: string
|
|
201
|
+
/**
|
|
202
|
+
* 仅 v1.9 支持,可指定风格:
|
|
203
|
+
* - 古风二次元风格
|
|
204
|
+
* - 都市二次元风格
|
|
205
|
+
* - 悬疑风格
|
|
206
|
+
* - 校园风格
|
|
207
|
+
* - 都市异能风格
|
|
208
|
+
*/
|
|
209
|
+
style?: '古风二次元风格' | '都市二次元风格' | '悬疑风格' | '校园风格' | '都市异能风格' | (string & {})
|
|
210
|
+
/**
|
|
211
|
+
* 为 true 时对 prompt 进行改写,实际生成的图片会使用改写后的 prompt 进行生成
|
|
212
|
+
* 多数场景可提升生成的图片效果,默认为 true
|
|
213
|
+
*/
|
|
214
|
+
revise?: boolean
|
|
215
|
+
/**
|
|
216
|
+
* 生成图片个数,默认为 1
|
|
217
|
+
* 目前只支持生成一张,固定为 1,传其他的值会报错
|
|
218
|
+
*/
|
|
219
|
+
n?: number
|
|
220
|
+
/**
|
|
221
|
+
* 业务自定义水印内容
|
|
222
|
+
* 限制 16 个字符长度(不区分中英文),生成在图片右下角
|
|
223
|
+
*/
|
|
224
|
+
footnote?: string
|
|
225
|
+
/**
|
|
226
|
+
* 生成种子,仅当生成图片数为 1 时生效
|
|
227
|
+
* 范围 [1, 4294967295],不传时默认随机
|
|
228
|
+
*/
|
|
229
|
+
seed?: number
|
|
230
|
+
}
|
|
231
|
+
|
|
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'
|
|
235
|
+
/** 生成图片使用的文本,不超过 8192 字符 */
|
|
236
|
+
prompt: string
|
|
237
|
+
/**
|
|
238
|
+
* 图片尺寸,格式 "${宽}x${高}",默认 "1024x1024"
|
|
239
|
+
* hunyuan-image-v3.0-v1.0.4:宽高 [512, 2048],面积不超过 1024x1024
|
|
240
|
+
* hunyuan-image-v3.0-v1.0.1:支持固定尺寸列表
|
|
241
|
+
*/
|
|
242
|
+
size?: string
|
|
243
|
+
/** 生成种子,仅当生成图片数为 1 时生效,范围 [1, 4294967295] */
|
|
244
|
+
seed?: number
|
|
245
|
+
/** 业务自定义水印内容,限制 16 字符,生成在图片右下角 */
|
|
246
|
+
footnote?: string
|
|
247
|
+
/** 是否对 prompt 改写,默认开启。改写会增加约 30s 耗时 */
|
|
248
|
+
revise?: { value: boolean }
|
|
249
|
+
/** 改写是否开启 thinking 模式,默认开启。开启后效果提升但耗时增加(最大 60s) */
|
|
250
|
+
enable_thinking?: { value: boolean }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface HunyuanARGenerateImageOutput {
|
|
254
|
+
/** 此次请求的 id */
|
|
255
|
+
id: string
|
|
256
|
+
/** unix 时间戳 */
|
|
257
|
+
created: number
|
|
258
|
+
/** 返回的图片生成内容 */
|
|
259
|
+
data: Array<{
|
|
260
|
+
/** 生成的图片 url,有效期 24 小时 */
|
|
261
|
+
url: string
|
|
262
|
+
/** 改写后的 prompt */
|
|
263
|
+
revised_prompt?: string
|
|
264
|
+
}>
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export type HunyuanEditImageInput = {
|
|
268
|
+
/** 模型名称,本期为 hunyuan-image,即为商品图编辑场景 */
|
|
269
|
+
model: 'hunyuan-image' | (string & {})
|
|
270
|
+
/** 编辑的文本描述 */
|
|
271
|
+
prompt: string
|
|
272
|
+
/** 模型版本 */
|
|
273
|
+
version?: string
|
|
274
|
+
/**
|
|
275
|
+
* 图片尺寸,默认 "800x800"
|
|
276
|
+
* 支持用户自定义尺寸,但输入尺寸只支持 512-1280 之间
|
|
277
|
+
* 目前只支持 1:1、16:9、9:16 三个尺寸
|
|
278
|
+
*/
|
|
279
|
+
size?: string
|
|
280
|
+
/**
|
|
281
|
+
* 生成图片个数,默认为 1
|
|
282
|
+
* 目前只支持生成一张,固定为 1,传其他的值会报错
|
|
283
|
+
*/
|
|
284
|
+
n?: number
|
|
285
|
+
/**
|
|
286
|
+
* beta 参数,随机种子
|
|
287
|
+
* 范围 [1, 4294967295],不传时默认随机
|
|
288
|
+
*/
|
|
289
|
+
seed?: number
|
|
290
|
+
/**
|
|
291
|
+
* 业务自定义角标内容
|
|
292
|
+
* 限制 16 个字符长度(不区分中英文)
|
|
293
|
+
*/
|
|
294
|
+
footnote?: string
|
|
295
|
+
/**
|
|
296
|
+
* 图 base64 编码,大小不超过 6M
|
|
297
|
+
* 支持 jpg/jpeg 格式,和 image_url 二选一
|
|
298
|
+
* 长短边比例要求 2:1 内
|
|
299
|
+
*/
|
|
300
|
+
image?: string
|
|
301
|
+
/**
|
|
302
|
+
* 图片 url
|
|
303
|
+
* 长短边比例要求 2:1 内,和 image 二选一
|
|
304
|
+
*/
|
|
305
|
+
image_url?: string
|
|
306
|
+
} & (
|
|
307
|
+
| {
|
|
308
|
+
mask: string
|
|
309
|
+
mask_url?: never
|
|
310
|
+
}
|
|
311
|
+
| {
|
|
312
|
+
mask?: never
|
|
313
|
+
mask_url: string
|
|
314
|
+
}
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 混元图片生成输出
|
|
319
|
+
*/
|
|
320
|
+
export interface HunyuanGenerateImageOutput {
|
|
321
|
+
/** 此次请求的 id */
|
|
322
|
+
id: string
|
|
323
|
+
/** unix 时间戳 */
|
|
324
|
+
created: number
|
|
325
|
+
/** 返回的图片生成内容 */
|
|
326
|
+
data: Array<{
|
|
327
|
+
/** 生成的图片 url,有效期为 24 小时 */
|
|
328
|
+
url: string
|
|
329
|
+
/** 原 prompt 改写后的文本。若 revise 为 false,则为原 prompt */
|
|
330
|
+
revised_prompt?: string
|
|
331
|
+
}>
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* 混元图片编辑输出
|
|
336
|
+
*/
|
|
337
|
+
export interface HunyuanEditImageOutput {
|
|
338
|
+
/** 此次请求的 id */
|
|
339
|
+
id: string
|
|
340
|
+
/** unix 时间戳 */
|
|
341
|
+
created: number
|
|
342
|
+
/** 返回的图片生成内容 */
|
|
343
|
+
data: Array<{
|
|
344
|
+
/** 生成的图片 url,有效期为 24 小时 */
|
|
345
|
+
url: string
|
|
346
|
+
}>
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// #endregion
|
package/src/utils.ts
CHANGED
|
@@ -103,8 +103,9 @@ export class TextDecoderStream {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
export function createAsyncIterable<T>(stream: ReadableStream<T>) {
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
106
107
|
const _stream = stream as AsyncIterableReadableStream<T>
|
|
107
|
-
_stream[Symbol.asyncIterator] = () => {
|
|
108
|
+
(_stream as any)[Symbol.asyncIterator] = () => {
|
|
108
109
|
const reader = stream.getReader()
|
|
109
110
|
return {
|
|
110
111
|
async next(): Promise<IteratorResult<T>> {
|
|
@@ -166,6 +167,7 @@ export function createPromise<T = unknown>() {
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
export function isToolCallAssistantMessage(message: ChatModelMessage): message is ToolCallAssistantMessage {
|
|
170
|
+
// eslint-disable-next-line eqeqeq
|
|
169
171
|
return message.role === 'assistant' && 'tool_calls' in message && message.tool_calls?.[0] != null
|
|
170
172
|
}
|
|
171
173
|
|