Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Prompt hub to manage prompts (#442)

Aman Agarwal9ffeb7c69771
diff --git a/docker-compose.yml b/docker-compose.yml
index 00ffd34f..9779baab 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,5 +1,3 @@
-version: '3.8'
-
services:
clickhouse:
image: clickhouse/clickhouse-server:24.2.2
diff --git a/docs/images/docs-prompt-hub-create.png b/docs/images/docs-prompt-hub-create.png
new file mode 100644
index 00000000..2f370603
Binary files /dev/null and b/docs/images/docs-prompt-hub-create.png differ
diff --git a/docs/images/docs-prompt-hub-details.png b/docs/images/docs-prompt-hub-details.png
new file mode 100644
index 00000000..899721fd
Binary files /dev/null and b/docs/images/docs-prompt-hub-details.png differ
diff --git a/docs/images/docs-prompt-hub-list.png b/docs/images/docs-prompt-hub-list.png
new file mode 100644
index 00000000..98f6f82e
Binary files /dev/null and b/docs/images/docs-prompt-hub-list.png differ
diff --git a/docs/latest/api-reference/endpoint/connections/create.mdx b/docs/latest/api-reference/endpoint/connections/create.mdx
deleted file mode 100644
index bf65b9de..00000000
--- a/docs/latest/api-reference/endpoint/connections/create.mdx
+++ /dev/null
@@ -1,4 +0,0 @@
----
-title: 'Create Connection'
-openapi: 'POST /api/connections'
----
\ No newline at end of file
diff --git a/docs/latest/api-reference/endpoint/connections/delete.mdx b/docs/latest/api-reference/endpoint/connections/delete.mdx
deleted file mode 100644
index db7b942d..00000000
--- a/docs/latest/api-reference/endpoint/connections/delete.mdx
+++ /dev/null
@@ -1,4 +0,0 @@
----
-title: 'Delete Connection'
-openapi: 'DELETE /api/connections'
----
\ No newline at end of file
diff --git a/docs/latest/api-reference/endpoint/connections/get.mdx b/docs/latest/api-reference/endpoint/connections/get.mdx
deleted file mode 100644
index fbc924fc..00000000
--- a/docs/latest/api-reference/endpoint/connections/get.mdx
+++ /dev/null
@@ -1,4 +0,0 @@
----
-title: 'Get Connection'
-openapi: 'GET /api/connections'
----
\ No newline at end of file
diff --git a/docs/latest/api-reference/endpoint/prompt-hub/get.mdx b/docs/latest/api-reference/endpoint/prompt-hub/get.mdx
new file mode 100644
index 00000000..0a4f5815
--- /dev/null
+++ b/docs/latest/api-reference/endpoint/prompt-hub/get.mdx
@@ -0,0 +1,4 @@
+---
+title: 'Get Prompt'
+openapi: 'POST /api/prompt/get-compiled'
+---
\ No newline at end of file
diff --git a/docs/latest/api-reference/introduction.mdx b/docs/latest/api-reference/introduction.mdx
index fb4d34fb..d8bb54be 100644
--- a/docs/latest/api-reference/introduction.mdx
+++ b/docs/latest/api-reference/introduction.mdx
@@ -4,15 +4,9 @@ description: 'OpenAPI specification for API Endpoints in OpenLIT'
---
<CardGroup cols={2}>
<Card
- title="API Keys"
- icon="key"
- href="/latest/api-reference/endpoint/api-keys/create"
- >
- </Card>
- <Card
- title="Connections API"
+ title="Prompt Hub API"
icon="link"
- href="/latest/api-reference/endpoint/connections/create"
+ href="/latest/api-reference/endpoint/prompt-hub/get"
>
</Card>
</CardGroup>
@@ -21,12 +15,6 @@ description: 'OpenAPI specification for API Endpoints in OpenLIT'
All API endpoints are authenticated using Authetication Header.
-```json
-Authorization: <api-key>
-```
-
-<Info>
-For your initial API call to `/api/keys` endpoint, Authorization header can be set to "".
-
-Store the provided API key securely; it will be required to pass the generated and a valid API Key in Authorization header for subsequent API calls.
-</Info>
\ No newline at end of file
+```yaml
+Authorization: Bearer <api-key>
+```
\ No newline at end of file
diff --git a/docs/latest/api-reference/openiapi.yml b/docs/latest/api-reference/openiapi.yml
index 8771c604..62958e69 100644
--- a/docs/latest/api-reference/openiapi.yml
+++ b/docs/latest/api-reference/openiapi.yml
@@ -1,216 +1,92 @@
openapi: 3.0.0
info:
- title: API Keys Service
- description: This service manages API keys through GET, POST, and DELETE operations.
- version: "0.0.2"
-
+ title: Get Compiled Prompt API
+ description: An API to fetch compiled prompts by providing necessary parameters.
+ version: 1.0.0
servers:
- - url: https://127.0.0.1:9044/
-
+ - url: http://localhost:3000
paths:
- /api/connections:
+ /api/prompt/get-compiled:
post:
- summary: Create a new connection
- operationId: createConnection
- tags:
- - Connection
- security:
- - ApiKeyAuth: []
- requestBody:
- required: true
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/ConnectionRequest'
- responses:
- '201':
- description: Connection created
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/json201Response'
- delete:
- summary: Delete a connection
- operationId: deleteConnection
- tags:
- - Connection
- security:
- - ApiKeyAuth: []
- responses:
- '200':
- description: Connection deleted
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/json201Response'
- get:
- summary: Get Connections details
- operationId: getConnection
- tags:
- - Connection
- security:
- - ApiKeyAuth: []
- responses:
- '200':
- description: Connection details retrieved successfully
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/ConnectionDetailsResponse'
- '404':
- description: Unauthorized request
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/json401Response'
- /api/keys:
- get:
- summary: Get a list of API keys
- operationId: getApiKeys
- tags:
- - API Key
- security:
- - ApiKeyAuth: []
+ summary: Get Compiled Prompt
+ description: Fetches a compiled prompt using the provided prompt ID, version, and variables.
+ operationId: getCompiledPrompt
requestBody:
required: true
content:
- application/json:
+ text/plain:
schema:
- $ref: '#/components/schemas/APIKeyRequest'
+ type: object
+ properties:
+ name:
+ type: string
+ example: tester
+ promptId:
+ type: string
+ format: uuid
+ example: a7a55e48-1588-44ee-99e2-d9de5c026238
+ version:
+ type: string
+ example: '1.0.0'
+ metaProperties:
+ type: object
+ additionalProperties: true
+ compile:
+ type: boolean
+ example: true
+ variables:
+ type: object
+ properties:
+ name:
+ type: string
+ example: John
responses:
'200':
- description: A list of API keys
+ description: Successfully retrieved compiled prompt.
content:
application/json:
schema:
- $ref: '#/components/schemas/json201Response'
- post:
- summary: Create a new API key
- operationId: createApiKey
- tags:
- - API Key
+ type: object
+ properties:
+ err:
+ type: object
+ nullable: true
+ res:
+ type: object
+ properties:
+ promptId:
+ type: string
+ format: uuid
+ example: a7a55e48-1588-44ee-99e2-d9de5c026238
+ versionId:
+ type: string
+ format: uuid
+ example: 2d44f89a-2222-498d-869b-795296496ca3
+ name:
+ type: string
+ example: tester
+ version:
+ type: string
+ example: '1.0.0'
+ tags:
+ type: array
+ items:
+ type: string
+ metaProperties:
+ type: object
+ additionalProperties: true
+ prompt:
+ type: string
+ example: 'Hello {{name}}'
+ compiledPrompt:
+ type: string
+ example: 'Hello John'
security:
- - ApiKeyAuth: []
- requestBody:
- required: true
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/APIKeyRequest'
- responses:
- '201':
- description: API key created
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/json201Response'
- delete:
- summary: Delete an API key
- operationId: deleteApiKey
- tags:
- - API Key
- security:
- - ApiKeyAuth: []
- requestBody:
- required: true
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/APIKeyRequest'
- responses:
- '200':
- description: API key deleted
- content:
- application/json:
- schema:
- $ref: '#/components/schemas/json201Response'
+ - apiKeyAuth: []
components:
- schemas:
- APIKeyRequest:
- type: object
- required:
- - name
- properties:
- name:
- type: string
- description: Name of the API key to be created or deleted. Example - `BIGDADDY`
- ConnectionRequest:
- type: object
- required:
- - Platform
- - MetricsUrl
- - LogsUrl
- - MetricsUsername
- - LogsUsername
- - ApiKey
- properties:
- Platform:
- type: string
- description: Name of the Observability Platform. Can be one of the following - `grafana`, `newrelic`, `datadog`, `dynatrace` and `signoz`
- MetricsUrl:
- type: string
- description: URL of the metrics endpoint of the Observability Platform. Example - `https://api.datadoghq.com/api/v1/query`
- LogsUrl:
- type: string
- description: URL of the logs endpoint of the Observability Platform. Example - `https://api.datadoghq.com/api/v1/query`
- MetricsUsername:
- type: string
- description: Username for the metrics endpoint of the Observability Platform. Only required when Platform is passed as `grafana`.
- LogsUsername:
- type: string
- description: Username for the logs endpoint of the Observability Platform. Only required when Platform is passed as `grafana`.
- ApiKey:
- type: string
- description: API Key for the Observability Platform. Example - `glc*******`
- ConnectionDetailsResponse:
- type: object
- properties:
- connections:
- type: object
- properties:
- apiKey:
- type: string
- created_at:
- type: string
- format: date-time
- logsUrl:
- type: string
- logsUsername:
- type: string
- metricsUrl:
- type: string
- metricsUsername:
- type: string
- platform:
- type: string
- status:
- type: integer
- example: 200
- json201Response:
- type: object
- required:
- - status
- - message
- properties:
- status:
- type: integer
- message:
- type: string
-
- json401Response:
- type: object
- required:
- - status
- - message
- properties:
- status:
- type: integer
- message:
- type: string
-
securitySchemes:
- ApiKeyAuth:
- type: apiKey
- in: header
- name: Authorization
\ No newline at end of file
+ apiKeyAuth:
+ type: http
+ scheme: bearer
+ bearerFormat: JWT
\ No newline at end of file
diff --git a/docs/latest/features/prompt-hub.mdx b/docs/latest/features/prompt-hub.mdx
new file mode 100644
index 00000000..af552e0f
--- /dev/null
+++ b/docs/latest/features/prompt-hub.mdx
@@ -0,0 +1,158 @@
+---
+title: 'Prompt Hub'
+sidebarTitle: 'Prompt Hub'
+---
+
+The Prompt-Hub allows you to manage prompts, fetch specific versions of the prompt, and compile prompts with variables.
+
+## Key Features
+
+- **Prompt Management**: Create, edit, and track different versions of your prompts.
+- **Versioning**: Supports major, minor, and patch updates for clear version management.
+- **Dynamic Variables**: Use `{{variableName}}` placeholders that are dynamically replaced at runtime.
+- **Statistics**: View download stats and version history directly in the UI.
+
+
+## Get Started
+
+<Steps>
+ <Step title="List of Prompts">
+ Get a quick overview of all prompts created.
+ 1. Navigate to the Prompt Hub in OpenLIT.
+ 2. Explore the available prompts listed.
+ <Frame>
+ <img src="/images/docs-prompt-hub-list.png" />
+ </Frame>
+ </Step>
+ <Step title="Create or Edit a Prompt">
+ Build new prompts or edit existing ones with ease.
+ 1. Click on **+ New** to create a new prompt
+ 2. In the prompt editor, add the prompt name, Meta Properties and use placeholders like `{{variableName}}` to represent dynamic data that will be substituted when the prompt is compiled.
+ 3. Specify major, minor, or patch updates for versioning as you create or modify prompts.
+ <Frame>
+ <img src="/images/docs-prompt-hub-create.png" />
+ </Frame>
+ </Step>
+ <Step title="View Prompt Details" stepNumber=" ">
+ Once the prompt is creaetd, You can see information about the prompt along with details on all past versions.
+ <Frame>
+ <img src="/images/docs-prompt-hub-details.png" />
+ </Frame>
+ </Step>
+ <Step title="Retrieve the Prompt">
+ <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 UI.
+ - 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 prompt using the SDK">
+ <Tabs>
+ <Tab title="Python">
+ Here's how you can fetch and compile a prompt in Python:
+
+ ```python
+ import openlit
+
+ response = openlit.get_prompt(
+ url="http://127.0.0.1:3000",
+ api_key="_OPENLIT_API_KEY",
+ name="prompt_name",
+ should_compile=True,
+ variables={
+ "name": "John",
+ },
+ )
+
+ print(response)
+ ```
+
+ ```shell Output
+ {
+ err: null,
+ res: {
+ promptId: '88c4cbcd-87f9-4957-a37b-b41066e17471',
+ name: 'prompt_name',
+ version: '3.3.3',
+ tags: [ 'user', 'greeting' ],
+ metaProperties: { model: 'gpt4' },
+ prompt: 'Hello {{name}}, how are you today?',
+ compiledPrompt: 'Hello John, how are you today?'
+ }
+ }
+ ```
+ ### 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. |
+ | `name` | Sets the name to fetch a unique prompt. Use this or `prompt_id`. |
+ | `prompt_id` | Sets the ID to fetch a unique prompt. Use this or `name`. Optional |
+ | `version` | Set to `True` to get the prompt with variable substitution.. Optional |
+ | `shouldCompile` | Boolean value that compiles the prompt using the provided variables. Optional |
+ | `variables` | Sets the variables for prompt compilation. Optional |
+ | `meta_properties` | Sets the meta-properties for storing in the prompt's access history metadata. 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.getPrompts({
+ name: "prompt_name", // Fetch the prompt by name
+ shouldCompile: true, // Compile the prompt with provided variables
+ variables: {
+ name: "John", // Pass variables for prompt compilation
+ }
+ });
+
+ console.log(response); // Print or process the fetched and compiled prompt
+ ```
+
+ ```shell Output
+ {
+ err: null,
+ res: {
+ promptId: '88c4cbcd-87f9-4957-a37b-b41066e17471',
+ name: 'prompt_name',
+ version: '3.3.3',
+ tags: [ 'user', 'greeting' ],
+ metaProperties: { model: 'gpt4' },
+ prompt: 'Hello {{name}}, how are you today?',
+ compiledPrompt: 'Hello John, how are you today?'
+ }
+ }
+ ```
+ ### 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. |
+ | `name` | Sets the name to fetch a unique prompt. Use this or `promptId`. |
+ | `promptId` | Sets the ID to fetch a unique prompt. Use this or `name`. Optional |
+ | `version` | Sets the version to retrieve a specific prompt. Optional |
+ | `shouldCompile` | Boolean value that compiles the prompt using the provided variables. Optional |
+ | `variables` | Sets the variables for prompt compilation. Optional |
+ | `metaProperties` | Sets the meta-properties for storing in the prompt's access history metadata. |
+
+ </Tab>
+ <Tab title="API">
+ To retrieve the prompt via an API request, Refer to the [**GET Prompt**](/latest/api-reference/endpoint/prompt-hub/get) API Reference Documentation.
+ </Tab>
+ </Tabs>
+ </Step>
+ </Steps>
+ </Step>
+</Steps>
diff --git a/docs/mint.json b/docs/mint.json
index 4a8ffd28..3684dcc3 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -56,6 +56,18 @@
],
"version": "latest"
},
+ {
+ "group": "Features",
+ "pages": [
+ "latest/features/tracing",
+ "latest/features/metrics",
+ "latest/features/gpu",
+ "latest/features/pricing",
+ "latest/features/prompt-hub",
+ "latest/features/multiple-db"
+ ],
+ "version": "latest"
+ },
{
"group": "Integrations",
"version": "latest",
@@ -115,17 +127,6 @@
}
]
},
- {
- "group": "Features",
- "pages": [
- "latest/features/tracing",
- "latest/features/metrics",
- "latest/features/gpu",
- "latest/features/pricing",
- "latest/features/multiple-db"
- ],
- "version": "latest"
- },
{
"group": "Connections",
"pages": [
@@ -146,6 +147,20 @@
"latest/connections/murnitur"
],
"version": "latest"
+ },
+ {
+ "group": "API Reference",
+ "pages": [
+ "latest/api-reference/introduction",
+ {
+ "group": "Prompt Hub",
+ "pages": [
+ "latest/api-reference/endpoint/prompt-hub/get"
+ ],
+ "version": "latest"
+ }
+ ],
+ "version": "latest"
}
],
"analytics": {
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 5e970697..6e88a412 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.24.1"
+version = "1.25.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/__init__.py b/sdk/python/src/openlit/__init__.py
index 0d94ab31..fab9a699 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -7,9 +7,11 @@ large language models (LLMs).
from typing import Dict
import logging
+import os
from importlib.util import find_spec
from functools import wraps
from contextlib import contextmanager
+import requests
# Import internal modules for setting up tracing and fetching pricing info.
@@ -316,6 +318,73 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
except Exception as e:
logger.error("Error during openLIT initialization: %s", e)
+def get_prompt(url=None, name=None, api_key=None, prompt_id=None,
+ version=None, should_compile=None, variables=None, meta_properties=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 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/prompt/get-compiled"
+
+ # Prepare the payload
+ payload = {
+ 'name': name,
+ 'promptId': prompt_id,
+ 'version': version,
+ 'shouldCompile': should_compile,
+ 'variables': variables,
+ 'metaProperties': meta_properties
+ }
+
+ # 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
+ return response.json()
+ except requests.RequestException as error:
+ print(f"Error fetching prompt: {error}")
+ return None
def trace(wrapped):
"""
diff --git a/sdk/typescript/package-lock.json b/sdk/typescript/package-lock.json
index 579b65ae..30f27c08 100644
--- a/sdk/typescript/package-lock.json
+++ b/sdk/typescript/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "openlit",
- "version": "1.1.0",
+ "version": "1.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 99f25258..3291600e 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -1,6 +1,6 @@
{
"name": "openlit",
- "version": "1.1.0",
+ "version": "1.2.0",
"homepage": "https://github.com/openlit/openlit#readme",
"bugs": {
"url": "https://github.com/openlit/openlit/issues",
diff --git a/sdk/typescript/src/constant.ts b/sdk/typescript/src/constant.ts
index 46dfbc7f..5a26eb8f 100644
--- a/sdk/typescript/src/constant.ts
+++ b/sdk/typescript/src/constant.ts
@@ -1,5 +1,6 @@
-export const SDK_NAME = "openlit";
-export const DEFAULT_ENVIRONMENT = "default";
-export const DEFAULT_APPLICATION_NAME = "default";
-export const INSTRUMENTATION_PREFIX = "@openlit";
-export const TELEMETRY_SDK_NAME = 'opentelemetry';
\ No newline at end of file
+export const SDK_NAME = 'openlit';
+export const DEFAULT_ENVIRONMENT = 'default';
+export const DEFAULT_APPLICATION_NAME = 'default';
+export const INSTRUMENTATION_PREFIX = '@openlit';
+export const TELEMETRY_SDK_NAME = 'opentelemetry';
+export const OPENLIT_URL = 'http://127.0.0.1:3000';
diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts
index c8320236..e50e5398 100644
--- a/sdk/typescript/src/index.ts
+++ b/sdk/typescript/src/index.ts
@@ -9,8 +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';
-export default class Openlit {
+export default class Openlit extends PromptHub {
static resource: Resource;
static options: OpenlitOptions;
static _sdk: NodeSDK;
diff --git a/sdk/typescript/src/prompt-hub.ts b/sdk/typescript/src/prompt-hub.ts
new file mode 100644
index 00000000..8ce05a1d
--- /dev/null
+++ b/sdk/typescript/src/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/types.ts b/sdk/typescript/src/types.ts
index 1afb895c..c55f4468 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -46,3 +46,14 @@ export type OpenlitOptions = {
export type SetupTracerOptions = OpenlitOptions & {
resource: Resource;
};
+
+export type PromptHubOptions = {
+ url?: string;
+ apiKey?: string;
+ name?: string;
+ version?: string;
+ shouldCompile?: boolean;
+ variables?: Record<string, any>;
+ promptId?: string;
+ metaProperties?: Record<string, any>;
+};
diff --git a/src/client/package-lock.json b/src/client/package-lock.json
index 167bd1ec..0d867d05 100644
--- a/src/client/package-lock.json
+++ b/src/client/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "openlit",
- "version": "0.1.0",
+ "version": "1.9.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "openlit",
- "version": "0.1.0",
+ "version": "1.9.1",
"dependencies": {
"@anthropic-ai/sdk": "^0.21.1",
"@clickhouse/client": "^1.1.0",
@@ -22,11 +22,12 @@
"@radix-ui/react-hover-card": "^1.0.7",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-popover": "^1.0.7",
+ "@radix-ui/react-radio-group": "^1.2.0",
"@radix-ui/react-scroll-area": "^1.0.5",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slider": "^1.1.2",
- "@radix-ui/react-slot": "^1.0.2",
+ "@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.0.3",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
@@ -54,6 +55,7 @@
"react-syntax-highlighter": "^15.5.0",
"recharts": "^2.12.7",
"sonner": "^1.5.0",
+ "sqlstring": "^2.3.3",
"tailwind-merge": "^2.2.2",
"tailwindcss-animate": "^1.0.7",
"tiny-cookie": "^2.5.1",
@@ -65,6 +67,7 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.13",
+ "@types/sqlstring": "^2.3.2",
"autoprefixer": "^10.0.1",
"eslint": "^8.57.0",
"eslint-config-next": "^14.1.4",
@@ -2195,23 +2198,6 @@
}
}
},
- "node_modules/@radix-ui/react-checkbox/node_modules/@radix-ui/react-slot": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.1.0.tgz",
- "integrity": "sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==",
- "dependencies": {
- "@radix-ui/react-compose-refs": "1.1.0"
- },
- "peerDependencies": {
- "@types/react": "*",
- "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
- },
- "peerDependenciesMeta": {
- "@types/react": {
- "optional": true
- }
- }
- },
"node_modules/@radix-ui/react-checkbox/node_modules/@radix-ui/react-use-callback-ref": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.0.tgz",
@@ -2412,23 +2398,6 @@
}
}
},
- "node_modules/@radix-ui/react-collapsible/node_modules/@radix-ui/react-slot": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.1.0.tgz",
- "integrity": "sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==",
- "dependencies": {
- "@radix-ui/react-compose-refs": "1.1.0"
- },
- "peerDependencies": {
- "@types/react": "*",
- "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
- },
- "peerDependenciesMeta": {
- "@types/react": {
- "optional": true
- }
- }
- },
"node_modules/@radix-ui/react-collapsible/node_modules/@radix-ui/react-use-callback-ref": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.0.tgz",
@@ -2500,6 +2469,24 @@
}
}
},
+ "node_modules/@radix-ui/react-collection/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-compose-refs": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@radix-ui/react-compose-refs/-/react-compose-refs-1.0.1.tgz",
@@ -2570,6 +2557,24 @@
}
}
},
+ "node_modules/@radix-ui/react-dialog/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-direction": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@radix-ui/react-direction/-/react-direction-1.0.1.tgz",
@@ -2797,6 +2802,24 @@
}
}
},
+ "node_modules/@radix-ui/react-menu/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-popover": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@radix-ui/react-popover/-/react-popover-1.0.7.tgz",
@@ -2834,6 +2857,24 @@
}
}
},
+ "node_modules/@radix-ui/react-popover/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-popper": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@radix-ui/react-popper/-/react-popper-1.1.3.tgz",
@@ -2936,6 +2977,295 @@
}
}
},
+ "node_modules/@radix-ui/react-primitive/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group": {
+ "version": "1.2.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-radio-group/-/react-radio-group-1.2.0.tgz",
+ "integrity": "sha512-yv+oiLaicYMBpqgfpSPw6q+RyXlLdIpQWDHZbUKURxe+nEh53hFXPPlfhfQQtYkS5MMK/5IWIa76SksleQZSzw==",
+ "dependencies": {
+ "@radix-ui/primitive": "1.1.0",
+ "@radix-ui/react-compose-refs": "1.1.0",
+ "@radix-ui/react-context": "1.1.0",
+ "@radix-ui/react-direction": "1.1.0",
+ "@radix-ui/react-presence": "1.1.0",
+ "@radix-ui/react-primitive": "2.0.0",
+ "@radix-ui/react-roving-focus": "1.1.0",
+ "@radix-ui/react-use-controllable-state": "1.1.0",
+ "@radix-ui/react-use-previous": "1.1.0",
+ "@radix-ui/react-use-size": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/primitive": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/primitive/-/primitive-1.1.0.tgz",
+ "integrity": "sha512-4Z8dn6Upk0qk4P74xBhZ6Hd/w0mPEzOOLxy4xiPXOXqjF7jZS0VAKk7/x/H6FyY2zCkYJqePf1G5KmkmNJ4RBA=="
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-collection": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-collection/-/react-collection-1.1.0.tgz",
+ "integrity": "sha512-GZsZslMJEyo1VKm5L1ZJY8tGDxZNPAoUeQUIbKeJfoi7Q4kmig5AsgLMYYuyYbfjd8fBmFORAIwYAkXMnXZgZw==",
+ "dependencies": {
+ "@radix-ui/react-compose-refs": "1.1.0",
+ "@radix-ui/react-context": "1.1.0",
+ "@radix-ui/react-primitive": "2.0.0",
+ "@radix-ui/react-slot": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-compose-refs": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-compose-refs/-/react-compose-refs-1.1.0.tgz",
+ "integrity": "sha512-b4inOtiaOnYf9KWyO3jAeeCG6FeyfY6ldiEPanbUjWd+xIk5wZeHa8yVwmrJ2vderhu/BQvzCrJI0lHd+wIiqw==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-context": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-context/-/react-context-1.1.0.tgz",
+ "integrity": "sha512-OKrckBy+sMEgYM/sMmqmErVn0kZqrHPJze+Ql3DzYsDDp0hl0L62nx/2122/Bvps1qz645jlcu2tD9lrRSdf8A==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-direction": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-direction/-/react-direction-1.1.0.tgz",
+ "integrity": "sha512-BUuBvgThEiAXh2DWu93XsT+a3aWrGqolGlqqw5VU1kG7p/ZH2cuDlM1sRLNnY3QcBS69UIz2mcKhMxDsdewhjg==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-id": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-id/-/react-id-1.1.0.tgz",
+ "integrity": "sha512-EJUrI8yYh7WOjNOqpoJaf1jlFIH2LvtgAl+YcFqNCa+4hj64ZXmPkAKOFs/ukjz3byN6bdb/AVUqHkI8/uWWMA==",
+ "dependencies": {
+ "@radix-ui/react-use-layout-effect": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-presence": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-presence/-/react-presence-1.1.0.tgz",
+ "integrity": "sha512-Gq6wuRN/asf9H/E/VzdKoUtT8GC9PQc9z40/vEr0VCJ4u5XvvhWIrSsCB6vD2/cH7ugTdSfYq9fLJCcM00acrQ==",
+ "dependencies": {
+ "@radix-ui/react-compose-refs": "1.1.0",
+ "@radix-ui/react-use-layout-effect": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-primitive": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-primitive/-/react-primitive-2.0.0.tgz",
+ "integrity": "sha512-ZSpFm0/uHa8zTvKBDjLFWLo8dkr4MBsiDLz0g3gMUwqgLHz9rTaRRGYDgvZPtBJgYCBKXkS9fzmoySgr8CO6Cw==",
+ "dependencies": {
+ "@radix-ui/react-slot": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-roving-focus": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-roving-focus/-/react-roving-focus-1.1.0.tgz",
+ "integrity": "sha512-EA6AMGeq9AEeQDeSH0aZgG198qkfHSbvWTf1HvoDmOB5bBG/qTxjYMWUKMnYiV6J/iP/J8MEFSuB2zRU2n7ODA==",
+ "dependencies": {
+ "@radix-ui/primitive": "1.1.0",
+ "@radix-ui/react-collection": "1.1.0",
+ "@radix-ui/react-compose-refs": "1.1.0",
+ "@radix-ui/react-context": "1.1.0",
+ "@radix-ui/react-direction": "1.1.0",
+ "@radix-ui/react-id": "1.1.0",
+ "@radix-ui/react-primitive": "2.0.0",
+ "@radix-ui/react-use-callback-ref": "1.1.0",
+ "@radix-ui/react-use-controllable-state": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-use-callback-ref": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.0.tgz",
+ "integrity": "sha512-CasTfvsy+frcFkbXtSJ2Zu9JHpN8TYKxkgJGWbjiZhFivxaeW7rMeZt7QELGVLaYVfFMsKHjb7Ak0nMEe+2Vfw==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-use-controllable-state": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-use-controllable-state/-/react-use-controllable-state-1.1.0.tgz",
+ "integrity": "sha512-MtfMVJiSr2NjzS0Aa90NPTnvTSg6C/JLCV7ma0W6+OMV78vd8OyRpID+Ng9LxzsPbLeuBnWBA1Nq30AtBIDChw==",
+ "dependencies": {
+ "@radix-ui/react-use-callback-ref": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-use-layout-effect": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-use-layout-effect/-/react-use-layout-effect-1.1.0.tgz",
+ "integrity": "sha512-+FPE0rOdziWSrH9athwI1R0HDVbWlEhd+FR+aSDk4uWGmSJ9Z54sdZVDQPZAinJhJXwfT+qnj969mCsT2gfm5w==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-use-previous": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-use-previous/-/react-use-previous-1.1.0.tgz",
+ "integrity": "sha512-Z/e78qg2YFnnXcW88A4JmTtm4ADckLno6F7OXotmkQfeuCVaKuYzqAATPhVzl3delXE7CxIV8shofPn3jPc5Og==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-radio-group/node_modules/@radix-ui/react-use-size": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-use-size/-/react-use-size-1.1.0.tgz",
+ "integrity": "sha512-XW3/vWuIXHa+2Uwcc2ABSfcCledmXhhQPlGbfcRXbiUQI5Icjcg19BGCZVKKInYbvUCut/ufbbLLPFC5cbb1hw==",
+ "dependencies": {
+ "@radix-ui/react-use-layout-effect": "1.1.0"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-roving-focus": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@radix-ui/react-roving-focus/-/react-roving-focus-1.0.4.tgz",
@@ -3041,6 +3371,24 @@
}
}
},
+ "node_modules/@radix-ui/react-select/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-separator": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@radix-ui/react-separator/-/react-separator-1.0.3.tgz",
@@ -3098,16 +3446,29 @@
}
},
"node_modules/@radix-ui/react-slot": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
- "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.1.0.tgz",
+ "integrity": "sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==",
"dependencies": {
- "@babel/runtime": "^7.13.10",
- "@radix-ui/react-compose-refs": "1.0.1"
+ "@radix-ui/react-compose-refs": "1.1.0"
},
"peerDependencies": {
"@types/react": "*",
- "react": "^16.8 || ^17.0 || ^18.0"
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@radix-ui/react-slot/node_modules/@radix-ui/react-compose-refs": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-compose-refs/-/react-compose-refs-1.1.0.tgz",
+ "integrity": "sha512-b4inOtiaOnYf9KWyO3jAeeCG6FeyfY6ldiEPanbUjWd+xIk5wZeHa8yVwmrJ2vderhu/BQvzCrJI0lHd+wIiqw==",
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
},
"peerDependenciesMeta": {
"@types/react": {
@@ -3208,6 +3569,24 @@
}
}
},
+ "node_modules/@radix-ui/react-tooltip/node_modules/@radix-ui/react-slot": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.0.2.tgz",
+ "integrity": "sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-compose-refs": "1.0.1"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "react": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-use-callback-ref": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.0.1.tgz",
@@ -4124,6 +4503,12 @@
"@types/react": "*"
}
},
+ "node_modules/@types/sqlstring": {
+ "version": "2.3.2",
+ "resolved": "https://registry.npmjs.org/@types/sqlstring/-/sqlstring-2.3.2.tgz",
+ "integrity": "sha512-lVRe4Iz9UNgiHelKVo8QlC8fb5nfY8+p+jNQNE+UVsuuVlQnWhyWmQ/wF5pE8Ys6TdjfVpqTG9O9i2vi6E0+Sg==",
+ "dev": true
+ },
"node_modules/@types/unist": {
"version": "2.0.10",
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.10.tgz",
@@ -9111,6 +9496,14 @@
"url": "https://github.com/sponsors/wooorm"
}
},
+ "node_modules/sqlstring": {
+ "version": "2.3.3",
+ "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz",
+ "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==",
+ "engines": {
+ "node": ">= 0.6"
+ }
+ },
"node_modules/streamsearch": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
diff --git a/src/client/package.json b/src/client/package.json
index 222f49f9..5f18895d 100644
--- a/src/client/package.json
+++ b/src/client/package.json
@@ -1,6 +1,6 @@
{
"name": "openlit",
- "version": "1.9.2",
+ "version": "1.10.0",
"private": true,
"scripts": {
"dev": "next dev",
@@ -24,11 +24,12 @@
"@radix-ui/react-hover-card": "^1.0.7",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-popover": "^1.0.7",
+ "@radix-ui/react-radio-group": "^1.2.0",
"@radix-ui/react-scroll-area": "^1.0.5",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slider": "^1.1.2",
- "@radix-ui/react-slot": "^1.0.2",
+ "@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.0.3",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
@@ -56,6 +57,7 @@
"react-syntax-highlighter": "^15.5.0",
"recharts": "^2.12.7",
"sonner": "^1.5.0",
+ "sqlstring": "^2.3.3",
"tailwind-merge": "^2.2.2",
"tailwindcss-animate": "^1.0.7",
"tiny-cookie": "^2.5.1",
@@ -67,6 +69,7 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.13",
+ "@types/sqlstring": "^2.3.2",
"autoprefixer": "^10.0.1",
"eslint": "^8.57.0",
"eslint-config-next": "^14.1.4",
diff --git a/src/client/prisma/migrations/20241007094128_prompt_hub/migration.sql b/src/client/prisma/migrations/20241007094128_prompt_hub/migration.sql
new file mode 100644
index 00000000..2e848694
--- /dev/null
+++ b/src/client/prisma/migrations/20241007094128_prompt_hub/migration.sql
@@ -0,0 +1,25 @@
+-- CreateTable
+CREATE TABLE "APIKeys" (
+ "id" TEXT NOT NULL PRIMARY KEY,
+ "name" TEXT NOT NULL DEFAULT 'default',
+ "apiKey" TEXT NOT NULL,
+ "database_config_id" TEXT NOT NULL,
+ "isDeleted" BOOLEAN NOT NULL DEFAULT false,
+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "createdByUserId" TEXT NOT NULL,
+ "deletedAt" DATETIME,
+ "deletedByUserId" TEXT,
+ CONSTRAINT "APIKeys_database_config_id_fkey" FOREIGN KEY ("database_config_id") REFERENCES "databaseconfig" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
+ CONSTRAINT "APIKeys_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
+ CONSTRAINT "APIKeys_deletedByUserId_fkey" FOREIGN KEY ("deletedByUserId") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE CASCADE
+);
+
+-- CreateTable
+CREATE TABLE "ClickhouseMigrations" (
+ "id" TEXT NOT NULL PRIMARY KEY,
+ "databaseConfigId" TEXT NOT NULL,
+ "clickhouseMigrationId" TEXT NOT NULL
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "APIKeys_apiKey_key" ON "APIKeys"("apiKey");
diff --git a/src/client/prisma/schema.prisma b/src/client/prisma/schema.prisma
index 43a488fe..3bc000c4 100644
--- a/src/client/prisma/schema.prisma
+++ b/src/client/prisma/schema.prisma
@@ -55,6 +55,8 @@ model User {
dbConfigs DatabaseConfigUser[]
createdDBConfigs DatabaseConfig[]
OpenGround OpenGround[]
+ createdApiKeys APIKeys[] @relation("createdByUser")
+ deletedApiKeys APIKeys[] @relation("deletedByUser")
@@map("users")
}
@@ -89,6 +91,7 @@ model DatabaseConfig {
createdByUser User @relation(fields: [createdByUserId], references: [id])
OpenGround OpenGround[]
dbInvitedUsers DatabaseConfigInvitedUser[]
+ promptAPIKey APIKeys[]
@@unique([name])
@@map("databaseconfig")
@@ -103,8 +106,8 @@ model DatabaseConfigUser {
canEdit Boolean @default(false)
canShare Boolean @default(false)
canDelete Boolean @default(false)
- createdAt DateTime? @default(now())
- updatedAt DateTime? @updatedAt
+ createdAt DateTime? @default(now())
+ updatedAt DateTime? @updatedAt
@@unique([databaseConfigId, userId])
@@map("databaseconfiguser")
@@ -134,3 +137,24 @@ model DatabaseConfigInvitedUser {
@@unique([databaseConfigId, email])
}
+
+model APIKeys {
+ id String @id @default(cuid())
+ name String @default("default")
+ apiKey String @unique
+ databaseConfigId String @map("database_config_id")
+ databaseConfig DatabaseConfig? @relation(fields: [databaseConfigId], references: [id])
+ isDeleted Boolean @default(false)
+ createdAt DateTime @default(now())
+ createdByUserId String
+ createdByUser User @relation("createdByUser", fields: [createdByUserId], references: [id])
+ deletedAt DateTime?
+ deletedByUserId String?
+ deletedByUser User? @relation("deletedByUser", fields: [deletedByUserId], references: [id])
+}
+
+model ClickhouseMigrations {
+ id String @id @default(cuid())
+ databaseConfigId String
+ clickhouseMigrationId String
+}
diff --git a/src/client/src/app/(playground)/api-keys/page.tsx b/src/client/src/app/(playground)/api-keys/page.tsx
new file mode 100644
index 00000000..b17f9d7a
--- /dev/null
+++ b/src/client/src/app/(playground)/api-keys/page.tsx
@@ -0,0 +1,30 @@
+import ManageKeys from "@/components/(playground)/api-keys/manage";
+
+export default function APIKeys() {
+ return (
+ <div className="flex flex-col grow w-full h-full rounded overflow-hidden p-2 text-sm text-stone-900 dark:text-stone-300 gap-3">
+ <p>
+ Welcome to the API Key Management page. Here, you can view, generate,
+ and manage API keys for seamless integration with our services. Please
+ note that we do not display your secret API keys again after you
+ generate them.
+ </p>
+ <ul className="list-disc list-inside">
+ <li>
+ <span className="font-medium">Keep Your Keys Secure:</span> Treat your
+ API keys like passwords. Do not share them publicly or expose them in
+ places where unauthorized individuals may access them.
+ </li>
+ <li>
+ <span className="font-medium">Rotate Keys Regularly:</span> For
+ enhanced security, consider rotating your keys periodically.
+ </li>
+ <li>
+ <span className="font-medium">Revoke Unused Keys:</span> If a key is
+ no longer needed or compromised, revoke it immediately.
+ </li>
+ </ul>
+ <ManageKeys />
+ </div>
+ );
+}
diff --git a/src/client/src/app/(playground)/database-config/page.tsx b/src/client/src/app/(playground)/database-config/page.tsx
index 33718b96..c67c4382 100644
--- a/src/client/src/app/(playground)/database-config/page.tsx
+++ b/src/client/src/app/(playground)/database-config/page.tsx
@@ -1,6 +1,8 @@
"use client";
-import FormBuilder from "@/components/common/form-builder";
+import FormBuilder, {
+ FormBuilderEvent,
+} from "@/components/common/form-builder";
import DatabaseConfigTabs, {
DatabaseConfigTabItemProps,
} from "@/app/(playground)/database-config/database-config-tabs";
@@ -18,12 +20,7 @@ import {
import { useRootStore } from "@/store";
import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
import { isNil, keyBy } from "lodash";
-import {
- FormEventHandler,
- MouseEventHandler,
- useCallback,
- useState,
-} from "react";
+import { MouseEventHandler, useCallback, useState } from "react";
import { toast } from "sonner";
function ModifyDatabaseConfig({
@@ -33,7 +30,7 @@ function ModifyDatabaseConfig({
}) {
const { fireRequest, isLoading } = useFetchWrapper();
- const modifyDetails: FormEventHandler<HTMLFormElement> = useCallback(
+ const modifyDetails: FormBuilderEvent = useCallback(
(event) => {
event.preventDefault();
const formElement = event.target as HTMLFormElement;
@@ -87,74 +84,98 @@ function ModifyDatabaseConfig({
fields={[
{
label: "Config Name",
- type: "text",
- name: "name",
- placeholder: "db-config",
- defaultValue: dbConfig?.name,
inputKey: `${dbConfig?.id}-name`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "name",
+ placeholder: "db-config",
+ defaultValue: dbConfig?.name,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Environment",
- type: "text",
- name: "environment",
- placeholder: "production",
- defaultValue: dbConfig?.environment,
inputKey: `${dbConfig?.id}-environment`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "environment",
+ placeholder: "production",
+ defaultValue: dbConfig?.environment,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Username",
- type: "text",
- name: "username",
- placeholder: "username",
- defaultValue: dbConfig?.username,
+ fieldType: "INPUT",
inputKey: `${dbConfig?.id}-username`,
- disabled: formFieldsDisabled,
+ fieldTypeProps: {
+ type: "text",
+ name: "username",
+ placeholder: "username",
+ defaultValue: dbConfig?.username,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Password",
- type: "password",
- name: "password",
- placeholder: "*******",
inputKey: `${dbConfig?.id}-password`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "password",
+ name: "password",
+ placeholder: "*******",
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Host",
- type: "text",
- name: "host",
- placeholder: "127.0.0.1",
- defaultValue: dbConfig?.host,
inputKey: `${dbConfig?.id}-host`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "host",
+ placeholder: "127.0.0.1",
+ defaultValue: dbConfig?.host,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Port",
- type: "number",
- name: "port",
- placeholder: "8123",
- defaultValue: dbConfig?.port,
inputKey: `${dbConfig?.id}-port`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "number",
+ name: "port",
+ placeholder: "8123",
+ defaultValue: dbConfig?.port,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Database",
- type: "text",
- name: "database",
- placeholder: "default",
- defaultValue: dbConfig?.database,
inputKey: `${dbConfig?.id}-database`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "database",
+ placeholder: "default",
+ defaultValue: dbConfig?.database,
+ disabled: formFieldsDisabled,
+ },
},
{
label: "Query params",
- type: "text",
- name: "query",
- placeholder: "a=b&c=d",
- defaultValue: dbConfig?.query,
inputKey: `${dbConfig?.id}-query`,
- disabled: formFieldsDisabled,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "query",
+ placeholder: "a=b&c=d",
+ defaultValue: dbConfig?.query,
+ disabled: formFieldsDisabled,
+ },
},
]}
heading={`${
@@ -242,7 +263,7 @@ function DatabaseList({
}));
return (
- <div className="flex flex-col w-full flex-1 h-full relative">
+ <div className="flex flex-col w-full flex-1 h-full relative gap-4">
<DatabaseConfigTabs
addButton
items={items}
diff --git a/src/client/src/app/(playground)/profile/page.tsx b/src/client/src/app/(playground)/profile/page.tsx
index 2c2d8f82..2230233c 100644
--- a/src/client/src/app/(playground)/profile/page.tsx
+++ b/src/client/src/app/(playground)/profile/page.tsx
@@ -1,11 +1,12 @@
"use client";
-import FormBuilder from "@/components/common/form-builder";
+import FormBuilder, {
+ FormBuilderEvent,
+} from "@/components/common/form-builder";
import { getUserDetails, setUser } from "@/selectors/user";
import { useRootStore } from "@/store";
import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
import { User } from "@prisma/client";
-import { FormEventHandler } from "react";
import { toast } from "sonner";
const PROFILE_TOAST_ID = "profile-details";
@@ -19,7 +20,7 @@ function ModifyProfileDetails({
}) {
const { fireRequest, isLoading } = useFetchWrapper();
- const modifyDetails: FormEventHandler<HTMLFormElement> = (event) => {
+ const modifyDetails: FormBuilderEvent = (event) => {
event.preventDefault();
const formElement = event.target as HTMLFormElement;
@@ -67,41 +68,56 @@ function ModifyProfileDetails({
fields={[
{
label: "Email",
- type: "text",
- name: "email",
- placeholder: "db-config",
- defaultValue: user?.email || "",
inputKey: `${user?.email}-email`,
- disabled: true,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "email",
+ placeholder: "db-config",
+ defaultValue: user?.email || "",
+ disabled: true,
+ },
},
{
label: "Profile Name",
- type: "text",
- name: "name",
- placeholder: "db-config",
- defaultValue: user?.name || "",
inputKey: `${user?.id}-name`,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "name",
+ placeholder: "db-config",
+ defaultValue: user?.name || "",
+ },
},
{
label: "Current Password",
- type: "password",
- name: "currentPassword",
- placeholder: "*******",
inputKey: `${user?.id}-currentPassword`,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "password",
+ name: "currentPassword",
+ placeholder: "*******",
+ },
},
{
label: "New Password",
- type: "password",
- name: "newPassword",
- placeholder: "*******",
+ fieldType: "INPUT",
inputKey: `${user?.id}-newPassword`,
+ fieldTypeProps: {
+ type: "password",
+ name: "newPassword",
+ placeholder: "*******",
+ },
},
{
label: "Confirm New Password",
- type: "password",
- name: "confirmNewPassword",
- placeholder: "*******",
+ fieldType: "INPUT",
inputKey: `${user?.id}-confirmNewPassword`,
+ fieldTypeProps: {
+ type: "password",
+ name: "confirmNewPassword",
+ placeholder: "*******",
+ },
},
]}
heading={`Update profile details`}
diff --git a/src/client/src/app/(playground)/prompt-hub/[id]/page.tsx b/src/client/src/app/(playground)/prompt-hub/[id]/page.tsx
new file mode 100644
index 00000000..4d9f5e51
--- /dev/null
+++ b/src/client/src/app/(playground)/prompt-hub/[id]/page.tsx
@@ -0,0 +1,280 @@
+"use client";
+import PromptForm from "@/components/(playground)/prompt-hub/form";
+import { Badge } from "@/components/ui/badge";
+import { Button } from "@/components/ui/button";
+import {
+ Table,
+ TableBody,
+ TableCell,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from "@/components/ui/table";
+import { getPingStatus } from "@/selectors/database-config";
+import { useRootStore } from "@/store";
+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
+import { jsonParse } from "@/utils/json";
+import { objectEntries } from "@/utils/object";
+import { format, formatDistance } from "date-fns";
+import { CalendarDays, CloudDownload, Rocket } from "lucide-react";
+import Link from "next/link";
+import {
+ useParams,
+ useSearchParams,
+ useRouter,
+ usePathname,
+} from "next/navigation";
+import { useCallback, useEffect } from "react";
+import { toast } from "sonner";
+
+export default function PromptHub() {
+ const pingStatus = useRootStore(getPingStatus);
+ const pathName = usePathname();
+ const params = useParams();
+ const router = useRouter();
+ const searchParams = useSearchParams();
+ const version = searchParams.get("version") || undefined;
+ const { data, fireRequest, isFetched, isLoading } = useFetchWrapper();
+ const fetchData = useCallback(async () => {
+ fireRequest({
+ requestType: "GET",
+ url: `/api/prompt/get/${params.id}${
+ version ? "?version=" + version : ""
+ }`,
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "prompt-hub",
+ });
+ },
+ });
+ }, [version]);
+
+ const successCallback = useCallback(async () => {
+ if (version) {
+ router.replace(pathName);
+ } else {
+ fetchData();
+ }
+ }, [version, pathName]);
+
+ useEffect(() => {
+ if (pingStatus === "success") fetchData();
+ }, [version, pingStatus]);
+
+ const updatedData = (data as any)?.data?.[0];
+
+ if (!isFetched || (!updatedData?.promptId && isLoading)) {
+ return (
+ <div className="flex flex-col w-full h-full overflow-hidden gap-6 items-center justify-center">
+ <div className="h-4 w-1/5 bg-secondary/[0.9] rounded" />
+ <div className="h-4 w-3/5 bg-secondary/[0.9] rounded" />
+ <div className="h-4 w-2/3 bg-secondary/[0.9] rounded" />
+ <div className="h-4 w-1/3 bg-secondary/[0.9] rounded" />
+ </div>
+ );
+ }
+
+ if (!updatedData || !updatedData?.promptId) {
+ return (
+ <div className="flex w-full h-full overflow-hidden items-center justify-center text-stone-600">
+ No such prompt exists!
+ </div>
+ );
+ }
+
+ if (
+ updatedData?.promptId &&
+ version &&
+ updatedData.version !== version &&
+ !isLoading
+ ) {
+ return (
+ <div className="flex w-full h-full overflow-hidden items-center justify-center text-stone-600">
+ No such version of the prompt{" "}
+ <span className="bg-secondary text-primary px-2 text-sm mx-3">
+ {" "}
+ {updatedData.name}{" "}
+ </span>{" "}
+ exists!
+ </div>
+ );
+ }
+
+ // const prompt = updatedData.versions[0].prompt;
+ const tags = jsonParse(updatedData.versions[0].tags) || [];
+ const metaProperties =
+ jsonParse(updatedData.versions[0].metaProperties) || {};
+ const metaPropertiesMap = objectEntries(metaProperties);
+
+ const latestVersion = updatedData.versions.find(
+ (version: any) => version.status !== "DRAFT"
+ );
+ const draftVersion = updatedData.versions.find(
+ (version: any) => version.status === "DRAFT"
+ );
+
+ return (
+ <div className="grid grid-cols-3 w-full h-full overflow-hidden gap-6">
+ <div className="flex flex-col space-y-1 col-span-2 gap-6 overflow-auto">
+ <div className="flex w-full">
+ <div className="flex flex-col grow">
+ <div className="flex items-center text-2xl font-semibold text-stone-800 dark:text-stone-200 gap-4">
+ <h1>{updatedData.name}</h1>
+ {updatedData.status === "DRAFT" && (
+ <Badge
+ variant="outline"
+ className="transition-none text-stone-500 dark:bg-stone-800 dark:text-stone-400"
+ >
+ Draft
+ </Badge>
+ )}
+ </div>
+ <div className="flex items-center text-stone-800 dark:text-stone-200 opacity-60 gap-8">
+ <div className="flex items-center">
+ <Rocket className="mr-2 h-4 w-4" />
+ <span className="text-xs text-muted-foreground">
+ {updatedData.version}
+ </span>
+ </div>
+ <div className="flex items-center">
+ <CalendarDays className="mr-2 h-4 w-4" />
+ <span className="text-xs text-muted-foreground">
+ Published on {format(updatedData.updatedAt, "MMM do, y")}
+ </span>
+ </div>
+ </div>
+ </div>
+ <div className="flex items-center">
+ {updatedData.versionId === draftVersion?.versionId ? (
+ <PromptForm
+ versionData={draftVersion}
+ 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 py-2 h-auto py-1 rounded-sm"
+ >
+ Publish Version
+ </Button>
+ </PromptForm>
+ ) : null}
+ {updatedData.version === latestVersion?.version &&
+ !draftVersion?.version ? (
+ <PromptForm
+ versionData={latestVersion}
+ 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 py-2 h-auto py-1 rounded-sm"
+ >
+ Create New Version
+ </Button>
+ </PromptForm>
+ ) : null}
+ </div>
+ </div>
+ {tags.length > 0 ? (
+ <div className="flex gap-2">
+ {tags.map((tag: string) => (
+ <Badge
+ key={tag}
+ className="rounded-pill bg-stone-500 transition-none"
+ >
+ {tag}
+ </Badge>
+ ))}
+ </div>
+ ) : null}
+ <div className="flex flex-col w-full">
+ <h3 className="text-sm text-stone-500 mb-2 dark:text-stone-400">
+ Prompt
+ </h3>
+ <div className="flex bg-stone-100 dark:bg-stone-900 text-stone-500 dark:text-stone-300 p-4 gap-8">
+ {updatedData.prompt}
+ </div>
+ </div>
+ {metaPropertiesMap.length > 0 ? (
+ <div className="flex flex-col gap-2">
+ <h3 className="text-sm text-stone-500 dark:text-stone-400">
+ Meta Properties
+ </h3>
+ <div className="rounded-sm border border-stone-200 dark:border-stone-700">
+ <Table>
+ <TableHeader className="bg-stone-100 dark:bg-stone-800">
+ <TableRow>
+ <TableHead className="h-8 text-stone-400">Key</TableHead>
+ <TableHead className="h-8 text-stone-400">Value</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {metaPropertiesMap.map(([itemKey, value]: string[]) => (
+ <TableRow
+ key={itemKey}
+ className="bg-stone-50 dark:bg-stone-900 data-[state=selected]:bg-stone-50 dark:data-[state=selected]:bg-stone-900 text-stone-600 dark:text-stone-300"
+ >
+ <TableCell className="p-0 px-4 align-middle font-medium h-10">
+ {itemKey}
+ </TableCell>
+ <TableCell className="p-0 px-4 align-middle h-10">
+ {value}
+ </TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ </div>
+ </div>
+ ) : null}
+ </div>
+ <div className="flex flex-col w-full space-y-1 p-4 bg-stone-100 dark:bg-stone-900 gap-4 overflow-hidden">
+ <h2 className="text-stone-700 dark:text-stone-300">Version</h2>
+ <div className="flex flex-col w-full gap-4 overflow-auto">
+ {updatedData.versions.map((versionItem: any) => (
+ <Link
+ href={`/prompt-hub/${params.id}${
+ versionItem.status === "DRAFT"
+ ? ""
+ : `?version=${versionItem.version}`
+ }`}
+ key={versionItem.versionId}
+ className="flex w-full items-center text-sm gap-2 text-stone-500"
+ >
+ <div className="flex w-min gap-2">
+ {versionItem.version}
+
+ {versionItem.versionId === latestVersion?.versionId ? (
+ <Badge
+ variant="outline"
+ className="rounded-pill text-xs text-stone-500 dark:text-stone-400 dark:border-stone-600 font-light transition-none"
+ >
+ latest
+ </Badge>
+ ) : null}
+ {versionItem.versionId === draftVersion?.versionId ? (
+ <Badge
+ variant="outline"
+ className="rounded-pill text-xs text-stone-500 dark:text-stone-400 dark:border-stone-600 font-light transition-none"
+ >
+ Draft
+ </Badge>
+ ) : null}
+ </div>
+ <hr className="flex-1 border-stone-200 dark:border-stone-700 " />
+ <div className="flex gap-1 text-xs items-center shrink-0">
+ <CloudDownload className="w-4" />
+ {versionItem.totalDownloads}
+ </div>
+ ...
+ <div className="w-max">
+ {formatDistance(versionItem.updatedAt, new Date(), {
+ addSuffix: true,
+ })}
+ </div>
+ </Link>
+ ))}
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/app/(playground)/prompt-hub/layout.tsx b/src/client/src/app/(playground)/prompt-hub/layout.tsx
new file mode 100644
index 00000000..f2d27564
--- /dev/null
+++ b/src/client/src/app/(playground)/prompt-hub/layout.tsx
@@ -0,0 +1,17 @@
+"use client";
+import PromptHubHeader from "@/components/(playground)/prompt-hub/header";
+import { useParams } from "next/navigation";
+
+export default function PromptLayout({
+ children,
+}: {
+ children: React.ReactNode;
+}) {
+ const params = useParams();
+ return (
+ <div className="flex flex-col w-full h-full gap-4">
+ <PromptHubHeader createNew={!params.id} />
+ {children}
+ </div>
+ );
+}
diff --git a/src/client/src/app/(playground)/prompt-hub/page.tsx b/src/client/src/app/(playground)/prompt-hub/page.tsx
new file mode 100644
index 00000000..65fdf009
--- /dev/null
+++ b/src/client/src/app/(playground)/prompt-hub/page.tsx
@@ -0,0 +1,136 @@
+"use client";
+
+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
+import Link from "next/link";
+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 { BookOpenText, TrashIcon } from "lucide-react";
+import ConfirmationModal from "@/components/common/confirmation-modal";
+
+const columns = [
+ {
+ key: "name",
+ className: "col-span-2",
+ header: "Name",
+ },
+ {
+ key: "createdBy",
+ className: "col-span-2",
+ header: "Created By",
+ },
+ {
+ className: "col-span-2 text-center",
+ header: "Latest Version",
+ render: (data: any) => {
+ const latestVersion = get(data, "latestVersion");
+ return latestVersion || "draft";
+ },
+ },
+ {
+ header: "Downloads",
+ className: "col-span-2 text-center",
+ render: (data: any) => {
+ const totalDownloads = get(data, "totalDownloads");
+ const latestVersion = get(data, "latestVersion");
+ return latestVersion ? totalDownloads : "-";
+ },
+ },
+ {
+ header: "Last Released On",
+ className: "col-span-3",
+ render: (data: any) => {
+ const latestVersionDate = get(data, "latestVersionDate");
+ const latestVersion = get(data, "latestVersion");
+ return latestVersion ? format(latestVersionDate, "MMM do, y") : "-";
+ },
+ },
+ {
+ header: "Actions",
+ className: "col-span-1 text-center",
+ render: (data: any, extraFunction: { handleDelete: (p?: any) => void }) => (
+ <div className="flex justify-center gap-4">
+ <Link
+ href={`/prompt-hub/${data.promptId}`}
+ className="inline-block hover:text-stone-700 hover:dark:text-stone-300"
+ >
+ <BookOpenText className="w-4 hover:text-primary" />
+ </Link>
+ <ConfirmationModal
+ handleYes={extraFunction?.handleDelete}
+ title="Are you sure you want to delete this prompt?"
+ subtitle="Deleting prompts might result in breaking application if they are getting used. Please confirm before deleting it."
+ params={{
+ id: data.promptId,
+ }}
+ >
+ <TrashIcon className="w-4 cursor-pointer hover:text-primary" />
+ </ConfirmationModal>
+ </div>
+ ),
+ },
+];
+
+export default function PromptHub() {
+ const { data, fireRequest, isFetched, isLoading } = useFetchWrapper();
+ const { fireRequest: fireDeleteRequest, isLoading: isDeleting } =
+ useFetchWrapper();
+ const pingStatus = useRootStore(getPingStatus);
+ const fetchData = useCallback(async () => {
+ fireRequest({
+ requestType: "POST",
+ url: `/api/prompt/get`,
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "prompt-hub",
+ });
+ },
+ });
+ }, []);
+
+ const deletePrompt = useCallback(
+ async ({ id }: { id: string }) => {
+ fireDeleteRequest({
+ requestType: "DELETE",
+ url: `/api/prompt/${id}`,
+ successCb: (data: any) => {
+ toast.success(data, {
+ id: "prompt-hub",
+ });
+ fetchData();
+ },
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "prompt-hub",
+ });
+ },
+ });
+ },
+ [fetchData]
+ );
+
+ useEffect(() => {
+ if (pingStatus !== "pending") {
+ fetchData();
+ }
+ }, [pingStatus]);
+
+ const updatedData = (data as any) || [];
+
+ return (
+ <TableData
+ columns={columns}
+ data={updatedData}
+ isFetched={isFetched || pingStatus === "failure"}
+ isLoading={isLoading || isDeleting}
+ idKey="promptId"
+ extraFunction={{
+ handleDelete: deletePrompt,
+ }}
+ />
+ );
+}
diff --git a/src/client/src/app/api/api-key/[id]/route.ts b/src/client/src/app/api/api-key/[id]/route.ts
new file mode 100644
index 00000000..b4f46464
--- /dev/null
+++ b/src/client/src/app/api/api-key/[id]/route.ts
@@ -0,0 +1,13 @@
+import { deleteAPIKey } from "@/lib/platform/prompt/api-keys";
+
+export async function DELETE(_: Request, context: any) {
+ const { id } = context.params;
+ const [err, res] = await deleteAPIKey(id);
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/api-key/route.ts b/src/client/src/app/api/api-key/route.ts
new file mode 100644
index 00000000..eed9df94
--- /dev/null
+++ b/src/client/src/app/api/api-key/route.ts
@@ -0,0 +1,22 @@
+import { generateAPIKey, getAllAPIKeys } from "@/lib/platform/prompt/api-keys";
+import asaw from "@/utils/asaw";
+
+export async function GET() {
+ const res: any = await getAllAPIKeys();
+ return Response.json(res);
+}
+
+export async function POST(request: Request) {
+ const formData = await request.json();
+ const name = formData.name;
+
+ const [err, res]: any = await asaw(generateAPIKey(name));
+
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/clickhouse/migrate/route.ts b/src/client/src/app/api/clickhouse/migrate/route.ts
new file mode 100644
index 00000000..f9d88971
--- /dev/null
+++ b/src/client/src/app/api/clickhouse/migrate/route.ts
@@ -0,0 +1,12 @@
+import { runClickhouseMigrations } from "@/lib/platform/clickhouse";
+import asaw from "@/utils/asaw";
+
+export async function POST() {
+ const [err, res] = await asaw(runClickhouseMigrations());
+ if (err)
+ return Response.json(err, {
+ status: 400,
+ });
+
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/prompt/[id]/route.ts b/src/client/src/app/api/prompt/[id]/route.ts
new file mode 100644
index 00000000..7beb57d6
--- /dev/null
+++ b/src/client/src/app/api/prompt/[id]/route.ts
@@ -0,0 +1,13 @@
+import { deletePrompt } from "@/lib/platform/prompt";
+
+export async function DELETE(_: Request, context: any) {
+ const { id } = context.params;
+ const [err, res] = await deletePrompt(id);
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/prompt/get-compiled/route.ts b/src/client/src/app/api/prompt/get-compiled/route.ts
new file mode 100644
index 00000000..ad66d003
--- /dev/null
+++ b/src/client/src/app/api/prompt/get-compiled/route.ts
@@ -0,0 +1,48 @@
+import getMessage from "@/constants/messages";
+import { PromptCompiledInput } from "@/constants/prompts";
+import { getCompiledPrompt } from "@/lib/platform/prompt/compiled";
+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 promptInput: PromptCompiledInput = {
+ id: formData.id,
+ name: formData.name,
+ version: formData.version,
+ apiKey,
+ variables: formData.variables || {},
+ shouldCompile: !!formData.shouldCompile,
+ downloadMetaProperties: formData.metaProperties,
+ downloadSource: formData.source,
+ };
+
+ const [err, res]: any = await asaw(getCompiledPrompt(promptInput));
+
+ return Response.json({
+ err,
+ res,
+ });
+}
+
+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/prompt/get/[id]/route.ts b/src/client/src/app/api/prompt/get/[id]/route.ts
new file mode 100644
index 00000000..1267b362
--- /dev/null
+++ b/src/client/src/app/api/prompt/get/[id]/route.ts
@@ -0,0 +1,11 @@
+import { getPromptDetails } from "@/lib/platform/prompt";
+import { NextRequest } from "next/server";
+
+export async function GET(request: NextRequest, context: any) {
+ const { searchParams } = request.nextUrl;
+ const version = searchParams.get("version") || undefined;
+ const { id } = context.params || {};
+
+ const res: any = await getPromptDetails(id, { version });
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/prompt/get/route.ts b/src/client/src/app/api/prompt/get/route.ts
new file mode 100644
index 00000000..07b43a60
--- /dev/null
+++ b/src/client/src/app/api/prompt/get/route.ts
@@ -0,0 +1,12 @@
+import { getPrompts } from "@/lib/platform/prompt";
+
+export async function POST() {
+ const { err, data }: any = await getPrompts();
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(data);
+}
diff --git a/src/client/src/app/api/prompt/route.ts b/src/client/src/app/api/prompt/route.ts
new file mode 100644
index 00000000..f5f76cdb
--- /dev/null
+++ b/src/client/src/app/api/prompt/route.ts
@@ -0,0 +1,26 @@
+import { PromptInput } from "@/constants/prompts";
+import { createPrompt } from "@/lib/platform/prompt";
+import asaw from "@/utils/asaw";
+
+export async function POST(request: Request) {
+ const formData = await request.json();
+
+ const promptInput: PromptInput = {
+ name: formData.name,
+ prompt: formData.prompt,
+ version: formData.version,
+ status: formData.status,
+ tags: formData.tags,
+ metaProperties: formData.metaProperties,
+ };
+
+ const [err, res]: any = await asaw(createPrompt(promptInput));
+
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(res);
+}
diff --git a/src/client/src/app/api/prompt/version/route.ts b/src/client/src/app/api/prompt/version/route.ts
new file mode 100644
index 00000000..e2f33c8f
--- /dev/null
+++ b/src/client/src/app/api/prompt/version/route.ts
@@ -0,0 +1,27 @@
+import { PromptUpdate } from "@/constants/prompts";
+import { upsertPromptVersion } from "@/lib/platform/prompt/version";
+import asaw from "@/utils/asaw";
+
+export async function POST(request: Request) {
+ const formData = await request.json();
+
+ const promptInput: PromptUpdate = {
+ versionId: formData.versionId,
+ promptId: formData.promptId,
+ prompt: formData.prompt,
+ version: formData.version,
+ status: formData.status,
+ tags: formData.tags,
+ metaProperties: formData.metaProperties,
+ };
+
+ const [err, res]: any = await asaw(upsertPromptVersion(promptInput));
+
+ if (err) {
+ return Response.json(err, {
+ status: 400,
+ });
+ }
+
+ return Response.json(res);
+}
diff --git a/src/client/src/clickhouse/migrations/create-prompt-migration.ts b/src/client/src/clickhouse/migrations/create-prompt-migration.ts
new file mode 100644
index 00000000..6e38c835
--- /dev/null
+++ b/src/client/src/clickhouse/migrations/create-prompt-migration.ts
@@ -0,0 +1,103 @@
+import getMessage from "@/constants/messages";
+import { getDBConfigById, getDBConfigByUser } from "@/lib/db-config";
+import { dataCollector } from "@/lib/platform/common";
+import {
+ OPENLIT_PROMPTS_TABLE_NAME,
+ OPENLIT_PROMPT_VERSIONS_TABLE_NAME,
+ OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME,
+} from "@/lib/platform/prompt/table-details";
+import prisma from "@/lib/prisma";
+import asaw from "@/utils/asaw";
+import { consoleLog } from "@/utils/log";
+
+const MIGRATION_ID = "create-prompt-table";
+
+export default async function CreatePromptMigration(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_PROMPTS_TABLE_NAME} (
+ id UUID DEFAULT generateUUIDv4(), -- Unique ID for each prompt
+ name String, -- Unique name for the prompt
+ created_by String, -- Who created the prompt
+ created_at DateTime DEFAULT now(), -- Timestamp for when the prompt was created
+ PRIMARY KEY id, -- Unique primary key constraint
+ ) ENGINE = MergeTree()
+ ORDER BY id;
+ `,
+ `
+ CREATE TABLE IF NOT EXISTS ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME} (
+ version_id UUID DEFAULT generateUUIDv4(), -- Unique identifier for each version
+ prompt_id UUID, -- Foreign key to prompt
+ updated_by String, -- Who updated the version
+ updated_at DateTime DEFAULT now(), -- When the version was updated
+ version String, -- Version in '1.0.0' format
+ status Enum('PUBLISHED', 'DRAFT'), -- DRAFT or PUBLISHED status
+ prompt String DEFAULT '', -- The actual prompt content
+ tags String DEFAULT '[]', -- Tags for the version
+ meta_properties String DEFAULT '{}', -- Meta properties for the version
+ INDEX prompt_version_index (prompt_id, version) TYPE minmax GRANULARITY 1
+ ) ENGINE = MergeTree()
+ ORDER BY (prompt_id, version_id);
+ `,
+ `
+ CREATE TABLE IF NOT EXISTS ${OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME} (
+ download_id UUID DEFAULT generateUUIDv4(), -- Unique ID for each download
+ prompt_id UUID, -- Links to the prompts table
+ version_id UUID, -- Version id of the prompt
+ downloaded_at DateTime DEFAULT now(), -- Timestamp when the download occurred
+ download_source String DEFAULT 'api', -- Source of the download (e.g., 'python', 'typescript', 'api')
+ meta_properties String DEFAULT '{}' -- String field with default empty object for additional properties
+ )
+ ENGINE = MergeTree()
+ ORDER BY (prompt_id, version_id, downloaded_at);
+ `,
+ ];
+
+ 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.ts
new file mode 100644
index 00000000..368d15c0
--- /dev/null
+++ b/src/client/src/clickhouse/migrations/index.ts
@@ -0,0 +1,5 @@
+import createPromptMigration from "./create-prompt-migration";
+
+export default async function migrations(databaseConfigId?: string) {
+ await createPromptMigration(databaseConfigId);
+}
diff --git a/src/client/src/components/(playground)/api-keys/generate.tsx b/src/client/src/components/(playground)/api-keys/generate.tsx
new file mode 100644
index 00000000..a6f6f5e8
--- /dev/null
+++ b/src/client/src/components/(playground)/api-keys/generate.tsx
@@ -0,0 +1,100 @@
+import { Button } from "@/components/ui/button";
+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
+import { noop } from "@/utils/noop";
+import copy from "copy-to-clipboard";
+import { toast } from "sonner";
+import FormBuilder, {
+ FormBuilderEvent,
+} from "@/components/common/form-builder";
+import {
+ Dialog,
+ DialogContent,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+import { FieldProps } from "@/components/common/form-field";
+import { jsonStringify } from "@/utils/json";
+import { useState } from "react";
+
+export default function Generate({ refresh }: { refresh: () => void }) {
+ const [isOpen, setIsOpen] = useState(false);
+ const { fireRequest: fireCreateRequest, isLoading: isCreating } =
+ useFetchWrapper();
+ const handleCreation: FormBuilderEvent = async (event, formdata) => {
+ event.preventDefault();
+ if (formdata.name?.length < 3) {
+ toast.error("Name length should be greater than 2...", {
+ id: "api-key",
+ });
+ return;
+ }
+ toast.loading("Generating api key...", {
+ id: "api-key",
+ });
+ fireCreateRequest({
+ requestType: "POST",
+ url: `/api/api-key`,
+ body: jsonStringify({
+ name: formdata.name,
+ }),
+ successCb: (data: any) => {
+ copy(data.apiKey);
+ toast.success("Generated and Copied new API key!", {
+ id: "api-key",
+ });
+ setIsOpen(false);
+ refresh();
+ },
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "api-key",
+ });
+ },
+ });
+ };
+
+ const formFields: FieldProps[] = [
+ {
+ label: "Name",
+ description: "Assign a name to api key for better references in future",
+ inputKey: `api-name`,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "name",
+ placeholder: "",
+ defaultValue: "default",
+ },
+ },
+ ];
+
+ return (
+ <Dialog onOpenChange={setIsOpen} open={isOpen}>
+ <DialogTrigger asChild>
+ <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 py-2 h-auto py-2 rounded-sm self-end"
+ >
+ Generate New API Key
+ </Button>
+ </DialogTrigger>
+ <DialogContent className="max-w-lg">
+ <DialogHeader>
+ <DialogTitle className="dark:text-stone-200 text-stone-800">
+ Create a new api key
+ </DialogTitle>
+ </DialogHeader>
+ <div className="flex items-center overflow-y-auto">
+ <FormBuilder
+ alignment="vertical"
+ fields={formFields}
+ isLoading={isCreating}
+ onSubmit={isCreating ? noop : handleCreation}
+ submitButtonText="Create"
+ />
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/client/src/components/(playground)/api-keys/manage.tsx b/src/client/src/components/(playground)/api-keys/manage.tsx
new file mode 100644
index 00000000..29f1d673
--- /dev/null
+++ b/src/client/src/components/(playground)/api-keys/manage.tsx
@@ -0,0 +1,134 @@
+"use client";
+import ConfirmationModal from "@/components/common/confirmation-modal";
+import TableData from "@/components/common/table-data";
+import { Badge } from "@/components/ui/badge";
+import { getPingStatus } from "@/selectors/database-config";
+import { useRootStore } from "@/store";
+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
+import copy from "copy-to-clipboard";
+import { format } from "date-fns";
+import { get } from "lodash";
+import { CopyIcon, TrashIcon } from "lucide-react";
+import { useCallback, useEffect } from "react";
+import { toast } from "sonner";
+import Generate from "./generate";
+
+const columns = [
+ {
+ className: "col-span-3",
+ header: "Name",
+ key: "name",
+ },
+ {
+ className: "col-span-2",
+ header: "Api key",
+ render: (data: any) => {
+ const apiKey = get(data, "apiKey");
+ return (
+ <Badge variant="outline" className="rounded-md">
+ {apiKey.replace(/(openlit-.{4}).*(.{6})$/, "$1...$2")}
+ </Badge>
+ );
+ },
+ },
+ {
+ key: "createdByUser.email",
+ className: "col-span-3",
+ header: "Created By",
+ },
+ {
+ className: "col-span-2",
+ header: "Created At",
+ render: (data: any) => {
+ const createdAt = get(data, "createdAt");
+ return format(createdAt, "MMM do, y");
+ },
+ },
+ {
+ header: "Actions",
+ className: "col-span-2 text-center",
+ render: (data: any, extraFunction?: any) => {
+ const apiKey = get(data, "apiKey");
+ const copyAPIKey = () => {
+ copy(apiKey);
+ toast.success("Copied!", {
+ id: "api-key",
+ });
+ };
+ return (
+ <div className="flex gap-4 justify-center">
+ <ConfirmationModal
+ handleYes={extraFunction?.handleYes}
+ title="Are you sure you want to delete?"
+ subtitle="Deleting API keys 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>
+ <CopyIcon
+ className="w-4 cursor-pointer hover:text-primary"
+ onClick={copyAPIKey}
+ />
+ </div>
+ );
+ },
+ },
+];
+
+export default function ManageKeys() {
+ const { data, fireRequest, isFetched, isLoading } = useFetchWrapper();
+ const { fireRequest: fireDeleteRequest } = useFetchWrapper();
+ const pingStatus = useRootStore(getPingStatus);
+ const fetchData = useCallback(async () => {
+ fireRequest({
+ requestType: "GET",
+ url: `/api/api-key`,
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "api-key",
+ });
+ },
+ });
+ }, []);
+
+ useEffect(() => {
+ if (pingStatus !== "pending") {
+ fetchData();
+ }
+ }, [pingStatus]);
+
+ const handleYes = useCallback(
+ async ({ id }: { id: string }) => {
+ fireDeleteRequest({
+ requestType: "DELETE",
+ url: `/api/api-key/${id}`,
+ successCb: () => {
+ fetchData();
+ },
+ failureCb: (err?: string) => {
+ toast.error(err || `Cannot connect to server!`, {
+ id: "api-key",
+ });
+ },
+ });
+ },
+ [fetchData]
+ );
+
+ return (
+ <div className="flex flex-col w-full gap-3">
+ <Generate refresh={fetchData} />
+ <TableData
+ columns={columns}
+ data={(data || []) as any[]}
+ isFetched={isFetched}
+ isLoading={isLoading}
+ extraFunction={{
+ handleYes,
+ }}
+ />
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx
index e3829a78..eaf12baa 100644
--- a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx
+++ b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx
@@ -6,7 +6,12 @@ import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect } from "react";
-const ALLOWED_CONNECTIVITY_ALERT = ["/dashboard", "/requests"];
+const ALLOWED_CONNECTIVITY_ALERT = [
+ "/dashboard",
+ "/requests",
+ "/exceptions",
+ "/prompt-hub",
+];
export default function ClickhouseConnectivityWrapper() {
const pingDetails = useRootStore(getPingDetails);
diff --git a/src/client/src/components/(playground)/prompt-hub/form.tsx b/src/client/src/components/(playground)/prompt-hub/form.tsx
new file mode 100644
index 00000000..197a22c9
--- /dev/null
+++ b/src/client/src/components/(playground)/prompt-hub/form.tsx
@@ -0,0 +1,297 @@
+import FormBuilder, {
+ FormBuilderEvent,
+} from "@/components/common/form-builder";
+import { FieldProps } from "@/components/common/form-field";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+import { PromptInput, PromptUpdate } from "@/constants/prompts";
+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";
+import { jsonParse } from "@/utils/json";
+import { objectEntries } from "@/utils/object";
+import { useRouter } from "next/navigation";
+import { KeyboardEvent, useCallback, useState } from "react";
+import { toast } from "sonner";
+
+const getVersions = (startingVersion: string) => {
+ const versionNumbers = startingVersion
+ .split(".")
+ .map((val) => parseInt(val, 10));
+ return {
+ draft: startingVersion,
+ major: [versionNumbers[0] + 1, 0, 0].join("."),
+ minor: [versionNumbers[0], versionNumbers[1] + 1, 0].join("."),
+ patch: [versionNumbers[0], versionNumbers[1], versionNumbers[2] + 1].join(
+ "."
+ ),
+ };
+};
+
+const DEFAULT_META_PROPERTIES = {
+ model: "",
+ temperature: "",
+};
+
+export default function PromptForm({
+ versionData,
+ children,
+ successCallback,
+}: {
+ versionData?: any;
+ children: JSX.Element;
+ successCallback?: () => void;
+}) {
+ const router = useRouter();
+ const [isOpen, setIsOpen] = useState(false);
+ const { fireRequest, isLoading } = useFetchWrapper();
+ const { fireRequest: fireUpdateRequest, isLoading: isUpdateLoading } =
+ useFetchWrapper();
+ const createPrompt: FormBuilderEvent = useCallback((event, formdata) => {
+ event.preventDefault();
+ toast.loading("Creating prompt...", {
+ id: "prompt-hub",
+ });
+
+ const payload: PromptInput = formdata;
+
+ payload.metaProperties = (formdata.metaProperties || []).reduce(
+ (
+ acc: Record<string, unknown>,
+ { key, value }: { key: string; value: unknown }
+ ) => {
+ acc[key] = value;
+ return acc;
+ },
+ {}
+ );
+
+ payload.status = payload.version === "0.0.0" ? "DRAFT" : "PUBLISHED";
+
+ fireRequest({
+ body: JSON.stringify(payload),
+ requestType: "POST",
+ url: "/api/prompt",
+ successCb: (response: any) => {
+ toast.success("Created prompt successfully!", {
+ id: "prompt-hub",
+ });
+ setIsOpen(false);
+ if (response?.data?.promptId) {
+ router.push(`/prompt-hub/${response?.data?.promptId}`);
+ }
+ if (typeof successCallback === "function") {
+ successCallback();
+ }
+ },
+ failureCb: (err?: string) => {
+ toast.error(err || "Creation of prompt failed!", {
+ id: "prompt-hub",
+ });
+ },
+ });
+ }, []);
+
+ const updateVersion: FormBuilderEvent = useCallback((event, formdata) => {
+ event.preventDefault();
+ toast.loading("Update prompt version...", {
+ id: "prompt-hub",
+ });
+
+ const payload: PromptUpdate = formdata;
+
+ payload.metaProperties = (formdata.metaProperties || {}).reduce(
+ (
+ acc: Record<string, unknown>,
+ { key, value }: { key: string; value: unknown }
+ ) => {
+ acc[key] = value;
+ return acc;
+ },
+ {}
+ );
+
+ payload.status =
+ payload.version === versionData.version ? "DRAFT" : "PUBLISHED";
+
+ if (versionData.status === "DRAFT") {
+ payload.versionId = versionData.versionId;
+ }
+
+ payload.promptId = versionData.promptId;
+
+ fireUpdateRequest({
+ body: JSON.stringify(payload),
+ requestType: "POST",
+ url: "/api/prompt/version",
+ successCb: (response: any) => {
+ toast.success("Updated prompt version successfully!", {
+ id: "prompt-hub",
+ });
+ setIsOpen(false);
+ if (response?.data?.promptId) {
+ router.push(
+ `/prompt-hub/${response?.data?.promptId}?version=${payload.version}`
+ );
+ }
+ if (typeof successCallback === "function") {
+ successCallback();
+ }
+ },
+ failureCb: (err?: string) => {
+ toast.error(err || "Updation of prompt version failed!", {
+ id: "prompt-hub",
+ });
+ },
+ });
+ }, []);
+
+ let formFields: FieldProps[] = [];
+ const defaultTags = versionData?.tags ? jsonParse(versionData?.tags) : [];
+ let defaultMetaProperties = versionData?.metaProperties
+ ? jsonParse(versionData?.metaProperties)
+ : DEFAULT_META_PROPERTIES;
+ defaultMetaProperties = objectEntries(defaultMetaProperties).reduce(
+ (acc: any, [key, value]) => {
+ acc.push({ key, value });
+ return acc;
+ },
+ []
+ );
+
+ const newVersions = getVersions(versionData?.version || "0.0.0");
+
+ if (!versionData?.versionId) {
+ formFields.push({
+ label: "Name",
+ description: "Only small alphabets and _ are allowed",
+ inputKey: `${versionData?.versionId || "new"}-name`,
+ fieldType: "INPUT",
+ fieldTypeProps: {
+ type: "text",
+ name: "name",
+ placeholder: "",
+ defaultValue: "",
+ onKeyUp: (event: KeyboardEvent) => {
+ const target = event.target as HTMLInputElement;
+ let input = target.value;
+
+ // Replace spaces with underscores
+ input = input.toLowerCase().replace(/ /g, "_");
+
+ // Allow only lowercase alphabet letters and underscores
+ input = input.replace(/[^a-z_]/g, "");
+
+ // Update the input field value
+ target.value = input;
+ },
+ },
+ });
+ }
+ formFields = formFields.concat([
+ {
+ label: "Prompt",
+ description:
+ "You can use {{variableName}} in the prompt to create dynamic prompts.",
+ inputKey: `${versionData?.versionId || "new"}-prompt`,
+ fieldType: "TEXTAREA",
+ fieldTypeProps: {
+ name: "prompt",
+ placeholder: "How can I assist you?",
+ defaultValue: versionData?.prompt,
+ },
+ },
+ {
+ label: "Tags",
+ inputKey: `${versionData?.versionId || "new"}-tags`,
+ fieldType: "TAGSINPUT",
+ fieldTypeProps: {
+ name: "tags",
+ placeholder: "Add tags",
+ defaultValue: defaultTags,
+ },
+ },
+ {
+ label: "Meta Properties",
+ inputKey: `${versionData?.versionId || "new"}-metaProperties`,
+ fieldType: "KEYVALUE",
+ fieldTypeProps: {
+ name: "metaProperties",
+ placeholder: "Meta",
+ defaultValue: defaultMetaProperties,
+ },
+ },
+ {
+ label: "Version",
+ inputKey: `${versionData?.versionId || "new"}-version`,
+ fieldType: "RADIOGROUP",
+ fieldTypeProps: {
+ name: "version",
+ placeholder: "Select version",
+ defaultValue: newVersions.draft,
+ options: [
+ {
+ title: "Draft state",
+ subText: "No Version change",
+ value: newVersions.draft,
+ description: "This will be in draft state",
+ },
+ {
+ title: "Major",
+ subText: `Version : ${newVersions.major}`,
+ value: newVersions.major,
+ description:
+ "Significant changes that are not backwards compatible",
+ },
+ {
+ title: "Minor",
+ subText: `Version : ${newVersions.minor}`,
+ value: newVersions.minor,
+ description: "New features that are backwards compatible",
+ },
+ {
+ title: "Patch",
+ subText: `Version : ${newVersions.patch}`,
+ value: newVersions.patch,
+ description: "Bug fixes and minor updates",
+ },
+ ],
+ },
+ },
+ ]);
+
+ return (
+ <Dialog onOpenChange={setIsOpen} open={isOpen}>
+ <DialogTrigger asChild>{children}</DialogTrigger>
+ <DialogContent className="max-w-4xl h-[80%]">
+ <DialogHeader>
+ <DialogTitle className="dark:text-stone-200 text-stone-800">
+ {versionData?.versionId
+ ? "Create new / Update current version of prompt"
+ : "Create a new prompt"}
+ </DialogTitle>
+ <DialogDescription>
+ You can create a new version of prompt in Draft/Published mode
+ </DialogDescription>
+ </DialogHeader>
+ <div className="flex items-center overflow-y-auto">
+ <FormBuilder
+ alignment="vertical"
+ fields={formFields}
+ isLoading={isLoading || isUpdateLoading}
+ onSubmit={versionData?.versionId ? updateVersion : createPrompt}
+ submitButtonText={`${
+ versionData?.versionId
+ ? "Create new version"
+ : "Create new prompt"
+ }`}
+ />
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/client/src/components/(playground)/prompt-hub/header.tsx b/src/client/src/components/(playground)/prompt-hub/header.tsx
new file mode 100644
index 00000000..03f11ab4
--- /dev/null
+++ b/src/client/src/components/(playground)/prompt-hub/header.tsx
@@ -0,0 +1,31 @@
+import { Button } from "@/components/ui/button";
+import PromptForm from "./form";
+import RouteBreadcrumbs from "../route-breadcrumbs";
+import { useRootStore } from "@/store";
+import { getPingStatus } from "@/selectors/database-config";
+import PromptUsage from "./usage";
+
+export default function PromptHubHeader({
+ createNew,
+}: {
+ createNew?: boolean;
+}) {
+ const pingStatus = useRootStore(getPingStatus);
+
+ return (
+ <div className="flex w-full items-center">
+ <RouteBreadcrumbs />
+ {pingStatus === "success" && <PromptUsage />}
+ {createNew && pingStatus === "success" && (
+ <PromptForm>
+ <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>
+ </PromptForm>
+ )}
+ </div>
+ );
+}
diff --git a/src/client/src/components/(playground)/prompt-hub/usage.tsx b/src/client/src/components/(playground)/prompt-hub/usage.tsx
new file mode 100644
index 00000000..03cbebce
--- /dev/null
+++ b/src/client/src/components/(playground)/prompt-hub/usage.tsx
@@ -0,0 +1,185 @@
+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 PromptUsageType() {
+ 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 go prompting.
+ </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 go prompting.
+ </CardDescription>
+ </CardHeader>
+ <CardContent className="space-y-2">
+ <CodeBlock
+ className="text-xs"
+ code={`
+const response = await Openlit.getPrompts({
+ name: "prompt_name",
+ shouldCompile: true,
+ variables: {
+ name: "John",
+ }
+});
+ `}
+ 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: "name",
+ description:
+ "This key is used to fetch unique prompt by name. This is optional. You can use either id or name.",
+ },
+ {
+ key: "promptId",
+ description:
+ "This key is used to fetch unique prompt by id. This is optional. You can use either id or name.",
+ },
+ {
+ key: "version",
+ description:
+ "This key is used to fetch specific version. This is optional.",
+ },
+ {
+ key: "shouldCompile",
+ description:
+ "This key is used to fetch prompt compiled with passed variables. This is optional. ",
+ },
+ {
+ key: "variables",
+ description:
+ "This key is used to pass variables to compile prompt. This is optional. ",
+ },
+ {
+ key: "metaProperties",
+ description:
+ "This key is used to pass meta properties to store in meta data of prompt access history. This is optional. ",
+ },
+];
+
+export default function PromptUsage() {
+ 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 prompts ?
+ </Button>
+ </DialogTrigger>
+ <DialogContent className="max-w-4xl h-[80%]">
+ <DialogHeader>
+ <DialogTitle className="dark:text-stone-200 text-stone-800">
+ Want to use prompts in your application?
+ </DialogTitle>
+ <DialogDescription>
+ Create your prompts 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">
+ <PromptUsageType />
+ <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/(playground)/route-breadcrumbs.tsx b/src/client/src/components/(playground)/route-breadcrumbs.tsx
new file mode 100644
index 00000000..1564bb2c
--- /dev/null
+++ b/src/client/src/components/(playground)/route-breadcrumbs.tsx
@@ -0,0 +1,59 @@
+import {
+ Breadcrumb,
+ BreadcrumbList,
+ BreadcrumbItem,
+ BreadcrumbLink,
+ BreadcrumbSeparator,
+ BreadcrumbPage,
+} from "@/components/ui/breadcrumb";
+import { usePathname } from "next/navigation";
+import React from "react";
+
+const PATH_TO_TITLE_MAP = {
+ "/": "Home",
+ "/requests": "Request",
+ "/exceptions": "Exceptions",
+ "/prompt-hub": "Prompts",
+ "/openground": "Openground",
+ "/database-config": "Databases",
+ "/profile": "Profile",
+};
+
+export default function RouteBreadcrumbs() {
+ const params = usePathname();
+ const paths = params.split("/");
+ return (
+ <Breadcrumb className="grow">
+ <BreadcrumbList>
+ {paths.length > 1
+ ? paths.map((path, index) => {
+ const pathField = `/${path}`;
+ if (index === paths.length - 1) {
+ return (
+ <BreadcrumbItem key={pathField}>
+ <BreadcrumbPage>
+ {PATH_TO_TITLE_MAP[
+ pathField as keyof typeof PATH_TO_TITLE_MAP
+ ] || path}
+ </BreadcrumbPage>
+ </BreadcrumbItem>
+ );
+ }
+ return (
+ <React.Fragment key={pathField}>
+ <BreadcrumbItem>
+ <BreadcrumbLink href={pathField}>
+ {PATH_TO_TITLE_MAP[
+ pathField as keyof typeof PATH_TO_TITLE_MAP
+ ] || path}
+ </BreadcrumbLink>
+ </BreadcrumbItem>
+ <BreadcrumbSeparator />
+ </React.Fragment>
+ );
+ })
+ : null}
+ </BreadcrumbList>
+ </Breadcrumb>
+ );
+}
diff --git a/src/client/src/components/(playground)/sidebar.tsx b/src/client/src/components/(playground)/sidebar.tsx
index fc968a76..96d0f246 100644
--- a/src/client/src/components/(playground)/sidebar.tsx
+++ b/src/client/src/components/(playground)/sidebar.tsx
@@ -10,8 +10,10 @@ import {
import { Button, buttonVariants } from "@/components/ui/button";
import {
BookText,
+ Component,
DatabaseBackup,
FileJson2,
+ KeyIcon,
LayoutDashboard,
MonitorPlay,
ShieldAlert,
@@ -28,8 +30,7 @@ type SidebarItemProps = {
target?: string;
};
-const ICON_CLASSES =
- "flex-shrink-0 size-5 transition duration-75 transition duration-75";
+const ICON_CLASSES = "flex-shrink-0 size-5";
const SIDEBAR_ITEMS: SidebarItemProps[] = [
{
@@ -47,6 +48,11 @@ const SIDEBAR_ITEMS: SidebarItemProps[] = [
text: "Exceptions",
link: "/exceptions",
},
+ {
+ icon: <Component className={ICON_CLASSES} />,
+ text: "Prompt Hub",
+ link: "/prompt-hub",
+ },
{
icon: <MonitorPlay className={ICON_CLASSES} />,
text: "Openground",
@@ -54,9 +60,14 @@ const SIDEBAR_ITEMS: SidebarItemProps[] = [
},
{
icon: <DatabaseBackup className={ICON_CLASSES} />,
- text: "Settings",
+ text: "Databases",
link: "/database-config",
},
+ {
+ icon: <KeyIcon className={ICON_CLASSES} />,
+ text: "API Keys",
+ link: "/api-keys",
+ },
];
const SIDEBAR_BOTTOM_ITEMS: SidebarItemProps[] = [
diff --git a/src/client/src/components/common/confirmation-modal.tsx b/src/client/src/components/common/confirmation-modal.tsx
new file mode 100644
index 00000000..49065e26
--- /dev/null
+++ b/src/client/src/components/common/confirmation-modal.tsx
@@ -0,0 +1,48 @@
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+
+type ConfirmationModalProps = {
+ title: string;
+ subtitle: string;
+ children: JSX.Element;
+ handleYes: (p?: any) => void;
+ handleNo?: () => void;
+ params?: any;
+};
+
+export default function ConfirmationModal(props: ConfirmationModalProps) {
+ return (
+ <Dialog>
+ <DialogTrigger asChild>{props.children}</DialogTrigger>
+ <DialogContent className="">
+ <DialogHeader>
+ <DialogTitle>{props.title}</DialogTitle>
+ <DialogDescription>{props.subtitle}</DialogDescription>
+ </DialogHeader>
+ <DialogFooter className="justify-end">
+ <DialogClose asChild>
+ <Button variant="secondary" onClick={props.handleNo}>
+ No, cancel
+ </Button>
+ </DialogClose>
+
+ <Button
+ variant="default"
+ onClick={() => props.handleYes(props.params)}
+ >
+ Yes, I&apos;m sure
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/client/src/components/common/form-builder.tsx b/src/client/src/components/common/form-builder.tsx
index e35f9039..cd8ee951 100644
--- a/src/client/src/components/common/form-builder.tsx
+++ b/src/client/src/components/common/form-builder.tsx
@@ -1,48 +1,11 @@
import { noop } from "@/utils/noop";
-import React, { FormEventHandler, HTMLInputTypeAttribute } from "react";
-import { Label } from "@/components/ui/label";
-import { Input } from "@/components/ui/input";
+import React, { FormEvent } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
+import FormField, { FieldProps } from "./form-field";
+import { set } from "lodash";
-type FieldProps = {
- label: string;
- type: HTMLInputTypeAttribute;
- name: string;
- placeholder?: string;
- defaultValue?: string;
- inputKey?: string;
- disabled?: boolean;
-};
-
-const FormField = ({
- label,
- type,
- name,
- placeholder,
- defaultValue,
- inputKey,
- disabled,
-}: FieldProps) => {
- return (
- <div className="grid grid-cols-3 flex-1 items-center">
- <Label htmlFor={name} className="col-span-1">
- {label}
- </Label>
- <Input
- autoComplete="off"
- className="col-span-2"
- key={inputKey || `${name}-${defaultValue}`}
- type={type}
- name={name}
- id={name}
- placeholder={placeholder}
- defaultValue={defaultValue}
- disabled={disabled}
- />
- </div>
- );
-};
+export type FormBuilderEvent = (e: FormEvent<Element>, formData: any) => void;
const FormBuilder = ({
fields,
@@ -53,34 +16,100 @@ const FormBuilder = ({
onSubmit,
submitButtonText,
isAllowedToSubmit = true,
+ alignment = "horizontal",
}: {
fields: FieldProps[];
heading?: string;
subHeading?: string;
subHeadingClass?: string;
isLoading?: boolean;
- onSubmit: FormEventHandler<HTMLFormElement>;
+ onSubmit: FormBuilderEvent;
submitButtonText: string;
isAllowedToSubmit?: boolean;
+ alignment?: "horizontal" | "vertical";
}) => {
+ const getFormData = (e: FormEvent) => {
+ const formElement = e.target as HTMLFormElement;
+ return fields.reduce((acc: any, field) => {
+ if (
+ field.fieldType === "INPUT" ||
+ field.fieldType === "TEXTAREA" ||
+ field.fieldType === "RADIOGROUP"
+ ) {
+ if (field.fieldTypeProps.name) {
+ acc[field.fieldTypeProps.name] =
+ formElement[field.fieldTypeProps.name].value;
+ }
+ } else if (field.fieldType === "TAGSINPUT") {
+ if (
+ field.fieldTypeProps.name &&
+ formElement[field.fieldTypeProps.name]
+ ) {
+ if (
+ NodeList.prototype.isPrototypeOf(
+ formElement[field.fieldTypeProps.name]
+ )
+ ) {
+ acc[field.fieldTypeProps.name] = [
+ ...formElement[field.fieldTypeProps.name],
+ ].map((i) => i.value);
+ } else {
+ acc[field.fieldTypeProps.name] = [
+ formElement[field.fieldTypeProps.name].value,
+ ];
+ }
+ }
+ } else if (field.fieldType === "KEYVALUE" && field.fieldTypeProps.name) {
+ formElement
+ .querySelectorAll(`[name*=${field.fieldTypeProps.name}]`)
+ .forEach((element) => {
+ set(
+ acc,
+ element.getAttribute("name")!,
+ element.getAttribute("value")
+ );
+ });
+ }
+ return acc;
+ }, {});
+ };
+
return (
<form
- className="flex flex-col w-full"
- onSubmit={isLoading || !isAllowedToSubmit ? noop : onSubmit}
+ className="flex flex-col w-full h-full"
+ onSubmit={(e) => {
+ e.preventDefault();
+ if (isLoading || !isAllowedToSubmit) {
+ return noop();
+ }
+
+ return onSubmit(e, getFormData(e));
+ }}
+ onKeyDown={(e) => (!(e.key === "Enter"))}
>
<Card className="w-full border-0 flex flex-col h-full">
- <CardHeader className="shrink-0">
- <CardTitle className="text-2xl">{heading}</CardTitle>
- {subHeading && (
- <CardTitle className={`text-sm ${subHeadingClass}`}>
- {subHeading}
- </CardTitle>
- )}
- </CardHeader>
- <CardContent className="flex gap-4 flex-col overflow-hidden">
- <div className="grid gap-5 relative flex-1 overflow-y-auto ">
+ {heading && (
+ <CardHeader className="shrink-0 px-0 pt-0 pb-4">
+ <CardTitle className="text-2xl">{heading}</CardTitle>
+ {subHeading && (
+ <CardTitle className={`text-sm ${subHeadingClass}`}>
+ {subHeading}
+ </CardTitle>
+ )}
+ </CardHeader>
+ )}
+ <CardContent className="flex gap-4 flex-col overflow-hidden p-0">
+ <div className="grid gap-6 relative flex-1 overflow-y-auto">
{fields.map((field, index) => (
- <FormField key={index} {...field} />
+ <FormField
+ key={index}
+ {...field}
+ boundaryClass={
+ alignment === "horizontal"
+ ? "grid grid-cols-3 flex-1 items-center"
+ : "grid grid-cols-1 flex-1 items-center gap-2"
+ }
+ />
))}
</div>
<div className="flex items-center justify-end w-full gap-3">
diff --git a/src/client/src/components/common/form-field.tsx b/src/client/src/components/common/form-field.tsx
new file mode 100644
index 00000000..2ed3c772
--- /dev/null
+++ b/src/client/src/components/common/form-field.tsx
@@ -0,0 +1,303 @@
+import {
+ Fragment,
+ KeyboardEvent,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import { Label } from "@/components/ui/label";
+import { Input, InputProps } from "@/components/ui/input";
+import { Textarea, TextareaProps } from "@/components/ui/textarea";
+import { PlusIcon, XIcon } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
+
+export type FieldTypes =
+ | "INPUT"
+ | "TEXTAREA"
+ | "TAGSINPUT"
+ | "KEYVALUE"
+ | "RADIOGROUP";
+interface RadioGroupProps {
+ name: string;
+ options: {
+ title: string;
+ subText: string;
+ description?: string;
+ value: string;
+ }[];
+ defaultValue?: string;
+ placeholder?: string;
+ onKeyUp?: any;
+ onChange?: any;
+}
+
+interface KeyValuePairProps {
+ name: string;
+ defaultValue?: { key: string; value: any }[];
+ placeholder?: string;
+ onKeyUp?: any;
+ onChange?: any;
+}
+
+export type FieldTypeProps =
+ | InputProps
+ | TextareaProps
+ | RadioGroupProps
+ | KeyValuePairProps;
+
+export type FieldProps = {
+ label: string;
+ description?: string;
+ fieldType: FieldTypes;
+ fieldTypeProps: FieldTypeProps;
+ inputKey?: string;
+};
+
+function FormTagsInputField(props: FieldProps) {
+ const [tags, setTags] = useState<string[]>(
+ (props.fieldTypeProps.defaultValue as string[]) || []
+ );
+ const removeTag = (tagToRemove: string) => {
+ setTags(tags.filter((tag) => tag !== tagToRemove));
+ };
+ const addTag = useCallback(
+ (e: KeyboardEvent<HTMLInputElement>) => {
+ const value = e.currentTarget.value.trim();
+ if (value.trim() !== "") {
+ setTags((t) => {
+ return Array.from(new Set([...t, value.trim()]));
+ });
+ e.currentTarget.value = "";
+ }
+ },
+ [tags, setTags]
+ );
+
+ const onKeyDownHandler = (e: KeyboardEvent<HTMLInputElement>) => {
+ typeof props.fieldTypeProps.onKeyUp === "function" &&
+ props.fieldTypeProps.onKeyUp(e as any);
+ if (e.key === "Enter") {
+ e.preventDefault();
+ addTag(e);
+ }
+ };
+
+ return (
+ <div className="grid grid-col-1 gap-2">
+ <Input
+ key={
+ props.inputKey ||
+ `${props.fieldTypeProps.name}-${props.fieldTypeProps.defaultValue}`
+ }
+ placeholder={props.fieldTypeProps.placeholder}
+ onKeyDown={onKeyDownHandler}
+ />
+ <div className="flex flex-wrap gap-2 mb-2">
+ {tags.map((tag, index) => (
+ <Fragment key={tag}>
+ <Input
+ type="hidden"
+ name={`${props.fieldTypeProps.name}`}
+ value={tag}
+ />
+ <span className="flex items-center gap-2 px-3 py-1 rounded-full text-sm flex items-center bg-stone-200 text-stone-500 dark:bg-stone-800 dark:text-stone-300 transition-none">
+ {tag}
+ <button onClick={() => removeTag(tag)}>
+ <XIcon size={14} />
+ </button>
+ </span>
+ </Fragment>
+ ))}
+ </div>
+ </div>
+ );
+}
+
+function FormKeyValueField(props: FieldProps) {
+ const [metaProperties, setMetaProperties] = useState<
+ { key: string; value: string }[]
+ >(
+ (props.fieldTypeProps.defaultValue as { key: string; value: string }[]) ||
+ []
+ );
+ const addMetaProperty = () => {
+ setMetaProperties([...metaProperties, { key: "", value: "" }]);
+ };
+
+ const updateMetaProperty = (index: number, key: string, value: string) => {
+ const updatedProperties = [...metaProperties];
+ updatedProperties[index] = { ...updatedProperties[index], [key]: value };
+ setMetaProperties(updatedProperties);
+ };
+
+ const removeMetaProperty = (index: number) => {
+ setMetaProperties(metaProperties.filter((_, i) => i !== index));
+ };
+
+ const onKeyDownHandler = (e: KeyboardEvent<HTMLInputElement>) => {
+ if (e.key === "Enter") {
+ e.preventDefault();
+ }
+ };
+
+ const { name = "keyValue", placeholder = "" } = props.fieldTypeProps;
+
+ return (
+ <div className="grid grid-col-1 w-100 gap-4">
+ {metaProperties.map((prop, index) => (
+ <div key={index} className="flex gap-2 items-center">
+ <Input
+ placeholder={`${placeholder} Key`}
+ name={`${name}[${index}].key`}
+ value={prop.key}
+ onChange={(e) => updateMetaProperty(index, "key", e.target.value)}
+ onKeyDown={onKeyDownHandler}
+ />
+ <Input
+ placeholder={`${placeholder} Value`}
+ name={`${name}[${index}].value`}
+ value={prop.value}
+ onChange={(e) => updateMetaProperty(index, "value", e.target.value)}
+ onKeyDown={onKeyDownHandler}
+ />
+ <Button
+ onClick={() => removeMetaProperty(index)}
+ variant="ghost"
+ size="icon"
+ >
+ <XIcon className="h-4 w-4" />
+ </Button>
+ </div>
+ ))}
+ <Button
+ onClick={addMetaProperty}
+ type="button"
+ variant="outline"
+ className="mt-2"
+ >
+ <PlusIcon className="mr-2 h-4 w-4" /> Add
+ </Button>
+ </div>
+ );
+}
+
+function FormRadioGroupField(props: FieldProps) {
+ const [currentDescription, setCurrentDescription] = useState<string>("");
+ const itemRef = useRef(null);
+ const fieldTypeProps = props.fieldTypeProps as RadioGroupProps;
+ const findIndexAndSet = (value: string) => {
+ const findIndex = fieldTypeProps.options.findIndex(
+ (option) => option.value === value
+ );
+ setCurrentDescription(fieldTypeProps.options[findIndex]?.description || "");
+ };
+ const onMouseEnter = (ev: any) => {
+ const { value } = ev.currentTarget.dataset;
+ findIndexAndSet(value);
+ };
+
+ const onMouseLeave = () => {
+ let value;
+ if (
+ itemRef.current &&
+ typeof (itemRef.current as HTMLElement).querySelector === "function"
+ ) {
+ value = (
+ (itemRef.current as HTMLElement).querySelector(
+ "input:checked"
+ ) as HTMLInputElement
+ )?.value;
+ }
+ value = value || (props.fieldTypeProps.defaultValue as string) || "";
+ findIndexAndSet(value);
+ };
+
+ useEffect(() => {
+ findIndexAndSet(props.fieldTypeProps.defaultValue as string);
+ }, []);
+
+ return (
+ <>
+ <RadioGroup
+ onValueChange={fieldTypeProps.onChange}
+ defaultValue={fieldTypeProps.defaultValue as string}
+ name={fieldTypeProps.name}
+ ref={itemRef}
+ className="grid grid-cols-4 gap-8 pt-2"
+ >
+ {fieldTypeProps.options.map((option) => {
+ return (
+ <Label
+ className="p-1 cursor-pointer rounded-md border dark:border-stone-800 has-[:checked]:border-stone-400 has-[:checked]:dark:border-stone-500"
+ onMouseEnter={onMouseEnter}
+ onMouseLeave={onMouseLeave}
+ data-value={option.value}
+ key={option.value}
+ >
+ <RadioGroupItem value={option.value} className="sr-only" />
+ <div className="space-y-1 bg-stone-100 dark:bg-stone-800 p-4">
+ <h4 className="text-sm font-semibold">{option.title}</h4>
+ <p className="text-sm">{option.subText}</p>
+ </div>
+ </Label>
+ );
+ })}
+ </RadioGroup>
+ <div className="h-4 text-xs text-stone-400">
+ {currentDescription || "Choose one version type"}
+ </div>
+ </>
+ );
+}
+
+function FormInputField(props: FieldProps) {
+ return (
+ <Input
+ className="col-span-2"
+ key={props.inputKey || `${name}-${props.fieldTypeProps.defaultValue}`}
+ {...(props.fieldTypeProps as InputProps)}
+ />
+ );
+}
+
+function FormTextareaField(props: FieldProps) {
+ return (
+ <Textarea
+ className="col-span-2"
+ key={props.inputKey || `${name}-${props.fieldTypeProps.defaultValue}`}
+ {...(props.fieldTypeProps as TextareaProps)}
+ />
+ );
+}
+
+export default function FormField(
+ props: FieldProps & {
+ boundaryClass?: string;
+ }
+) {
+ return (
+ <div className={props.boundaryClass}>
+ <Label htmlFor={props.fieldTypeProps.name} className="col-span-1">
+ {props.label}
+ </Label>
+ {props.fieldType === "INPUT" ? (
+ <FormInputField {...props} />
+ ) : props.fieldType === "TEXTAREA" ? (
+ <FormTextareaField {...props} />
+ ) : props.fieldType === "TAGSINPUT" ? (
+ <FormTagsInputField {...props} />
+ ) : props.fieldType === "KEYVALUE" ? (
+ <FormKeyValueField {...props} />
+ ) : props.fieldType === "RADIOGROUP" ? (
+ <FormRadioGroupField {...props} />
+ ) : null}
+ {props.description ? (
+ <span className="text-xs text-stone-400 -mt-[5px]">
+ {props.description}
+ </span>
+ ) : null}
+ </div>
+ );
+}
diff --git a/src/client/src/components/common/table-data.tsx b/src/client/src/components/common/table-data.tsx
new file mode 100644
index 00000000..116ce6f8
--- /dev/null
+++ b/src/client/src/components/common/table-data.tsx
@@ -0,0 +1,83 @@
+import { get } from "lodash";
+import IntermediateState from "@/components/(playground)/intermediate-state";
+
+export type TableDataProps = {
+ className?: string;
+ columns: {
+ key?: string;
+ className?: string;
+ header: string;
+ render?: (data: any, extraFunction?: any) => any;
+ }[];
+ isFetched?: boolean;
+ isLoading?: boolean;
+ data: any[];
+ idKey?: string;
+ extraFunction?: any;
+};
+
+export default function TableData({
+ className,
+ columns,
+ isFetched,
+ isLoading,
+ data,
+ idKey = "id",
+ extraFunction,
+}: TableDataProps) {
+ return (
+ <div
+ className={`flex flex-col w-full relative overflow-hidden rounded-md border dark:border-stone-500 ${
+ className || ""
+ }`}
+ >
+ <div className="grid grid-cols-12 border-b text-stone-500 text-sm bg-stone-200 dark:bg-stone-800 dark:text-stone-400 dark:border-stone-500">
+ {columns.map((column, index) => {
+ return (
+ <div
+ key={index}
+ className={`items-center py-2 px-3 ${column.className}`}
+ >
+ {column.header}
+ </div>
+ );
+ })}
+ </div>
+ <div
+ className={`flex flex-col w-full text-sm text-left relative overflow-auto ${
+ isFetched && isLoading ? "animate-pulse" : ""
+ }`}
+ >
+ {(!isFetched || (isLoading && !data?.length)) && (
+ <div className={`flex items-center justify-center py-4 px-3`}>
+ <div className="h-2 w-full bg-stone-100 dark:bg-stone-900 rounded col-span-1" />
+ </div>
+ )}
+ {data.map((item: any, index: number) => {
+ return (
+ <div
+ className={`grid grid-cols-12 ${
+ index === data.length - 1
+ ? ""
+ : "border-b dark:border-stone-500"
+ } items-center text-stone-600 dark:text-stone-300 group`}
+ key={item[idKey]}
+ >
+ {columns.map(({ key, render, className }, index) => (
+ <div
+ className={`${className} items-center py-2 px-3 text-ellipsis overflow-hidden`}
+ key={`${item[idKey]}-column-${index}`}
+ >
+ {render ? render(item, extraFunction) : get(item, key || "")}
+ </div>
+ ))}
+ </div>
+ );
+ })}
+ {!data?.length && !isLoading && isFetched && (
+ <IntermediateState type="nodata" classNames="!p-3 text-xs" />
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/src/components/ui/breadcrumb.tsx b/src/client/src/components/ui/breadcrumb.tsx
new file mode 100644
index 00000000..085ac3fd
--- /dev/null
+++ b/src/client/src/components/ui/breadcrumb.tsx
@@ -0,0 +1,115 @@
+import * as React from "react"
+import { Slot } from "@radix-ui/react-slot"
+import { ChevronRight, MoreHorizontal } from "lucide-react"
+
+import { cn } from "@/lib/utils"
+
+const Breadcrumb = React.forwardRef<
+ HTMLElement,
+ React.ComponentPropsWithoutRef<"nav"> & {
+ separator?: React.ReactNode
+ }
+>(({ ...props }, ref) => <nav ref={ref} aria-label="breadcrumb" {...props} />)
+Breadcrumb.displayName = "Breadcrumb"
+
+const BreadcrumbList = React.forwardRef<
+ HTMLOListElement,
+ React.ComponentPropsWithoutRef<"ol">
+>(({ className, ...props }, ref) => (
+ <ol
+ ref={ref}
+ className={cn(
+ "flex flex-wrap items-center gap-1.5 break-words text-sm text-stone-500 sm:gap-2.5 dark:text-stone-400",
+ className
+ )}
+ {...props}
+ />
+))
+BreadcrumbList.displayName = "BreadcrumbList"
+
+const BreadcrumbItem = React.forwardRef<
+ HTMLLIElement,
+ React.ComponentPropsWithoutRef<"li">
+>(({ className, ...props }, ref) => (
+ <li
+ ref={ref}
+ className={cn("inline-flex items-center gap-1.5", className)}
+ {...props}
+ />
+))
+BreadcrumbItem.displayName = "BreadcrumbItem"
+
+const BreadcrumbLink = React.forwardRef<
+ HTMLAnchorElement,
+ React.ComponentPropsWithoutRef<"a"> & {
+ asChild?: boolean
+ }
+>(({ asChild, className, ...props }, ref) => {
+ const Comp = asChild ? Slot : "a"
+
+ return (
+ <Comp
+ ref={ref}
+ className={cn("transition-colors hover:text-stone-950 dark:hover:text-stone-50", className)}
+ {...props}
+ />
+ )
+})
+BreadcrumbLink.displayName = "BreadcrumbLink"
+
+const BreadcrumbPage = React.forwardRef<
+ HTMLSpanElement,
+ React.ComponentPropsWithoutRef<"span">
+>(({ className, ...props }, ref) => (
+ <span
+ ref={ref}
+ role="link"
+ aria-disabled="true"
+ aria-current="page"
+ className={cn("font-normal text-stone-950 dark:text-stone-50", className)}
+ {...props}
+ />
+))
+BreadcrumbPage.displayName = "BreadcrumbPage"
+
+const BreadcrumbSeparator = ({
+ children,
+ className,
+ ...props
+}: React.ComponentProps<"li">) => (
+ <li
+ role="presentation"
+ aria-hidden="true"
+ className={cn("[&>svg]:size-3.5", className)}
+ {...props}
+ >
+ {children ?? <ChevronRight />}
+ </li>
+)
+BreadcrumbSeparator.displayName = "BreadcrumbSeparator"
+
+const BreadcrumbEllipsis = ({
+ className,
+ ...props
+}: React.ComponentProps<"span">) => (
+ <span
+ role="presentation"
+ aria-hidden="true"
+ className={cn("flex h-9 w-9 items-center justify-center", className)}
+ {...props}
+ >
+ <MoreHorizontal className="h-4 w-4" />
+ <span className="sr-only">More</span>
+ </span>
+)
+BreadcrumbEllipsis.displayName = "BreadcrumbElipssis"
+
+export {
+ Breadcrumb,
+ BreadcrumbList,
+ BreadcrumbItem,
+ BreadcrumbLink,
+ BreadcrumbPage,
+ BreadcrumbSeparator,
+ BreadcrumbEllipsis,
+}
diff --git a/src/client/src/components/ui/button.tsx b/src/client/src/components/ui/button.tsx
index 184451dd..41a4ff44 100644
--- a/src/client/src/components/ui/button.tsx
+++ b/src/client/src/components/ui/button.tsx
@@ -13,7 +13,7 @@ const buttonVariants = cva(
destructive:
"bg-red-500 text-stone-50 hover:bg-red-500/90 dark:bg-red-900 dark:text-stone-50 dark:hover:bg-red-900/90",
outline:
- "border border-stone-200 bg-white hover:bg-stone-100 hover:text-stone-900 dark:border-stone-800 dark:bg-stone-950 dark:hover:bg-stone-800 dark:hover:text-stone-50",
+ "border border-stone-200 bg-stone-100 hover:bg-stone-100 hover:text-stone-900 dark:border-stone-800 dark:bg-stone-950 dark:hover:bg-stone-800 dark:text-stone-50 dark:hover:text-stone-50",
secondary:
"bg-stone-100 text-stone-900 hover:bg-stone-100/80 dark:bg-stone-800 dark:text-stone-50 dark:hover:bg-stone-800/80",
ghost: "hover:bg-stone-100 hover:text-stone-900 dark:hover:bg-stone-800 dark:hover:text-stone-50",
diff --git a/src/client/src/components/ui/dialog.tsx b/src/client/src/components/ui/dialog.tsx
index 17cc184f..2a22b65f 100644
--- a/src/client/src/components/ui/dialog.tsx
+++ b/src/client/src/components/ui/dialog.tsx
@@ -88,7 +88,7 @@ const DialogTitle = React.forwardRef<
<DialogPrimitive.Title
ref={ref}
className={cn(
- "text-lg font-semibold leading-none tracking-tight",
+ "text-lg font-semibold leading-none tracking-tight text-stone-800 dark:text-stone-200",
className
)}
{...props}
diff --git a/src/client/src/components/ui/radio-group.tsx b/src/client/src/components/ui/radio-group.tsx
new file mode 100644
index 00000000..662cf5a8
--- /dev/null
+++ b/src/client/src/components/ui/radio-group.tsx
@@ -0,0 +1,44 @@
+"use client"
+
+import * as React from "react"
+import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"
+import { Circle } from "lucide-react"
+
+import { cn } from "@/lib/utils"
+
+const RadioGroup = React.forwardRef<
+ React.ElementRef<typeof RadioGroupPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>
+>(({ className, ...props }, ref) => {
+ return (
+ <RadioGroupPrimitive.Root
+ className={cn("grid gap-2", className)}
+ {...props}
+ ref={ref}
+ />
+ )
+})
+RadioGroup.displayName = RadioGroupPrimitive.Root.displayName
+
+const RadioGroupItem = React.forwardRef<
+ React.ElementRef<typeof RadioGroupPrimitive.Item>,
+ React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>
+>(({ className, ...props }, ref) => {
+ return (
+ <RadioGroupPrimitive.Item
+ ref={ref}
+ className={cn(
+ "aspect-square h-4 w-4 rounded-full border border-stone-200 border-stone-900 text-stone-900 ring-offset-white focus:outline-none focus-visible:ring-2 focus-visible:ring-stone-950 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-stone-800 dark:border-stone-50 dark:text-stone-50 dark:ring-offset-stone-950 dark:focus-visible:ring-stone-300",
+ className
+ )}
+ {...props}
+ >
+ <RadioGroupPrimitive.Indicator className="flex items-center justify-center">
+ <Circle className="h-2.5 w-2.5 fill-current text-current" />
+ </RadioGroupPrimitive.Indicator>
+ </RadioGroupPrimitive.Item>
+ )
+})
+RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName
+
+export { RadioGroup, RadioGroupItem }
diff --git a/src/client/src/components/ui/table.tsx b/src/client/src/components/ui/table.tsx
index 3ea800e7..79c7b948 100644
--- a/src/client/src/components/ui/table.tsx
+++ b/src/client/src/components/ui/table.tsx
@@ -56,7 +56,7 @@ const TableRow = React.forwardRef<
<tr
ref={ref}
className={cn(
- "border-b transition-colors hover:bg-stone-100/50 data-[state=selected]:bg-stone-100 dark:hover:bg-stone-800/50 dark:data-[state=selected]:bg-stone-800 border-none",
+ "border-b hover:bg-stone-100/50 data-[state=selected]:bg-stone-100 dark:hover:bg-stone-800/50 dark:data-[state=selected]:bg-stone-800 border-none",
className
)}
{...props}
diff --git a/src/client/src/components/ui/textarea.tsx b/src/client/src/components/ui/textarea.tsx
index 5bc0dcc1..3068674e 100644
--- a/src/client/src/components/ui/textarea.tsx
+++ b/src/client/src/components/ui/textarea.tsx
@@ -10,7 +10,7 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
return (
<textarea
className={cn(
- "flex min-h-[80px] w-full rounded-md border border-stone-200 bg-white px-3 py-2 text-sm ring-offset-white placeholder:text-stone-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-950 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-stone-800 dark:bg-stone-950 dark:ring-offset-stone-950 dark:placeholder:text-stone-400 dark:focus-visible:ring-stone-300",
+ "flex min-h-[80px] w-full rounded-md border border-stone-200 bg-white px-3 py-2 text-sm ring-offset-white placeholder:text-stone-500 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:border-stone-800 dark:bg-stone-950 dark:ring-offset-stone-950 dark:placeholder:text-stone-400",
className
)}
ref={ref}
diff --git a/src/client/src/constants/messages/en.ts b/src/client/src/constants/messages/en.ts
new file mode 100644
index 00000000..81ba561e
--- /dev/null
+++ b/src/client/src/constants/messages/en.ts
@@ -0,0 +1,18 @@
+export const DATABASE_CONFIG_NOT_FOUND = "No database config present!";
+export const UNAUTHORIZED_USER = "Unauthorized user!";
+export const MALFORMED_INPUTS = "Malformed input! Please check the docs";
+
+// API Keys
+export const NO_API_KEY = "No such apiKey exists!";
+
+// Prompts
+export const PROMPT_NAME_TAKEN = "Prompt name is already taken!";
+export const PROMPT_NOT_CREATED = "Prompt cannot be created!";
+export const PROMPT_SAVED = "Prompt saved successfully!";
+export const NO_PROMPT = "No such prompt exists or isn't released yet!";
+export const PROMPT_DELETED = "Prompt deleted successfully!";
+export const PROMPT_NOT_DELETED = "Error deleting prompt!";
+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!";
diff --git a/src/client/src/constants/messages/index.ts b/src/client/src/constants/messages/index.ts
new file mode 100644
index 00000000..173a4735
--- /dev/null
+++ b/src/client/src/constants/messages/index.ts
@@ -0,0 +1,5 @@
+import * as enMessages from "./en";
+
+export default function getMessage() {
+ return enMessages;
+}
diff --git a/src/client/src/constants/prompts.ts b/src/client/src/constants/prompts.ts
new file mode 100644
index 00000000..e685b7fc
--- /dev/null
+++ b/src/client/src/constants/prompts.ts
@@ -0,0 +1,63 @@
+export type Prompt = {
+ id: string;
+ name: string;
+ createdBy: string;
+ createdAt: Date;
+};
+
+export type PromptStatus = "PUBLISHED" | "DRAFT";
+
+export interface PromptInput {
+ name: string;
+ prompt: string;
+ version: string;
+ status: PromptStatus;
+ tags: string[];
+ metaProperties: Record<string, unknown>;
+}
+
+export interface PromptUpdate {
+ versionId?: string;
+ promptId: string;
+ prompt?: string;
+ version?: string;
+ status?: PromptStatus;
+ tags?: string[];
+ metaProperties?: Record<string, unknown>;
+}
+
+export type PromptVersion = {
+ id: string;
+ promptId: string;
+ version: string;
+ updatedBy: string;
+ updatedAt: Date;
+ prompt: string;
+ status: PromptStatus;
+ tags: string;
+ metaProperties: Record<string, unknown>;
+};
+
+export type PromptCompiledInput = {
+ id?: string;
+ name?: string;
+ apiKey: string;
+ variables?: Record<string, any>;
+ version?: string;
+ shouldCompile?: boolean;
+ downloadSource?: string;
+ downloadMetaProperties?: Record<string, unknown>;
+};
+
+export type SpecificPromptInput = {
+ id?: string;
+ name?: string;
+ version?: string;
+};
+
+export type PromptDownloadInput = {
+ promptId: string;
+ versionId: string;
+ downloadSource: string;
+ metaProperties?: Record<string, unknown>;
+};
diff --git a/src/client/src/constants/route.ts b/src/client/src/constants/route.ts
index c053b2bf..ef32ff72 100644
--- a/src/client/src/constants/route.ts
+++ b/src/client/src/constants/route.ts
@@ -1 +1,4 @@
export const DEFAULT_LOGGED_IN_ROUTE = "/getting-started";
+export const ALLOWED_OPENLIT_ROUTES_WITHOUT_TOKEN = [
+ "/api/prompt/get-compiled",
+];
diff --git a/src/client/src/helpers/prompt.ts b/src/client/src/helpers/prompt.ts
new file mode 100644
index 00000000..cf81b15b
--- /dev/null
+++ b/src/client/src/helpers/prompt.ts
@@ -0,0 +1,39 @@
+import { PromptCompiledInput, PromptInput } from "@/constants/prompts";
+
+export function verifyPromptInput(promptInput: PromptInput) {
+ if (promptInput.name.length === 0) {
+ return {
+ success: false,
+ err: "Name should be present!",
+ };
+ }
+
+ if (promptInput.prompt.length === 0) {
+ return {
+ success: false,
+ err: "Prompt should be present!",
+ };
+ }
+
+ return { success: true };
+}
+
+export function validatePromptCompiledInput(
+ promptCompiledInput: PromptCompiledInput
+) {
+ if (!promptCompiledInput.apiKey) {
+ return {
+ success: false,
+ err: "API key should be present!",
+ };
+ }
+
+ if (!promptCompiledInput.id && !promptCompiledInput.name) {
+ return {
+ success: false,
+ err: "Both Id and Name cannot be undefined!",
+ };
+ }
+
+ return { success: true };
+}
diff --git a/src/client/src/helpers/user.ts b/src/client/src/helpers/user.ts
index d6e41dc8..01e46f72 100644
--- a/src/client/src/helpers/user.ts
+++ b/src/client/src/helpers/user.ts
@@ -1,14 +1,19 @@
import { useRootStore } from "@/store";
import { getData } from "@/utils/api";
import asaw from "@/utils/asaw";
+import { signOut } from "next-auth/react";
export const fetchAndPopulateCurrentUserStore = async () => {
- const [, user] = await asaw(
+ const [err, user] = await asaw(
getData({
url: "/api/user/profile",
method: "GET",
})
);
+ if (err) {
+ signOut();
+ }
+
useRootStore.getState().user.set(user);
};
diff --git a/src/client/src/lib/db-config.ts b/src/client/src/lib/db-config.ts
index d1b60368..b08c943f 100644
--- a/src/client/src/lib/db-config.ts
+++ b/src/client/src/lib/db-config.ts
@@ -2,11 +2,14 @@ import asaw from "@/utils/asaw";
import prisma from "./prisma";
import { getCurrentUser } from "./session";
import { DatabaseConfig, DatabaseConfigInvitedUser } from "@prisma/client";
+import migrations from "@/clickhouse/migrations";
+import getMessage from "@/constants/messages";
+import { throwIfError } from "@/utils/error";
export const getDBConfigByUser = async (currentOnly?: boolean) => {
const user = await getCurrentUser();
- if (!user) throw new Error("Unauthorized user!");
+ if (!user) throw new Error(getMessage().UNAUTHORIZED_USER);
if (currentOnly) {
const dbConfig = await prisma.databaseConfigUser.findFirst({
@@ -76,7 +79,7 @@ export const upsertDBConfig = async (
const user = await getCurrentUser();
- if (!user) throw new Error("Unauthorized user!");
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
const existingDBName = await prisma.databaseConfig.findUnique({
where: {
@@ -94,26 +97,33 @@ export const upsertDBConfig = async (
else whereObject.name = dbConfig.name;
if (id) {
- await checkPermissionForDbAction(user.id, id, "EDIT");
+ await checkPermissionForDbAction(user!.id, id, "EDIT");
}
- const createddbConfig = await prisma.databaseConfig.upsert({
- where: whereObject,
- create: {
- ...(dbConfig as any),
- createdByUserId: user.id,
- },
- update: {
- ...dbConfig,
- },
- });
+ const [err, createddbConfig] = await asaw(
+ prisma.databaseConfig.upsert({
+ where: whereObject,
+ create: {
+ ...(dbConfig as any),
+ createdByUserId: user!.id,
+ },
+ update: {
+ ...dbConfig,
+ },
+ })
+ );
+
+ if (err) {
+ console.log(err, createddbConfig);
+ }
if (!id) {
- await addDatabaseConfigUserEntry(user.id, createddbConfig.id, {
+ await addDatabaseConfigUserEntry(user!.id, createddbConfig.id, {
canEdit: true,
canDelete: true,
canShare: true,
});
+ migrations(createddbConfig.id);
}
return `${id ? "Updated" : "Added"} db details successfully`;
@@ -122,7 +132,7 @@ export const upsertDBConfig = async (
export async function deleteDBConfig(id: string) {
const user = await getCurrentUser();
- if (!user) throw new Error("Unauthorized user!");
+ if (!user) throw new Error(getMessage().UNAUTHORIZED_USER);
await checkPermissionForDbAction(user.id, id, "DELETE");
@@ -147,7 +157,7 @@ export async function deleteDBConfig(id: string) {
export async function setCurrentDBConfig(id: string) {
const user = await getCurrentUser();
- if (!user) throw new Error("Unauthorized user!");
+ if (!user) throw new Error(getMessage().UNAUTHORIZED_USER);
const currentConfig = await getDBConfigByUser(true);
@@ -198,7 +208,7 @@ export async function shareDBConfig({
const user = await getCurrentUser();
- if (!user) throw new Error("Unauthorized user!");
+ if (!user) throw new Error(getMessage().UNAUTHORIZED_USER);
const { dbUserConfig } = await checkPermissionForDbAction(
user.id,
diff --git a/src/client/src/lib/platform/clickhouse.ts b/src/client/src/lib/platform/clickhouse.ts
index 152f2693..7cce6035 100644
--- a/src/client/src/lib/platform/clickhouse.ts
+++ b/src/client/src/lib/platform/clickhouse.ts
@@ -1,6 +1,12 @@
+import migrations from "@/clickhouse/migrations";
import { dataCollector } from "./common";
export async function pingClickhouse() {
- const data = await dataCollector({}, "ping");
+ const data = await dataCollector({}, "ping");
+ runClickhouseMigrations()
return data;
}
+
+export async function runClickhouseMigrations() {
+ await migrations();
+}
diff --git a/src/client/src/lib/platform/common.ts b/src/client/src/lib/platform/common.ts
index a239e068..2bbd01e7 100644
--- a/src/client/src/lib/platform/common.ts
+++ b/src/client/src/lib/platform/common.ts
@@ -1,11 +1,12 @@
import { Pool } from "generic-pool";
-import { getDBConfigByUser } from "../db-config";
+import { getDBConfigById, getDBConfigByUser } from "../db-config";
import createClickhousePool from "./clickhouse-client";
import asaw from "@/utils/asaw";
import {
ClickHouseClient,
QueryParams,
InsertParams,
+ ExecParams,
CommandParams,
} from "@clickhouse/client-common";
@@ -52,10 +53,17 @@ export async function dataCollector(
format = "JSONEachRow",
table,
values,
- }: Partial<QueryParams & InsertParams & CommandParams>,
- clientQueryType: "query" | "command" | "insert" | "ping" = "query"
+ }: Partial<QueryParams & InsertParams & ExecParams & CommandParams>,
+ clientQueryType: "query" | "command" | "insert" | "exec" | "ping" = "query",
+ dbConfigId?: string
): Promise<DataCollectorType> {
- const [err, dbConfig] = await asaw(getDBConfigByUser(true));
+ let err, dbConfig;
+ if (dbConfigId) {
+ [err, dbConfig] = await asaw(getDBConfigById({ id: dbConfigId }));
+ } else {
+ [err, dbConfig] = await asaw(getDBConfigByUser(true));
+ }
+
if (err) return { err, data: [] };
let clickhousePool: Pool<ClickHouseClient> | undefined;
let client: ClickHouseClient | undefined;
@@ -96,8 +104,19 @@ export async function dataCollector(
})
);
- if (result?.query_id) {
- return { data: "Added successfully!" };
+ if (!respErr) {
+ return { data: result };
+ }
+ } else if (clientQueryType === "exec") {
+ if (!query) return { err: "No query specified!" };
+ [respErr, result] = await asaw(
+ client.exec({
+ query,
+ })
+ );
+
+ if (!respErr) {
+ return { data: result };
}
} else if (clientQueryType === "ping") {
[respErr, result] = await asaw(client.ping());
diff --git a/src/client/src/lib/platform/prompt/api-keys.ts b/src/client/src/lib/platform/prompt/api-keys.ts
new file mode 100644
index 00000000..a8db0b21
--- /dev/null
+++ b/src/client/src/lib/platform/prompt/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.ts
new file mode 100644
index 00000000..1e302f03
--- /dev/null
+++ b/src/client/src/lib/platform/prompt/compiled.ts
@@ -0,0 +1,79 @@
+import { PromptCompiledInput } from "@/constants/prompts";
+import { getAPIKeyInfo } from "./api-keys";
+import { validatePromptCompiledInput } from "@/helpers/prompt";
+import { getSpecificPrompt } from ".";
+import { objectEntries } from "@/utils/object";
+import { updateDownloadDetails } from "./version";
+import { jsonParse } from "@/utils/json";
+import { throwIfError } from "@/utils/error";
+import getMessage from "@/constants/messages";
+
+function compilePrompt(
+ prompt: string,
+ variables: Record<string, unknown>
+): string {
+ return objectEntries(variables).reduce((acc, [key, value]) => {
+ return acc.replaceAll(`{{${key}}}`, value as string);
+ }, prompt);
+}
+
+export async function getCompiledPrompt(
+ promptCompiledInput: PromptCompiledInput
+) {
+ const verifiedInput = validatePromptCompiledInput(promptCompiledInput);
+ throwIfError(
+ !!(verifiedInput.err || !verifiedInput.success),
+ verifiedInput.err || getMessage().MALFORMED_INPUTS
+ );
+
+ const [err, apiInfo] = await getAPIKeyInfo({
+ apiKey: promptCompiledInput.apiKey,
+ });
+
+ throwIfError(
+ !!(err || !apiInfo?.databaseConfigId),
+ err || getMessage().NO_API_KEY
+ );
+
+ const { err: promptErr, data: promptData } = await getSpecificPrompt(
+ {
+ id: promptCompiledInput.id,
+ name: promptCompiledInput.name,
+ version: promptCompiledInput.version,
+ },
+ apiInfo?.databaseConfigId
+ );
+
+ const promptObject = (promptData as any)?.[0] || {};
+
+ throwIfError(
+ !!(promptErr || !promptObject?.promptId || !promptObject?.version),
+ (promptErr as any) || getMessage().NO_PROMPT
+ );
+
+ await updateDownloadDetails(
+ {
+ versionId: promptObject.versionId,
+ promptId: promptObject.promptId,
+ metaProperties: {
+ ...(promptCompiledInput.downloadMetaProperties || {}),
+ apiKeyId: apiInfo.id,
+ },
+ downloadSource: promptCompiledInput.downloadSource || "api",
+ },
+ apiInfo?.databaseConfigId
+ );
+
+ promptObject.metaProperties = jsonParse(promptObject.metaProperties);
+ promptObject.tags = jsonParse(promptObject.tags);
+
+ if (promptCompiledInput.shouldCompile === false) {
+ promptObject.compiledPrompt = promptObject.prompt;
+ } else {
+ promptObject.compiledPrompt = compilePrompt(
+ promptObject.prompt,
+ promptCompiledInput.variables || {}
+ );
+ }
+ return promptObject;
+}
diff --git a/src/client/src/lib/platform/prompt/index.ts b/src/client/src/lib/platform/prompt/index.ts
new file mode 100644
index 00000000..9a34f1e5
--- /dev/null
+++ b/src/client/src/lib/platform/prompt/index.ts
@@ -0,0 +1,276 @@
+import { PromptInput, SpecificPromptInput } from "@/constants/prompts";
+import { dataCollector } from "../common";
+import {
+ OPENLIT_PROMPTS_TABLE_NAME,
+ OPENLIT_PROMPT_VERSIONS_TABLE_NAME,
+ OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME,
+} from "./table-details";
+import { verifyPromptInput } from "@/helpers/prompt";
+import { getCurrentUser } from "@/lib/session";
+import getMessage from "@/constants/messages";
+import Sanitizer from "@/utils/sanitizer";
+import { throwIfError } from "@/utils/error";
+
+export async function getPromptByName({ name }: { name: string }) {
+ const query = `
+ SELECT * FROM ${OPENLIT_PROMPTS_TABLE_NAME} WHERE name='${Sanitizer.sanitizeValue(
+ name
+ )}';
+ `;
+
+ const { data }: { data?: any } = await dataCollector({ query });
+ return data?.[0];
+}
+
+export async function checkNameValidity({ name }: { name: string }) {
+ const data = await getPromptByName({ name });
+ return { isValid: !data?.id };
+}
+
+export async function createPrompt(promptInputParams: PromptInput) {
+ const user = await getCurrentUser();
+
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
+
+ const promptInput = Sanitizer.sanitizeObject(promptInputParams);
+
+ const verifiedPrompt = verifyPromptInput(promptInput);
+ throwIfError(!verifiedPrompt.success, verifiedPrompt.err!);
+
+ const { isValid } = await checkNameValidity({ name: promptInput.name });
+ throwIfError(!isValid, getMessage().PROMPT_NAME_TAKEN);
+
+ const { err } = await dataCollector(
+ {
+ table: OPENLIT_PROMPTS_TABLE_NAME,
+ values: [{ name: promptInput.name, created_by: user!.email }],
+ },
+ "insert"
+ );
+
+ throwIfError(
+ !!err,
+ typeof err?.toString === "function" ? err.toString() : (err as string)
+ );
+
+ const promptData = await getPromptByName({
+ name: promptInput.name,
+ });
+
+ throwIfError(!promptData?.id, getMessage().PROMPT_NOT_CREATED);
+
+ const { err: versionErr, data: versionData } = await dataCollector(
+ {
+ table: OPENLIT_PROMPT_VERSIONS_TABLE_NAME,
+ values: [
+ {
+ prompt_id: promptData.id,
+ updated_by: user!.email,
+ version: promptInput.version,
+ status: promptInput.status,
+ prompt: promptInput.prompt,
+ tags: promptInput.tags,
+ meta_properties: promptInput.metaProperties,
+ },
+ ],
+ },
+ "insert"
+ );
+
+ throwIfError(
+ !!(versionErr || !(versionData as { query_id: unknown })?.query_id),
+
+ typeof versionErr?.toString === "function"
+ ? versionErr.toString()
+ : (versionErr as string) || getMessage().VERSION_NOT_CREATED
+ );
+
+ return {
+ data: { promptId: promptData.id },
+ message: getMessage().PROMPT_SAVED,
+ };
+}
+
+export async function getPrompts() {
+ const user = await getCurrentUser();
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
+
+ const query = `SELECT
+ p.id AS promptId,
+ p.name AS name,
+ p.created_by AS createdBy,
+ COUNT(DISTINCT v.version) AS totalVersions, -- Total number of versions for each prompt
+ MAX(v.version) AS latestVersion, -- Latest version by version number
+ any(v.updated_at) AS latestVersionDate, -- The date when the latest version was updated
+ any(v.status) AS latestVersionStatus, -- Status of the latest version in PUBLISHED state
+ countDistinctIf(d.download_id, d.download_id != '00000000-0000-0000-0000-000000000000') AS totalDownloads
+ FROM
+ ${OPENLIT_PROMPTS_TABLE_NAME} p
+ LEFT JOIN
+ ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME} v ON p.id = v.prompt_id
+ AND v.status != 'DRAFT'
+ LEFT JOIN
+ ${OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME} d ON p.id = d.prompt_id -- Join with downloads table
+ GROUP BY
+ p.id, p.name, p.created_by, p.created_at
+ ORDER BY
+ p.created_at DESC;
+ `;
+
+ return await dataCollector({ query });
+}
+
+export async function getSpecificPrompt(
+ promptInputParams: Partial<SpecificPromptInput>,
+ dbConfigId?: string
+) {
+ const promptInput = Sanitizer.sanitizeObject(promptInputParams);
+ const conditionClauses = [];
+ if (promptInput.id) {
+ conditionClauses.push(`p.id = '${promptInput.id}'`);
+ }
+
+ if (promptInput.name) {
+ conditionClauses.push(`p.name = '${promptInput.name}'`);
+ }
+
+ if (promptInput.version) {
+ conditionClauses.push(`v.version = '${promptInput.version}'`);
+ }
+
+ const query = `
+ SELECT
+ p.id AS promptId,
+ v.version_id AS versionId,
+ p.name AS name,
+ v.version AS version,
+ v.tags AS tags,
+ v.meta_properties AS metaProperties,
+ v.prompt AS prompt
+ FROM
+ ${OPENLIT_PROMPTS_TABLE_NAME} p
+ LEFT JOIN
+ ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME} v ON p.id = v.prompt_id
+ AND v.status != 'DRAFT'
+ WHERE ${conditionClauses.join(" AND ")}
+ ORDER BY
+ v.version DESC
+ `;
+
+ return dataCollector({ query }, "query", dbConfigId);
+}
+
+export async function getPromptDetails(
+ promptId: string,
+ parameters?: { version?: string }
+) {
+ const params = Sanitizer.sanitizeObject(parameters || {});
+ const user = await getCurrentUser();
+
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
+
+ const promptVersionQuery = `
+ SELECT
+ p.id AS promptId,
+ p.name AS name,
+ p.created_by AS createdBy,
+ p.created_at AS createdAt,
+ v.version_id AS versionId,
+ v.prompt AS prompt, -- Selected prompt content
+ toString(v.status) AS status, -- Version status (e.g., DRAFT, PUBLISHED)
+ v.updated_at AS updatedAt, -- When the version was last updated
+ v.version AS version, -- Version number (e.g., 1.0.0)
+ v.meta_properties AS metaProperties, -- Meta properties for the version
+ v.tags AS tags -- Tags for the version
+FROM
+ ${OPENLIT_PROMPTS_TABLE_NAME} p
+LEFT JOIN
+ (
+ SELECT
+ version_id, prompt_id, prompt, version, status, meta_properties, tags, updated_by, updated_at
+ FROM
+ ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME}
+ WHERE
+ prompt_id = '${promptId}'
+ ${
+ params?.version
+ ? `AND version='${params.version}' AND status != 'DRAFT'`
+ : ""
+ }
+ ORDER BY
+ status DESC, -- Prioritize draft versions (DRAFT first)
+ version DESC -- Then order by version number (latest first)
+ LIMIT 1 -- Fetch the highest priority version (either DRAFT or latest PUBLISHED)
+ ) v
+ON p.id = v.prompt_id
+WHERE
+ p.id = '${promptId}' -- Filter by specific prompt ID
+ORDER BY
+ p.created_at DESC;
+
+ `;
+
+ const promptVersionsListQuery = `SELECT
+ v.version_id AS versionId,
+ v.prompt_id AS promptId,
+ v.prompt AS prompt,
+ v.version AS version, -- Version number (e.g., 1.0.0)
+ v.status AS status, -- Status of the version (e.g., DRAFT or PUBLISHED)
+ v.meta_properties AS metaProperties, -- Meta properties for the version
+ v.tags AS tags, -- Tags for the version
+ v.updated_by AS updatedBy, -- Who updated the version
+ v.updated_at AS updatedAt, -- When the version was last updated
+ countDistinctIf(d.download_id, d.download_id != '00000000-0000-0000-0000-000000000000') AS totalDownloads -- Total downloads for the prompt
+FROM
+ ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME} v
+LEFT JOIN
+ ${OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME} d ON v.version_id = d.version_id -- Join with downloads table
+WHERE
+ v.prompt_id = '${promptId}' -- Filter by specific prompt ID
+GROUP BY
+ v.version_id, v.prompt_id, v.prompt, v.version, v.status, v.meta_properties, v.tags, v.updated_by, v.updated_at
+ORDER BY
+ v.status = 'DRAFT' DESC, -- Prioritize DRAFT versions
+ v.version DESC;
+`;
+
+ return Promise.all([
+ await dataCollector({ query: promptVersionQuery }, "query"),
+ await dataCollector({ query: promptVersionsListQuery }, "query"),
+ ]).then(([versionData, versionsList]) => {
+ let combinedData: any | null = null;
+ if ((versionData.data as any[])?.[0]) {
+ combinedData = (versionData.data as any[])[0];
+ combinedData.versions = versionsList.data || [];
+ }
+
+ return {
+ err: versionData.err || versionsList.err,
+ data: [combinedData],
+ };
+ });
+}
+
+export async function deletePrompt(promptIdParam: string) {
+ const user = await getCurrentUser();
+
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
+ const promptId = Sanitizer.sanitizeValue(promptIdParam);
+
+ const queries = [
+ `DELETE FROM ${OPENLIT_PROMPTS_TABLE_NAME} WHERE id = '${promptId}';`,
+ `DELETE FROM ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME} WHERE prompt_id = '${promptId}';`,
+ `DELETE FROM ${OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME} WHERE prompt_id = '${promptId}';`,
+ ];
+
+ const queryResponses = await Promise.all(
+ queries.map(async (query) => await dataCollector({ query }, "exec"))
+ );
+
+ const errors = queryResponses.map(({ err }) => err).filter((e) => !!e);
+ if (errors.length > 0) {
+ return [getMessage().PROMPT_NOT_DELETED];
+ }
+
+ return [undefined, getMessage().PROMPT_DELETED];
+}
diff --git a/src/client/src/lib/platform/prompt/table-details.ts b/src/client/src/lib/platform/prompt/table-details.ts
new file mode 100644
index 00000000..3519f5a4
--- /dev/null
+++ b/src/client/src/lib/platform/prompt/table-details.ts
@@ -0,0 +1,4 @@
+export const OPENLIT_PROMPTS_TABLE_NAME = "openlit_prompts";
+export const OPENLIT_PROMPT_VERSIONS_TABLE_NAME = "openlit_prompt_versions";
+export const OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME =
+ "openlit_prompt_version_downloads";
diff --git a/src/client/src/lib/platform/prompt/version.ts b/src/client/src/lib/platform/prompt/version.ts
new file mode 100644
index 00000000..fb08ccd9
--- /dev/null
+++ b/src/client/src/lib/platform/prompt/version.ts
@@ -0,0 +1,107 @@
+import { PromptDownloadInput, PromptUpdate } from "@/constants/prompts";
+import { dataCollector } from "../common";
+import {
+ OPENLIT_PROMPT_VERSIONS_TABLE_NAME,
+ OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME,
+} from "./table-details";
+import { consoleLog } from "@/utils/log";
+import { getCurrentUser } from "@/lib/session";
+import { jsonStringify } from "@/utils/json";
+import getMessage from "@/constants/messages";
+import Sanitizer from "@/utils/sanitizer";
+import { throwIfError } from "@/utils/error";
+
+export async function upsertPromptVersion(promptInputParams: PromptUpdate) {
+ const user = await getCurrentUser();
+
+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);
+
+ const promptInput = Sanitizer.sanitizeObject(promptInputParams);
+
+ let versionErr;
+ let versionData;
+
+ if (promptInput.versionId) {
+ const updateValues = [
+ `updated_by = '${user!.email}'`,
+ promptInput.version && "version = '" + promptInput.version + "'",
+ promptInput.status && "status = '" + promptInput.status + "'",
+ promptInput.prompt && "prompt = '" + promptInput.prompt + "'",
+ promptInput.tags && "tags = '" + jsonStringify(promptInput.tags) + "'",
+ promptInput.metaProperties &&
+ "meta_properties = '" + jsonStringify(promptInput.metaProperties) + "'",
+ ];
+ const updateQuery = `
+ ALTER TABLE ${OPENLIT_PROMPT_VERSIONS_TABLE_NAME}
+ UPDATE
+ ${updateValues.filter((e) => e).join(" , ")}
+ WHERE version_id = '${promptInput.versionId}'`;
+ const { err, data } = await dataCollector(
+ {
+ query: updateQuery,
+ },
+ "exec"
+ );
+
+ versionErr = err;
+ versionData = data;
+ } else {
+ const { err, data } = await dataCollector(
+ {
+ table: OPENLIT_PROMPT_VERSIONS_TABLE_NAME,
+ values: [
+ {
+ prompt_id: promptInput.promptId,
+ updated_by: user!.email,
+ version: promptInput.version,
+ status: promptInput.status,
+ prompt: promptInput.prompt,
+ tags: jsonStringify(promptInput.tags),
+ meta_properties: jsonStringify(promptInput.metaProperties),
+ },
+ ],
+ },
+ "insert"
+ );
+ versionErr = err;
+ versionData = data;
+ }
+
+ throwIfError(
+ !!(versionErr || !(versionData as { query_id: unknown })?.query_id),
+ typeof versionErr?.toString === "function"
+ ? versionErr.toString()
+ : (versionErr as string) || getMessage().VERSION_NOT_SAVED
+ );
+
+ return getMessage().VERSION_SAVED;
+}
+
+export async function updateDownloadDetails(
+ promptDownloadInputParams: PromptDownloadInput,
+ dbConfigId?: string
+) {
+ const promptDownloadInput = Sanitizer.sanitizeObject(
+ promptDownloadInputParams
+ );
+
+ const { err, data } = await dataCollector(
+ {
+ table: OPENLIT_PROMPT_VERSION_DOWNLOADS_TABLE_NAME,
+ values: [
+ {
+ prompt_id: promptDownloadInput.promptId,
+ version_id: promptDownloadInput.versionId,
+ meta_properties: jsonStringify(promptDownloadInput.metaProperties),
+ download_source: promptDownloadInput.downloadSource,
+ },
+ ],
+ },
+ "insert",
+ dbConfigId
+ );
+
+ if (err || !(data as any)?.query_id)
+ consoleLog(err || getMessage().DOWNLOAD_INFO_NOT_SAVED);
+ return true;
+}
diff --git a/src/client/src/lib/user.ts b/src/client/src/lib/user.ts
index f2cd34ed..2f15c699 100644
--- a/src/client/src/lib/user.ts
+++ b/src/client/src/lib/user.ts
@@ -4,6 +4,7 @@ import asaw from "@/utils/asaw";
import { getCurrentUser } from "./session";
import { User } from "@prisma/client";
import { moveSharedDBConfigToDBUser } from "./db-config";
+import getMessage from "@/constants/messages";
function exclude<User extends Record<string, unknown>, K extends keyof User>(
user: User,
@@ -110,7 +111,7 @@ export const updateUserProfile = async ({
}) => {
const user = await getCurrentUser({ selectPassword: true });
- if (!user) throw new Error("Unauthorized user!");
+ if (!user) throw new Error(getMessage().UNAUTHORIZED_USER);
const updatedUserObject: Partial<User> = {};
diff --git a/src/client/src/middleware.ts b/src/client/src/middleware.ts
index a9ffd777..546d34ee 100644
--- a/src/client/src/middleware.ts
+++ b/src/client/src/middleware.ts
@@ -1,7 +1,10 @@
import { getToken } from "next-auth/jwt";
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
-import { DEFAULT_LOGGED_IN_ROUTE } from "@/constants/route";
+import {
+ DEFAULT_LOGGED_IN_ROUTE,
+ ALLOWED_OPENLIT_ROUTES_WITHOUT_TOKEN,
+} from "@/constants/route";
export default withAuth(
async function middleware(req) {
@@ -15,6 +18,8 @@ export default withAuth(
const token = await getToken({ req });
const isAuth = !!token;
+ const isAllowedRequestWithoutToken =
+ ALLOWED_OPENLIT_ROUTES_WITHOUT_TOKEN.includes(pathname);
const isAuthPage =
pathname.startsWith("/login") || pathname.startsWith("/register");
const isApiPage = pathname.startsWith("/api");
@@ -28,7 +33,7 @@ export default withAuth(
}
if (isApiPage) {
- if (isAuth) {
+ if (isAuth || isAllowedRequestWithoutToken) {
return NextResponse.next();
}
}
@@ -67,5 +72,7 @@ export const config = {
"/database-config",
"/openground",
"/exceptions",
+ "/prompt-hub",
+ "/api-keys",
],
};
diff --git a/src/client/src/utils/error.ts b/src/client/src/utils/error.ts
new file mode 100644
index 00000000..e69f4e10
--- /dev/null
+++ b/src/client/src/utils/error.ts
@@ -0,0 +1,5 @@
+export function throwIfError(condition: boolean, message: string): void {
+ if (condition) {
+ throw new Error(message);
+ }
+}
diff --git a/src/client/src/utils/json.ts b/src/client/src/utils/json.ts
new file mode 100644
index 00000000..c4bd13c3
--- /dev/null
+++ b/src/client/src/utils/json.ts
@@ -0,0 +1,21 @@
+import { consoleLog } from "./log";
+
+export function jsonStringify(json: any) {
+ try {
+ const jsonS = JSON.stringify(json);
+ return jsonS;
+ } catch (e) {
+ consoleLog(e);
+ return "";
+ }
+}
+
+export function jsonParse(str: string) {
+ try {
+ const jsonP = JSON.parse(str);
+ return jsonP;
+ } catch (e) {
+ consoleLog(e);
+ return undefined;
+ }
+}
diff --git a/src/client/src/utils/log.ts b/src/client/src/utils/log.ts
new file mode 100644
index 00000000..8864c591
--- /dev/null
+++ b/src/client/src/utils/log.ts
@@ -0,0 +1,3 @@
+export function consoleLog(...rest: any) {
+ console.log(rest);
+}
diff --git a/src/client/src/utils/object.ts b/src/client/src/utils/object.ts
new file mode 100644
index 00000000..9ba1157d
--- /dev/null
+++ b/src/client/src/utils/object.ts
@@ -0,0 +1,9 @@
+export function objectKeys<T extends object>(object: T): Array<keyof T> {
+ return Object.keys(object) as Array<keyof T>;
+}
+
+export function objectEntries<T extends object>(
+ object: T
+): Array<[keyof T, T[keyof T]]> {
+ return Object.entries(object) as Array<[keyof T, T[keyof T]]>;
+}
diff --git a/src/client/src/utils/sanitizer.ts b/src/client/src/utils/sanitizer.ts
new file mode 100644
index 00000000..0c0e19b5
--- /dev/null
+++ b/src/client/src/utils/sanitizer.ts
@@ -0,0 +1,33 @@
+import sqlString from "sqlstring";
+import { objectKeys } from "./object";
+import { isArray, isNil, isObject } from "lodash";
+
+export default class Sanitizer {
+ static options = { xss: true, noSql: true, sql: true, level: 5 };
+ static sanitizeObject<T extends object>(obj: T): T {
+ const v = objectKeys(obj).reduce((acc: Partial<T>, key) => {
+ let value: T[keyof T] = obj[key];
+ if (isArray(value)) {
+ value = value.map((val) => Sanitizer.sanitizeValue(val)) as T[keyof T];
+ } else if (isObject(value)) {
+ value = Sanitizer.sanitizeObject(value);
+ } else {
+ value = Sanitizer.sanitizeValue(value) as T[keyof T];
+ }
+ acc[key] = value;
+ return acc;
+ }, {}) as T;
+ return v;
+ }
+ static sanitizeValue<T>(value: T): T {
+ if (!isNil(value)) {
+ if (typeof value === "string") {
+ return sqlString.escape(value).slice(1, -1) as T; // Remove outer quotes and escape the string
+ } else {
+ return sqlString.escape(value) as T; // For other types (numbers, booleans), use default escape
+ }
+ }
+
+ return value;
+ }
+}
diff --git a/src/dev-docker-compose.yml b/src/dev-docker-compose.yml
index d4aeba25..e8eaeb26 100644
--- a/src/dev-docker-compose.yml
+++ b/src/dev-docker-compose.yml
@@ -1,5 +1,3 @@
-version: '3.8'
-
services:
clickhouse:
image: clickhouse/clickhouse-server:24.2.2