Skip to content
Snippets Groups Projects
Unverified Commit e71392d8 authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

Feature/thread creation slug name (#2512)


* thread creation additional params name and slug, with api

* typo fix

* Rebuild openai Swagger docs
Handle validations for fields to prevent invalid field inputs for .new
Enforce sluggification of `slug` to prevent breaking of URL structs

---------

Co-authored-by: default avatarabrakadobr <abrakadobr@gmail.com>
parent 446164d7
No related branches found
No related tags found
No related merge requests found
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
"textgenwebui", "textgenwebui",
"togetherai", "togetherai",
"Unembed", "Unembed",
"uuidv",
"vectordbs", "vectordbs",
"Weaviate", "Weaviate",
"Zilliz" "Zilliz"
......
...@@ -31,12 +31,14 @@ function apiWorkspaceThreadEndpoints(app) { ...@@ -31,12 +31,14 @@ function apiWorkspaceThreadEndpoints(app) {
type: 'string' type: 'string'
} }
#swagger.requestBody = { #swagger.requestBody = {
description: 'Optional userId associated with the thread', description: 'Optional userId associated with the thread, thread slug and thread name',
required: false, required: false,
content: { content: {
"application/json": { "application/json": {
example: { example: {
userId: 1 userId: 1,
name: 'Name',
slug: 'thread-slug'
} }
} }
} }
...@@ -67,9 +69,9 @@ function apiWorkspaceThreadEndpoints(app) { ...@@ -67,9 +69,9 @@ function apiWorkspaceThreadEndpoints(app) {
} }
*/ */
try { try {
const { slug } = request.params; const wslug = request.params.slug;
let { userId = null } = reqBody(request); let { userId = null, name = null, slug = null } = reqBody(request);
const workspace = await Workspace.get({ slug }); const workspace = await Workspace.get({ slug: wslug });
if (!workspace) { if (!workspace) {
response.sendStatus(400).end(); response.sendStatus(400).end();
...@@ -83,7 +85,8 @@ function apiWorkspaceThreadEndpoints(app) { ...@@ -83,7 +85,8 @@ function apiWorkspaceThreadEndpoints(app) {
const { thread, message } = await WorkspaceThread.new( const { thread, message } = await WorkspaceThread.new(
workspace, workspace,
userId ? Number(userId) : null userId ? Number(userId) : null,
{ name, slug }
); );
await Telemetry.sendTelemetry("workspace_thread_created", { await Telemetry.sendTelemetry("workspace_thread_created", {
......
const prisma = require("../utils/prisma"); const prisma = require("../utils/prisma");
const slugifyModule = require("slugify");
const { v4: uuidv4 } = require("uuid"); const { v4: uuidv4 } = require("uuid");
const WorkspaceThread = { const WorkspaceThread = {
defaultName: "Thread", defaultName: "Thread",
writable: ["name"], writable: ["name"],
new: async function (workspace, userId = null) { /**
* The default Slugify module requires some additional mapping to prevent downstream issues
* if the user is able to define a slug externally. We have to block non-escapable URL chars
* so that is the slug is rendered it doesn't break the URL or UI when visited.
* @param {...any} args - slugify args for npm package.
* @returns {string}
*/
slugify: function (...args) {
slugifyModule.extend({
"+": " plus ",
"!": " bang ",
"@": " at ",
"*": " splat ",
".": " dot ",
":": "",
"~": "",
"(": "",
")": "",
"'": "",
'"': "",
"|": "",
});
return slugifyModule(...args);
},
new: async function (workspace, userId = null, data = {}) {
try { try {
const thread = await prisma.workspace_threads.create({ const thread = await prisma.workspace_threads.create({
data: { data: {
name: this.defaultName, name: data.name ? String(data.name) : this.defaultName,
slug: uuidv4(), slug: data.slug
? this.slugify(data.slug, { lowercase: true })
: uuidv4(),
user_id: userId ? Number(userId) : null, user_id: userId ? Number(userId) : null,
workspace_id: workspace.id, workspace_id: workspace.id,
}, },
......
...@@ -2391,12 +2391,14 @@ ...@@ -2391,12 +2391,14 @@
} }
}, },
"requestBody": { "requestBody": {
"description": "Optional userId associated with the thread", "description": "Optional userId associated with the thread, thread slug and thread name",
"required": false, "required": false,
"content": { "content": {
"application/json": { "application/json": {
"example": { "example": {
"userId": 1 "userId": 1,
"name": "Name",
"slug": "thread-slug"
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment