Compare commits
No commits in common. "6257a0a704e64cc2a2206e445543a6b558b35367" and "49e561da12286cae4cb036792d9a400a3d6366f5" have entirely different histories.
6257a0a704
...
49e561da12
28
src/bot.ts
28
src/bot.ts
|
@ -8,8 +8,6 @@ import {
|
||||||
isUserBot,
|
isUserBot,
|
||||||
contentOf,
|
contentOf,
|
||||||
messageOf,
|
messageOf,
|
||||||
formatImageMessage,
|
|
||||||
formatUserMessage,
|
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import {
|
import {
|
||||||
allowedChat,
|
allowedChat,
|
||||||
|
@ -117,7 +115,21 @@ client.connect().then(async () => {
|
||||||
content: "[md]`Analyzing image...`[/md]",
|
content: "[md]`Analyzing image...`[/md]",
|
||||||
});
|
});
|
||||||
|
|
||||||
session.push(formatImageMessage(username, imageData));
|
session.push({
|
||||||
|
role: "user",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `${username} said: Describe what is in this image.`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "image_url",
|
||||||
|
image_url: {
|
||||||
|
url: imageData,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
const response = await assistant.chat.completions.create({
|
const response = await assistant.chat.completions.create({
|
||||||
messages: session,
|
messages: session,
|
||||||
|
@ -149,7 +161,10 @@ client.connect().then(async () => {
|
||||||
content: THINKING,
|
content: THINKING,
|
||||||
});
|
});
|
||||||
|
|
||||||
session.push(formatUserMessage(username, message.content));
|
session.push({
|
||||||
|
role: "user",
|
||||||
|
content: `${username} said: ${stripMentionTag(message.content).trim()}`,
|
||||||
|
});
|
||||||
|
|
||||||
const response = await assistant.chat.completions.create({
|
const response = await assistant.chat.completions.create({
|
||||||
messages: session,
|
messages: session,
|
||||||
|
@ -187,7 +202,10 @@ client.connect().then(async () => {
|
||||||
content: THINKING,
|
content: THINKING,
|
||||||
});
|
});
|
||||||
|
|
||||||
session.push(formatUserMessage(username, message.content));
|
session.push({
|
||||||
|
role: "user",
|
||||||
|
content: `[DIRECT MESSAGE] ${username} said: ${stripMentionTag(message.content).trim()}`,
|
||||||
|
});
|
||||||
|
|
||||||
const response = await assistant.chat.completions.create({
|
const response = await assistant.chat.completions.create({
|
||||||
messages: session,
|
messages: session,
|
||||||
|
|
|
@ -11,7 +11,7 @@ export interface ImageMessage {
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: "text";
|
type: "text";
|
||||||
text: `${string} said: Describe what is in this image.`;
|
text: string;
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "image_url";
|
type: "image_url";
|
||||||
|
@ -30,7 +30,7 @@ export type AnyChatCompletion =
|
||||||
export enum ImageSize {
|
export enum ImageSize {
|
||||||
Small = "256x256",
|
Small = "256x256",
|
||||||
Medium = "512x512",
|
Medium = "512x512",
|
||||||
Large = "1024x1024",
|
Large = "1024x1024"
|
||||||
}
|
}
|
||||||
|
|
||||||
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
|
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
|
||||||
|
|
71
src/utils.ts
71
src/utils.ts
|
@ -1,13 +1,21 @@
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { AnyChatCompletion, ImageMessage, Messages } from "./types";
|
import { Messages } from "./types";
|
||||||
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
||||||
import { stripMentionTag } from "tailchat-client-sdk";
|
|
||||||
|
|
||||||
export function checkFile(
|
export function checkFile(
|
||||||
file: string,
|
file: string,
|
||||||
encoding: fs.EncodingOption,
|
encoding: fs.EncodingOption,
|
||||||
defaultContent: string,
|
defaultContent: string,
|
||||||
): Messages {
|
): 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;
|
let final: Messages;
|
||||||
const generic: Messages = [
|
const generic: Messages = [
|
||||||
{
|
{
|
||||||
|
@ -28,37 +36,6 @@ export function checkFile(
|
||||||
return final;
|
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 {
|
export function messageOf(response: ChatCompletion): ChatCompletionMessage {
|
||||||
return response.choices.at(0)!.message;
|
return response.choices.at(0)!.message;
|
||||||
}
|
}
|
||||||
|
@ -68,6 +45,14 @@ export function contentOf(response: ChatCompletion): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUserInfo(host: string, id: 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 {
|
try {
|
||||||
const reply = await fetch(`${host}/api/user/getUserInfo`, {
|
const reply = await fetch(`${host}/api/user/getUserInfo`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -94,16 +79,28 @@ export async function getUserInfo(host: string, id: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUsername(host: string, id: string) {
|
export async function getUsername(host: string, id: string) {
|
||||||
if (!id) {
|
/**
|
||||||
throw new Error("No user ID was given.");
|
* 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.
|
||||||
|
*/
|
||||||
const reply = await getUserInfo(host, id);
|
const reply = await getUserInfo(host, id);
|
||||||
|
|
||||||
return reply.nickname;
|
return reply.nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isUserBot(host: string, id: string) {
|
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);
|
const reply = await getUserInfo(host, id);
|
||||||
|
|
||||||
return reply.type === "openapiBot";
|
return reply.type === "openapiBot";
|
||||||
|
|
Loading…
Reference in a new issue