-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates a /chat/api-keys route that allows the user to create and delete API keys
- Loading branch information
1 parent
7abd2ed
commit 1fa59ee
Showing
58 changed files
with
1,333 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/leapfrogai_ui/src/lib/components/modals/CopyApiKeyModal.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<script lang="ts"> | ||
import { formatKeyLong } from '$helpers/apiKeyHelpers.js'; | ||
import { Copy } from 'carbon-icons-svelte'; | ||
import { calculateDays, formatDate } from '$helpers/dates.js'; | ||
import { Button, Modal, TextInput } from 'carbon-components-svelte'; | ||
import type { APIKeyRow } from '$lib/types/apiKeys'; | ||
export let copyKeyModalOpen: boolean; | ||
export let handleCloseCopyKeyModal: () => void; | ||
export let handleCopyKey: () => void; | ||
export let createdKey: APIKeyRow | null; | ||
let saveKeyModalRef: HTMLDivElement; | ||
</script> | ||
|
||
<Modal | ||
bind:ref={saveKeyModalRef} | ||
bind:open={copyKeyModalOpen} | ||
preventCloseOnClickOutside | ||
modalHeading="Save secret key" | ||
primaryButtonText="Close" | ||
on:close={handleCloseCopyKeyModal} | ||
on:submit={handleCloseCopyKeyModal} | ||
> | ||
{#if createdKey} | ||
<div class="centered-spaced-container" style="flex-direction: column"> | ||
<p> | ||
Please store this secret key in a safe and accessible place. For security purposes, it | ||
cannot be viewed again through your LeapfrogAI account. If you lose it, you'll need to | ||
create a new one. | ||
</p> | ||
<div class="centered-spaced-lg-container" style="width: 100%"> | ||
<TextInput | ||
readonly | ||
labelText="Key" | ||
value={formatKeyLong(createdKey.api_key, saveKeyModalRef?.offsetWidth || 200)} | ||
/> | ||
<Button kind="tertiary" icon={Copy} size="small" on:click={handleCopyKey}>Copy</Button> | ||
</div> | ||
<div style="width: 100%"> | ||
<label for="saved-expiration" class:bx--label={true}>Expiration</label> | ||
<p id="saved-expiration"> | ||
{`${calculateDays(createdKey.created_at, createdKey.expires_at)} days - ${formatDate(new Date(createdKey.expires_at * 1000))}`} | ||
</p> | ||
</div> | ||
</div> | ||
{/if} | ||
</Modal> | ||
|
||
<style lang="scss"> | ||
.centered-spaced-container { | ||
display: flex; | ||
gap: layout.$spacing-06; | ||
align-items: center; | ||
} | ||
.centered-spaced-lg-container { | ||
display: flex; | ||
gap: layout.$spacing-07; | ||
align-items: center; | ||
:global(.bx--text-input__readonly-icon) { | ||
display: none; | ||
} | ||
} | ||
</style> |
65 changes: 65 additions & 0 deletions
65
src/leapfrogai_ui/src/lib/components/modals/CreateApiKeyModal.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<script lang="ts"> | ||
import { ContentSwitcher, Modal, Switch, TextInput } from 'carbon-components-svelte'; | ||
export let modalOpen: boolean; | ||
export let handleCancel: () => void; | ||
export let submit: () => void; | ||
export let name: string | undefined; | ||
export let invalidText: string | undefined; | ||
export let selectedExpirationIndex: number; | ||
export let selectedExpirationDate: number; | ||
</script> | ||
|
||
<Modal | ||
bind:open={modalOpen} | ||
preventCloseOnClickOutside | ||
modalHeading="Create new secret key" | ||
primaryButtonText="Create" | ||
secondaryButtonText="Cancel" | ||
hasForm | ||
on:click:button--secondary={handleCancel} | ||
on:close={handleCancel} | ||
on:submit={submit} | ||
> | ||
<div class="modal-inner-content"> | ||
<p style="width: 70%;"> | ||
This API key is linked to your user account and gives full access to it. Please be careful and | ||
keep it secret. | ||
</p> | ||
|
||
<TextInput | ||
id="name" | ||
name="name" | ||
labelText="Name" | ||
placeholder="Test Key" | ||
size="sm" | ||
autocomplete="off" | ||
bind:value={name} | ||
invalid={!!invalidText} | ||
{invalidText} | ||
/> | ||
<div> | ||
<label for="expiration" class:bx--label={true}>Expiration</label> | ||
<ContentSwitcher | ||
id="expiration" | ||
size="xl" | ||
style="width: 60%" | ||
bind:selectedIndex={selectedExpirationIndex} | ||
> | ||
<Switch text="7 Days" /> | ||
<Switch text="30 Days" /> | ||
<Switch text="60 Days" /> | ||
<Switch text="90 Days" /> | ||
</ContentSwitcher> | ||
</div> | ||
<input type="hidden" name="expires_at" value={selectedExpirationDate} /> | ||
</div> | ||
</Modal> | ||
|
||
<style lang="scss"> | ||
.modal-inner-content { | ||
display: flex; | ||
flex-direction: column; | ||
gap: layout.$spacing-06; | ||
} | ||
</style> |
24 changes: 24 additions & 0 deletions
24
src/leapfrogai_ui/src/lib/components/modals/DeleteApiKeyModal.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script lang="ts"> | ||
import { Modal } from 'carbon-components-svelte'; | ||
export let confirmDeleteModalOpen: boolean; | ||
export let keyNames: string; | ||
export let deleting: boolean; | ||
export let handleCancelConfirmDelete: () => void; | ||
export let handleDelete: () => void; | ||
</script> | ||
|
||
<Modal | ||
danger | ||
preventCloseOnClickOutside | ||
bind:open={confirmDeleteModalOpen} | ||
modalHeading={`Delete API ${keyNames.length > 0 ? 'Keys' : 'Key'}`} | ||
primaryButtonText="Delete" | ||
secondaryButtonText="Cancel" | ||
primaryButtonDisabled={deleting} | ||
on:click:button--secondary={() => handleCancelConfirmDelete()} | ||
on:close={() => handleCancelConfirmDelete()} | ||
on:submit={() => handleDelete()} | ||
> | ||
<p>Are you sure you want to delete <span style="font-weight: bold">{keyNames}</span>?</p> | ||
</Modal> |
8 changes: 4 additions & 4 deletions
8
...gai_ui/src/lib/constants/errorMessages.ts → ...gai_ui/src/lib/constants/toastMessages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
type ToastErrorText = { | ||
type ToastData = { | ||
title: string; | ||
subtitle?: string; | ||
}; | ||
|
||
export const ERROR_SAVING_MSG_TEXT: ToastErrorText = { | ||
export const ERROR_SAVING_MSG_TEXT: ToastData = { | ||
title: 'Error', | ||
subtitle: 'Error saving message. Please try again.' | ||
}; | ||
|
||
export const ERROR_GETTING_AI_RESPONSE_TEXT: ToastErrorText = { | ||
export const ERROR_GETTING_AI_RESPONSE_TEXT: ToastData = { | ||
title: 'Error', | ||
subtitle: 'Error getting AI Response' | ||
}; | ||
|
||
export const ERROR_GETTING_ASSISTANT_MSG_TEXT: ToastErrorText = { | ||
export const ERROR_GETTING_ASSISTANT_MSG_TEXT: ToastData = { | ||
title: 'Error', | ||
subtitle: 'Error getting Assistant Response' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Keys returned from the API list call should already be masked for security | ||
// We use this to mask the created key before the user copies it | ||
export const formatKeyShort = (key: string) => { | ||
const firstTwo = key.slice(0, 5); | ||
const lastFour = key.slice(-4); | ||
return `${firstTwo}...${lastFour}`; | ||
}; | ||
|
||
// Formats a key with several starts in the middle of it based on screen size | ||
// This is used to ensure the "copy key" text input is filled with text regardless of the | ||
// actual key length | ||
export const formatKeyLong = (key: string, width: number) => { | ||
const approxNumStars = (width * 0.65) / 14; | ||
const firstTwo = key.slice(0, 5); | ||
const lastFour = key.slice(-4); | ||
return `${firstTwo}${'*'.repeat(approxNumStars)}${lastFour}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.