@botpress/cognitive 0.4.6 → 0.5.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/.turbo/turbo-build.log +7 -7
- package/dist/index.cjs +784 -111
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +178 -112
- package/dist/index.mjs +784 -111
- package/dist/index.mjs.map +2 -2
- package/package.json +1 -1
- package/refresh-models.ts +51 -30
package/package.json
CHANGED
package/refresh-models.ts
CHANGED
|
@@ -2,7 +2,7 @@ import 'dotenv/config'
|
|
|
2
2
|
import axios from 'axios'
|
|
3
3
|
import * as fs from 'fs'
|
|
4
4
|
import * as path from 'path'
|
|
5
|
-
import { Model } from 'src/
|
|
5
|
+
import { Model } from 'src/cognitive-v2/types'
|
|
6
6
|
|
|
7
7
|
const builtInModels = ['auto', 'best', 'fast']
|
|
8
8
|
const filteredLifecycles = ['deprecated', 'discontinued']
|
|
@@ -10,9 +10,7 @@ const filteredLifecycles = ['deprecated', 'discontinued']
|
|
|
10
10
|
const modelsListPath = path.resolve(__dirname, 'src/cognitive-v2', 'models.ts')
|
|
11
11
|
const typesPath = path.resolve(__dirname, 'src/cognitive-v2', 'types.ts')
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const toRef = (m: RemoteModel | string | null | undefined): string | null => {
|
|
13
|
+
const toRef = (m: Model | string | null | undefined): string | null => {
|
|
16
14
|
if (!m) return null
|
|
17
15
|
if (typeof m === 'string') return m
|
|
18
16
|
|
|
@@ -21,22 +19,22 @@ const toRef = (m: RemoteModel | string | null | undefined): string | null => {
|
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
async function main(): Promise<void> {
|
|
24
|
-
const server = process.env.
|
|
25
|
-
const key = process.env.
|
|
26
|
-
const botId = process.env.
|
|
22
|
+
const server = process.env.CLOUD_COGNITIVE_ENDPOINT || 'https://api.botpress.cloud/v2/cognitive'
|
|
23
|
+
const key = process.env.CLOUD_PAT
|
|
24
|
+
const botId = process.env.CLOUD_BOT_ID
|
|
27
25
|
|
|
28
26
|
const {
|
|
29
27
|
data: { models },
|
|
30
|
-
} = await axios.get<{ models:
|
|
28
|
+
} = await axios.get<{ models: Model[] }>(`${server}/models?includeDeprecated=true`, {
|
|
31
29
|
headers: {
|
|
32
30
|
Authorization: `Bearer ${key}`,
|
|
33
31
|
'X-Bot-Id': botId,
|
|
34
32
|
},
|
|
35
33
|
})
|
|
36
34
|
|
|
37
|
-
const modelsObj = models.reduce((acc, m) => ((acc[m.id] = m), acc), {} as Record<string,
|
|
35
|
+
const modelsObj = models.reduce((acc, m) => ((acc[m.id] = m), acc), {} as Record<string, Model>)
|
|
38
36
|
|
|
39
|
-
const defaultModel:
|
|
37
|
+
const defaultModel: Model = {
|
|
40
38
|
id: '',
|
|
41
39
|
name: '',
|
|
42
40
|
description: '',
|
|
@@ -46,39 +44,62 @@ async function main(): Promise<void> {
|
|
|
46
44
|
lifecycle: 'production',
|
|
47
45
|
}
|
|
48
46
|
|
|
49
|
-
const newFile = `import { Model } from '
|
|
50
|
-
export
|
|
51
|
-
export const
|
|
52
|
-
export const defaultModel: RemoteModel = ${JSON.stringify(defaultModel, undefined, 2)}
|
|
47
|
+
const newFile = `import { Model } from './types'\n
|
|
48
|
+
export const models: Record<string, Model> = ${JSON.stringify(modelsObj, null, 2)}\n
|
|
49
|
+
export const defaultModel: Model = ${JSON.stringify(defaultModel, undefined, 2)}
|
|
53
50
|
`
|
|
54
51
|
|
|
55
52
|
fs.writeFileSync(modelsListPath, newFile, 'utf8')
|
|
56
53
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
(m.aliases || []).map((a) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
)
|
|
54
|
+
const collectRefs = (list: Model[]) =>
|
|
55
|
+
Array.from(new Set(list.map(toRef).filter(Boolean))).sort((a, b) => a.localeCompare(b))
|
|
56
|
+
const collectAliases = (list: Model[]) =>
|
|
57
|
+
Array.from(new Set(list.flatMap((m) => (m.aliases || []).map((a) => `${m.id.split(':')[0]}:${a}`))))
|
|
58
|
+
|
|
59
|
+
const active = models.filter((m) => !filteredLifecycles.includes(m.lifecycle))
|
|
60
|
+
const activeLlm = active.filter((m) => !m.capabilities?.supportsTranscription)
|
|
61
|
+
const activeStt = active.filter((m) => m.capabilities?.supportsTranscription)
|
|
62
|
+
|
|
63
|
+
const refs = collectRefs(activeLlm)
|
|
64
|
+
const aliases = collectAliases(models.filter((m) => !m.capabilities?.supportsTranscription))
|
|
65
|
+
|
|
66
|
+
const sttRefs = collectRefs(activeStt)
|
|
67
|
+
const sttAliases = collectAliases(activeStt)
|
|
65
68
|
|
|
66
69
|
const content = fs.readFileSync(typesPath, 'utf8')
|
|
67
70
|
|
|
68
|
-
|
|
69
|
-
const
|
|
71
|
+
// Update Models union
|
|
72
|
+
const modelsStartMarker = 'export type Models ='
|
|
73
|
+
const modelsEndMarker = 'export type SttModels ='
|
|
70
74
|
|
|
71
|
-
const
|
|
72
|
-
const
|
|
75
|
+
const modelsStartIdx = content.indexOf(modelsStartMarker)
|
|
76
|
+
const modelsEndIdx = content.indexOf(modelsEndMarker)
|
|
73
77
|
|
|
74
|
-
if (
|
|
75
|
-
throw new Error('Could not locate Models union block in
|
|
78
|
+
if (modelsStartIdx === -1 || modelsEndIdx === -1 || modelsEndIdx <= modelsStartIdx) {
|
|
79
|
+
throw new Error('Could not locate Models union block in types.ts')
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
const items = [...builtInModels, ...refs, ...aliases].map((r) => ` | '${r}'`)
|
|
79
|
-
const
|
|
83
|
+
const modelsUnionBlock = ['export type Models =', ...items, ' | ({} & string)', '', ''].join('\n')
|
|
84
|
+
|
|
85
|
+
let nextContent = content.slice(0, modelsStartIdx) + modelsUnionBlock + content.slice(modelsEndIdx)
|
|
86
|
+
|
|
87
|
+
// Update SttModels union
|
|
88
|
+
const sttStartMarker = 'export type SttModels ='
|
|
89
|
+
const sttEndMarker = 'export type CognitiveContentPart'
|
|
90
|
+
|
|
91
|
+
const sttStartIdx = nextContent.indexOf(sttStartMarker)
|
|
92
|
+
const sttEndIdx = nextContent.indexOf(sttEndMarker)
|
|
93
|
+
|
|
94
|
+
if (sttStartIdx === -1 || sttEndIdx === -1 || sttEndIdx <= sttStartIdx) {
|
|
95
|
+
throw new Error('Could not locate SttModels union block in types.ts')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const sttItems = [...builtInModels, ...sttRefs, ...sttAliases].map((r) => ` | '${r}'`)
|
|
99
|
+
const sttUnionBlock = ['export type SttModels =', ...sttItems, ' | ({} & string)', '', ''].join('\n')
|
|
100
|
+
|
|
101
|
+
nextContent = nextContent.slice(0, sttStartIdx) + sttUnionBlock + nextContent.slice(sttEndIdx)
|
|
80
102
|
|
|
81
|
-
const nextContent = content.slice(0, startIdx) + unionBlock + content.slice(endIdx)
|
|
82
103
|
fs.writeFileSync(typesPath, nextContent, 'utf8')
|
|
83
104
|
}
|
|
84
105
|
|