Compare commits

...

2 commits

3 changed files with 44 additions and 59 deletions

View file

@ -8,6 +8,8 @@ import {
isUserBot,
contentOf,
messageOf,
formatImageMessage,
formatUserMessage,
} from "./utils";
import {
allowedChat,
@ -115,21 +117,7 @@ client.connect().then(async () => {
content: "[md]`Analyzing image...`[/md]",
});
session.push({
role: "user",
content: [
{
type: "text",
text: `${username} said: Describe what is in this image.`,
},
{
type: "image_url",
image_url: {
url: imageData,
},
},
],
});
session.push(formatImageMessage(username, imageData));
const response = await assistant.chat.completions.create({
messages: session,
@ -161,10 +149,7 @@ client.connect().then(async () => {
content: THINKING,
});
session.push({
role: "user",
content: `${username} said: ${stripMentionTag(message.content).trim()}`,
});
session.push(formatUserMessage(username, message.content));
const response = await assistant.chat.completions.create({
messages: session,
@ -202,10 +187,7 @@ client.connect().then(async () => {
content: THINKING,
});
session.push({
role: "user",
content: `[DIRECT MESSAGE] ${username} said: ${stripMentionTag(message.content).trim()}`,
});
session.push(formatUserMessage(username, message.content));
const response = await assistant.chat.completions.create({
messages: session,

View file

@ -11,7 +11,7 @@ export interface ImageMessage {
content: [
{
type: "text";
text: string;
text: `${string} said: Describe what is in this image.`;
},
{
type: "image_url";
@ -30,7 +30,7 @@ export type AnyChatCompletion =
export enum ImageSize {
Small = "256x256",
Medium = "512x512",
Large = "1024x1024"
Large = "1024x1024",
}
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;

View file

@ -1,21 +1,13 @@
import * as fs from "fs";
import { Messages } from "./types";
import { AnyChatCompletion, ImageMessage, Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
import { stripMentionTag } from "tailchat-client-sdk";
export function checkFile(
file: string,
encoding: fs.EncodingOption,
defaultContent: string,
): Messages {
/**
* Checks if a given JSON file exists and contains valid JSON from a previous message history.
*
* @param file - The file to check.
* @param encoding - The text encoding the given file uses.
* @param defaultContent - The default system message to use if no JSON file was found, or the JSON file does not contain valid JSON.
*
* @returns A list of messages from the given JSON file, or a starter list with just the system messsage.
*/
let final: Messages;
const generic: Messages = [
{
@ -36,6 +28,37 @@ export function checkFile(
return final;
}
export function formatUserMessage(
username: string,
content: string,
): AnyChatCompletion {
return {
role: "user",
content: `${username} said: ${stripMentionTag(content).trim()}`,
};
}
export function formatImageMessage(
username: string,
imageUrl: string,
): ImageMessage {
return {
role: "user",
content: [
{
type: "text",
text: `${username} said: Describe what is in this image.`,
},
{
type: "image_url",
image_url: {
url: imageUrl,
},
},
],
};
}
export function messageOf(response: ChatCompletion): ChatCompletionMessage {
return response.choices.at(0)!.message;
}
@ -45,14 +68,6 @@ export function contentOf(response: ChatCompletion): string {
}
export async function getUserInfo(host: string, id: string) {
/**
* Returns various info about a user given a host and server ID.
*
* @param host - The address of the Tailchat server.
* @param id - The user ID to check.
*
* @returns An object containing all the user data recieved from the server.
*/
try {
const reply = await fetch(`${host}/api/user/getUserInfo`, {
method: "POST",
@ -79,28 +94,16 @@ export async function getUserInfo(host: string, id: string) {
}
export async function getUsername(host: string, id: string) {
/**
* Gets the username of a given user.
*
* @param host - The address of the Tailchat server.
* @param id - The user ID to check.
*
* @returns The username associated with the given user ID.
*/
if (!id) {
throw new Error("No user ID was given.");
}
const reply = await getUserInfo(host, id);
return reply.nickname;
}
export async function isUserBot(host: string, id: string) {
/**
* Returns if the given user is a bot.
*
* @param host - The address of the Tailchat server.
* @param id - The user ID to check.
*
* @returns true if the user ID is associated with a bot, false if not.
*/
const reply = await getUserInfo(host, id);
return reply.type === "openapiBot";