Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
[Feat]: Implementing vault for managing your secrets (#466)
Aman Agarwal96e27c9d4a07
diff --git a/docs/images/docs-vault-create.png b/docs/images/docs-vault-create.pngnew file mode 100644index 00000000..9c73b9f7Binary files /dev/null and b/docs/images/docs-vault-create.png differdiff --git a/docs/images/docs-vault-list.png b/docs/images/docs-vault-list.pngnew file mode 100644index 00000000..3a0e9faeBinary files /dev/null and b/docs/images/docs-vault-list.png differdiff --git a/docs/latest/api-reference/openiapi.yml b/docs/latest/api-reference/endpoint/prompt-hub/prompt-hub.ymlsimilarity index 99%rename from docs/latest/api-reference/openiapi.ymlrename to docs/latest/api-reference/endpoint/prompt-hub/prompt-hub.ymlindex 62958e69..6aa58913 100644--- a/docs/latest/api-reference/openiapi.yml+++ b/docs/latest/api-reference/endpoint/prompt-hub/prompt-hub.yml@@ -83,7 +83,6 @@ paths:example: 'Hello John'security:- apiKeyAuth: []-components:securitySchemes:apiKeyAuth:diff --git a/docs/latest/api-reference/endpoint/vault/get.mdx b/docs/latest/api-reference/endpoint/vault/get.mdxnew file mode 100644index 00000000..6e5dbea9--- /dev/null+++ b/docs/latest/api-reference/endpoint/vault/get.mdx@@ -0,0 +1,4 @@+---+title: 'Get Secret(s)'+openapi: 'POST /api/vault/get-secrets'+---\ No newline at end of filediff --git a/docs/latest/api-reference/endpoint/vault/vault.yml b/docs/latest/api-reference/endpoint/vault/vault.ymlnew file mode 100644index 00000000..d26649c0--- /dev/null+++ b/docs/latest/api-reference/endpoint/vault/vault.yml@@ -0,0 +1,50 @@+openapi: 3.0.0+info:+ title: Get Vault API+ description: An API to fetch secret(s) by providing necessary parameters.+ version: 1.0.0+servers:+ - url: http://localhost:3000+paths:+ /api/vault/get-secrets:+ post:+ summary: Get Secret(s)+ description: Fetches secret(s) using the provided key or tags.+ operationId: getSecrets+ requestBody:+ required: true+ content:+ text/plain:+ schema:+ type: object+ properties:+ key:+ type: string+ example: OPENAI_API_KEY+ tags:+ type: array+ items:+ type: string+ example: ["openai"]+ responses:+ '200':+ description: Successfully retrieved secret(s).+ content:+ application/json:+ schema:+ type: object+ properties:+ err:+ type: object+ nullable: true+ res:+ type: object+ example: { "OPEN_API_KEY": "OPEN_API_VALUE" }+ security:+ - apiKeyAuth: []+components:+ securitySchemes:+ apiKeyAuth:+ type: http+ scheme: bearer+ bearerFormat: JWT\ No newline at end of filediff --git a/docs/latest/features/vault.mdx b/docs/latest/features/vault.mdxnew file mode 100644index 00000000..b53fcbb0--- /dev/null+++ b/docs/latest/features/vault.mdx@@ -0,0 +1,118 @@+---+title: 'Vault'+sidebarTitle: 'Vault'+---++Vault is designed to securely store and access sensitive information, such as API keys (e.g., `OPENAI_API_TOKEN`), eliminating the need to directly embed them in your code or environment variables. This centralized approach ensures that all developers can securely share and use the same API key without the necessity of distributing individual keys.++## Key Features++- **Secrets Management**: Seamlessly create, edit, and monitor the secrets associated with your applications.+- **Secure Access**: Retrieve secrets based on keys or tags, and safely integrate them into your Node.js or Python environments.++## Get Started++<Steps>+ <Step title="List of Secrets">+ Get a quick overview of all secrets created.+ 1. Navigate to the Vault in OpenLIT.+ 2. Explore the available secrets listed.+ <Frame>+ <img src="/images/docs-vault-list.png" />+ </Frame>+ </Step>+ <Step title="Create or Edit a secret">+ Build new secret with ease.+ 1. Click on **+ New** to create a new secret+ 2. In the secret form, add the secret key, secret value and add tags to easily manage and access the secrets via sdk.+ <Frame>+ <img src="/images/docs-vault-create.png" />+ </Frame>+ </Step>+ <Step title="Retrieve the secret(s)">+ <Steps>+ <Step title="Create an API Key">+ To authenticate your requests, you need an API key. Here's how you can create one:++ - Go to the OpenLIT.+ - Navigate to the **API Keys** page.+ - Click on **Create API Key**.+ - Enter a name for your API key.+ - Save the API key displayed. Ensure you store it securely as it will be used for authentication in the SDK.+ </Step>+ <Step title="Get secret(s) using the SDK">+ <Tabs>+ <Tab title="Python">+ Here's how you can fetch and set environment secret(s) in Python:++ ```python+ import openlit++ response = openlit.get_secrets(+ url="http://127.0.0.1:3000",+ api_key="_OPENLIT_API_KEY",+ should_set_env=True+ )++ print(response)+ ```++ ```shell Output+ {+ err: null,+ res: { ANTHROPIC_API_KEY: 'ANTHROPIC_API_VALUE', OPENAI_API_KEY: 'OPENAI_API_VALUE' }+ }+ ```+ ### SDK Parameters++ Below are the parameters for use with the SDK, formatted to indicate whether each is required or optional:++ | Parameter | Description |+ |-------------------|------------------------------------------------------------------------------------------------------------------------------------|+ | `url` | Sets the Openlit URL. Defaults to the `OPENLIT_URL` environment variable. |+ | `api_key` | Sets the OpenLIT API Key. Can also be provided via the `OPENLIT_API_KEY` environment variable. |+ | `key` | Sets the key to fetch a specific secret. Optional |+ | `should_set_env` | Boolean value that sets all the secrets as environment variables for the application. Optional |+ | `tags` | Sets the tags for fetching only the secrets that have the mentioned tags assigned. Optional |++ </Tab>+ <Tab title="Typescript">+ Here's how you can fetch and compile a prompt in Typescript/Javascript:++ ```typescript+ import Openlit from 'openlit'; // Import the Openlit SDK++ const response = await Openlit.getSecrets({+ shouldSetEnv: true, // Set the secrets as the environment variables for the application+ });++ console.log(response); // Print or process the fetched and compiled prompt+ ```++ ```shell Output+ {+ err: null,+ res: { ANTHROPIC_API_KEY: 'ANTHROPIC_API_VALUE', OPENAI_API_KEY: 'OPENAI_API_VALUE' }+ }+ ```+ ### SDK Parameters++ Below are the parameters for use with the SDK, formatted to indicate whether each is required or optional:++ | Parameter | Description |+ |------------------|------------------------------------------------------------------------------------------------------------------------------------|+ | `url` | Sets the Openlit URL. Defaults to the `OPENLIT_URL` environment variable or `http://127.0.0.1:3000` if not set. |+ | `apiKey` | Sets the OpenLIT API Key. Can also be provided via the `OPENLIT_API_KEY` environment variable. |+ | `key` | Sets the key to fetch a specific secret. Optional |+ | `tags` | Sets the tags for fetching only the secrets that have the mentioned tags assigned. Optional |+ | `shouldSetEnv` | Boolean value that sets all the secrets as environment variables for the application. Optional |++ </Tab>+ <Tab title="API">+ To retrieve the secret via an API request, Refer to the [**GET Secret(s)**](/latest/api-reference/endpoint/vault/get) API Reference Documentation.+ </Tab>+ </Tabs>+ </Step>+ </Steps>+ </Step>+</Steps>diff --git a/docs/mint.json b/docs/mint.jsonindex 3684dcc3..47ae36b7 100644--- a/docs/mint.json+++ b/docs/mint.json@@ -64,6 +64,7 @@"latest/features/gpu","latest/features/pricing","latest/features/prompt-hub",+ "latest/features/vault","latest/features/multiple-db"],"version": "latest"@@ -158,6 +159,13 @@"latest/api-reference/endpoint/prompt-hub/get"],"version": "latest"+ },+ {+ "group": "Vault",+ "pages": [+ "latest/api-reference/endpoint/vault/get"+ ],+ "version": "latest"}],"version": "latest"diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.tomlindex 223faed1..8a804429 100644--- a/sdk/python/pyproject.toml+++ b/sdk/python/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "openlit"-version = "1.25.1"+version = "1.26.0"description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"authors = ["OpenLIT"]repository = "https://github.com/openlit/openlit/tree/main/openlit/python"diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.pyindex aebae355..e9a26e42 100644--- a/sdk/python/src/openlit/__helpers.py+++ b/sdk/python/src/openlit/__helpers.py@@ -2,6 +2,7 @@"""This module has functions to calculate model costs based on tokens and to fetch pricing information."""+import osimport jsonimport loggingfrom urllib.parse import urlparse@@ -12,6 +13,19 @@ from opentelemetry.trace import Status, StatusCode# Set up logginglogger = logging.getLogger(__name__)+def get_env_variable(name, arg_value, error_message):+ """+ Retrieve an environment variable if the argument is not provided+ and raise an error if both are not set.+ """+ if arg_value is not None:+ return arg_value+ value = os.getenv(name)+ if not value:+ logging.error(error_message)+ raise RuntimeError(error_message)+ return value+def openai_tokens(text, model):"""Calculate the number of tokens a given text would take up for a specified model.diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.pyindex fab9a699..ece65a48 100644--- a/sdk/python/src/openlit/__init__.py+++ b/sdk/python/src/openlit/__init__.py@@ -20,7 +20,7 @@ from opentelemetry.trace import SpanKind, Status, StatusCode, Spanfrom openlit.semcov import SemanticConvetionfrom openlit.otel.tracing import setup_tracingfrom openlit.otel.metrics import setup_meter-from openlit.__helpers import fetch_pricing_info+from openlit.__helpers import fetch_pricing_info, get_env_variable# Instrumentors for various large language models.@@ -324,19 +324,6 @@ def get_prompt(url=None, name=None, api_key=None, prompt_id=None,Retrieve and returns the prompt from OpenLIT Prompt Hub"""- def get_env_variable(name, arg_value, error_message):- """- Retrieve an environment variable if the argument is not provided- and raise an error if both are not set.- """- if arg_value is not None:- return arg_value- value = os.getenv(name)- if not value:- logging.error(error_message)- raise RuntimeError(error_message)- return value-# Validate and set the base URLurl = get_env_variable('OPENLIT_URL',@@ -386,6 +373,63 @@ def get_prompt(url=None, name=None, api_key=None, prompt_id=None,print(f"Error fetching prompt: {error}")return None+def get_secrets(url=None, api_key=None, key=None, tags=None, should_set_env=None):+ """+ Retrieve & returns the secrets from OpenLIT Vault & sets all to env is should_set_env is True+ """++ # Validate and set the base URL+ url = get_env_variable(+ 'OPENLIT_URL',+ url,+ 'Missing OpenLIT URL: Provide as arg or set OPENLIT_URL env var.'+ )++ # Validate and set the API key+ api_key = get_env_variable(+ 'OPENLIT_API_KEY',+ api_key,+ 'Missing API key: Provide as arg or set OPENLIT_API_KEY env var.'+ )++ # Construct the API endpoint+ endpoint = url + "/api/vault/get-secrets"++ # Prepare the payload+ payload = {+ 'key': key,+ 'tags': tags,+ }++ # Remove None values from payload+ payload = {k: v for k, v in payload.items() if v is not None}++ # Prepare headers+ headers = {+ 'Authorization': f'Bearer {api_key}',+ 'Content-Type': 'application/json'+ }++ try:+ # Make the POST request to the API with headers+ response = requests.post(endpoint, json=payload, headers=headers, timeout=120)++ # Check if the response is successful+ response.raise_for_status()++ # Return the JSON response+ vault_response = response.json()++ res = vault_response.get('res', [])++ if should_set_env is True:+ for token, value in res.items():+ os.environ[token] = str(value)+ return vault_response+ except requests.RequestException as error:+ print(f"Error fetching secrets: {error}")+ return None+def trace(wrapped):"""Generates a telemetry wrapper for messages to collect metrics.diff --git a/sdk/typescript/src/features/base.ts b/sdk/typescript/src/features/base.tsnew file mode 100644index 00000000..58c40c62--- /dev/null+++ b/sdk/typescript/src/features/base.ts@@ -0,0 +1,7 @@+import PromptHub from './prompt-hub';+import Vault from './vault';++export default class BaseOpenlit {+ static getPrompts = PromptHub.getPrompts;+ static getSecrets = Vault.getSecrets;+}diff --git a/sdk/typescript/src/features/prompt-hub.ts b/sdk/typescript/src/features/prompt-hub.tsnew file mode 100644index 00000000..8c1b084d--- /dev/null+++ b/sdk/typescript/src/features/prompt-hub.ts@@ -0,0 +1,64 @@+import OpenlitConfig from '../config';+import { OPENLIT_URL } from '../constant';+import { PromptHubOptions } from '../types';++export default class PromptHub {+ static async getPrompts(options: PromptHubOptions) {+ const url = process.env.OPENLIT_URL || options.url || OPENLIT_URL;+ const apiKey = process.env.OPENLIT_API_KEY || options.apiKey || '';+ let metaProperties = {+ applicationName: OpenlitConfig.applicationName,+ environment: OpenlitConfig.environment,+ };+ if (+ options.metaProperties &&+ typeof options.metaProperties === 'object' &&+ !Array.isArray(options.metaProperties)+ ) {+ metaProperties = {+ ...metaProperties,+ ...options.metaProperties,+ };+ }++ try {+ return await fetch(`${url}/api/prompt/get-compiled`, {+ method: 'POST',+ body: JSON.stringify({+ name: options.name,+ version: options.version,+ shouldCompile: !!options.shouldCompile,+ variables: options.variables || {},+ id: options.promptId,+ metaProperties,+ source: 'ts-sdk',+ }),+ headers: {+ Authorization: `Bearer ${apiKey}`,+ },+ })+ .then(async (response) => {+ if (!response.ok) {+ return {+ err: `Openlit Error : HTTP error! Status: ${response.status}`,+ };+ }+ return response.json();+ })+ .then((resp: any) => {+ return resp;+ });+ } catch (e: any) {+ if (e && typeof e.toString === 'function') {+ return {+ err: `Openlit Error : ${e.toString()}`,+ data: null,+ };+ }++ return {+ err: `Openlit Error : ${e}`,+ };+ }+ }+}diff --git a/sdk/typescript/src/features/vault.ts b/sdk/typescript/src/features/vault.tsnew file mode 100644index 00000000..1b672efb--- /dev/null+++ b/sdk/typescript/src/features/vault.ts@@ -0,0 +1,54 @@+import { OPENLIT_URL } from '../constant';+import { VaultOptions } from '../types';++export default class Vault {+ static async getSecrets(options: VaultOptions) {+ const url = process.env.OPENLIT_URL || options.url || OPENLIT_URL;+ const apiKey = process.env.OPENLIT_API_KEY || options.apiKey || '';++ try {+ const vaultResponse = await fetch(`${url}/api/vault/get-secrets`, {+ method: 'POST',+ body: JSON.stringify({+ key: options.key,+ tags: options.tags,+ }),+ headers: {+ Authorization: `Bearer ${apiKey}`,+ },+ })+ .then(async (response) => {+ if (!response.ok) {+ return {+ err: `Openlit Error : HTTP error! Status: ${response.status}`,+ };+ }+ return response.json();+ })+ .then((resp: any) => {+ return resp;+ });++ const { res = {} } = vaultResponse;+ if (!!options.shouldSetEnv) {+ Object.entries(res).forEach(([key, value]: [string, unknown]) => {+ process.env[key] = value as string;+ });+ }++ return vaultResponse;+ } catch (e: any) {+ console.log(e);+ if (e && typeof e.toString === 'function') {+ return {+ err: `Openlit Error : ${e.toString()}`,+ data: null,+ };+ }++ return {+ err: `Openlit Error : ${e}`,+ };+ }+ }+}diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.tsindex e50e5398..cc62eaf2 100644--- a/sdk/typescript/src/index.ts+++ b/sdk/typescript/src/index.ts@@ -9,9 +9,9 @@ import { OpenlitOptions } from './types';import Tracing from './tracing';import { DEFAULT_APPLICATION_NAME, DEFAULT_ENVIRONMENT, SDK_NAME } from './constant';import { SpanExporter } from '@opentelemetry/sdk-trace-base';-import PromptHub from './prompt-hub';+import BaseOpenlit from './features/base';-export default class Openlit extends PromptHub {+export default class Openlit extends BaseOpenlit {static resource: Resource;static options: OpenlitOptions;static _sdk: NodeSDK;diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.tsindex c55f4468..2e6ec659 100644--- a/sdk/typescript/src/types.ts+++ b/sdk/typescript/src/types.ts@@ -47,13 +47,22 @@ export type SetupTracerOptions = OpenlitOptions & {resource: Resource;};-export type PromptHubOptions = {+export interface BaseOpenlitOptions {url?: string;apiKey?: string;+}++export interface PromptHubOptions extends BaseOpenlitOptions {name?: string;version?: string;shouldCompile?: boolean;variables?: Record<string, any>;promptId?: string;metaProperties?: Record<string, any>;-};+}++export interface VaultOptions extends BaseOpenlitOptions {+ key?: string;+ tags?: string[];+ shouldSetEnv?: boolean;+}diff --git a/src/client/package-lock.json b/src/client/package-lock.jsonindex f7e0598f..66a69b4f 100644--- a/src/client/package-lock.json+++ b/src/client/package-lock.json@@ -1,6 +1,6 @@{"name": "openlit",- "version": "1.10.1",+ "version": "1.11.0","lockfileVersion": 3,"requires": true,"packages": {diff --git a/src/client/package.json b/src/client/package.jsonindex 418d4389..f7d152f3 100644--- a/src/client/package.json+++ b/src/client/package.json@@ -1,6 +1,6 @@{"name": "openlit",- "version": "1.10.1",+ "version": "1.11.0","private": true,"scripts": {"dev": "next dev",diff --git a/src/client/src/app/(playground)/vault/page.tsx b/src/client/src/app/(playground)/vault/page.tsxnew file mode 100644index 00000000..7748ad73--- /dev/null+++ b/src/client/src/app/(playground)/vault/page.tsx@@ -0,0 +1,131 @@+"use client";+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";+import { useCallback, useEffect } from "react";+import { toast } from "sonner";+import { get } from "lodash";+import { format } from "date-fns";+import { useRootStore } from "@/store";+import { getPingStatus } from "@/selectors/database-config";+import TableData from "@/components/common/table-data";+import { EditIcon, TrashIcon } from "lucide-react";+import ConfirmationModal from "@/components/common/confirmation-modal";+import VaultHeader from "@/components/(playground)/vault/header";+import { useParams } from "next/navigation";+import SecretForm from "@/components/(playground)/vault/form";++const columns = [+ {+ key: "key",+ className: "col-span-3",+ header: "Key",+ },+ {+ key: "created_by",+ className: "col-span-3",+ header: "Created By",+ },+ {+ header: "Last Updated On",+ className: "col-span-3",+ render: (data: any) => {+ const updatedAt = get(data, "updated_at");+ return updatedAt ? format(updatedAt, "MMM do, y") : "-";+ },+ },+ {+ header: "Actions",+ className: "col-span-3 text-center justify-center flex",+ render: (+ data: any,+ extraFunction: {+ handleDelete: (p?: any) => void;+ successCallback: () => void;+ }+ ) => (+ <div className="flex justify-center gap-4">+ <SecretForm+ secretData={data}+ successCallback={extraFunction.successCallback}+ >+ <EditIcon className="w-4 cursor-pointer hover:text-primary" />+ </SecretForm>+ <ConfirmationModal+ handleYes={extraFunction?.handleDelete}+ title="Are you sure you want to delete this secret?"+ subtitle="Deleting secrets might result in breaking application if they are getting used. Please confirm before deleting it."+ params={{+ id: data.id,+ }}+ >+ <TrashIcon className="w-4 cursor-pointer hover:text-primary" />+ </ConfirmationModal>+ </div>+ ),+ },+];++export default function Vault() {+ const { data, fireRequest, isFetched, isLoading } = useFetchWrapper();+ const params = useParams();+ const { fireRequest: fireDeleteRequest, isLoading: isDeleting } =+ useFetchWrapper();+ const pingStatus = useRootStore(getPingStatus);+ const fetchData = useCallback(async () => {+ fireRequest({+ requestType: "POST",+ url: `/api/vault/get`,+ body: JSON.stringify(""),+ failureCb: (err?: string) => {+ toast.error(err || `Cannot connect to server!`, {+ id: "vault",+ });+ },+ });+ }, []);++ const deleteSecret = useCallback(+ async ({ id }: { id: string }) => {+ fireDeleteRequest({+ requestType: "DELETE",+ url: `/api/vault/${id}`,+ successCb: (data: any) => {+ toast.success(data, {+ id: "vault",+ });+ fetchData();+ },+ failureCb: (err?: string) => {+ toast.error(err || `Cannot connect to server!`, {+ id: "vault",+ });+ },+ });+ },+ [fetchData]+ );++ useEffect(() => {+ if (pingStatus !== "pending") {+ fetchData();+ }+ }, [pingStatus]);++ const updatedData = (data as any) || [];++ return (+ <div className="flex flex-col w-full h-full gap-4">+ <VaultHeader createNew={!params.id} successCallback={fetchData} />+ <TableData+ columns={columns}+ data={updatedData}+ isFetched={isFetched || pingStatus === "failure"}+ isLoading={isLoading || isDeleting}+ idKey="id"+ extraFunction={{+ handleDelete: deleteSecret,+ successCallback: fetchData,+ }}+ />+ </div>+ );+}diff --git a/src/client/src/app/api/api-key/[id]/route.ts b/src/client/src/app/api/api-key/[id]/route.tsindex b4f46464..e1b4d068 100644--- a/src/client/src/app/api/api-key/[id]/route.ts+++ b/src/client/src/app/api/api-key/[id]/route.ts@@ -1,4 +1,4 @@-import { deleteAPIKey } from "@/lib/platform/prompt/api-keys";+import { deleteAPIKey } from "@/lib/platform/api-keys";export async function DELETE(_: Request, context: any) {const { id } = context.params;diff --git a/src/client/src/app/api/api-key/route.ts b/src/client/src/app/api/api-key/route.tsindex eed9df94..91a5b86e 100644--- a/src/client/src/app/api/api-key/route.ts+++ b/src/client/src/app/api/api-key/route.ts@@ -1,4 +1,4 @@-import { generateAPIKey, getAllAPIKeys } from "@/lib/platform/prompt/api-keys";+import { generateAPIKey, getAllAPIKeys } from "@/lib/platform/api-keys";import asaw from "@/utils/asaw";export async function GET() {diff --git a/src/client/src/app/api/vault/[id]/route.ts b/src/client/src/app/api/vault/[id]/route.tsnew file mode 100644index 00000000..8f39432d--- /dev/null+++ b/src/client/src/app/api/vault/[id]/route.ts@@ -0,0 +1,13 @@+import { deleteSecret } from "@/lib/platform/vault";++export async function DELETE(_: Request, context: any) {+ const { id } = context.params;+ const [err, res] = await deleteSecret(id);+ if (err) {+ return Response.json(err, {+ status: 400,+ });+ }++ return Response.json(res);+}diff --git a/src/client/src/app/api/vault/get-secrets/route.ts b/src/client/src/app/api/vault/get-secrets/route.tsnew file mode 100644index 00000000..c94ed0b4--- /dev/null+++ b/src/client/src/app/api/vault/get-secrets/route.ts@@ -0,0 +1,43 @@+import getMessage from "@/constants/messages";+import { SecretGetFiltersWithApiKey } from "@/constants/vault";+import { getSecretsFromDatabaseId } from "@/lib/platform/vault";+import asaw from "@/utils/asaw";++export async function POST(request: Request) {+ const authorizationHeader = request.headers.get("Authorization") || "";+ let apiKey: string = "";+ if (authorizationHeader.startsWith("Bearer ")) {+ apiKey = authorizationHeader.replace(/^Bearer /, "");+ } else {+ return Response.json({+ err: getMessage().NO_API_KEY,+ res: null,+ });+ }++ const formData = await request.json();++ const filters: SecretGetFiltersWithApiKey = {+ apiKey,+ key: formData.key,+ tags: formData.tags,+ };++ const [err, data]: any = await asaw(getSecretsFromDatabaseId(filters));++ return Response.json({+ err,+ res: data,+ });+}++export async function OPTIONS() {+ return new Response(null, {+ status: 200,+ headers: {+ "Access-Control-Allow-Origin": "*",+ "Access-Control-Allow-Methods": "POST, OPTIONS",+ "Access-Control-Allow-Headers": "Content-Type, Authorization",+ },+ });+}diff --git a/src/client/src/app/api/vault/get/route.ts b/src/client/src/app/api/vault/get/route.tsnew file mode 100644index 00000000..31e5e1c0--- /dev/null+++ b/src/client/src/app/api/vault/get/route.ts@@ -0,0 +1,20 @@+import { SecretGetFilters } from "@/constants/vault";+import { getSecrets } from "@/lib/platform/vault";++export async function POST(request: Request) {+ const formData = await request.json();++ const filters: SecretGetFilters = {+ key: formData.key,+ tags: formData.tags,+ };++ const { err, data }: any = await getSecrets(filters);+ if (err) {+ return Response.json(err, {+ status: 400,+ });+ }++ return Response.json(data);+}diff --git a/src/client/src/app/api/vault/route.ts b/src/client/src/app/api/vault/route.tsnew file mode 100644index 00000000..827bc8ee--- /dev/null+++ b/src/client/src/app/api/vault/route.ts@@ -0,0 +1,44 @@+import { SecretInput } from "@/constants/vault";+import { upsertSecret } from "@/lib/platform/vault";+import asaw from "@/utils/asaw";++export async function POST(request: Request) {+ const formData = await request.json();++ const promptInput: SecretInput = {+ key: formData.key,+ value: formData.value,+ tags: formData.tags,+ };++ const [err, res]: any = await asaw(upsertSecret(promptInput));++ if (err) {+ return Response.json(err, {+ status: 400,+ });+ }++ return Response.json(res);+}++export async function PUT(request: Request) {+ const formData = await request.json();++ const secretInput: SecretInput = {+ id: formData.id,+ key: formData.key,+ value: formData.value,+ tags: formData.tags,+ };++ const [err, res]: any = await asaw(upsertSecret(secretInput));++ if (err) {+ return Response.json(err, {+ status: 400,+ });+ }++ return Response.json(res);+}diff --git a/src/client/src/clickhouse/migrations/create-vault-migration.ts b/src/client/src/clickhouse/migrations/create-vault-migration.tsnew file mode 100644index 00000000..d1b6c285--- /dev/null+++ b/src/client/src/clickhouse/migrations/create-vault-migration.ts@@ -0,0 +1,76 @@+import getMessage from "@/constants/messages";+import { getDBConfigById, getDBConfigByUser } from "@/lib/db-config";+import { dataCollector } from "@/lib/platform/common";+import { OPENLIT_VAULT_TABLE_NAME } from "@/lib/platform/vault/table-details";+import prisma from "@/lib/prisma";+import asaw from "@/utils/asaw";+import { consoleLog } from "@/utils/log";++const MIGRATION_ID = "create-vault-table";++export default async function CreateVaultMigration(databaseConfigId?: string) {+ let err, dbConfig;+ if (databaseConfigId) {+ [err, dbConfig] = await asaw(getDBConfigById({ id: databaseConfigId }));+ } else {+ [err, dbConfig] = await asaw(getDBConfigByUser(true));+ }++ if (err || !dbConfig?.id) throw err || getMessage().DATABASE_CONFIG_NOT_FOUND;++ const [, migrationExist] = await asaw(+ prisma.clickhouseMigrations.findFirst({+ where: {+ AND: {+ databaseConfigId: dbConfig.id as string,+ clickhouseMigrationId: MIGRATION_ID,+ },+ },+ })+ );++ if (migrationExist === 1) {+ return;+ }++ const queries = [+ `+ CREATE TABLE IF NOT EXISTS ${OPENLIT_VAULT_TABLE_NAME} (+ id UUID DEFAULT generateUUIDv4(), -- Unique ID for each prompt+ key String, -- Name of the secret+ value String DEFAULT '', -- Value for the secret key+ created_by String, -- Who created the prompt+ created_at DateTime DEFAULT now(), -- Timestamp for when the secret was created+ updated_by String, -- Who updated the version+ updated_at DateTime DEFAULT now(), -- When the version was updated+ tags String DEFAULT '[]', -- Tags for the version+ PRIMARY KEY id, -- Unique primary key constraint+ ) ENGINE = MergeTree()+ ORDER BY id;+ `,+ ];++ const queriesRun = await Promise.all(+ queries.map(async (query) => {+ const { err } = await dataCollector({ query });+ if (err) {+ console.log(`********* Migration Error : ${MIGRATION_ID} *********`);+ consoleLog(err);+ console.log(`********* Migration Error : ${MIGRATION_ID} *********`);+ }++ return { err };+ })+ );++ if (queriesRun.filter(({ err }) => !err).length === queries.length) {+ await asaw(+ prisma.clickhouseMigrations.create({+ data: {+ databaseConfigId: dbConfig.id,+ clickhouseMigrationId: MIGRATION_ID,+ },+ })+ );+ }+}diff --git a/src/client/src/clickhouse/migrations/index.ts b/src/client/src/clickhouse/migrations/index.tsindex 368d15c0..543e3cfe 100644--- a/src/client/src/clickhouse/migrations/index.ts+++ b/src/client/src/clickhouse/migrations/index.ts@@ -1,5 +1,7 @@-import createPromptMigration from "./create-prompt-migration";+import CreatePromptMigration from "./create-prompt-migration";+import CreateVaultMigration from "./create-vault-migration";export default async function migrations(databaseConfigId?: string) {- await createPromptMigration(databaseConfigId);+ await CreatePromptMigration(databaseConfigId);+ await CreateVaultMigration(databaseConfigId);}diff --git a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsxindex eaf12baa..bcf7a84d 100644--- a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx+++ b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx@@ -11,6 +11,7 @@ const ALLOWED_CONNECTIVITY_ALERT = ["/requests","/exceptions","/prompt-hub",+ "/vault",];export default function ClickhouseConnectivityWrapper() {diff --git a/src/client/src/components/(playground)/route-breadcrumbs.tsx b/src/client/src/components/(playground)/route-breadcrumbs.tsxindex 1564bb2c..f5d23848 100644--- a/src/client/src/components/(playground)/route-breadcrumbs.tsx+++ b/src/client/src/components/(playground)/route-breadcrumbs.tsx@@ -14,6 +14,7 @@ const PATH_TO_TITLE_MAP = {"/requests": "Request","/exceptions": "Exceptions","/prompt-hub": "Prompts",+ "/vault": "Vault","/openground": "Openground","/database-config": "Databases","/profile": "Profile",diff --git a/src/client/src/components/(playground)/sidebar.tsx b/src/client/src/components/(playground)/sidebar.tsxindex 96d0f246..db1dde30 100644--- a/src/client/src/components/(playground)/sidebar.tsx+++ b/src/client/src/components/(playground)/sidebar.tsx@@ -9,6 +9,7 @@ import {} from "@/components/ui/tooltip";import { Button, buttonVariants } from "@/components/ui/button";import {+ BookKey,BookText,Component,DatabaseBackup,@@ -53,6 +54,11 @@ const SIDEBAR_ITEMS: SidebarItemProps[] = [text: "Prompt Hub",link: "/prompt-hub",},+ {+ icon: <BookKey className={ICON_CLASSES} />,+ text: "Vault",+ link: "/vault",+ },{icon: <MonitorPlay className={ICON_CLASSES} />,text: "Openground",diff --git a/src/client/src/components/(playground)/vault/form.tsx b/src/client/src/components/(playground)/vault/form.tsxnew file mode 100644index 00000000..8828aa01--- /dev/null+++ b/src/client/src/components/(playground)/vault/form.tsx@@ -0,0 +1,146 @@+import FormBuilder, {+ FormBuilderEvent,+} from "@/components/common/form-builder";+import { FieldProps } from "@/components/common/form-field";+import {+ Dialog,+ DialogContent,+ DialogHeader,+ DialogTitle,+ DialogTrigger,+} from "@/components/ui/dialog";+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";+import { jsonParse } from "@/utils/json";+import { isEmpty } from "lodash";+import { KeyboardEvent, useCallback, useState } from "react";+import { toast } from "sonner";++export default function SecretForm({+ secretData,+ children,+ successCallback,+}: {+ secretData?: any;+ children: JSX.Element;+ successCallback?: () => void;+}) {+ const [isOpen, setIsOpen] = useState(false);+ const { fireRequest, isLoading } = useFetchWrapper();+ const upsertSecret: FormBuilderEvent = useCallback((event, formdata) => {+ event.preventDefault();+ toast.loading("Creating secret...", {+ id: "vault",+ });++ const payload: any = formdata;+ if (isEmpty(payload.value)) {+ delete payload.value;+ }+ payload.id = secretData?.id;++ fireRequest({+ body: JSON.stringify(payload),+ requestType: payload.id ? "PUT" : "POST",+ url: "/api/vault",+ successCb: () => {+ toast.success(+ `${payload.id ? "Updated" : "Created"} secret successfully!`,+ {+ id: "vault",+ }+ );+ setIsOpen(false);+ if (typeof successCallback === "function") {+ successCallback();+ }+ },+ failureCb: (err?: string) => {+ toast.error(+ err || `${payload.id ? "Updation" : "Creation"} of secret failed!`,+ {+ id: "vault",+ }+ );+ },+ });+ }, []);++ const defaultTags = secretData?.tags ? jsonParse(secretData?.tags) : [];+ const formFields: FieldProps[] = [+ {+ label: "Key",+ description: `Only capital alphabets, digits and _ are allowed. ${+ secretData?.id ? "Cannot update the key" : ""+ }`,+ inputKey: `${secretData?.id || "new"}-key`,+ fieldType: "INPUT",+ fieldTypeProps: {+ type: "text",+ name: "key",+ placeholder: "YOUR_SECRET_KEY",+ defaultValue: secretData?.key || "",+ readOnly: !!secretData?.key,+ disabled: !!secretData?.key,+ onKeyUp: (event: KeyboardEvent) => {+ const target = event.target as HTMLInputElement;+ let input = target.value;++ // Replace spaces with underscores+ input = input.toUpperCase().replace(/ /g, "_");++ // Allow only uppercase alphabet letters and underscores+ input = input.replace(/[^A-Z0-9_]/g, "");++ // Update the input field value+ target.value = input;+ },+ },+ },+ {+ label: "Value",+ description: "Add your secret values",+ inputKey: `${secretData?.id || "new"}-value`,+ fieldType: "INPUT",+ fieldTypeProps: {+ type: "password",+ name: "value",+ placeholder: "*****",+ defaultValue: secretData?.value,+ },+ },+ {+ label: "Tags",+ inputKey: `${secretData?.versionId || "new"}-tags`,+ fieldType: "TAGSINPUT",+ fieldTypeProps: {+ name: "tags",+ placeholder: "Add tags",+ defaultValue: defaultTags,+ },+ },+ ];++ return (+ <Dialog onOpenChange={setIsOpen} open={isOpen}>+ <DialogTrigger asChild>{children}</DialogTrigger>+ <DialogContent className="max-w-xl">+ <DialogHeader>+ <DialogTitle className="dark:text-stone-200 text-stone-800">+ {secretData?.id ? "Update a secret" : "Create a new secret"}+ </DialogTitle>+ </DialogHeader>+ <div className="flex items-center overflow-y-auto">+ <FormBuilder+ alignment="vertical"+ fields={formFields}+ isLoading={isLoading}+ onSubmit={upsertSecret}+ submitButtonText={`${+ secretData?.id ? "Update secret" : "Create secret"+ }`}+ />+ </div>+ </DialogContent>+ </Dialog>+ );+}diff --git a/src/client/src/components/(playground)/vault/header.tsx b/src/client/src/components/(playground)/vault/header.tsxnew file mode 100644index 00000000..c57c945b--- /dev/null+++ b/src/client/src/components/(playground)/vault/header.tsx@@ -0,0 +1,33 @@+import { Button } from "@/components/ui/button";+import SecretForm from "./form";+import RouteBreadcrumbs from "../route-breadcrumbs";+import { useRootStore } from "@/store";+import { getPingStatus } from "@/selectors/database-config";+import VaultUsage from "./usage";++export default function VaultHeader({+ createNew,+ successCallback,+}: {+ createNew?: boolean;+ successCallback?: () => void;+}) {+ const pingStatus = useRootStore(getPingStatus);++ return (+ <div className="flex w-full items-center">+ <RouteBreadcrumbs />+ {pingStatus === "success" && <VaultUsage />}+ {createNew && pingStatus === "success" && (+ <SecretForm successCallback={successCallback}>+ <Button+ variant="secondary"+ className="bg-primary hover:bg-primary dark:bg-primary dark:hover:bg-primary text-stone-100 dark:text-stone-100 px-8 h-auto py-1 rounded-sm"+ >+ + New+ </Button>+ </SecretForm>+ )}+ </div>+ );+}diff --git a/src/client/src/components/(playground)/vault/usage.tsx b/src/client/src/components/(playground)/vault/usage.tsxnew file mode 100644index 00000000..1835662b--- /dev/null+++ b/src/client/src/components/(playground)/vault/usage.tsx@@ -0,0 +1,165 @@+import CodeBlock from "@/components/common/code-block";+import TableData from "@/components/common/table-data";+import { Button } from "@/components/ui/button";+import {+ Card,+ CardContent,+ CardDescription,+ CardHeader,+ CardTitle,+} from "@/components/ui/card";+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogHeader,+ DialogTitle,+ DialogTrigger,+} from "@/components/ui/dialog";+import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";+import Link from "next/link";++function VaultUsageType() {+ return (+ <Tabs defaultValue="python" className="w-full flex flex-col gap-4">+ <div className="grid grid-cols-4">+ <TabsList className="col-span-2 grid w-auto grid-cols-2">+ <TabsTrigger value="python">Python</TabsTrigger>+ <TabsTrigger value="typescript">Typescript</TabsTrigger>+ </TabsList>+ <div className="col-start-4">+ <Link href="/api-keys">+ <Button variant="outline" className="w-full">+ Get API Key+ </Button>+ </Link>+ </div>+ </div>+ <TabsContent value="python">+ <Card>+ <CardHeader>+ <CardDescription>+ Copy the code and update the options and use Vault.+ </CardDescription>+ </CardHeader>+ <CardContent className="space-y-2">+ <CodeBlock+ className="text-xs"+ code={`+from openlit import Openlit # Import the Openlit SDK++response = openlit.get_prompt(+ name="prompt_name", # Fetch the prompt by name+ should_compile=True, # Compile the prompt with provided variables+ variables={+ "name": "John", # Pass variables for prompt compilation+ }+)++print(response) # Print or process the fetched and compiled prompt+ `}+ language="python"+ />+ </CardContent>+ </Card>+ </TabsContent>+ <TabsContent value="typescript">+ <Card>+ <CardHeader>+ <CardDescription>+ Copy the code and update the options and use Vault.+ </CardDescription>+ </CardHeader>+ <CardContent className="space-y-2">+ <CodeBlock+ className="text-xs"+ code={`+const response = await Openlit.getSecrets({+ shouldSetEnv: true,+}); // This sets all the stored secrets in the process.env object.+ `}+ language="typescript"+ />+ </CardContent>+ </Card>+ </TabsContent>+ </Tabs>+ );+}++const columns = [+ {+ key: "key",+ className: "col-span-4",+ header: "Key",+ },+ {+ key: "description",+ className: "col-span-8",+ header: "Description",+ },+];++const data = [+ {+ key: "url",+ description:+ "The sdk uses this key to look for openlit url else picks OPENLIT_URL from the environment variable and defaults to http://127.0.0.1:3000. This is optional.",+ },+ {+ key: "apiKey",+ description:+ "Sets the OpenLIT API Key. Can also be provided via the `OPENLIT_API_KEY` environment variable.",+ },+ {+ key: "key",+ description: "This is used to fetch secret by key. This is optional.",+ },+ {+ key: "tags",+ description:+ "This is used to fetch secrets matching the assigned tags. This is optional. ",+ },+ {+ key: "shouldSetEnv",+ description:+ "This key is used to set the secrets as environment variables. This is optional. ",+ },+];++export default function VaultUsage() {+ return (+ <Dialog>+ <DialogTrigger asChild>+ <Button+ variant="outline"+ className="px-8 h-auto py-1 rounded-sm mr-6 dark:text-stone-300"+ >+ How to use vault ?+ </Button>+ </DialogTrigger>+ <DialogContent className="max-w-4xl h-[80%]">+ <DialogHeader>+ <DialogTitle className="dark:text-stone-200 text-stone-800">+ Want to use vault in your application?+ </DialogTitle>+ <DialogDescription>+ Create your secrets in the Openlit UI and use them via our sdks.+ </DialogDescription>+ </DialogHeader>+ <div className="flex flex-col w-full overflow-y-auto py-2 gap-6">+ <VaultUsageType />+ <h2 className="text-stone-700 dark:text-stone-300">Options</h2>+ <TableData+ className="overflow-visible rounded-none"+ columns={columns}+ data={data}+ isFetched+ isLoading={false}+ idKey="key"+ />+ </div>+ </DialogContent>+ </Dialog>+ );+}diff --git a/src/client/src/components/common/form-builder.tsx b/src/client/src/components/common/form-builder.tsxindex cd8ee951..755d9989 100644--- a/src/client/src/components/common/form-builder.tsx+++ b/src/client/src/components/common/form-builder.tsx@@ -58,6 +58,8 @@ const FormBuilder = ({formElement[field.fieldTypeProps.name].value,];}+ } else if (field.fieldTypeProps.name) {+ acc[field.fieldTypeProps.name] = [];}} else if (field.fieldType === "KEYVALUE" && field.fieldTypeProps.name) {formElement@@ -85,7 +87,7 @@ const FormBuilder = ({return onSubmit(e, getFormData(e));}}- onKeyDown={(e) => (!(e.key === "Enter"))}+ onKeyDown={(e) => !(e.key === "Enter")}><Card className="w-full border-0 flex flex-col h-full">{heading && (diff --git a/src/client/src/constants/messages/en.ts b/src/client/src/constants/messages/en.tsindex 81ba561e..ab2f842b 100644--- a/src/client/src/constants/messages/en.ts+++ b/src/client/src/constants/messages/en.ts@@ -16,3 +16,10 @@ export const VERSION_NOT_CREATED = "Version cannot be created";export const VERSION_NOT_SAVED = "Version cannot be saved";export const VERSION_SAVED = "Prompt Version saved successfully!";export const DOWNLOAD_INFO_NOT_SAVED = "Download info cannot be saved!";++// Vault+export const SECRET_NAME_TAKEN = "Secret name is taken!";+export const SECRET_SAVED = "Secret saved successfully!";+export const SECRET_NOT_SAVED = "Secret cannot be saved";+export const SECRET_DELETED = "Secret deleted successfully!";+export const SECRET_NOT_DELETED = "Error deleting secret!";diff --git a/src/client/src/constants/route.ts b/src/client/src/constants/route.tsindex ef32ff72..199a9f68 100644--- a/src/client/src/constants/route.ts+++ b/src/client/src/constants/route.ts@@ -1,4 +1,5 @@export const DEFAULT_LOGGED_IN_ROUTE = "/getting-started";export const ALLOWED_OPENLIT_ROUTES_WITHOUT_TOKEN = ["/api/prompt/get-compiled",+ "/api/vault/get-secrets",];diff --git a/src/client/src/constants/vault.ts b/src/client/src/constants/vault.tsnew file mode 100644index 00000000..ff9b0c4f--- /dev/null+++ b/src/client/src/constants/vault.ts@@ -0,0 +1,16 @@+export interface SecretInput {+ id?: string;+ key: string;+ value: string;+ tags: string[];+}++export interface SecretGetFilters {+ databaseConfigId?: string;+ tags?: string[];+ key?: string;+}++export interface SecretGetFiltersWithApiKey extends SecretGetFilters {+ apiKey: string;+}diff --git a/src/client/src/helpers/vault.ts b/src/client/src/helpers/vault.tsnew file mode 100644index 00000000..72a8b812--- /dev/null+++ b/src/client/src/helpers/vault.ts@@ -0,0 +1,19 @@+import { SecretInput } from "@/constants/vault";++export function verifySecretInput(secretInput: SecretInput) {+ if (secretInput.key.length === 0) {+ return {+ success: false,+ err: "Key should be present!",+ };+ }++ return { success: true };+}++export function normalizeSecretDataForSDK(secretData: any[]) {+ return secretData.reduce((acc, secret) => {+ acc[secret.key] = secret.value;+ return acc;+ }, {});+}diff --git a/src/client/src/lib/platform/api-keys.ts b/src/client/src/lib/platform/api-keys.tsnew file mode 100644index 00000000..a8db0b21--- /dev/null+++ b/src/client/src/lib/platform/api-keys.ts@@ -0,0 +1,102 @@+import crypto from "crypto";+import { getDBConfigByUser } from "@/lib/db-config";+import asaw from "@/utils/asaw";+import prisma from "@/lib/prisma";+import { getCurrentUser } from "@/lib/session";+import { throwIfError } from "@/utils/error";+import getMessage from "@/constants/messages";++const APIKEY_PREFIX = "openlit-";++function createAPIKey() {+ // Generate 32 random bytes+ const key = crypto.randomBytes(32);++ // Convert the byte array to a Base64 string+ return `${APIKEY_PREFIX}${key.toString("base64")}`;+}++export async function generateAPIKey(name: string) {+ const user = await getCurrentUser();++ throwIfError(!user, getMessage().UNAUTHORIZED_USER);++ const [err, dbConfig] = await asaw(getDBConfigByUser(true));++ throwIfError(err, err);+ throwIfError(!dbConfig?.id, getMessage().DATABASE_CONFIG_NOT_FOUND);++ const apiKey = createAPIKey();++ await prisma.aPIKeys.create({+ data: {+ apiKey,+ name,+ databaseConfigId: dbConfig.id,+ createdByUserId: user!.id,+ },+ });++ return {+ apiKey,+ databaseConfigId: dbConfig.id,+ };+}++export async function getAPIKeyInfo({ apiKey }: { apiKey: string }) {+ return await asaw(+ prisma.aPIKeys.findFirst({+ where: {+ AND: [+ {+ apiKey,+ },+ {+ isDeleted: false,+ },+ ],+ },+ })+ );+}++export async function getAllAPIKeys() {+ const [err, dbConfig] = await asaw(getDBConfigByUser(true));+ throwIfError(err, err);++ throwIfError(!dbConfig?.id, getMessage().DATABASE_CONFIG_NOT_FOUND);++ const [, data] = await asaw(+ prisma.aPIKeys.findMany({+ where: {+ AND: [+ {+ databaseConfigId: dbConfig?.id,+ },+ { isDeleted: false },+ ],+ },+ select: {+ name: true,+ apiKey: true,+ createdAt: true,+ id: true,+ createdByUser: {+ select: {+ email: true,+ },+ },+ },+ orderBy: {+ createdAt: "desc",+ },+ })+ );+ return data;+}++export async function deleteAPIKey(id: string) {+ return await asaw(+ prisma.aPIKeys.update({ where: { id }, data: { isDeleted: true } })+ );+}diff --git a/src/client/src/lib/platform/prompt/compiled.ts b/src/client/src/lib/platform/prompt/compiled.tsindex 1e302f03..9e7fe3ab 100644--- a/src/client/src/lib/platform/prompt/compiled.ts+++ b/src/client/src/lib/platform/prompt/compiled.ts@@ -1,5 +1,5 @@import { PromptCompiledInput } from "@/constants/prompts";-import { getAPIKeyInfo } from "./api-keys";+import { getAPIKeyInfo } from "../api-keys";import { validatePromptCompiledInput } from "@/helpers/prompt";import { getSpecificPrompt } from ".";import { objectEntries } from "@/utils/object";diff --git a/src/client/src/lib/platform/vault/index.ts b/src/client/src/lib/platform/vault/index.tsnew file mode 100644index 00000000..81528390--- /dev/null+++ b/src/client/src/lib/platform/vault/index.ts@@ -0,0 +1,180 @@+import getMessage from "@/constants/messages";+import {+ SecretGetFilters,+ SecretGetFiltersWithApiKey,+ SecretInput,+} from "@/constants/vault";+import { normalizeSecretDataForSDK, verifySecretInput } from "@/helpers/vault";+import { getCurrentUser } from "@/lib/session";+import { throwIfError } from "@/utils/error";+import Sanitizer from "@/utils/sanitizer";+import { OPENLIT_VAULT_TABLE_NAME } from "./table-details";+import { dataCollector } from "../common";+import { jsonStringify } from "@/utils/json";+import { getAPIKeyInfo } from "../api-keys";++export async function getSecretByName({ key }: { key: string }) {+ const query = `+ SELECT * FROM ${OPENLIT_VAULT_TABLE_NAME} WHERE key='${Sanitizer.sanitizeValue(+ key+ )}';+ `;++ const { data }: { data?: any } = await dataCollector({ query });+ return data?.[0];+}++export async function checkNameValidity({ key }: { key: string }) {+ const data = await getSecretByName({ key });+ return { isValid: !data?.id };+}++export async function upsertSecret(secretInputParams: SecretInput) {+ const user = await getCurrentUser();++ throwIfError(!user, getMessage().UNAUTHORIZED_USER);++ const secretInput = Sanitizer.sanitizeObject(secretInputParams);++ const verifiedSecretObj = verifySecretInput(secretInput);+ throwIfError(!verifiedSecretObj.success, verifiedSecretObj.err!);++ if (!secretInputParams.id) {+ const { isValid } = await checkNameValidity({ key: secretInput.key });+ throwIfError(!isValid, getMessage().SECRET_NAME_TAKEN);+ }++ if (secretInputParams.id) {+ const updateValues = [+ `updated_by = '${user!.email}'`,+ secretInput.key && "key = '" + secretInput.key + "'",+ secretInput.value && "value = '" + secretInput.value + "'",+ secretInput.tags && "tags = '" + jsonStringify(secretInput.tags) + "'",+ ];+ const updateQuery = `+ ALTER TABLE ${OPENLIT_VAULT_TABLE_NAME}+ UPDATE+ ${updateValues.filter((e) => e).join(" , ")}+ WHERE id = '${secretInput.id}'`;+ const { err, data } = await dataCollector(+ {+ query: updateQuery,+ },+ "exec"+ );+ throwIfError(+ !!(err || !(data as { query_id: unknown })?.query_id),+ typeof err?.toString === "function"+ ? err.toString()+ : (err as string) || getMessage().SECRET_NOT_SAVED+ );++ return getMessage().SECRET_SAVED;+ } else {+ const { err } = await dataCollector(+ {+ table: OPENLIT_VAULT_TABLE_NAME,+ values: [+ {+ key: secretInput.key,+ value: secretInput.value,+ tags: secretInput.tags,+ created_by: user!.email,+ },+ ],+ },+ "insert"+ );++ throwIfError(+ !!err,+ typeof err?.toString === "function" ? err.toString() : (err as string)+ );+ }++ return {+ data: {},+ message: getMessage().SECRET_SAVED,+ };+}++export async function deleteSecret(secretIdParam: string) {+ const user = await getCurrentUser();++ throwIfError(!user, getMessage().UNAUTHORIZED_USER);+ const secretId = Sanitizer.sanitizeValue(secretIdParam);++ const { err } = await dataCollector(+ {+ query: `DELETE FROM ${OPENLIT_VAULT_TABLE_NAME} WHERE id = '${secretId}';`,+ },+ "exec"+ );++ if (err) {+ return [getMessage().SECRET_NOT_DELETED];+ }++ return [undefined, getMessage().SECRET_DELETED];+}++export async function getSecrets(+ filters: SecretGetFilters,+ { selectValue }: { selectValue?: boolean } = {}+) {+ if (!filters.databaseConfigId) {+ const user = await getCurrentUser();+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);+ }++ const filteredConditions: string[] = [+ filters.key ? "v.key = '" + filters.key + "'" : "",+ filters.tags && filters.tags.length > 0+ ? `hasAny(JSONExtractArrayRaw(v.tags), ['"` ++ filters.tags.join(`"' , '"`) ++ `"'])`+ : "",+ ].filter((e) => !!e);++ const query = `SELECT * ${!!selectValue ? "" : "EXCEPT value"}+ FROM+ ${OPENLIT_VAULT_TABLE_NAME} v+ ${+ filteredConditions.length > 0+ ? `WHERE ${filteredConditions.join(" AND ")}`+ : ""+ }+ ORDER BY+ v.created_at DESC;+ `;++ return await dataCollector({ query }, "query", filters.databaseConfigId);+}++export async function getSecretsFromDatabaseId(+ filters: SecretGetFiltersWithApiKey+) {+ const [err, apiInfo] = await getAPIKeyInfo({+ apiKey: filters.apiKey,+ });++ throwIfError(+ !!(err || !apiInfo?.databaseConfigId),+ err || getMessage().NO_API_KEY+ );++ const { err: secretErr, data: secretData } = await getSecrets(+ {+ ...filters,+ databaseConfigId: apiInfo.databaseConfigId,+ },+ { selectValue: true }+ );++ throwIfError(+ !!(secretErr || !secretData),+ (secretErr as any) || getMessage().NO_PROMPT+ );++ return normalizeSecretDataForSDK(secretData as any[]);+}diff --git a/src/client/src/lib/platform/vault/table-details.ts b/src/client/src/lib/platform/vault/table-details.tsnew file mode 100644index 00000000..e0d0fe8e--- /dev/null+++ b/src/client/src/lib/platform/vault/table-details.ts@@ -0,0 +1 @@+export const OPENLIT_VAULT_TABLE_NAME = "openlit_vault";diff --git a/src/client/src/middleware.ts b/src/client/src/middleware.tsindex 546d34ee..0714dffb 100644--- a/src/client/src/middleware.ts+++ b/src/client/src/middleware.ts@@ -73,6 +73,7 @@ export const config = {"/openground","/exceptions","/prompt-hub",+ "/vault","/api-keys",],};diff --git a/src/client/src/utils/api.ts b/src/client/src/utils/api.tsindex b62eb186..d6031876 100644--- a/src/client/src/utils/api.ts+++ b/src/client/src/utils/api.ts@@ -1,6 +1,6 @@type GET_DATA = {body?: string;- method?: "GET" | "POST";+ method?: "GET" | "POST" | "PUT";url: string;};diff --git a/src/client/src/utils/hooks/useFetchWrapper.ts b/src/client/src/utils/hooks/useFetchWrapper.tsindex 7d234a94..0e28a804 100644--- a/src/client/src/utils/hooks/useFetchWrapper.ts+++ b/src/client/src/utils/hooks/useFetchWrapper.ts@@ -6,7 +6,7 @@ type useFetchWrapperProps = {body?: string;failureCb?: (s?: string) => void;url: string;- requestType: "GET" | "POST" | "DELETE";+ requestType: "GET" | "POST" | "DELETE" | "PUT";responseDataKey?: string;successCb?: (res?: any) => void;};@@ -30,7 +30,11 @@ export default function useFetchWrapper() {setError(null);try {let response;- if (requestType === "GET" || requestType === "POST") {+ if (+ requestType === "GET" ||+ requestType === "POST" ||+ requestType === "PUT"+ ) {response = await getData({body,url,