From 4d7425b9294f71a1e7e845df25bb386c2cf0e31b Mon Sep 17 00:00:00 2001 From: Dany Date: Tue, 24 Dec 2024 13:12:59 +0100 Subject: [PATCH] update send method to work with multimodality --- .changeset/dirty-meals-confess.md | 5 +++++ api/client.ts | 24 +++++++++++------------- api/models/Plugin.ts | 4 ++-- api/utils.ts | 17 +++++++++++++---- catapi.json | 8 ++++++++ 5 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 .changeset/dirty-meals-confess.md diff --git a/.changeset/dirty-meals-confess.md b/.changeset/dirty-meals-confess.md new file mode 100644 index 0000000..d15ff7e --- /dev/null +++ b/.changeset/dirty-meals-confess.md @@ -0,0 +1,5 @@ +--- +"ccat-api": minor +--- + +update send method to work with multimodality diff --git a/api/client.ts b/api/client.ts index 1cd3753..6717fa5 100644 --- a/api/client.ts +++ b/api/client.ts @@ -4,6 +4,7 @@ import { SocketResponse, SocketError, WebSocketState, WebSocketSettings, isMessageResponse, CatSettings, + SocketRequest, } from './utils' /** @@ -114,13 +115,12 @@ export class CatClient { /** * Sends a message to the Cat through the WebSocket connection. - * @param message The message to send to the Cat. - * @param data The custom data to send to the Cat. + * @param msg The message to send to the Cat. * @param userId The ID of the user sending the message. Defaults to "user". + * @throws If the message does not contain text, audio or image. * @returns The `CatClient` instance. */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - send(message: string, data?: Record, userId?: string): CatClient { + send(msg: SocketRequest, userId?: string): CatClient { if (this.ws?.readyState !== WebSocket.OPEN) { this.errorHandler?.({ name: 'SocketClosed', @@ -128,15 +128,13 @@ export class CatClient { }) return this } - if (data && ('text' in data || 'user_id' in data)) { - throw new Error('The data object should not have a "text" or a "user_id" property') - } - const jsonMessage = JSON.stringify({ - text: message, - user_id: userId ?? (this.config.userId ?? 'user'), - ...data - }) - this.ws.send(jsonMessage) + if ('text' in msg || 'audio' in msg || 'image' in msg) { + const jsonMessage = JSON.stringify({ + ...msg, + user_id: userId ?? (this.config.userId ?? 'user'), + }) + this.ws.send(jsonMessage) + } else throw new Error('The message argument must contain either text, audio or image.') return this } diff --git a/api/models/Plugin.ts b/api/models/Plugin.ts index 5f1b843..8818245 100644 --- a/api/models/Plugin.ts +++ b/api/models/Plugin.ts @@ -12,6 +12,8 @@ export type Plugin = { tags: string; thumb: string; version: string; + min_cat_version?: string; + max_cat_version?: string; active?: boolean; url?: string; upgrade?: string; @@ -22,7 +24,5 @@ export type Plugin = { tools?: Array<{ name: string; }>; - min_cat_version: string; - max_cat_version: string; }; diff --git a/api/utils.ts b/api/utils.ts index 3871e8b..e551a25 100644 --- a/api/utils.ts +++ b/api/utils.ts @@ -74,11 +74,18 @@ export enum WebSocketState { CONNECTING, OPEN, CLOSING, CLOSED } -export interface SocketResponse { +export interface SocketRequest { + text?: string + audio?: string + image?: string + [key: string]: any +} + +export interface SocketResponse extends SocketRequest { type: 'notification' | 'chat' | 'chat_token' - content: string + user_id: string + who: string why?: MessageWhy & Record - [key: string]: any } export interface SocketError { @@ -88,7 +95,9 @@ export interface SocketError { export const isMessageResponse = (value: unknown): value is SocketResponse => { return !!(value && typeof value === 'object' - && 'content' in value + && ('text' in value || 'audio' in value || 'image' in value) + && 'user_id' in value + && 'who' in value && 'type' in value && value.type !== 'error' ) diff --git a/catapi.json b/catapi.json index 7754620..416c99e 100644 --- a/catapi.json +++ b/catapi.json @@ -2391,6 +2391,14 @@ "title": "Version", "type": "string" }, + "min_cat_version": { + "title": "Min Cat Version", + "type": "string" + }, + "max_cat_version": { + "title": "Max Cat Version", + "type": "string" + }, "active": { "title": "Active", "type": "boolean"