Skip to content

ppagent / Agent

Class: Agent

Defined in: src/agent/agent.ts:145

一个智能体,用于连接source和bot。 各自职责如下:

  • source:负责收消息、发消息、提供外部消息服务的接口能力
  • bot:负责判断是否有能力响应某种消息、构建后端工具所需的消息体、发送消息给后端并回调给agent
  • agent:负责监听source的消息、判断是否应该响应、找到bot获取响应、调用技能、回传响应结果给source

Extends

  • default

Implements

Constructors

Constructor

new Agent(app, _options): Agent

Defined in: src/agent/agent.ts:533

Parameters

app

PPAgent

_options

IAgentOptions

Returns

Agent

Overrides

Emittery.constructor

Properties

debug

debug: DebugOptions<Record<PropertyKey, any>>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:308

Debugging options for the current instance.

Inherited from

Emittery.debug


isDebugEnabled

static isDebugEnabled: boolean

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:229

Toggle debug mode for all instances.

Default: true if the DEBUG environment variable is set to emittery or *, otherwise false.

Example

import Emittery from 'emittery';

Emittery.isDebugEnabled = true;

const emitter1 = new Emittery({debug: {name: 'myEmitter1'}});
const emitter2 = new Emittery({debug: {name: 'myEmitter2'}});

emitter1.on('test', data => {
	// …
});

emitter2.on('otherTest', data => {
	// …
});

emitter1.emit('test');
//=> [16:43:20.417][emittery:subscribe][myEmitter1] Event Name: test
//	data: undefined

emitter2.emit('otherTest');
//=> [16:43:20.417][emittery:subscribe][myEmitter2] Event Name: otherTest
//	data: undefined

Inherited from

Emittery.isDebugEnabled


listenerAdded

readonly static listenerAdded: typeof listenerAdded

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:255

Fires when an event listener was added.

An object with listener and eventName (if on or off was used) is provided as event data.

Example

import Emittery from 'emittery';

const emitter = new Emittery();

emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
	console.log(listener);
	//=> data => {}

	console.log(eventName);
	//=> '🦄'
});

emitter.on('🦄', data => {
	// Handle data
});

Inherited from

Emittery.listenerAdded


listenerRemoved

readonly static listenerRemoved: typeof listenerRemoved

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:283

Fires when an event listener was removed.

An object with listener and eventName (if on or off was used) is provided as event data.

Example

import Emittery from 'emittery';

const emitter = new Emittery();

const off = emitter.on('🦄', data => {
	// Handle data
});

emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
	console.log(listener);
	//=> data => {}

	console.log(eventName);
	//=> '🦄'
});

off();

Inherited from

Emittery.listenerRemoved


params

static params: IConfigParams

Defined in: src/agent/agent.ts:146

Accessors

name

Get Signature

get name(): string

Defined in: src/agent/agent.ts:580

Returns

string


options

Get Signature

get options(): IAgentOptions

Defined in: src/agent/agent.ts:576

Returns

IAgentOptions

Methods

anyEvent()

anyEvent(): AsyncIterableIterator<[PropertyKey, any]>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:562

Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.

Call return() on the iterator to remove the subscription.

In the same way as for events, you can subscribe by using the for await statement.

Returns

AsyncIterableIterator<[PropertyKey, any]>

Example

import Emittery from 'emittery';

const emitter = new Emittery();
const iterator = emitter.anyEvent();

emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🌟', '🌈2'); // Buffered

iterator.next()
	.then(({value, done}) => {
		// done is false
		// value is ['🦄', '🌈1']
		return iterator.next();
	})
	.then(({value, done}) => {
		// done is false
		// value is ['🌟', '🌈2']
		// revoke subscription
		return iterator.return();
	})
	.then(({done}) => {
		// done is true
	});

Inherited from

Emittery.anyEvent


bindMethods()

bindMethods(target, methodNames?): void

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:602

Bind the given methodNames, or all Emittery methods if methodNames is not defined, into the target object.

Parameters

target

Record<string, unknown>

methodNames?

readonly string[]

Returns

void

Example

import Emittery from 'emittery';

const object = {};

new Emittery().bindMethods(object);

object.emit('event');

Inherited from

