ctod 0.7.1 → 0.7.2
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/examples/basic.ts +97 -0
- package/examples/llama.cpp.ts +56 -0
- package/examples/plugin.ts +118 -0
- package/lib/broker/chat.ts +435 -0
- package/lib/core/parser.ts +62 -0
- package/lib/core/plugin.ts +46 -0
- package/lib/core/translator.ts +115 -0
- package/lib/ctod.ts +71 -0
- package/lib/index.ts +41 -0
- package/lib/plugins/index.ts +38 -0
- package/lib/plugins/limiter.ts +103 -0
- package/lib/plugins/print-log.ts +35 -0
- package/lib/plugins/retry.ts +25 -0
- package/lib/plugins/role.ts +28 -0
- package/lib/service/llama3.cpp/completion.ts +313 -0
- package/lib/service/llama3.cpp/index.ts +53 -0
- package/lib/service/openai/chat.ts +244 -0
- package/lib/service/openai/images-generation.ts +64 -0
- package/lib/service/openai/index.ts +97 -0
- package/lib/service/openai/vision.ts +111 -0
- package/lib/shims.d.ts +4 -0
- package/lib/templates.ts +71 -0
- package/lib/types.ts +4 -0
- package/lib/utils/error.ts +14 -0
- package/lib/utils/validate.ts +64 -0
- package/package.json +1 -1
- package/types/examples/basic.d.ts +1 -0
- package/types/examples/chat-demo.d.ts +2 -0
- package/types/examples/chat-for-llama.cpp-demo.d.ts +2 -0
- package/types/examples/chat-with-json-schema-demo.d.ts +2 -0
- package/types/examples/llama.cpp.d.ts +2 -0
- package/types/examples/plugin-demo.d.ts +2 -0
- package/types/examples/plugin.d.ts +2 -0
- package/types/examples/stream-for-llama.cpp-demo.d.ts +2 -0
- package/types/examples/vision-demo.d.ts +2 -0
- package/types/lib/broker/chat.d.ts +150 -0
- package/types/lib/core/parser.d.ts +32 -0
- package/types/lib/core/plugin.d.ts +34 -0
- package/types/lib/core/translator.d.ts +67 -0
- package/types/lib/ctod.d.ts +32 -0
- package/types/lib/index.d.ts +34 -0
- package/types/lib/plugins/index.d.ts +47 -0
- package/types/lib/plugins/limiter.d.ts +36 -0
- package/types/lib/plugins/print-log.d.ts +5 -0
- package/types/lib/plugins/retry.d.ts +6 -0
- package/types/lib/plugins/role.d.ts +5 -0
- package/types/lib/service/llama3.cpp/completion.d.ts +61 -0
- package/types/lib/service/llama3.cpp/index.d.ts +19 -0
- package/types/lib/service/openai/chat.d.ts +110 -0
- package/types/lib/service/openai/completion.d.ts +59 -0
- package/types/lib/service/openai/images-generation.d.ts +35 -0
- package/types/lib/service/openai/index.d.ts +29 -0
- package/types/lib/service/openai/vision.d.ts +74 -0
- package/types/lib/templates.d.ts +20 -0
- package/types/lib/types.d.ts +1 -0
- package/types/lib/utils/error.d.ts +11 -0
- package/types/lib/utils/validate.d.ts +16 -0
- package/.api-key +0 -1
- package/.nyc_output/42919e68-b472-4a5d-b2d3-5d5153f28467.json +0 -1
- package/.nyc_output/processinfo/42919e68-b472-4a5d-b2d3-5d5153f28467.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/cover-0.png +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/cover-1.png +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/story-config.json +0 -4
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/story.json +0 -22
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/cover-0.png +0 -1
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/cover-1.png +0 -1
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/story-config.json +0 -4
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/story.json +0 -24
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/cover-0.png +0 -1
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/cover-1.png +0 -1
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/story-config.json +0 -4
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/story.json +0 -24
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/cover-0.png +0 -1
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/cover-1.png +0 -1
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/story-config.json +0 -4
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/story.json +0 -28
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/cover-0.png +0 -1
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/cover-1.png +0 -1
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/story-config.json +0 -4
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/story.json +0 -18
- package/.output/.output/stores//350/250/255/350/250/210/350/210/207/351/226/213/347/231/274/347/232/204/346/214/221/346/210/260/content.json +0 -22
- package/.output/.output/talks//344/273/245/347/254/221/350/251/261/347/202/272/345/237/272/347/244/216/347/232/204/351/235/242/350/251/246/content.json +0 -30
- package/.output/.output/talks//344/273/245/347/254/221/350/251/261/347/202/272/345/237/272/347/244/216/347/232/204/351/235/242/350/251/246/cover-1684055229695.png +0 -0
- package/.output/.output/talks//345/234/250/346/224/277/346/262/273/345/200/231/351/201/270/344/272/272/350/276/257/350/253/226/344/270/255/347/232/204/350/252/252/346/234/215/346/200/247/346/272/235/351/200/232/content.json +0 -30
- package/.output/.output/talks//345/234/250/346/224/277/346/262/273/345/200/231/351/201/270/344/272/272/350/276/257/350/253/226/344/270/255/347/232/204/350/252/252/346/234/215/346/200/247/346/272/235/351/200/232/cover-1684056611678.png +0 -0
- package/.output/.output/talks//346/224/276/351/254/206/347/232/204/345/200/231/351/201/270/344/272/272/351/200/262/350/241/214/346/224/277/346/262/273/350/276/257/350/253/226/content.json +0 -36
- package/.output/.output/talks//346/224/276/351/254/206/347/232/204/345/200/231/351/201/270/344/272/272/351/200/262/350/241/214/346/224/277/346/262/273/350/276/257/350/253/226/cover-1684055140609.png +0 -0
- package/.output/.output/talks//346/224/277/346/262/273/347/254/221/350/251/261/content.json +0 -30
- package/.output/.output/talks//346/224/277/346/262/273/347/254/221/350/251/261/cover-1684056246465.png +0 -0
- package/.output/.output/talks//350/251/274/350/253/247/345/256/266/351/225/267/346/225/231/345/270/253/346/234/203/350/255/260/content.json +0 -26
- package/.output/.output/talks//350/251/274/350/253/247/345/256/266/351/225/267/346/225/231/345/270/253/346/234/203/350/255/260/cover-1685785935121.png +0 -0
- package/.output/.output/talks//350/262/241/345/213/231/351/241/247/345/225/217/350/253/256/350/251/242/content.json +0 -26
- package/.output/.output/talks//350/262/241/345/213/231/351/241/247/345/225/217/350/253/256/350/251/242/cover-1685785115833.png +0 -0
- package/.output/.output/talks//351/206/253/347/224/237/345/222/214/347/227/205/344/272/272/350/250/216/350/253/226/346/202/262/345/202/267/content.json +0 -32
- package/.output/.output/talks//351/206/253/347/224/237/345/222/214/347/227/205/344/272/272/350/250/216/350/253/226/346/202/262/345/202/267/cover-1684055075942.png +0 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import { OpenAIVision } from './vision'
|
|
3
|
+
import { OpenAIChat, Config } from './chat'
|
|
4
|
+
import { OpenAIImagesGeneration } from './images-generation'
|
|
5
|
+
import { validateToJsonSchema } from '../../utils/validate'
|
|
6
|
+
|
|
7
|
+
export class OpenAI {
|
|
8
|
+
_axios = axios.create()
|
|
9
|
+
_apiKey = ''
|
|
10
|
+
|
|
11
|
+
static createChatRequest(
|
|
12
|
+
apiKey: string | (() => Promise<string>),
|
|
13
|
+
config: Partial<Config> | (() => Promise<Partial<Config>>) = {},
|
|
14
|
+
options?: {
|
|
15
|
+
axios?: any
|
|
16
|
+
}
|
|
17
|
+
) {
|
|
18
|
+
return async(messages: any[], { onCancel }: any) => {
|
|
19
|
+
const openai = new OpenAI(typeof apiKey === 'string' ? apiKey : await apiKey())
|
|
20
|
+
const chat = openai.createChat()
|
|
21
|
+
const abortController = new AbortController()
|
|
22
|
+
if (options && options.axios) {
|
|
23
|
+
openai.setAxios(options.axios)
|
|
24
|
+
}
|
|
25
|
+
chat.setConfig(typeof config === 'function' ? await config() : config)
|
|
26
|
+
onCancel(() => abortController.abort())
|
|
27
|
+
const { text } = await chat.talk(messages, {
|
|
28
|
+
abortController
|
|
29
|
+
})
|
|
30
|
+
return text
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static createChatRequestWithJsonSchema(params:{
|
|
35
|
+
axios?: any,
|
|
36
|
+
apiKey: string | (() => Promise<string>),
|
|
37
|
+
config?: Partial<Pick<Config, 'model' | 'temperature'>> | (() => Promise<Partial<Pick<Config, 'model' | 'temperature'>>>)
|
|
38
|
+
}) {
|
|
39
|
+
return async(messages: any[], { schema, onCancel }: any) => {
|
|
40
|
+
const openai = new OpenAI(typeof params.apiKey === 'string' ? params.apiKey : await params.apiKey())
|
|
41
|
+
const chat = openai.createChat()
|
|
42
|
+
const abortController = new AbortController()
|
|
43
|
+
if (params.config) {
|
|
44
|
+
chat.setConfig(typeof params.config === 'function' ? await params.config() : params.config)
|
|
45
|
+
}
|
|
46
|
+
if (params.axios) {
|
|
47
|
+
openai.setAxios(params.axios)
|
|
48
|
+
}
|
|
49
|
+
onCancel(() => abortController.abort())
|
|
50
|
+
const jsonSchema = validateToJsonSchema(schema.output)
|
|
51
|
+
const { text } = await chat.talk(messages, {
|
|
52
|
+
abortController,
|
|
53
|
+
jsonSchema: {
|
|
54
|
+
name: 'data',
|
|
55
|
+
strict: true,
|
|
56
|
+
schema: jsonSchema
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
return text
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
constructor(apiKey = '') {
|
|
65
|
+
this._apiKey = apiKey
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @zh 如果你有需要特別設定 axios,請使用這方法。
|
|
70
|
+
* @en If you need to set axios, please use this method.
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
setAxios(axios: any) {
|
|
74
|
+
this._axios = axios
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @zh 設定 api key。
|
|
79
|
+
* @en Set api key.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
setConfiguration(apiKey: string) {
|
|
83
|
+
this._apiKey = apiKey
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
createChat() {
|
|
87
|
+
return new OpenAIChat(this)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
createVision() {
|
|
91
|
+
return new OpenAIVision(this)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
createImagesGeneration() {
|
|
95
|
+
return new OpenAIImagesGeneration(this)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { OpenAI } from './index'
|
|
2
|
+
import { PromiseResponseType } from '../../types'
|
|
3
|
+
|
|
4
|
+
type ImageContent = {
|
|
5
|
+
type: 'image_url' | 'text'
|
|
6
|
+
text?: string
|
|
7
|
+
image_url?: {
|
|
8
|
+
url: string
|
|
9
|
+
detail?: string
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type VisionMessage = {
|
|
14
|
+
role: 'system' | 'user' | 'assistant'
|
|
15
|
+
name?: string
|
|
16
|
+
content: string | ImageContent[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type ApiResponse = {
|
|
20
|
+
id: string
|
|
21
|
+
object: string
|
|
22
|
+
created: number
|
|
23
|
+
model: string
|
|
24
|
+
usage: {
|
|
25
|
+
prompt_tokens: number
|
|
26
|
+
completion_tokens: number
|
|
27
|
+
total_tokens: number
|
|
28
|
+
}
|
|
29
|
+
choices: Array<{
|
|
30
|
+
message: {
|
|
31
|
+
role: string
|
|
32
|
+
content: string
|
|
33
|
+
}
|
|
34
|
+
finish_details: {
|
|
35
|
+
type: string
|
|
36
|
+
}
|
|
37
|
+
index: number
|
|
38
|
+
}>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type Config = {
|
|
42
|
+
/**
|
|
43
|
+
* @zh 選擇運行的模型。
|
|
44
|
+
* @en How many chat completion choices to generate for each input message.
|
|
45
|
+
*/
|
|
46
|
+
model: 'gpt-4-vision-preview'
|
|
47
|
+
/**
|
|
48
|
+
* @zh 冒險指數,數值由 0 ~ 2 之間。
|
|
49
|
+
* @en What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
|
|
50
|
+
*/
|
|
51
|
+
temperature: number
|
|
52
|
+
/**
|
|
53
|
+
* @zh 每次對話最多產生幾個 tokens。
|
|
54
|
+
* @en How many tokens to complete to.
|
|
55
|
+
*/
|
|
56
|
+
maxTokens?: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class OpenAIVision {
|
|
60
|
+
openai: OpenAI
|
|
61
|
+
config: Config = {
|
|
62
|
+
model: 'gpt-4-vision-preview',
|
|
63
|
+
maxTokens: undefined,
|
|
64
|
+
temperature: 1
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
constructor(openai: OpenAI) {
|
|
68
|
+
this.openai = openai
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @zh 改變對話的一些設定
|
|
73
|
+
* @en Change some settings of the conversation
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
setConfig(options: Partial<Config>) {
|
|
77
|
+
Object.assign(this.config, options)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @zh 辨識圖片
|
|
82
|
+
* @en Recognize images
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
async view(messages: VisionMessage[]) {
|
|
86
|
+
const result = await this.openai._axios.post<ApiResponse>('https://api.openai.com/v1/chat/completions', {
|
|
87
|
+
model: this.config.model,
|
|
88
|
+
n: 1,
|
|
89
|
+
messages,
|
|
90
|
+
max_tokens: this.config.maxTokens,
|
|
91
|
+
temperature: this.config.temperature
|
|
92
|
+
}, {
|
|
93
|
+
headers: {
|
|
94
|
+
'Content-Type': 'application/json',
|
|
95
|
+
'Authorization': `Bearer ${this.openai._apiKey}`
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
const choices = result.data.choices || []
|
|
99
|
+
const message = choices[0]?.message || {
|
|
100
|
+
role: 'assistant',
|
|
101
|
+
content: ''
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
id: result?.data.id as string,
|
|
105
|
+
text: message.content as string,
|
|
106
|
+
apiReseponse: result.data
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export type OpenAIChatVisionResponse = PromiseResponseType<OpenAIVision['view']>
|
package/lib/shims.d.ts
ADDED
package/lib/templates.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import Handlebars from 'handlebars'
|
|
2
|
+
import { record } from 'power-helper'
|
|
3
|
+
|
|
4
|
+
type JsonResponseFormat = {
|
|
5
|
+
desc: string
|
|
6
|
+
example: any
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @zh 協助使用者將格式需求轉成論敘語句。
|
|
11
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const requireJsonResponse = (question: string | string[], format: Record<string, JsonResponseFormat>) => {
|
|
15
|
+
return [
|
|
16
|
+
...(Array.isArray(question) ? question : [question]),
|
|
17
|
+
'Please respond using the following JSON format and minify the JSON without including any explanation: ',
|
|
18
|
+
'{',
|
|
19
|
+
Object.entries(format).map(([key, value]) => {
|
|
20
|
+
return [
|
|
21
|
+
`/* ${value.desc} */`,
|
|
22
|
+
`"${key}": ${JSON.stringify(value.example)}`,
|
|
23
|
+
].join('\n')
|
|
24
|
+
}).join(',\n'),
|
|
25
|
+
'}'
|
|
26
|
+
].join('\n')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @zh 協助使用者將格式需求轉成論敘語句,支援 Handlebars。
|
|
31
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences, support Handlebars.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export const requireJsonResponseWithHandlebars = (
|
|
35
|
+
value: Record<string, any>,
|
|
36
|
+
question: string | string[],
|
|
37
|
+
format: Record<string, JsonResponseFormat>
|
|
38
|
+
) => {
|
|
39
|
+
const handlebars = Handlebars.create()
|
|
40
|
+
|
|
41
|
+
handlebars.registerHelper('DATA', function(this: any, value) {
|
|
42
|
+
return JSON.stringify(value)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
handlebars.registerHelper('ENV', function(this: any, value) {
|
|
46
|
+
return this.__envs && value ? this.__envs[value] : ''
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
handlebars.registerHelper('INPUT', function(this: any) {
|
|
50
|
+
return JSON.stringify(record.omit(this, ['__envs']))
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
handlebars.registerHelper('JOIN', function(this: any, value) {
|
|
54
|
+
return Array.isArray(value) ? value.join() : JSON.stringify(value)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
return handlebars.compile(requireJsonResponse(question, format))(value)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @zh 協助使用者將格式需求轉成論敘語句,並支援 Json Schema。
|
|
62
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences, and support Json Schema.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
export const requireJsonResponseWithJsonSchema = (question: string | string[], format: Record<string, any>) => {
|
|
66
|
+
return [
|
|
67
|
+
...(Array.isArray(question) ? question : [question]),
|
|
68
|
+
'Please provide JSON data according to the following JSON Schema format:',
|
|
69
|
+
JSON.stringify(format)
|
|
70
|
+
].join('\n')
|
|
71
|
+
}
|
package/lib/types.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type ParserFail = {
|
|
2
|
+
name: string
|
|
3
|
+
error: any
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class ParserError {
|
|
7
|
+
isParserError = true
|
|
8
|
+
parserFails: ParserFail[] = []
|
|
9
|
+
error: any
|
|
10
|
+
constructor(error: any, parserFails: ParserFail[]) {
|
|
11
|
+
this.error = error
|
|
12
|
+
this.parserFails = parserFails
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as Yup from 'yup'
|
|
2
|
+
import { convertSchema } from '@sodaru/yup-to-json-schema'
|
|
3
|
+
import { Schema } from 'yup'
|
|
4
|
+
|
|
5
|
+
export type ValidateCallback<T extends Record<string, Schema>> = (_yup: typeof Yup) => {
|
|
6
|
+
[K in keyof T]: T[K]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type ValidateCallbackOutputs<
|
|
10
|
+
T extends ValidateCallback<any>,
|
|
11
|
+
R = ReturnType<T>
|
|
12
|
+
> = {
|
|
13
|
+
[K in keyof R]: R[K] extends { '__outputType': any } ? R[K]['__outputType'] : R[K]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function definedValidateSchema<T extends ValidateCallback<any>>(cb: T): T {
|
|
17
|
+
return cb
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function validate<
|
|
21
|
+
T extends ValidateCallback<any>,
|
|
22
|
+
R = ReturnType<T>
|
|
23
|
+
>(target: any, schemaCallback: T) {
|
|
24
|
+
return Yup.object(schemaCallback(Yup)).required().validateSync(target || {}) as {
|
|
25
|
+
[K in keyof R]: R[K] extends { '__outputType': any } ? R[K]['__outputType'] : R[K]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const defineYupSchema = <T extends Record<string, Schema>>(cb: ValidateCallback<T>): T => {
|
|
30
|
+
return cb(Yup)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const validateToJsonSchema = <T extends ValidateCallback<any>>(cb: T) => {
|
|
34
|
+
const removeAllDefault = (schema: any) => {
|
|
35
|
+
if (schema.default) {
|
|
36
|
+
delete schema.default
|
|
37
|
+
}
|
|
38
|
+
if (schema.properties) {
|
|
39
|
+
for (let key in schema.properties) {
|
|
40
|
+
if (schema.properties[key].default) {
|
|
41
|
+
delete schema.properties[key].default
|
|
42
|
+
}
|
|
43
|
+
removeAllDefault(schema.properties[key])
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (schema.items) {
|
|
47
|
+
removeAllDefault(schema.items)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const addAllAdditionalProperties = (jsonSchema: any) => {
|
|
51
|
+
if (jsonSchema.type === 'object') {
|
|
52
|
+
jsonSchema.additionalProperties = false
|
|
53
|
+
for (const key in jsonSchema.properties) {
|
|
54
|
+
addAllAdditionalProperties(jsonSchema.properties[key])
|
|
55
|
+
}
|
|
56
|
+
} else if (jsonSchema.type === 'array') {
|
|
57
|
+
addAllAdditionalProperties(jsonSchema.items)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const jsonSchema = convertSchema(Yup.object(cb(Yup)))
|
|
61
|
+
removeAllDefault(jsonSchema)
|
|
62
|
+
addAllAdditionalProperties(jsonSchema)
|
|
63
|
+
return jsonSchema
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { ChatBrokerPlugin } from '../core/plugin';
|
|
2
|
+
import { Event, Hook, Log } from 'power-helper';
|
|
3
|
+
import { Translator, TranslatorParams } from '../core/translator';
|
|
4
|
+
import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate';
|
|
5
|
+
export type Message = {
|
|
6
|
+
role: 'system' | 'user' | 'assistant' | (string & Record<string, unknown>);
|
|
7
|
+
name?: string;
|
|
8
|
+
content: string;
|
|
9
|
+
};
|
|
10
|
+
export type ChatBrokerHooks<S extends ValidateCallback<any>, O extends ValidateCallback<any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>> = {
|
|
11
|
+
/**
|
|
12
|
+
* @zh 第一次聊天的時候觸發
|
|
13
|
+
* @en Triggered when chatting for the first time
|
|
14
|
+
*/
|
|
15
|
+
start: {
|
|
16
|
+
id: string;
|
|
17
|
+
data: ValidateCallbackOutputs<S>;
|
|
18
|
+
metadata: Map<string, any>;
|
|
19
|
+
plugins: {
|
|
20
|
+
[K in keyof PS]: {
|
|
21
|
+
send: (data: PS[K]['__receiveData']) => void;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
schema: {
|
|
25
|
+
input?: S;
|
|
26
|
+
output: O;
|
|
27
|
+
};
|
|
28
|
+
messages: Message[];
|
|
29
|
+
setPreMessages: (messages: (Omit<Message, 'content'> & {
|
|
30
|
+
content: string | string[];
|
|
31
|
+
})[]) => void;
|
|
32
|
+
changeMessages: (messages: Message[]) => void;
|
|
33
|
+
changeOutputSchema: (output: O) => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* @zh 發送聊天訊息給機器人前觸發
|
|
37
|
+
* @en Triggered before sending chat message to bot
|
|
38
|
+
*/
|
|
39
|
+
talkBefore: {
|
|
40
|
+
id: string;
|
|
41
|
+
data: ValidateCallbackOutputs<S>;
|
|
42
|
+
messages: Message[];
|
|
43
|
+
metadata: Map<string, any>;
|
|
44
|
+
lastUserMessage: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* @zh 當聊天機器人回傳資料的時候觸發
|
|
48
|
+
* @en Triggered when the chatbot returns data
|
|
49
|
+
*/
|
|
50
|
+
talkAfter: {
|
|
51
|
+
id: string;
|
|
52
|
+
data: ValidateCallbackOutputs<S>;
|
|
53
|
+
response: any;
|
|
54
|
+
messages: Message[];
|
|
55
|
+
parseText: string;
|
|
56
|
+
metadata: Map<string, any>;
|
|
57
|
+
lastUserMessage: string;
|
|
58
|
+
/**
|
|
59
|
+
* @zh 宣告解析失敗
|
|
60
|
+
* @en Declare parsing failure
|
|
61
|
+
*/
|
|
62
|
+
parseFail: (error: any) => void;
|
|
63
|
+
changeParseText: (text: string) => void;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* @zh 當回傳資料符合規格時觸發
|
|
67
|
+
* @en Triggered when the returned data meets the specifications
|
|
68
|
+
*/
|
|
69
|
+
succeeded: {
|
|
70
|
+
id: string;
|
|
71
|
+
metadata: Map<string, any>;
|
|
72
|
+
output: ValidateCallbackOutputs<O>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @zh 當回傳資料不符合規格,或是解析錯誤時觸發
|
|
76
|
+
* @en Triggered when the returned data does not meet the specifications or parsing errors
|
|
77
|
+
*/
|
|
78
|
+
parseFailed: {
|
|
79
|
+
id: string;
|
|
80
|
+
error: any;
|
|
81
|
+
retry: () => void;
|
|
82
|
+
count: number;
|
|
83
|
+
response: any;
|
|
84
|
+
metadata: Map<string, any>;
|
|
85
|
+
parserFails: {
|
|
86
|
+
name: string;
|
|
87
|
+
error: any;
|
|
88
|
+
}[];
|
|
89
|
+
messages: Message[];
|
|
90
|
+
lastUserMessage: string;
|
|
91
|
+
changeMessages: (messages: Message[]) => void;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* @zh 不論成功失敗,執行結束的時候會執行。
|
|
95
|
+
* @en It will be executed when the execution is completed, regardless of success or failure.
|
|
96
|
+
*/
|
|
97
|
+
done: {
|
|
98
|
+
id: string;
|
|
99
|
+
metadata: Map<string, any>;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export type RequestContext = {
|
|
103
|
+
id: string;
|
|
104
|
+
count: number;
|
|
105
|
+
isRetry: boolean;
|
|
106
|
+
metadata: Map<string, any>;
|
|
107
|
+
onCancel: (cb: () => void) => void;
|
|
108
|
+
schema: {
|
|
109
|
+
input: any;
|
|
110
|
+
output: any;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export type Params<S extends ValidateCallback<any>, O extends ValidateCallback<any>, C extends Record<string, any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>> = Omit<TranslatorParams<S, O>, 'parsers'> & {
|
|
114
|
+
name?: string;
|
|
115
|
+
plugins?: PS | (() => PS);
|
|
116
|
+
request: (messages: Message[], context: RequestContext) => Promise<string>;
|
|
117
|
+
install?: (context: {
|
|
118
|
+
log: Log;
|
|
119
|
+
attach: Hook<C>['attach'];
|
|
120
|
+
attachAfter: Hook<C>['attachAfter'];
|
|
121
|
+
translator: Translator<S, O>;
|
|
122
|
+
}) => void;
|
|
123
|
+
};
|
|
124
|
+
export declare class ChatBroker<S extends ValidateCallback<any>, O extends ValidateCallback<any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>, C extends ChatBrokerHooks<S, O, P, PS> = ChatBrokerHooks<S, O, P, PS>> {
|
|
125
|
+
protected __hookType: C;
|
|
126
|
+
protected log: Log;
|
|
127
|
+
protected hook: Hook<C>;
|
|
128
|
+
protected params: Params<S, O, C, P, PS>;
|
|
129
|
+
protected plugins: PS;
|
|
130
|
+
protected installed: boolean;
|
|
131
|
+
protected translator: Translator<S, O>;
|
|
132
|
+
protected event: Event<{
|
|
133
|
+
cancel: {
|
|
134
|
+
requestId: string;
|
|
135
|
+
};
|
|
136
|
+
cancelAll: any;
|
|
137
|
+
}>;
|
|
138
|
+
constructor(params: Params<S, O, C, P, PS>);
|
|
139
|
+
protected _install(): any;
|
|
140
|
+
cancel(requestId?: string): Promise<void>;
|
|
141
|
+
requestWithId<T extends Translator<S, O>>(data: T['__schemeType']): {
|
|
142
|
+
id: string;
|
|
143
|
+
request: Promise<T['__outputType']>;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* @zh 將請求發出至聊天機器人。
|
|
147
|
+
* @en Send request to chatbot.
|
|
148
|
+
*/
|
|
149
|
+
request<T extends Translator<S, O>>(data: T['__schemeType']): Promise<T['__outputType']>;
|
|
150
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type TextParserParams = {
|
|
2
|
+
/**
|
|
3
|
+
* @zh 解讀器名字。
|
|
4
|
+
* @en The name of the parser.
|
|
5
|
+
*/
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* @zh 解讀文本。
|
|
9
|
+
* @en Read the text.
|
|
10
|
+
*/
|
|
11
|
+
handler: (text: string) => Promise<any>;
|
|
12
|
+
};
|
|
13
|
+
export declare class TextParser {
|
|
14
|
+
private params;
|
|
15
|
+
/**
|
|
16
|
+
* @zh 盡可能將文字內的 json 解讀出來。
|
|
17
|
+
* @en Try to read the json in the text as much as possible.
|
|
18
|
+
*/
|
|
19
|
+
static JsonMessage(): TextParser;
|
|
20
|
+
constructor(params: TextParserParams);
|
|
21
|
+
/**
|
|
22
|
+
* @zh 解讀器名字。
|
|
23
|
+
* @en The name of the parser.
|
|
24
|
+
*/
|
|
25
|
+
get name(): string;
|
|
26
|
+
/**
|
|
27
|
+
* @zh 解讀文本。
|
|
28
|
+
* @en Read the text.
|
|
29
|
+
*/
|
|
30
|
+
read(text: string): Promise<any>;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Translator } from './translator';
|
|
2
|
+
import { ChatBrokerHooks } from '../broker/chat';
|
|
3
|
+
import { Log, Hook, Event } from 'power-helper';
|
|
4
|
+
import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate';
|
|
5
|
+
type BrokerHooks = ChatBrokerHooks<any, any, any, any>;
|
|
6
|
+
type BrokerPluginParams<T extends ValidateCallback<any>, R extends ValidateCallback<any>> = {
|
|
7
|
+
name: string;
|
|
8
|
+
params: T;
|
|
9
|
+
receiveData: R;
|
|
10
|
+
onInstall: (context: {
|
|
11
|
+
log: Log;
|
|
12
|
+
params: ValidateCallbackOutputs<T>;
|
|
13
|
+
attach: Hook<BrokerHooks>['attach'];
|
|
14
|
+
attachAfter: Hook<BrokerHooks>['attachAfter'];
|
|
15
|
+
translator: Translator<any, any>;
|
|
16
|
+
receive: (callback: (params: {
|
|
17
|
+
id: string;
|
|
18
|
+
data: ValidateCallbackOutputs<R>;
|
|
19
|
+
}) => void) => void;
|
|
20
|
+
}) => void;
|
|
21
|
+
};
|
|
22
|
+
export declare class ChatBrokerPlugin<T extends ValidateCallback<any>, R extends ValidateCallback<any>> {
|
|
23
|
+
_event: Event<Record<string, Record<string, any>>>;
|
|
24
|
+
_params: BrokerPluginParams<T, R>;
|
|
25
|
+
constructor(params: BrokerPluginParams<T, R>);
|
|
26
|
+
use(params: ValidateCallbackOutputs<T>): {
|
|
27
|
+
instance: any;
|
|
28
|
+
params: ValidateCallbackOutputs<T, ReturnType<T>>;
|
|
29
|
+
send: (data: ValidateCallbackOutputs<R>) => void;
|
|
30
|
+
receive: (callback: any) => void;
|
|
31
|
+
__receiveData: ValidateCallbackOutputs<R, ReturnType<R>>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export {};
|