Skip to content

Commit

Permalink
update send method to work with multimodality
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Dec 24, 2024
1 parent fabad68 commit 4d7425b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-meals-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ccat-api": minor
---

update send method to work with multimodality
24 changes: 11 additions & 13 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SocketResponse, SocketError,
WebSocketState, WebSocketSettings,
isMessageResponse, CatSettings,
SocketRequest,
} from './utils'

/**
Expand Down Expand Up @@ -114,29 +115,26 @@ 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<string, any>, userId?: string): CatClient {
send(msg: SocketRequest, userId?: string): CatClient {
if (this.ws?.readyState !== WebSocket.OPEN) {
this.errorHandler?.({
name: 'SocketClosed',
description: 'The connection to the server was closed'
})
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
}

Expand Down
4 changes: 2 additions & 2 deletions api/models/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +24,5 @@ export type Plugin = {
tools?: Array<{
name: string;
}>;
min_cat_version: string;
max_cat_version: string;
};

17 changes: 13 additions & 4 deletions api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>
[key: string]: any
}

export interface SocketError {
Expand All @@ -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'
)
Expand Down
8 changes: 8 additions & 0 deletions catapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4d7425b

Please sign in to comment.