Emittery.bindMethods


clearListeners()

clearListeners<Name>(eventName?): void

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:581

Clear all event listeners on the instance.

If eventName is given, only the listeners for that event are cleared.

Type Parameters

Name

Name extends PropertyKey

Parameters

eventName?

Name | readonly Name[]

Returns

void

Inherited from

Emittery.clearListeners


dispose()

dispose(): Promise<string>

Defined in: src/agent/agent.ts:650

Returns

Promise<string>

Implementation of

IDisposable.dispose


emit()

Call Signature

emit<Name>(eventName): Promise<void>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:496

Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.

Type Parameters
Name

Name extends DatalessEventNames<Record<PropertyKey, any>>

Parameters
eventName

Name

Returns

Promise<void>

A promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.

Inherited from

Emittery.emit

Call Signature

emit<Name>(eventName, eventData): Promise<void>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:497

Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.

Type Parameters
Name

Name extends PropertyKey

Parameters
eventName

Name

eventData

Record<PropertyKey, any>[Name]

Returns

Promise<void>

A promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.

Inherited from

Emittery.emit


emitSerial()

Call Signature

emitSerial<Name>(eventName): Promise<void>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:509

Same as emit(), but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.

If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.

Type Parameters
Name

Name extends DatalessEventNames<Record<PropertyKey, any>>

Parameters
eventName

Name

Returns

Promise<void>

A promise that resolves when all the event listeners are done.

Inherited from

Emittery.emitSerial

Call Signature

emitSerial<Name>(eventName, eventData): Promise<void>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:510

Same as emit(), but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.

If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.

Type Parameters
Name

Name extends PropertyKey

Parameters
eventName

Name

eventData

Record<PropertyKey, any>[Name]

Returns

Promise<void>

A promise that resolves when all the event listeners are done.

Inherited from

Emittery.emitSerial


events()

events<Name>(eventName): AsyncIterableIterator<Record<PropertyKey, any>[Name]>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:431

Get an async iterator which buffers data each time an event is emitted.

Call return() on the iterator to remove the subscription.

Type Parameters

Name

Name extends PropertyKey

Parameters

eventName

Name | readonly Name[]

Returns

AsyncIterableIterator<Record<PropertyKey, any>[Name]>

Examples

import Emittery from 'emittery';

const emitter = new Emittery();
const iterator = emitter.events('🦄');

emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered

iterator
	.next()
	.then(({value, done}) => {
		// done === false
		// value === '🌈1'
		return iterator.next();
	})
	.then(({value, done}) => {
		// done === false
		// value === '🌈2'
		// Revoke subscription
		return iterator.return();
	})
	.then(({done}) => {
		// done === true
	});

In practice you would usually consume the events using the for await statement. In that case, to revoke the subscription simply break the loop.

import Emittery from 'emittery';

const emitter = new Emittery();
const iterator = emitter.events('🦄');

emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦄', '🌈2'); // Buffered

// In an async context.
for await (const data of iterator) {
	if (data === '🌈2') {
		break; // Revoke the subscription when we see the value `🌈2`.
	}
}

It accepts multiple event names.

import Emittery from 'emittery';

const emitter = new Emittery();
const iterator = emitter.events(['🦄', '🦊']);

emitter.emit('🦄', '🌈1'); // Buffered
emitter.emit('🦊', '🌈2'); // Buffered

iterator
	.next()
	.then(({value, done}) => {
		// done === false
		// value === '🌈1'
		return iterator.next();
	})
	.then(({value, done}) => {
		// done === false
		// value === '🌈2'
		// Revoke subscription
		return iterator.return();
	})
	.then(({done}) => {
		// done === true
	});

Inherited from

Emittery.events


listenerCount()

listenerCount<Name>(eventName?): number

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:586

The number of listeners for the eventName or all events if not specified.

Type Parameters

Name

Name extends PropertyKey

Parameters

eventName?

Name | readonly Name[]

Returns

number

Inherited from

Emittery.listenerCount


off()

off<Name>(eventName, listener): void

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:459

Remove one or more event subscriptions.

Type Parameters

Name

Name extends string | number | symbol

Parameters

eventName

Name | readonly Name[]

listener

