Add additional embed API endpoints (#3264)

* wip embed dev api endpoints

* fix /v1/embed/new endpoint

* add delete endpoint to embeds

* linting

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
This commit is contained in:
Sean Hatfield 2025-02-19 05:56:56 +08:00 committed by GitHub
parent c6ff3a7765
commit 354b66e09e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 432 additions and 33 deletions

View File

@ -1,6 +1,8 @@
const { EmbedConfig } = require("../../../models/embedConfig");
const { EmbedChats } = require("../../../models/embedChats");
const { validApiKey } = require("../../../utils/middleware/validApiKey");
const { reqBody } = require("../../../utils/http");
const { Workspace } = require("../../../models/workspace");
function apiEmbedEndpoints(app) {
if (!app) return;
@ -124,21 +126,10 @@ function apiEmbedEndpoints(app) {
*/
try {
const { embedUuid } = request.params;
const embed = await EmbedConfig.get({ uuid: String(embedUuid) });
if (!embed) {
return response.status(404).json({ error: "Embed not found" });
}
const chats = await EmbedChats.where({ embed_id: embed.id });
const formattedChats = chats.map((chat) => ({
id: chat.id,
session_id: chat.session_id,
prompt: chat.prompt,
response: chat.response,
createdAt: chat.createdAt,
}));
response.status(200).json({ chats: formattedChats });
const chats = await EmbedChats.where({
embed_config: { uuid: String(embedUuid) },
});
response.status(200).json({ chats });
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();
@ -195,30 +186,218 @@ function apiEmbedEndpoints(app) {
*/
try {
const { embedUuid, sessionUuid } = request.params;
const chats = await EmbedChats.where({
embed_config: { uuid: String(embedUuid) },
session_id: String(sessionUuid),
});
response.status(200).json({ chats });
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();
}
}
);
app.post("/v1/embed/new", [validApiKey], async (request, response) => {
/*
#swagger.tags = ['Embed']
#swagger.description = 'Create a new embed configuration'
#swagger.requestBody = {
description: 'JSON object containing embed configuration details',
required: true,
content: {
"application/json": {
schema: {
type: 'object',
example: {
"workspace_slug": "workspace-slug-1",
"chat_mode": "chat",
"allowlist_domains": ["example.com"],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10
}
}
}
}
}
#swagger.responses[200] = {
content: {
"application/json": {
schema: {
type: 'object',
example: {
"embed": {
"id": 1,
"uuid": "embed-uuid-1",
"enabled": true,
"chat_mode": "chat",
"allowlist_domains": ["example.com"],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10,
"createdAt": "2023-04-01T12:00:00Z",
"workspace_slug": "workspace-slug-1"
},
"error": null
}
}
}
}
}
#swagger.responses[403] = {
schema: {
"$ref": "#/definitions/InvalidAPIKey"
}
}
#swagger.responses[404] = {
description: "Workspace not found"
}
*/
try {
const data = reqBody(request);
if (!data.workspace_slug)
return response
.status(400)
.json({ error: "Workspace slug is required" });
const workspace = await Workspace.get({
slug: String(data.workspace_slug),
});
if (!workspace)
return response.status(404).json({ error: "Workspace not found" });
const { embed, message: error } = await EmbedConfig.new({
...data,
workspace_id: workspace.id,
});
response.status(200).json({ embed, error });
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();
}
});
app.post("/v1/embed/:embedUuid", [validApiKey], async (request, response) => {
/*
#swagger.tags = ['Embed']
#swagger.description = 'Update an existing embed configuration'
#swagger.parameters['embedUuid'] = {
in: 'path',
description: 'UUID of the embed to update',
required: true,
type: 'string'
}
#swagger.requestBody = {
description: 'JSON object containing embed configuration updates',
required: true,
content: {
"application/json": {
schema: {
type: 'object',
example: {
"enabled": true,
"chat_mode": "chat",
"allowlist_domains": ["example.com"],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10
}
}
}
}
}
#swagger.responses[200] = {
content: {
"application/json": {
schema: {
type: 'object',
example: {
"success": true,
"error": null
}
}
}
}
}
#swagger.responses[403] = {
schema: {
"$ref": "#/definitions/InvalidAPIKey"
}
}
#swagger.responses[404] = {
description: "Embed not found"
}
*/
try {
const { embedUuid } = request.params;
const data = reqBody(request);
const embed = await EmbedConfig.get({ uuid: String(embedUuid) });
if (!embed) {
return response.status(404).json({ error: "Embed not found" });
}
const chats = await EmbedChats.where({
embed_id: embed.id,
session_id: String(sessionUuid),
const { success, error } = await EmbedConfig.update(embed.id, data);
response.status(200).json({ success, error });
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();
}
});
if (!chats || chats.length === 0) {
return response
.status(404)
.json({ error: "No chats found for this session" });
app.delete(
"/v1/embed/:embedUuid",
[validApiKey],
async (request, response) => {
/*
#swagger.tags = ['Embed']
#swagger.description = 'Delete an existing embed configuration'
#swagger.parameters['embedUuid'] = {
in: 'path',
description: 'UUID of the embed to delete',
required: true,
type: 'string'
}
const formattedChats = chats.map((chat) => ({
id: chat.id,
prompt: chat.prompt,
response: chat.response,
createdAt: chat.createdAt,
}));
response.status(200).json({ chats: formattedChats });
#swagger.responses[200] = {
content: {
"application/json": {
schema: {
type: 'object',
example: {
"success": true,
"error": null
}
}
}
}
}
#swagger.responses[403] = {
schema: {
"$ref": "#/definitions/InvalidAPIKey"
}
}
#swagger.responses[404] = {
description: "Embed not found"
}
*/
try {
const { embedUuid } = request.params;
const embed = await EmbedConfig.get({ uuid: String(embedUuid) });
if (!embed)
return response.status(404).json({ error: "Embed not found" });
const success = await EmbedConfig.delete({ id: embed.id });
response
.status(200)
.json({ success, error: success ? null : "Failed to delete embed" });
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();

View File

@ -3577,6 +3577,226 @@
}
}
}
},
"/v1/embed/new": {
"post": {
"tags": [
"Embed"
],
"description": "Create a new embed configuration",
"parameters": [],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"embed": {
"id": 1,
"uuid": "embed-uuid-1",
"enabled": true,
"chat_mode": "chat",
"allowlist_domains": [
"example.com"
],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10,
"createdAt": "2023-04-01T12:00:00Z",
"workspace_slug": "workspace-slug-1"
},
"error": null
}
}
}
}
},
"400": {
"description": "Bad Request"
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
}
}
},
"404": {
"description": "Workspace not found"
},
"500": {
"description": "Internal Server Error"
}
},
"requestBody": {
"description": "JSON object containing embed configuration details",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"workspace_slug": "workspace-slug-1",
"chat_mode": "chat",
"allowlist_domains": [
"example.com"
],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10
}
}
}
}
}
}
},
"/v1/embed/{embedUuid}": {
"post": {
"tags": [
"Embed"
],
"description": "Update an existing embed configuration",
"parameters": [
{
"name": "embedUuid",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"description": "UUID of the embed to update"
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"success": true,
"error": null
}
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
}
}
},
"404": {
"description": "Embed not found"
},
"500": {
"description": "Internal Server Error"
}
},
"requestBody": {
"description": "JSON object containing embed configuration updates",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"enabled": true,
"chat_mode": "chat",
"allowlist_domains": [
"example.com"
],
"allow_model_override": false,
"allow_temperature_override": false,
"allow_prompt_override": false,
"max_chats_per_day": 100,
"max_chats_per_session": 10
}
}
}
}
}
},
"delete": {
"tags": [
"Embed"
],
"description": "Delete an existing embed configuration",
"parameters": [
{
"name": "embedUuid",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"description": "UUID of the embed to delete"
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"success": true,
"error": null
}
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
}
}
},
"404": {
"description": "Embed not found"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"components": {