Client
A class for interacting with the ClashAI API.
new Client(api_key, model)
Properties
The API key used to authenticate with the ClashAI API.
The base URL for the ClashAI API.
private #model: Models
The model used for generating completions.
A map of user IDs to their conversation history.
public static captureRejectionSymbol: unique symbol
Value: Symbol.for('nodejs.rejection')
See how to write a custom rejection handler
.
public static captureRejections: boolean
Value: boolean
Change the default captureRejections
option on all new EventEmitter
objects.
public static defaultMaxListeners: number
By default, a maximum of 10
listeners can be registered for any single event. This limit can be changed for individual EventEmitter
instances using the emitter.setMaxListeners(n)
method. To change the default for allEventEmitter
instances, the events.defaultMaxListeners
property can be used. If this value is not a positive number, a RangeError
is thrown.
Take caution when setting the events.defaultMaxListeners
because the change affects all EventEmitter
instances, including those created before the change is made. However, calling emitter.setMaxListeners(n)
still has precedence over events.defaultMaxListeners
.
This is not a hard limit. The EventEmitter
instance will allow more listeners to be added but will output a trace warning to stderr indicating that a "possible EventEmitter memory leak" has been detected. For any single EventEmitter
, the emitter.getMaxListeners()
and emitter.setMaxListeners()
methods can be used to temporarily avoid this warning:
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
// do stuff
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});
The --trace-warnings
command-line flag can be used to display the stack trace for such warnings.
The emitted warning can be inspected with process.on('warning')
and will have the additional emitter
, type
, and count
properties, referring to the event emitter instance, the event's name and the number of attached listeners, respectively. Its name
property is set to 'MaxListenersExceededWarning'
.
public static errorMonitor: unique symbol
This symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular 'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an 'error'
event is emitted. Therefore, the process will still crash if no regular 'error'
listener is installed.
Returns the user histories map.
Methods
Gets the conversation history for a user.
Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbol
s.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
public getMaxListeners(): number
Returns the current max listener value for the EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to defaultMaxListeners.
Returns the statistics of a specific user.
const client = new Client("your clash ai api key", "chatgpt-4o-latest");
const stats = await client.getUsage("your user id");
Returns the number of listeners listening for the event named eventName
. If listener
is provided, it will return how many times the listener is found in the list of the listeners of the event.
eventName
K
❌
The name of the event being listened for
public listeners<K extends keyof ClientEvents>(eventName): Array<Function>
Returns a copy of the array of listeners for the event named eventName
.
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
eventName
K
❌
public async makeRequest(messages, user_id?): Promise<Response | null>
Asks the model a question and returns the response.
const client = new Client("your clash ai api key", "gpt-4o");
const response = await client.makeRequest([{ role: "system", content: "You are a friendly chatbot." }, { role: "user", content: "Hello, how are you?" }]);
public off(eventName, listener): this
Alias for emitter.removeListener()
.
public on<K extends keyof ClientEvents>(event, listener): this
Adds the listener
function to the end of the listeners array for the event named eventName
. No checks are made to see if the listener
has already been added. Multiple calls passing the same combination of eventName
and listener
will result in the listener
being added, and called, multiple times.
server.on('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependListener()
method can be used as an alternative to add the event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
event
K
❌
The event to listen for.
public once<K extends keyof ClientEvents>(event, listener): this
Adds a one-time listener
function for the event named eventName
. The next time eventName
is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener()
method can be used as an alternative to add the event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
event
K
❌
The event to listen for.
public prependListener<K>(eventName, listener): this
Adds the listener
function to the beginning of the listeners array for the event named eventName
. No checks are made to see if the listener
has already been added. Multiple calls passing the same combination of eventName
and listener
will result in the listener
being added, and called, multiple times.
server.prependListener('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
eventName
K
❌
The event to listen for.
public prependOnceListener<K>(eventName, listener): this
Adds a one-timelistener
function for the event named eventName
to the beginning of the listeners array. The next time eventName
is triggered, this listener is removed, and then invoked.
server.prependOnceListener('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
eventName
K
❌
The event to listen for.
Last updated