Skip to content

Commit

Permalink
Feature/thread creation slug name (#2512)
Browse files Browse the repository at this point in the history
* 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: abrakadobr <[email protected]>
  • Loading branch information
timothycarambat and abrakadobr authored Oct 21, 2024
1 parent 446164d commit e71392d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"textgenwebui",
"togetherai",
"Unembed",
"uuidv",
"vectordbs",
"Weaviate",
"Zilliz"
Expand Down
15 changes: 9 additions & 6 deletions server/endpoints/api/workspaceThread/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ function apiWorkspaceThreadEndpoints(app) {
type: 'string'
}
#swagger.requestBody = {
description: 'Optional userId associated with the thread',
description: 'Optional userId associated with the thread, thread slug and thread name',
required: false,
content: {
"application/json": {
example: {
userId: 1
userId: 1,
name: 'Name',
slug: 'thread-slug'
}
}
}
Expand Down Expand Up @@ -67,9 +69,9 @@ function apiWorkspaceThreadEndpoints(app) {
}
*/
try {
const { slug } = request.params;
let { userId = null } = reqBody(request);
const workspace = await Workspace.get({ slug });
const wslug = request.params.slug;
let { userId = null, name = null, slug = null } = reqBody(request);
const workspace = await Workspace.get({ slug: wslug });

if (!workspace) {
response.sendStatus(400).end();
Expand All @@ -83,7 +85,8 @@ function apiWorkspaceThreadEndpoints(app) {

const { thread, message } = await WorkspaceThread.new(
workspace,
userId ? Number(userId) : null
userId ? Number(userId) : null,
{ name, slug }
);

await Telemetry.sendTelemetry("workspace_thread_created", {
Expand Down
34 changes: 31 additions & 3 deletions server/models/workspaceThread.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
const prisma = require("../utils/prisma");
const slugifyModule = require("slugify");
const { v4: uuidv4 } = require("uuid");

const WorkspaceThread = {
defaultName: "Thread",
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 {
const thread = await prisma.workspace_threads.create({
data: {
name: this.defaultName,
slug: uuidv4(),
name: data.name ? String(data.name) : this.defaultName,
slug: data.slug
? this.slugify(data.slug, { lowercase: true })
: uuidv4(),
user_id: userId ? Number(userId) : null,
workspace_id: workspace.id,
},
Expand Down
6 changes: 4 additions & 2 deletions server/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2391,12 +2391,14 @@
}
},
"requestBody": {
"description": "Optional userId associated with the thread",
"description": "Optional userId associated with the thread, thread slug and thread name",
"required": false,
"content": {
"application/json": {
"example": {
"userId": 1
"userId": 1,
"name": "Name",
"slug": "thread-slug"
}
}
}
Expand Down

0 comments on commit e71392d

Please sign in to comment.