@budibase/worker 2.31.8 → 2.32.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,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.31.8",
4
+ "version": "2.32.0",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -37,10 +37,10 @@
37
37
  "author": "Budibase",
38
38
  "license": "GPL-3.0",
39
39
  "dependencies": {
40
- "@budibase/backend-core": "2.31.8",
41
- "@budibase/pro": "2.31.8",
42
- "@budibase/string-templates": "2.31.8",
43
- "@budibase/types": "2.31.8",
40
+ "@budibase/backend-core": "2.32.0",
41
+ "@budibase/pro": "2.32.0",
42
+ "@budibase/string-templates": "2.32.0",
43
+ "@budibase/types": "2.32.0",
44
44
  "@koa/router": "8.0.8",
45
45
  "@techpass/passport-openidconnect": "0.3.3",
46
46
  "@types/global-agent": "2.1.1",
@@ -107,5 +107,5 @@
107
107
  }
108
108
  }
109
109
  },
110
- "gitHead": "773a06abcdbf7b7bed843f909afc5ca2b11e7ce2"
110
+ "gitHead": "d4559773a0cb8173869a2622e4905bcbc92f7f67"
111
111
  }
@@ -29,6 +29,10 @@ import {
29
29
  SSOConfigType,
30
30
  UserCtx,
31
31
  OIDCLogosConfig,
32
+ AIConfig,
33
+ PASSWORD_REPLACEMENT,
34
+ isAIConfig,
35
+ AIInnerConfig,
32
36
  } from "@budibase/types"
33
37
  import * as pro from "@budibase/pro"
34
38
 
