Merge branch 'master' into remove-anthropic

This commit is contained in:
Sam Rose 2025-04-15 16:07:55 +01:00 committed by GitHub
commit 5eeebbc4e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 138 additions and 57 deletions

View File

@ -1,5 +1,16 @@
<script lang="ts"> <script lang="ts">
import { ActionButton, Icon, notifications } from "@budibase/bbui" import {
ActionButton,
Icon,
notifications,
Button,
Modal,
ModalContent,
Body,
Link,
} from "@budibase/bbui"
import { auth, admin, licensing } from "@/stores/portal"
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
import { API } from "@/api" import { API } from "@/api"
import type { EnrichedBinding } from "@budibase/types" import type { EnrichedBinding } from "@budibase/types"
@ -8,6 +19,8 @@
export let bindings: EnrichedBinding[] = [] export let bindings: EnrichedBinding[] = []
export let value: string | null = "" export let value: string | null = ""
export let expandedOnly: boolean = false
export let parentWidth: number | null = null export let parentWidth: number | null = null
export const dispatch = createEventDispatcher<{ export const dispatch = createEventDispatcher<{
update: { code: string } update: { code: string }
@ -15,29 +28,28 @@
reject: { code: string | null } reject: { code: string | null }
}>() }>()
let buttonContainer: HTMLElement let promptInput: HTMLInputElement
let promptInput: HTMLTextAreaElement
let buttonElement: HTMLButtonElement let buttonElement: HTMLButtonElement
let promptLoading = false let promptLoading = false
let suggestedCode: string | null = null let suggestedCode: string | null = null
let previousContents: string | null = null let previousContents: string | null = null
let expanded = false let expanded = false
let containerWidth = "auto" let containerWidth = "auto"
let containerHeight = "40px"
let promptText = "" let promptText = ""
let animateBorder = false let animateBorder = false
let switchOnAIModal: Modal
let addCreditsModal: Modal
function adjustContainerHeight() { $: accountPortalAccess = $auth?.user?.accountPortalAccess
if (promptInput && buttonElement) { $: accountPortal = $admin.accountPortalUrl
promptInput.style.height = "0px" $: aiEnabled = $auth?.user?.llm
const newHeight = Math.min(promptInput.scrollHeight, 100) $: expanded = expandedOnly ? true : expanded
promptInput.style.height = `${newHeight}px` $: creditsExceeded = $licensing.aiCreditsExceeded
containerHeight = `${Math.max(40, newHeight + 20)}px` $: disabled = suggestedCode !== null || !aiEnabled || creditsExceeded
} $: if (expandedOnly) {
containerWidth = calculateExpandedWidth()
} }
$: if (promptInput && promptText) adjustContainerHeight()
async function generateJs(prompt: string) { async function generateJs(prompt: string) {
if (!prompt.trim()) return if (!prompt.trim()) return
@ -85,22 +97,23 @@
function resetExpand() { function resetExpand() {
expanded = false expanded = false
containerWidth = "auto" containerWidth = "auto"
containerHeight = "40px"
promptText = "" promptText = ""
suggestedCode = null suggestedCode = null
previousContents = null previousContents = null
animateBorder = false animateBorder = false
} }
function calculateExpandedWidth() {
return parentWidth
? `${Math.min(Math.max(parentWidth * 0.8, 300), 600)}px`
: "300px"
}
function toggleExpand() { function toggleExpand() {
if (!expanded) { if (!expanded) {
expanded = true expanded = true
animateBorder = true animateBorder = true
// Calculate width based on size of CodeEditor parent containerWidth = calculateExpandedWidth()
containerWidth = parentWidth
? `${Math.min(Math.max(parentWidth * 0.8, 300), 600)}px`
: "300px"
containerHeight = "40px"
setTimeout(() => { setTimeout(() => {
promptInput?.focus() promptInput?.focus()
}, 250) }, 250)
@ -125,11 +138,9 @@
} }
</script> </script>
<div <!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
class="ai-gen-container" <!-- svelte-ignore a11y-click-events-have-key-events -->
style="--container-width: {containerWidth}; --container-height: {containerHeight}" <div class="ai-gen-container" style="--container-width: {containerWidth}">
bind:this={buttonContainer}
>
{#if suggestedCode !== null} {#if suggestedCode !== null}
<div class="floating-actions"> <div class="floating-actions">
<ActionButton size="S" icon="CheckmarkCircle" on:click={acceptSuggestion}> <ActionButton size="S" icon="CheckmarkCircle" on:click={acceptSuggestion}>
@ -140,7 +151,6 @@
</ActionButton> </ActionButton>
</div> </div>
{/if} {/if}
<button <button
bind:this={buttonElement} bind:this={buttonElement}
class="spectrum-ActionButton fade" class="spectrum-ActionButton fade"
@ -149,18 +159,28 @@
on:click={!expanded ? toggleExpand : undefined} on:click={!expanded ? toggleExpand : undefined}
> >
<div class="button-content-wrapper"> <div class="button-content-wrapper">
<img src={BBAI} alt="AI" class="ai-icon" /> <img
src={BBAI}
alt="AI"
class="ai-icon"
class:disabled={expanded && disabled}
on:click={!expandedOnly
? e => {
e.stopPropagation()
toggleExpand()
}
: undefined}
/>
{#if expanded} {#if expanded}
<textarea <input
type="text"
bind:this={promptInput} bind:this={promptInput}
bind:value={promptText} bind:value={promptText}
class="prompt-input" class="prompt-input"
placeholder="Generate Javascript..." placeholder="Generate Javascript..."
on:keydown={handleKeyPress} on:keydown={handleKeyPress}
on:input={adjustContainerHeight} {disabled}
disabled={suggestedCode !== null}
readonly={suggestedCode !== null} readonly={suggestedCode !== null}
rows="1"
/> />
{:else} {:else}
<span class="spectrum-ActionButton-label ai-gen-text"> <span class="spectrum-ActionButton-label ai-gen-text">
@ -168,29 +188,57 @@
</span> </span>
{/if} {/if}
</div> </div>
{#if expanded} {#if expanded}
<div class="action-buttons"> <div class="action-buttons">
<Icon {#if !aiEnabled}
color={promptLoading <Button cta size="S" on:click={() => switchOnAIModal.show()}>
? "#6E56FF" Switch on AI
: "var(--spectrum-global-color-gray-600)"} </Button>
size="S" <Modal bind:this={switchOnAIModal}>
hoverable <ModalContent title="Switch on AI" showConfirmButton={false}>
hoverColor="#6E56FF" <div class="enable-ai">
name={promptLoading ? "StopCircle" : "PlayCircle"} <p>To enable BB AI:</p>
on:click={() => generateJs(promptText)} <ul>
/> <li>
<Icon Add your Budibase license key:
hoverable <Link href={accountPortal}>Budibase account portal</Link>
size="S" </li>
name="Close" <li>
hoverColor="#6E56FF" Go to the portal settings page, click AI and switch on BB AI
on:click={e => { </li>
e.stopPropagation() </ul>
if (!suggestedCode && !promptLoading) toggleExpand() </div>
}} </ModalContent>
/> </Modal>
{:else if creditsExceeded}
<Button cta size="S" on:click={() => addCreditsModal.show()}>
Add AI credits
</Button>
<Modal bind:this={addCreditsModal}>
<ModalContent title="Add AI credits" showConfirmButton={false}>
<Body size="S">
{#if accountPortalAccess}
<Link href={"https://budibase.com/contact/"}
>Contact sales</Link
> to unlock additional BB AI credits
{:else}
Contact your account holder to unlock additional BB AI credits
{/if}
</Body>
</ModalContent>
</Modal>
{:else}
<Icon
color={promptLoading
? "#6E56FF"
: "var(--spectrum-global-color-gray-600)"}
size="S"
hoverable
hoverColor="#6E56FF"
name={promptLoading ? "StopCircle" : "PlayCircle"}
on:click={() => generateJs(promptText)}
/>
{/if}
</div> </div>
{/if} {/if}
</button> </button>
@ -198,13 +246,12 @@
<style> <style>
.ai-gen-container { .ai-gen-container {
height: 40px;
--container-width: auto; --container-width: auto;
--container-height: 40px;
position: absolute; position: absolute;
right: 10px; right: 10px;
bottom: 10px; bottom: 10px;
width: var(--container-width); width: var(--container-width);
height: var(--container-height);
display: flex; display: flex;
overflow: visible; overflow: visible;
} }
@ -238,7 +285,7 @@
background: linear-gradient( background: linear-gradient(
125deg, 125deg,
transparent -10%, transparent -10%,
#6e56ff 5%, #6e56ff 2%,
#9f8fff 15%, #9f8fff 15%,
#9f8fff 25%, #9f8fff 25%,
transparent 35%, transparent 35%,
@ -320,6 +367,7 @@
height: 18px; height: 18px;
margin-right: 8px; margin-right: 8px;
flex-shrink: 0; flex-shrink: 0;
cursor: var(--ai-icon-cursor, pointer);
} }
.ai-gen-text { .ai-gen-text {
@ -335,18 +383,16 @@
border: none; border: none;
background: transparent; background: transparent;
outline: none; outline: none;
font-size: var(--font-size-s);
font-family: var(--font-sans); font-family: var(--font-sans);
color: var(--spectrum-alias-text-color); color: var(--spectrum-alias-text-color);
min-width: 0; min-width: 0;
resize: none; resize: none;
overflow: hidden; overflow: hidden;
line-height: 1.2;
min-height: 10px !important;
} }
.prompt-input::placeholder { .prompt-input::placeholder {
color: var(--spectrum-global-color-gray-600); color: var(--spectrum-global-color-gray-600);
font-family: var(--font-sans);
} }
.action-buttons { .action-buttons {
@ -354,6 +400,7 @@
gap: var(--spacing-s); gap: var(--spacing-s);
z-index: 4; z-index: 4;
flex-shrink: 0; flex-shrink: 0;
margin-right: var(--spacing-s);
} }
.button-content-wrapper { .button-content-wrapper {
@ -366,4 +413,15 @@
min-width: 0; min-width: 0;
margin-right: var(--spacing-s); margin-right: var(--spacing-s);
} }
.prompt-input:disabled,
.prompt-input[readonly] {
color: var(--spectrum-global-color-gray-500);
cursor: not-allowed;
}
.ai-icon.disabled {
filter: grayscale(1) brightness(1.5);
opacity: 0.5;
}
</style> </style>

View File

@ -469,6 +469,7 @@
{#if aiGenEnabled} {#if aiGenEnabled}
<AIGen <AIGen
expandedOnly={true}
{bindings} {bindings}
{value} {value}
parentWidth={editorWidth} parentWidth={editorWidth}

View File

@ -56,7 +56,9 @@ interface LicensingState {
// user limits // user limits
userCount?: number userCount?: number
userLimit?: number userLimit?: number
aiCreditsLimit?: number
userLimitReached: boolean userLimitReached: boolean
aiCreditsExceeded: boolean
errUserLimit: boolean errUserLimit: boolean
} }
@ -102,6 +104,8 @@ class LicensingStore extends BudiStore<LicensingState> {
userLimit: undefined, userLimit: undefined,
userLimitReached: false, userLimitReached: false,
errUserLimit: false, errUserLimit: false,
// AI Limits
aiCreditsExceeded: false,
}) })
} }
@ -119,6 +123,16 @@ class LicensingStore extends BudiStore<LicensingState> {
return userCount > userLimit return userCount > userLimit
} }
aiCreditsExceeded(
aiCredits: number,
aiCreditsLimit = get(this.store).aiCreditsLimit
) {
if (aiCreditsLimit === UNLIMITED || aiCreditsLimit === undefined) {
return false
}
return aiCredits > aiCreditsLimit
}
async isCloud() { async isCloud() {
let adminStore = get(admin) let adminStore = get(admin)
if (!adminStore.loaded) { if (!adminStore.loaded) {
@ -291,9 +305,15 @@ class LicensingStore extends BudiStore<LicensingState> {
const userQuota = license.quotas.usage.static.users const userQuota = license.quotas.usage.static.users
const userLimit = userQuota.value const userLimit = userQuota.value
const aiCreditsQuota = license.quotas.usage.monthly.budibaseAICredits
const aiCreditsLimit = aiCreditsQuota.value
const userCount = usage.usageQuota.users const userCount = usage.usageQuota.users
const userLimitReached = this.usersLimitReached(userCount, userLimit) const userLimitReached = this.usersLimitReached(userCount, userLimit)
const userLimitExceeded = this.usersLimitExceeded(userCount, userLimit) const userLimitExceeded = this.usersLimitExceeded(userCount, userLimit)
const aiCreditsExceeded = this.aiCreditsExceeded(
usage.monthly.current.budibaseAICredits,
aiCreditsLimit
)
const isCloudAccount = await this.isCloud() const isCloudAccount = await this.isCloud()
const errUserLimit = const errUserLimit =
isCloudAccount && isCloudAccount &&
@ -315,6 +335,8 @@ class LicensingStore extends BudiStore<LicensingState> {
userLimit, userLimit,
userLimitReached, userLimitReached,
errUserLimit, errUserLimit,
aiCreditsLimit,
aiCreditsExceeded,
} }
}) })
} }