@cloudbase/ai 3.3.14-beta.1 → 3.6.4-alpha.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 +5 -3
- package/dist/cjs/AI.js +64 -24
- package/dist/cjs/errors.d.ts +10 -0
- package/dist/cjs/errors.js +38 -0
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +14 -6
- package/dist/cjs/models/image-model.d.ts +2 -1
- package/dist/cjs/models/image-model.js +33 -12
- package/dist/cjs/type.d.ts +3 -1
- package/dist/cjs/type.js +1 -1
- package/dist/cjs/utils.d.ts +1 -1
- package/dist/cjs/utils.js +9 -4
- package/dist/esm/AI.d.ts +5 -3
- package/dist/esm/AI.js +64 -24
- package/dist/esm/errors.d.ts +10 -0
- package/dist/esm/errors.js +35 -0
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +14 -6
- package/dist/esm/models/image-model.d.ts +2 -1
- package/dist/esm/models/image-model.js +33 -12
- package/dist/esm/type.d.ts +3 -1
- package/dist/esm/type.js +1 -1
- package/dist/esm/utils.d.ts +1 -1
- package/dist/esm/utils.js +9 -4
- package/dist/miniprogram/index.js +1 -1
- package/dist/miniprogram/index.js.LICENSE.txt +2 -2
- package/package.json +8 -4
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -15
- package/src/AI.ts +0 -154
- package/src/bot/index.ts +0 -278
- package/src/bot/types.ts +0 -165
- package/src/eventsource_parser/index.ts +0 -2
- package/src/eventsource_parser/parse.ts +0 -169
- package/src/eventsource_parser/stream.ts +0 -35
- package/src/eventsource_parser/types.ts +0 -90
- package/src/index.ts +0 -156
- package/src/models/Ark/index.ts +0 -94
- package/src/models/Ark/type.ts +0 -82
- package/src/models/DashScope/index.ts +0 -97
- package/src/models/DashScope/type.ts +0 -76
- package/src/models/DeepSeek/index.ts +0 -80
- package/src/models/Default/index.ts +0 -70
- package/src/models/HunYuan/index.ts +0 -94
- package/src/models/HunYuan/type.ts +0 -85
- package/src/models/HunYuan/util.ts +0 -60
- package/src/models/HunYuanBeta/index.ts +0 -83
- package/src/models/HunYuanExp/index.ts +0 -80
- package/src/models/HunYuanOpen/index.ts +0 -83
- package/src/models/Moonshot/index.ts +0 -95
- package/src/models/Moonshot/type.ts +0 -82
- package/src/models/Yi/index.ts +0 -100
- package/src/models/Yi/type.ts +0 -77
- package/src/models/ZhiPu/index.ts +0 -104
- package/src/models/ZhiPu/type.ts +0 -71
- package/src/models/image-model.ts +0 -59
- package/src/models/index.ts +0 -41
- package/src/models/model.ts +0 -425
- package/src/type.ts +0 -349
- package/src/utils.ts +0 -183
- package/tsconfig.esm.json +0 -16
- package/tsconfig.json +0 -16
- package/webpack/web.prod.js +0 -100
- package/webpack/webpack.miniprogram.js +0 -24
|
@@ -15,8 +15,8 @@ PERFORMANCE OF THIS SOFTWARE.
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* @license
|
|
18
|
-
* web-streams-polyfill v4.
|
|
19
|
-
* Copyright
|
|
18
|
+
* web-streams-polyfill v4.3.0
|
|
19
|
+
* Copyright 2026 Mattias Buelens, Diwank Singh Tomer and other contributors.
|
|
20
20
|
* This code is released under the MIT license.
|
|
21
21
|
* SPDX-License-Identifier: MIT
|
|
22
22
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/ai",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.4-alpha.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.
|
|
34
|
-
"@cloudbase/utilities": "3.
|
|
33
|
+
"@cloudbase/types": "^3.6.4-alpha.1",
|
|
34
|
+
"@cloudbase/utilities": "^3.6.4-alpha.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,9 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@cloudbase/adapter-interface": "^0.7.1"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "6c8850e20cdad550454eaac0877aa4aeae66ce84",
|
|
43
|
+
"types": "dist/cjs/index.d.ts",
|
|
44
|
+
"files": [
|
|
45
|
+
"dist/"
|
|
46
|
+
]
|
|
43
47
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
package/src/AI.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/* eslint-disable eqeqeq */
|
|
2
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
3
|
-
import type { SDKRequestInterface } from '@cloudbase/adapter-interface'
|
|
4
|
-
import { Bot } from './bot'
|
|
5
|
-
import * as models from './models'
|
|
6
|
-
import * as types from './type'
|
|
7
|
-
import { readableStream2JsonObject } from './utils'
|
|
8
|
-
import { ICloudbaseConfig } from '@cloudbase/types'
|
|
9
|
-
import { langEvent } from '@cloudbase/utilities'
|
|
10
|
-
|
|
11
|
-
const { MODELS } = models
|
|
12
|
-
|
|
13
|
-
class AI {
|
|
14
|
-
public aiBaseUrl: string
|
|
15
|
-
public aiBotBaseUrl: string
|
|
16
|
-
public bot: Bot
|
|
17
|
-
public i18n: ICloudbaseConfig['i18n']
|
|
18
|
-
defaultHeaders: { [x: string]: any }
|
|
19
|
-
|
|
20
|
-
constructor(
|
|
21
|
-
private req: SDKRequestInterface,
|
|
22
|
-
public baseUrl: string,
|
|
23
|
-
i18n: ICloudbaseConfig['i18n'],
|
|
24
|
-
) {
|
|
25
|
-
this.aiBaseUrl = `${baseUrl}/ai`
|
|
26
|
-
this.aiBotBaseUrl = `${baseUrl}/aibot`
|
|
27
|
-
this.bot = new Bot(this.botRequest, this.aiBotBaseUrl)
|
|
28
|
-
this.i18n = i18n
|
|
29
|
-
|
|
30
|
-
langEvent.bus.on(langEvent.LANG_CHANGE_EVENT, (params) => {
|
|
31
|
-
this.i18n = params.data?.i18n || this.i18n
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async handleResponseData(responseData: Promise<unknown> | ReadableStream<Uint8Array>, header?: Headers) {
|
|
36
|
-
const GO_TO_AI_TEXT = `${this.i18n.t('请检查调用方式,或前往云开发 AI+ 首页查看文档')}:https://tcb.cloud.tencent.com/dev#/ai`
|
|
37
|
-
|
|
38
|
-
if (typeof responseData === 'object' && responseData && 'then' in responseData) {
|
|
39
|
-
// 非流式请求,直接返回 json 数据
|
|
40
|
-
const json = (await responseData) as Record<string, unknown>
|
|
41
|
-
if (typeof json === 'object' && json && 'code' in json && json.code !== 'NORMAL') {
|
|
42
|
-
throw new Error(`${this.i18n.t('AI+ 请求出错,错误码')}:${json.code},${this.i18n.t('错误信息')}:${
|
|
43
|
-
json.message
|
|
44
|
-
}\n${GO_TO_AI_TEXT}\n${JSON.stringify(json, null, 2)}`,)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return responseData
|
|
48
|
-
}
|
|
49
|
-
// 流式请求,如果接口出错,会变成传 json 格式的响应,通过 header 判断格式
|
|
50
|
-
if (header?.get?.('content-type')?.includes('application/json')) {
|
|
51
|
-
const json = await readableStream2JsonObject(responseData as ReadableStream<Uint8Array>)
|
|
52
|
-
if (typeof json === 'object' && json && 'code' in json && json.code !== 'NORMAL') {
|
|
53
|
-
throw new Error(`${this.i18n.t('AI+ 请求出错,错误码')}:${json.code},${this.i18n.t('错误信息')}:${
|
|
54
|
-
json.message
|
|
55
|
-
}\n${GO_TO_AI_TEXT}\n${JSON.stringify(json, null, 2)}`,)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return responseData
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
createModel(
|
|
62
|
-
model: string,
|
|
63
|
-
options?: {
|
|
64
|
-
defaultModelSubUrl?: string
|
|
65
|
-
},
|
|
66
|
-
) {
|
|
67
|
-
let simpleModel: types.SimpleChatModel
|
|
68
|
-
|
|
69
|
-
const SimpleModelConstructor = MODELS[model]
|
|
70
|
-
if (SimpleModelConstructor) {
|
|
71
|
-
simpleModel = new SimpleModelConstructor(this.modelRequest, this.aiBaseUrl)
|
|
72
|
-
} else {
|
|
73
|
-
const subUrl = typeof options?.defaultModelSubUrl === 'string' ? options.defaultModelSubUrl : '/chat/completions'
|
|
74
|
-
|
|
75
|
-
simpleModel = new models.DefaultSimpleModel(this.modelRequest, this.aiBaseUrl, `${model}${subUrl}`)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const reactModel = new models.ReactModel(simpleModel)
|
|
79
|
-
return reactModel
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
registerModel(name: string, model: types.ChatModelConstructor) {
|
|
83
|
-
if (MODELS[name] != null) {
|
|
84
|
-
console.warn(`AI model ${name} already exists!`)
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
MODELS[name] = model
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
createImageModel<T extends 'hunyuan-image' | (string & {})>(provider: T) {
|
|
91
|
-
return new models.DefaultImageModel<T>(this.modelRequest, this.aiBaseUrl, provider)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
modelRequest: types.ModelReq = async ({ url, data, headers, stream, timeout }) => {
|
|
95
|
-
const fetchHeaders = {
|
|
96
|
-
'Content-Type': 'application/json',
|
|
97
|
-
}
|
|
98
|
-
// 带 Accept: text/event-stream 请求头显式指定是 SSE 绕过 60s 的限制
|
|
99
|
-
stream && Object.assign(fetchHeaders, { Accept: 'text/event-stream' })
|
|
100
|
-
|
|
101
|
-
const { data: responseData, header } = (await this.req.fetch({
|
|
102
|
-
method: 'post',
|
|
103
|
-
headers: { [this.i18n?.LANG_HEADER_KEY]: this.i18n?.lang, ...fetchHeaders, ...headers },
|
|
104
|
-
body: JSON.stringify(data),
|
|
105
|
-
url,
|
|
106
|
-
stream,
|
|
107
|
-
timeout,
|
|
108
|
-
})) as { data: Promise<unknown> | ReadableStream<Uint8Array>; header?: Headers }
|
|
109
|
-
|
|
110
|
-
return this.handleResponseData(responseData, header) as any
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
botRequest: types.BotReq = async ({ method, url, data = {}, headers, stream, timeout }) => {
|
|
114
|
-
if (method === 'get') {
|
|
115
|
-
return this.handleResponseData((await this.req.fetch({ url: `${url}?${objectToParam(data)}`, method, headers, stream, timeout })).data,)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const fetchHeaders = {
|
|
119
|
-
'Content-Type': 'application/json',
|
|
120
|
-
}
|
|
121
|
-
// 带 Accept: text/event-stream 请求头显式指定是 SSE 绕过 60s 的限制
|
|
122
|
-
stream && Object.assign(fetchHeaders, { Accept: 'text/event-stream' })
|
|
123
|
-
|
|
124
|
-
const { data: responseData, header } = (await this.req.fetch({
|
|
125
|
-
url,
|
|
126
|
-
body: JSON.stringify(data),
|
|
127
|
-
headers: {
|
|
128
|
-
[this.i18n?.LANG_HEADER_KEY]: this.i18n?.lang,
|
|
129
|
-
...fetchHeaders,
|
|
130
|
-
...headers,
|
|
131
|
-
},
|
|
132
|
-
stream,
|
|
133
|
-
method,
|
|
134
|
-
timeout,
|
|
135
|
-
})) as { data: Promise<unknown> | ReadableStream<Uint8Array>; header?: Headers }
|
|
136
|
-
|
|
137
|
-
return this.handleResponseData(responseData, header) as any
|
|
138
|
-
|
|
139
|
-
function objectToParam(obj: Object) {
|
|
140
|
-
return Object.entries(obj)
|
|
141
|
-
.map(([key, value]) => `${key}=${value}`)
|
|
142
|
-
.join('&')
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
registerFunctionTool(functionTool: types.FunctionTool) {
|
|
147
|
-
if (models.toolMap.has(functionTool.name)) {
|
|
148
|
-
console.warn(`AI function tool ${functionTool.name} already exists and will be overwritten!`)
|
|
149
|
-
}
|
|
150
|
-
models.toolMap.set(functionTool.name, functionTool.fn)
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export { AI }
|
package/src/bot/index.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { type ParsedEvent } from '../eventsource_parser'
|
|
2
|
-
import { BotReq, ReqOptions } from '../type'
|
|
3
|
-
import {
|
|
4
|
-
IBotCreateConversation,
|
|
5
|
-
IBotDeleteConversation,
|
|
6
|
-
IBotGetConversation,
|
|
7
|
-
IBotGetTextToSpeechResult,
|
|
8
|
-
IBotPreview,
|
|
9
|
-
IBotSendMessage,
|
|
10
|
-
IBotSpeechToText,
|
|
11
|
-
IBotTextToSpeech,
|
|
12
|
-
IBotUploadFiles,
|
|
13
|
-
ICreateBot,
|
|
14
|
-
IDeleteBot,
|
|
15
|
-
IGenerateBot,
|
|
16
|
-
IGenerateImage,
|
|
17
|
-
IGetBot,
|
|
18
|
-
IGetBotChatRecords,
|
|
19
|
-
IGetBotFeedback,
|
|
20
|
-
IGetBotList,
|
|
21
|
-
IGetBotRecommendQuestions,
|
|
22
|
-
ISendBotFeedback,
|
|
23
|
-
IUpdateBot,
|
|
24
|
-
} from './types'
|
|
25
|
-
import {
|
|
26
|
-
createAsyncIterable,
|
|
27
|
-
toPolyfillReadable,
|
|
28
|
-
createEventSourceParserTransformStream,
|
|
29
|
-
TextDecoderStream,
|
|
30
|
-
TransformStream,
|
|
31
|
-
} from '../utils'
|
|
32
|
-
|
|
33
|
-
export class Bot {
|
|
34
|
-
req: BotReq
|
|
35
|
-
|
|
36
|
-
constructor(req: BotReq, public baseUrl: string) {
|
|
37
|
-
// eslint-disable-next-line prefer-rest-params
|
|
38
|
-
const token = arguments[2]
|
|
39
|
-
if (typeof token === 'string') {
|
|
40
|
-
this.req = ({ headers = {}, ...rest }) => req({ ...rest, headers: { ...headers, Authorization: `Bearer ${token}` } })
|
|
41
|
-
} else {
|
|
42
|
-
this.req = req
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
list(props: IGetBotList, options?: ReqOptions) {
|
|
47
|
-
return this.req({
|
|
48
|
-
method: 'get',
|
|
49
|
-
url: this.join('bots'),
|
|
50
|
-
data: props,
|
|
51
|
-
timeout: options?.timeout,
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
create({ botInfo }: ICreateBot, options?: ReqOptions) {
|
|
56
|
-
return this.req({
|
|
57
|
-
method: 'post',
|
|
58
|
-
url: this.join('bots'),
|
|
59
|
-
data: botInfo,
|
|
60
|
-
timeout: options?.timeout,
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
get({ botId }: IGetBot, options?: ReqOptions) {
|
|
65
|
-
return this.req({
|
|
66
|
-
method: 'get',
|
|
67
|
-
url: this.join(`bots/${botId}`),
|
|
68
|
-
timeout: options?.timeout,
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
update({ botId, botInfo }: IUpdateBot, options?: ReqOptions) {
|
|
73
|
-
return this.req({
|
|
74
|
-
method: 'PATCH',
|
|
75
|
-
url: this.join(`bots/${botId}`),
|
|
76
|
-
data: botInfo,
|
|
77
|
-
timeout: options?.timeout,
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
delete({ botId }: IDeleteBot, options?: ReqOptions) {
|
|
82
|
-
return this.req({ method: 'delete', url: this.join(`bots/${botId}`), timeout: options?.timeout })
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getChatRecords(props: IGetBotChatRecords, options?: ReqOptions) {
|
|
86
|
-
return this.req({
|
|
87
|
-
method: 'get',
|
|
88
|
-
url: this.join(`bots/${props.botId}/records`),
|
|
89
|
-
data: props,
|
|
90
|
-
timeout: options?.timeout,
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
sendFeedback({ userFeedback }: ISendBotFeedback, options?: ReqOptions) {
|
|
95
|
-
return this.req({
|
|
96
|
-
method: 'post',
|
|
97
|
-
url: this.join(`bots/${userFeedback.botId}/feedback`),
|
|
98
|
-
data: userFeedback,
|
|
99
|
-
timeout: options?.timeout,
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
getFeedback(props: IGetBotFeedback, options?: ReqOptions) {
|
|
104
|
-
return this.req({
|
|
105
|
-
method: 'get',
|
|
106
|
-
url: this.join(`bots/${props.botId}/feedback`),
|
|
107
|
-
data: props,
|
|
108
|
-
timeout: options?.timeout,
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async uploadFiles(props: IBotUploadFiles, options?: ReqOptions) {
|
|
113
|
-
return this.req({
|
|
114
|
-
method: 'post',
|
|
115
|
-
url: this.join(`bots/${props.botId}/files`),
|
|
116
|
-
data: props,
|
|
117
|
-
timeout: options?.timeout,
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async createConversation({ botId, ...rest }: IBotCreateConversation, options?: ReqOptions) {
|
|
122
|
-
return this.req({
|
|
123
|
-
method: 'post',
|
|
124
|
-
url: this.join(`bots/${botId}/conversation`),
|
|
125
|
-
data: rest,
|
|
126
|
-
timeout: options?.timeout,
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async getConversation({ pageSize = 10, pageNumber = 1, botId, ...rest }: IBotGetConversation, options?: ReqOptions) {
|
|
131
|
-
if (pageNumber < 1) throw new Error('pageNumber must be greater than 0')
|
|
132
|
-
const offset = pageSize * (pageNumber - 1)
|
|
133
|
-
const limit = pageSize
|
|
134
|
-
return this.req({
|
|
135
|
-
method: 'get',
|
|
136
|
-
url: this.join(`bots/${botId}/conversation`),
|
|
137
|
-
data: { ...rest, offset, limit },
|
|
138
|
-
timeout: options?.timeout,
|
|
139
|
-
})
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async deleteConversation({ botId, conversationId, ...rest }: IBotDeleteConversation, options?: ReqOptions) {
|
|
143
|
-
return this.req({
|
|
144
|
-
method: 'delete',
|
|
145
|
-
url: this.join(`bots/${botId}/conversation/${conversationId}`),
|
|
146
|
-
data: rest,
|
|
147
|
-
timeout: options?.timeout,
|
|
148
|
-
})
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async speechToText(props: IBotSpeechToText, options?: ReqOptions) {
|
|
152
|
-
return this.req({
|
|
153
|
-
method: 'post',
|
|
154
|
-
url: this.join(`bots/${props.botId}/speech-to-text`),
|
|
155
|
-
data: props,
|
|
156
|
-
timeout: options?.timeout,
|
|
157
|
-
})
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async textToSpeech(props: IBotTextToSpeech, options?: ReqOptions) {
|
|
161
|
-
return this.req({
|
|
162
|
-
method: 'post',
|
|
163
|
-
url: this.join(`bots/${props.botId}/text-to-speech`),
|
|
164
|
-
data: props,
|
|
165
|
-
timeout: options?.timeout,
|
|
166
|
-
})
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async getTextToSpeechResult(props: IBotGetTextToSpeechResult, options?: ReqOptions) {
|
|
170
|
-
return this.req({
|
|
171
|
-
method: 'get',
|
|
172
|
-
url: this.join(`bots/${props.botId}/text-to-speech`),
|
|
173
|
-
data: props,
|
|
174
|
-
timeout: options?.timeout,
|
|
175
|
-
})
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async getRecommendQuestions(props: IGetBotRecommendQuestions, options?: ReqOptions) {
|
|
179
|
-
const res = await this.req({
|
|
180
|
-
method: 'post',
|
|
181
|
-
url: this.join(`bots/${props.botId}/recommend-questions`),
|
|
182
|
-
data: props,
|
|
183
|
-
stream: true,
|
|
184
|
-
timeout: options?.timeout,
|
|
185
|
-
})
|
|
186
|
-
return new StreamResult(res)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async generateBot(props: IGenerateBot, options?: ReqOptions) {
|
|
190
|
-
const res = await this.req({
|
|
191
|
-
method: 'post',
|
|
192
|
-
url: this.join('generate-bot'),
|
|
193
|
-
data: props,
|
|
194
|
-
stream: true,
|
|
195
|
-
timeout: options?.timeout,
|
|
196
|
-
})
|
|
197
|
-
return new StreamResult(res)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async getPreview(props: IBotPreview, options?: ReqOptions) {
|
|
201
|
-
const res = await this.req({
|
|
202
|
-
method: 'post',
|
|
203
|
-
url: this.join('preview'),
|
|
204
|
-
data: props,
|
|
205
|
-
stream: true,
|
|
206
|
-
timeout: options?.timeout,
|
|
207
|
-
})
|
|
208
|
-
return new StreamResult(res)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
generateImage(props: IGenerateImage, options?: ReqOptions) {
|
|
212
|
-
return this.req({ method: 'post', url: this.join('generate-image'), data: props, timeout: options?.timeout })
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
async sendMessage(props: IBotSendMessage, options?: ReqOptions) {
|
|
216
|
-
const res = await this.req({
|
|
217
|
-
method: 'post',
|
|
218
|
-
url: this.join(`bots/${props.botId}/send-message`),
|
|
219
|
-
data: props,
|
|
220
|
-
stream: true,
|
|
221
|
-
timeout: options?.timeout,
|
|
222
|
-
})
|
|
223
|
-
return new StreamResult(res)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private join(url: string) {
|
|
227
|
-
return `${this.baseUrl}/${url}`
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
type BotEventStreamData = { content: string }
|
|
232
|
-
|
|
233
|
-
class StreamResult {
|
|
234
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
235
|
-
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
236
|
-
|
|
237
|
-
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
238
|
-
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
239
|
-
this._eventSourceStream = stream
|
|
240
|
-
.pipeThrough(new TextDecoderStream())
|
|
241
|
-
.pipeThrough(createEventSourceParserTransformStream())
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
private get teeedStream() {
|
|
245
|
-
const [s1, s2] = this._eventSourceStream.tee()
|
|
246
|
-
this._eventSourceStream = s2
|
|
247
|
-
return s1
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
get eventSourceStream() {
|
|
251
|
-
return createAsyncIterable(this.teeedStream)
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
get dataStream() {
|
|
255
|
-
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, BotEventStreamData>({
|
|
256
|
-
transform(chunk, controller) {
|
|
257
|
-
try {
|
|
258
|
-
const data = JSON.parse(chunk.data)
|
|
259
|
-
controller.enqueue(data)
|
|
260
|
-
} catch (e) {
|
|
261
|
-
if (chunk.data !== '[DONE]') {
|
|
262
|
-
console.warn('Error when transforming event source data to json', e, chunk)
|
|
263
|
-
} else {
|
|
264
|
-
controller.terminate()
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
}),),)
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
get textStream() {
|
|
272
|
-
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<BotEventStreamData, string>({
|
|
273
|
-
transform(chunk, controller) {
|
|
274
|
-
controller.enqueue(chunk?.content ?? '')
|
|
275
|
-
},
|
|
276
|
-
}),),)
|
|
277
|
-
}
|
|
278
|
-
}
|
package/src/bot/types.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
export interface IGetBotList {
|
|
2
|
-
name: string
|
|
3
|
-
introduction: string
|
|
4
|
-
information: string
|
|
5
|
-
enable: boolean
|
|
6
|
-
pageSize: number
|
|
7
|
-
pageNumber: number
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface ICreateBot {
|
|
11
|
-
botInfo: BotInfo
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface IGetBot {
|
|
15
|
-
botId: string
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface IUpdateBot {
|
|
19
|
-
botId: string
|
|
20
|
-
botInfo: BotInfo
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface IDeleteBot {
|
|
24
|
-
botId: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface IGetBotChatRecords {
|
|
28
|
-
botId: string
|
|
29
|
-
sort: 'asc' | 'desc' | string
|
|
30
|
-
pageSize: number
|
|
31
|
-
pageNumber: number
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface ISendBotFeedback {
|
|
35
|
-
userFeedback: IUserFeedback
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface IGetBotFeedback {
|
|
39
|
-
botId: string
|
|
40
|
-
type: 'upvote' | 'downvote' | string
|
|
41
|
-
sender: string
|
|
42
|
-
senderFilter: 'include' | 'exclude' | 'equal' | 'unequal' | 'prefix' | string
|
|
43
|
-
minRating: number
|
|
44
|
-
maxRating: number
|
|
45
|
-
from: number
|
|
46
|
-
to: number
|
|
47
|
-
pageSize: number
|
|
48
|
-
pageNumber: number
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface IGetBotRecommendQuestions {
|
|
52
|
-
botId: string
|
|
53
|
-
name: string
|
|
54
|
-
introduction: string
|
|
55
|
-
agentSetting: string
|
|
56
|
-
msg: string
|
|
57
|
-
history: Array<{
|
|
58
|
-
role: string
|
|
59
|
-
content: string
|
|
60
|
-
}>
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface IGenerateBot {
|
|
64
|
-
simple: boolean
|
|
65
|
-
prompt: string
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface IBotPreview {
|
|
69
|
-
name: string
|
|
70
|
-
introduction: string
|
|
71
|
-
agentSetting: string
|
|
72
|
-
msg: string
|
|
73
|
-
history: Array<{
|
|
74
|
-
role: string
|
|
75
|
-
content: string
|
|
76
|
-
}>
|
|
77
|
-
knowledgeBase: Array<string>
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface IGenerateImage {
|
|
81
|
-
prompt: string
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export interface IBotSendMessage {
|
|
85
|
-
botId: string
|
|
86
|
-
msg: string
|
|
87
|
-
history: Array<{
|
|
88
|
-
role: string
|
|
89
|
-
content: string
|
|
90
|
-
}>
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export interface IBotUploadFiles {
|
|
94
|
-
botId: string
|
|
95
|
-
fileList: Array<{
|
|
96
|
-
fileName: string
|
|
97
|
-
fileId: string
|
|
98
|
-
type: string
|
|
99
|
-
}>
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export interface IBotCreateConversation {
|
|
103
|
-
botId: string
|
|
104
|
-
title?: string
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export interface IBotGetConversation {
|
|
108
|
-
botId: string
|
|
109
|
-
pageSize?: number
|
|
110
|
-
pageNumber?: number
|
|
111
|
-
isDefault?: boolean
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface IBotDeleteConversation {
|
|
115
|
-
botId: string
|
|
116
|
-
conversationId: string
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface IBotSpeechToText {
|
|
120
|
-
botId: string
|
|
121
|
-
engSerViceType: string
|
|
122
|
-
voiceFormat: string
|
|
123
|
-
url: string
|
|
124
|
-
isPreview?: boolean
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export interface IBotTextToSpeech {
|
|
128
|
-
botId: string
|
|
129
|
-
voiceType: number
|
|
130
|
-
text: string
|
|
131
|
-
isPreview?: boolean
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export interface IBotGetTextToSpeechResult {
|
|
135
|
-
botId: string
|
|
136
|
-
taskId: string
|
|
137
|
-
isPreview?: boolean
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export interface IUserFeedback {
|
|
141
|
-
recordId: string
|
|
142
|
-
type: string
|
|
143
|
-
botId: string
|
|
144
|
-
comment: string
|
|
145
|
-
rating: number
|
|
146
|
-
tags: Array<string>
|
|
147
|
-
input: string
|
|
148
|
-
aiAnswer: string
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export interface BotInfo {
|
|
152
|
-
botId: string
|
|
153
|
-
name: string
|
|
154
|
-
introduction: string
|
|
155
|
-
agentSetting: string
|
|
156
|
-
welcomeMessage: string
|
|
157
|
-
avatar: string
|
|
158
|
-
background: string
|
|
159
|
-
tags: Array<string>
|
|
160
|
-
isNeedRecommend: boolean
|
|
161
|
-
knowledgeBase: Array<string>
|
|
162
|
-
type: string
|
|
163
|
-
initQuestions: Array<string>
|
|
164
|
-
enable: true
|
|
165
|
-
}
|