(eventData) => void | Promise<void>

Returns

void

Example

import Emittery from 'emittery';

const emitter = new Emittery();

const listener = data => {
	console.log(data);
};

emitter.on(['🦄', '🐶', '🦊'], listener);
await emitter.emit('🦄', 'a');
await emitter.emit('🐶', 'b');
await emitter.emit('🦊', 'c');
emitter.off('🦄', listener);
emitter.off(['🐶', '🦊'], listener);
await emitter.emit('🦄', 'a'); // nothing happens
await emitter.emit('🐶', 'b'); // nothing happens
await emitter.emit('🦊', 'c'); // nothing happens

Inherited from

Emittery.off


offAny()

offAny(listener): void

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:569

Remove an onAny subscription.

Parameters

listener

(eventName, eventData) => void | Promise<void>

Returns

void

Inherited from

Emittery.offAny


on()

on<Name>(eventName, listener, options?): UnsubscribeFunction

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:342

Subscribe to one or more events.

Using the same listener multiple times for the same event will result in only one method call per emitted event.

Type Parameters

Name

Name extends string | number | symbol

Parameters

eventName

Name | readonly Name[]

listener

(eventData) => void | Promise<void>

options?
signal?

AbortSignal

Returns

UnsubscribeFunction

An unsubscribe method.

Example

import Emittery from 'emittery';

const emitter = new Emittery();

emitter.on('🦄', data => {
	console.log(data);
});

emitter.on(['🦄', '🐶'], data => {
	console.log(data);
});

emitter.emit('🦄', '🌈'); // log => '🌈' x2
emitter.emit('🐶', '🍖'); // log => '🍖'

Inherited from

Emittery.on


onAny()

onAny(listener, options?): UnsubscribeFunction

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:520

Subscribe to be notified about any event.

Parameters

listener

(eventName, eventData) => void | Promise<void>

options?
signal?

AbortSignal

Returns

UnsubscribeFunction

A method to unsubscribe.

Inherited from

Emittery.onAny


once()

once<Name>(eventName): EmitteryOncePromise<Record<PropertyKey, any> & OmnipresentEventData[Name]>

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:489

Subscribe to one or more events only once. It will be unsubscribed after the first event.

Type Parameters

Name

Name extends string | number | symbol

Parameters

eventName

Name | readonly Name[]

Returns

EmitteryOncePromise<Record<PropertyKey, any> & OmnipresentEventData[Name]>

The promise of event data when eventName is emitted. This promise is extended with an off method.

Example

import Emittery from 'emittery';

const emitter = new Emittery();

emitter.once('🦄').then(data => {
	console.log(data);
	//=> '🌈'
});

emitter.once(['🦄', '🐶']).then(data => {
	console.log(data);
});

emitter.emit('🦄', '🌈'); // Logs `🌈` twice
emitter.emit('🐶', '🍖'); // Nothing happens

Inherited from

Emittery.once


sendMessage()

sendMessage(message, to, now, fromMessage?): Promise<string>

Defined in: src/agent/agent.ts:593

发送消息到指定的消息源

Parameters

message

ISourceChatMessage

要发送的消息

to

ISource

要发送的消息源

now

boolean = false

是否立即发送,如果否,则根据配置确定是否加入发送队列

fromMessage?

ISourceChatMessage

该消息的前置消息。如果source端只能被动回复消息,可能在发送的时候需要提供来源消息。

Returns

Promise<string>


mixin()

static mixin(emitteryPropertyName, methodNames?): <T>(klass) => T

Defined in: node_modules/.pnpm/emittery@1.1.0/node_modules/emittery/index.d.ts:300

In TypeScript, it returns a decorator which mixins Emittery as property emitteryPropertyName and methodNames, or all Emittery methods if methodNames is not defined, into the target class.

Parameters

emitteryPropertyName

string | symbol

methodNames?

readonly string[]

Returns

<T>(klass): T

Type Parameters
T

T extends (...arguments_) => any

Parameters
klass

T

Returns

T

Example

import Emittery from 'emittery';

@Emittery.mixin('emittery')
class MyClass {}

const instance = new MyClass();

instance.emit('event');

Inherited from

Emittery.mixin