@@ -38,6 +42,11 @@ const getEventFns = async (config: Config, existing?: Config) => {
38
42
  if (!existing) {
39
43
  if (isSMTPConfig(config)) {
40
44
  fns.push(events.email.SMTPCreated)
45
+ } else if (isAIConfig(config)) {
46
+ fns.push(() => events.ai.AIConfigCreated)
47
+ fns.push(() =>
48
+ pro.quotas.updateCustomAIConfigCount(Object.keys(config.config).length)
49
+ )
41
50
  } else if (isGoogleConfig(config)) {
42
51
  fns.push(() => events.auth.SSOCreated(ConfigType.GOOGLE))
43
52
  if (config.config.activated) {
@@ -74,6 +83,11 @@ const getEventFns = async (config: Config, existing?: Config) => {
74
83
  } else {
75
84
  if (isSMTPConfig(config)) {
76
85
  fns.push(events.email.SMTPUpdated)
86
+ } else if (isAIConfig(config)) {
87
+ fns.push(() => events.ai.AIConfigUpdated)
88
+ fns.push(() =>
89
+ pro.quotas.updateCustomAIConfigCount(Object.keys(config.config).length)
90
+ )
77
91
  } else if (isGoogleConfig(config)) {
78
92
  fns.push(() => events.auth.SSOUpdated(ConfigType.GOOGLE))
79
93
  if (!existing.config.activated && config.config.activated) {
@@ -122,7 +136,6 @@ const getEventFns = async (config: Config, existing?: Config) => {
122
136
  }
123
137
  }
124
138
  }
125
-
126
139
  return fns
127
140
  }
128
141
 
@@ -197,6 +210,18 @@ async function verifyOIDCConfig(config: OIDCConfigs) {
197
210
  await verifySSOConfig(ConfigType.OIDC, config.configs[0])
198
211
  }
199
212
 
213
+ export async function verifyAIConfig(
214
+ configToSave: AIInnerConfig,
215
+ existingConfig: AIConfig
216
+ ) {
217
+ // ensure that the redacted API keys are not overwritten in the DB
218
+ for (const uuid in existingConfig.config) {
219
+ if (configToSave[uuid]?.apiKey === PASSWORD_REPLACEMENT) {
220
+ configToSave[uuid].apiKey = existingConfig.config[uuid].apiKey
221
+ }
222
+ }
223
+ }
224
+
200
225
  export async function save(ctx: UserCtx<Config>) {
201
226
  const body = ctx.request.body
202
227
  const type = body.type
@@ -224,6 +249,11 @@ export async function save(ctx: UserCtx<Config>) {
224
249
  case ConfigType.OIDC:
225
250
  await verifyOIDCConfig(config)
226
251
  break
252
+ case ConfigType.AI:
253
+ if (existingConfig) {
254
+ await verifyAIConfig(config, existingConfig)
255
+ }
256
+ break
227
257
  }
228
258
  } catch (err: any) {
229
259
  ctx.throw(400, err)
@@ -304,6 +334,32 @@ function enrichOIDCLogos(oidcLogos: OIDCLogosConfig) {
304
334
  )
305
335
  }
306
336
 
337
+ async function enrichAIConfig(aiConfig: AIConfig) {
338
+ // Strip out the API Keys from the response so they don't show in the UI
339
+ for (const key in aiConfig.config) {
340
+ if (aiConfig.config[key].apiKey) {
341
+ aiConfig.config[key].apiKey = PASSWORD_REPLACEMENT
342
+ }
343
+ }
344
+
345
+ // Return the Budibase AI data source as part of the response if licensing allows
346
+ const budibaseAIEnabled = await pro.features.isBudibaseAIEnabled()
347
+ const defaultConfigExists = Object.keys(aiConfig.config).some(
348
+ key => aiConfig.config[key].isDefault
349
+ )
350
+ if (budibaseAIEnabled) {
351
+ aiConfig.config["budibase_ai"] = {
352
+ provider: "OpenAI",
353
+ active: true,
354
+ isDefault: !defaultConfigExists,
355
+ defaultModel: env.BUDIBASE_AI_DEFAULT_MODEL || "",
356
+ name: "Budibase AI",
357
+ }
358
+ }
359
+
360
+ return aiConfig
361
+ }
362
+
307
363
  export async function find(ctx: UserCtx) {
308
364
  try {
309
365
  // Find the config with the most granular scope based on context
@@ -314,6 +370,10 @@ export async function find(ctx: UserCtx) {
314
370
  if (type === ConfigType.OIDC_LOGOS) {
315
371
  enrichOIDCLogos(scopedConfig)
316
372
  }
373
+
374
+ if (type === ConfigType.AI) {
375
+ await enrichAIConfig(scopedConfig)
376
+ }
317
377
  ctx.body = scopedConfig
318
378
  } else {
319
379
  // don't throw an error, there simply is nothing to return
@@ -0,0 +1,106 @@
1
+ import * as pro from "@budibase/pro"
2
+ import { verifyAIConfig } from "../configs"
3
+ import { TestConfiguration, structures } from "../../../../tests"
4
+ import { AIInnerConfig } from "@budibase/types"
5
+
6
+ describe("Global configs controller", () => {
7
+ const config = new TestConfiguration()
8
+
9
+ beforeAll(async () => {
10
+ await config.beforeAll()
11
+ })
12
+
13
+ afterAll(async () => {
14
+ await config.afterAll()
15
+ })
16
+
17
+ afterEach(() => {
18
+ jest.resetAllMocks()
19
+ })
20
+
21
+ it("Should strip secrets when pulling AI config", async () => {
22
+ const data = structures.configs.ai()
23
+ await config.api.configs.saveConfig(data)
24
+ const response = await config.api.configs.getAIConfig()
25
+ expect(response.body.config).toEqual({
26
+ ai: {
27
+ active: true,
28
+ apiKey: "--secret-value--",
29
+ baseUrl: "https://api.example.com",
30
+ defaultModel: "gpt4",
31
+ isDefault: false,
32
+ name: "Test",
33
+ provider: "OpenAI",
34
+ },
35
+ })
36
+ })
37
+
38
+ it("Should return the default BB AI config when the feature is turned on", async () => {
39
+ jest
40
+ .spyOn(pro.features, "isBudibaseAIEnabled")
41
+ .mockImplementation(() => Promise.resolve(true))
42
+ const data = structures.configs.ai()
43
+ await config.api.configs.saveConfig(data)
44
+ const response = await config.api.configs.getAIConfig()
45
+
46
+ expect(response.body.config).toEqual({
47
+ budibase_ai: {
48
+ provider: "OpenAI",
49
+ active: true,
50
+ isDefault: true,
51
+ name: "Budibase AI",
52
+ defaultModel: "",
53
+ },
54
+ ai: {
55
+ active: true,
56
+ apiKey: "--secret-value--",
57
+ baseUrl: "https://api.example.com",
58
+ defaultModel: "gpt4",
59
+ isDefault: false,
60
+ name: "Test",
61
+ provider: "OpenAI",
62
+ },
63
+ })
64
+ })
65
+
66
+ it("Should not not return the default Budibase AI config when on self host", async () => {
67
+ jest
68
+ .spyOn(pro.features, "isBudibaseAIEnabled")
69
+ .mockImplementation(() => Promise.resolve(false))
70
+ const data = structures.configs.ai()
71
+ await config.api.configs.saveConfig(data)
72
+ const response = await config.api.configs.getAIConfig()
73
+
74
+ expect(response.body.config).toEqual({
75
+ ai: {
76
+ active: true,
77
+ apiKey: "--secret-value--",
78
+ baseUrl: "https://api.example.com",
79
+ defaultModel: "gpt4",
80
+ isDefault: false,
81
+ name: "Test",
82
+ provider: "OpenAI",
83
+ },
84
+ })
85
+ })
86
+
87
+ it("Should not update existing secrets when updating an existing AI Config", async () => {
88
+ const data = structures.configs.ai()
89
+ await config.api.configs.saveConfig(data)
90
+
91
+ const newConfig: AIInnerConfig = {
92
+ ai: {
93
+ provider: "OpenAI",
94
+ isDefault: true,
95
+ apiKey: "--secret-value--",
96
+ name: "MyConfig",
97
+ active: true,
98
+ defaultModel: "gpt4",
99
+ },
100
+ }
101
+
102
+ await verifyAIConfig(newConfig, data)
103
+ // should be unchanged
104
+ expect(newConfig.ai.apiKey).toEqual("myapikey")
105
+ })
106
+ })
@@ -65,6 +65,22 @@ function scimValidation() {
65
65
  }).unknown(true)
66
66
  }
67
67
 
68
+ function aiValidation() {
69
+ // prettier-ignore
70
+ return Joi.object().pattern(
71
+ Joi.string(),
72
+ Joi.object({
73
+ provider: Joi.string().required(),
74
+ isDefault: Joi.boolean().required(),
75
+ name: Joi.string().required(),
76
+ active: Joi.boolean().required(),
77
+ baseUrl: Joi.string().optional().allow("", null),
78
+ apiKey: Joi.string().required(),
79
+ defaultModel: Joi.string().optional(),
80
+ }).required()
81
+ )
82
+ }
83
+
68
84
  function buildConfigSaveValidation() {
69
85
  // prettier-ignore
70
86
  return auth.joiValidator.body(Joi.object({
@@ -82,7 +98,8 @@ function buildConfigSaveValidation() {
82
98
  { is: ConfigType.ACCOUNT, then: Joi.object().unknown(true) },
83
99
  { is: ConfigType.GOOGLE, then: googleValidation() },
84
100
  { is: ConfigType.OIDC, then: oidcValidation() },
85
- { is: ConfigType.SCIM, then: scimValidation() }
101
+ { is: ConfigType.SCIM, then: scimValidation() },
102
+ { is: ConfigType.AI, then: aiValidation() }
86
103
  ],
87
104
  }),
88
105
  }).required().unknown(true),
@@ -70,6 +70,9 @@ const environment = {
70
70
  PASSPORT_OIDCAUTH_FAILURE_REDIRECT:
71
71
  process.env.PASSPORT_OIDCAUTH_FAILURE_REDIRECT || "/error",
72
72
 
73
+ // Budibase AI
74
+ BUDIBASE_AI_API_KEY: process.env.BUDIBASE_AI_API_KEY,
75
+ BUDIBASE_AI_DEFAULT_MODEL: process.env.BUDIBASE_AI_DEFAULT_MODEL,
73
76
  _set(key: any, value: any) {
74
77
  process.env[key] = value
75
78
  // @ts-ignore
@@ -22,6 +22,14 @@ export class ConfigAPI extends TestAPI {
22
22
  .expect("Content-Type", /json/)
23
23
  }
24
24
 
25
+ getAIConfig = () => {
26
+ return this.request
27
+ .get(`/api/global/configs/ai`)
28
+ .set(this.config.defaultHeaders())
29
+ .expect(200)
30
+ .expect("Content-Type", /json/)
31
+ }
32
+
25
33
  saveConfig = (data: any) => {
26
34
  return this.request
27
35
  .post(`/api/global/configs`)
@@ -5,6 +5,7 @@ import {
5
5
  SMTPConfig,
6
6
  GoogleConfig,
7
7
  OIDCConfig,
8
+ AIConfig,
8
9
  } from "@budibase/types"
9
10
 
10
11
  export function oidc(conf?: any): OIDCConfig {
@@ -81,3 +82,20 @@ export function settings(conf?: any): SettingsConfig {
81
82
  },
82
83
  }
83
84
  }
85
+
86
+ export function ai(): AIConfig {
87
+ return {
88
+ type: ConfigType.AI,
89
+ config: {
90
+ ai: {
91
+ provider: "OpenAI",
92
+ isDefault: false,
93
+ name: "Test",
94
+ active: true,
95
+ defaultModel: "gpt4",
96
+ apiKey: "myapikey",
97
+ baseUrl: "https://api.example.com",
98
+ },
99
+ },
100
+ }
101
+ }