Skip to content

Commit

Permalink
Merge pull request #51 from cheshire-cat-ai/develop
Browse files Browse the repository at this point in the history
Version 0.12.0
  • Loading branch information
zAlweNy26 authored Jan 21, 2025
2 parents 738c22a + 267a1e8 commit 720e615
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 59 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
2 changes: 2 additions & 0 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 Down
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
22 changes: 15 additions & 7 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ type Plugin = {
tags: string;
thumb: string;
version: string;
min_cat_version?: string;
max_cat_version?: string;
active?: boolean;
url?: string;
upgrade?: string;
Expand Down Expand Up @@ -740,11 +742,17 @@ declare enum WebSocketState {
CLOSING = 2,
CLOSED = 3
}
interface SocketResponse {
interface SocketRequest {
text?: string;
audio?: string;
image?: string;
[key: string]: any;
}
interface SocketResponse extends SocketRequest {
type: 'notification' | 'chat' | 'chat_token';
content: string;
user_id: string;
who: string;
why?: MessageWhy & Record<string, any>;
[key: string]: any;
}
interface SocketError {
name: 'SocketError' | 'FailedRetry' | 'SocketClosed';
Expand Down Expand Up @@ -783,12 +791,12 @@ declare class CatClient {
init(): 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.
*/
send(message: string, data?: Record<string, any>, userId?: string): CatClient;
send(msg: SocketRequest, userId?: string): CatClient;
/**
* @returns The API Client
*/
Expand Down Expand Up @@ -865,4 +873,4 @@ type HTTPValidationError = {
};
};

export { type AcceptedMemoryType, AcceptedMemoryTypes, type AcceptedPluginType, AcceptedPluginTypes, ApiError, type AuthPermission, type AuthResource, type BodyInstallPlugin, type BodyUploadFile, type BodyUploadMemory, type BodyUploadUrl, CancelError, CancelablePromise, CatClient, type CatMessage, type CatSettings, type Collection, type CollectionData, type CollectionsList, type ConversationMessage, type DeleteResponse, type FileResponse, type HTTPValidationError, type JWTResponse, type MemoryRecall, type MessageWhy, type Metadata, type Plugin, type PluginsList, type QueryData, type Setting, type SettingBody, type SettingsResponse, type SocketError, type SocketResponse, type Status, type UserCreate, type UserCredentials, type UserResponse, type UserUpdate, type VectorsData, type WebResponse, type WebSocketSettings, WebSocketState, CatClient as default, isMessageResponse };
export { type AcceptedMemoryType, AcceptedMemoryTypes, type AcceptedPluginType, AcceptedPluginTypes, ApiError, type AuthPermission, type AuthResource, type BodyInstallPlugin, type BodyUploadFile, type BodyUploadMemory, type BodyUploadUrl, CancelError, CancelablePromise, CatClient, type CatMessage, type CatSettings, type Collection, type CollectionData, type CollectionsList, type ConversationMessage, type DeleteResponse, type FileResponse, type HTTPValidationError, type JWTResponse, type MemoryRecall, type MessageWhy, type Metadata, type Plugin, type PluginsList, type QueryData, type Setting, type SettingBody, type SettingsResponse, type SocketError, type SocketRequest, type SocketResponse, type Status, type UserCreate, type UserCredentials, type UserResponse, type UserUpdate, type VectorsData, type WebResponse, type WebSocketSettings, WebSocketState, CatClient as default, isMessageResponse };
22 changes: 15 additions & 7 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ type Plugin = {
tags: string;
thumb: string;
version: string;
min_cat_version?: string;
max_cat_version?: string;
active?: boolean;
url?: string;
upgrade?: string;
Expand Down Expand Up @@ -740,11 +742,17 @@ declare enum WebSocketState {
CLOSING = 2,
CLOSED = 3
}
interface SocketResponse {
interface SocketRequest {
text?: string;
audio?: string;
image?: string;
[key: string]: any;
}
interface SocketResponse extends SocketRequest {
type: 'notification' | 'chat' | 'chat_token';
content: string;
user_id: string;
who: string;
why?: MessageWhy & Record<string, any>;
[key: string]: any;
}
interface SocketError {
name: 'SocketError' | 'FailedRetry' | 'SocketClosed';
Expand Down Expand Up @@ -783,12 +791,12 @@ declare class CatClient {
init(): 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.
*/
send(message: string, data?: Record<string, any>, userId?: string): CatClient;
send(msg: SocketRequest, userId?: string): CatClient;
/**
* @returns The API Client
*/
Expand Down Expand Up @@ -865,4 +873,4 @@ type HTTPValidationError = {
};
};

export { type AcceptedMemoryType, AcceptedMemoryTypes, type AcceptedPluginType, AcceptedPluginTypes, ApiError, type AuthPermission, type AuthResource, type BodyInstallPlugin, type BodyUploadFile, type BodyUploadMemory, type BodyUploadUrl, CancelError, CancelablePromise, CatClient, type CatMessage, type CatSettings, type Collection, type CollectionData, type CollectionsList, type ConversationMessage, type DeleteResponse, type FileResponse, type HTTPValidationError, type JWTResponse, type MemoryRecall, type MessageWhy, type Metadata, type Plugin, type PluginsList, type QueryData, type Setting, type SettingBody, type SettingsResponse, type SocketError, type SocketResponse, type Status, type UserCreate, type UserCredentials, type UserResponse, type UserUpdate, type VectorsData, type WebResponse, type WebSocketSettings, WebSocketState, CatClient as default, isMessageResponse };
export { type AcceptedMemoryType, AcceptedMemoryTypes, type AcceptedPluginType, AcceptedPluginTypes, ApiError, type AuthPermission, type AuthResource, type BodyInstallPlugin, type BodyUploadFile, type BodyUploadMemory, type BodyUploadUrl, CancelError, CancelablePromise, CatClient, type CatMessage, type CatSettings, type Collection, type CollectionData, type CollectionsList, type ConversationMessage, type DeleteResponse, type FileResponse, type HTTPValidationError, type JWTResponse, type MemoryRecall, type MessageWhy, type Metadata, type Plugin, type PluginsList, type QueryData, type Setting, type SettingBody, type SettingsResponse, type SocketError, type SocketRequest, type SocketResponse, type Status, type UserCreate, type UserCredentials, type UserResponse, type UserUpdate, type VectorsData, type WebResponse, type WebSocketSettings, WebSocketState, CatClient as default, isMessageResponse };
25 changes: 11 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ var WebSocketState = /* @__PURE__ */ ((WebSocketState2) => {
return WebSocketState2;
})(WebSocketState || {});
var isMessageResponse = (value) => {
return !!(value && typeof value === "object" && "content" in value && "type" in value && value.type !== "error");
return !!(value && typeof value === "object" && ("text" in value || "audio" in value || "image" in value) && "user_id" in value && "who" in value && "type" in value && value.type !== "error");
};

// api/client.ts
Expand Down Expand Up @@ -1417,29 +1417,26 @@ var CatClient = class {
}
/**
* 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, data, userId) {
send(msg, userId) {
if (this.ws?.readyState !== import_isomorphic_ws.default.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
25 changes: 11 additions & 14 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ var WebSocketState = /* @__PURE__ */ ((WebSocketState2) => {
return WebSocketState2;
})(WebSocketState || {});
var isMessageResponse = (value) => {
return !!(value && typeof value === "object" && "content" in value && "type" in value && value.type !== "error");
return !!(value && typeof value === "object" && ("text" in value || "audio" in value || "image" in value) && "user_id" in value && "who" in value && "type" in value && value.type !== "error");
};

// api/client.ts
Expand Down Expand Up @@ -1373,29 +1373,26 @@ var CatClient = class {
}
/**
* 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, data, userId) {
send(msg, userId) {
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

0 comments on commit 720e615

Please sign in to comment.