Companion Server
The CompanionServer class provides the P2P server functionality for
companions. It integrates management of libp2p nodes, messaging, turn-taking,
and metadata exchange.
Imports
Section titled “Imports”import { CompanionServer } from "@aikyo/server";Constructor
Section titled “Constructor”constructor( companionAgent: CompanionAgent, history: Message[], config?: { timeoutDuration: number }, libp2pConfig?: Libp2pOptions<Services>)Parameters
Section titled “Parameters”| Parameter | Type | Description | Default |
|---|---|---|---|
companionAgent | CompanionAgent | Companion agent instance | - |
history | Message[] | Conversation history array | - |
config | object | Configuration settings | See below |
config.timeoutDuration | number | Turn delay in milliseconds | 5000 |
libp2pConfig | Libp2pOptions<Services> | Custom libp2p config | - |
Parameter Details:
history: Must reference the same array as CompanionAgentconfig: Default is{ timeoutDuration: 5000 }config.timeoutDuration: Delay before allowing another turn after a turn-taking event
Usage Example
Section titled “Usage Example”import { CompanionAgent, CompanionServer, type Message } from "@aikyo/server";import { anthropic } from "@ai-sdk/anthropic";
const history: Message[] = [];
const companion = new CompanionAgent( companionCard, anthropic("claude-3-5-haiku-latest"), history);
const server = new CompanionServer(companion, history, { timeoutDuration: 1000});
await server.start();When using custom libp2p configuration, you must provide complete settings.
import { CompanionAgent, CompanionServer, type Message } from "@aikyo/server";import { anthropic } from "@ai-sdk/anthropic";import { gossipsub } from "@chainsafe/libp2p-gossipsub";import { noise } from "@chainsafe/libp2p-noise";import { yamux } from "@chainsafe/libp2p-yamux";import { identify } from "@libp2p/identify";import { mdns } from "@libp2p/mdns";import { tcp } from "@libp2p/tcp";
const history: Message[] = [];const companion = new CompanionAgent( companionCard, anthropic("claude-3-5-haiku-latest"), history);
const customServer = new CompanionServer( companion, history, { timeoutDuration: 1000 }, { addresses: { listen: ["/ip4/0.0.0.0/tcp/9000"] }, transports: [tcp()], peerDiscovery: [mdns()], connectionEncrypters: [noise()], streamMuxers: [yamux()], services: { pubsub: gossipsub({ allowPublishToZeroTopicPeers: true, emitSelf: true, }), identify: identify(), }, });
await customServer.start();Properties
Section titled “Properties”companionAgent
Section titled “companionAgent”companionAgent: CompanionAgentInstance of the companion agent.
history
Section titled “history”history: Message[]Array of conversation history (reference).
turnTakingManager
Section titled “turnTakingManager”turnTakingManager: TurnTakingManagerManager responsible for handling turn-taking. For details, see Turn-Taking.
this.turnTakingManager = new TurnTakingManager( this.companionAgent, config ? config.timeoutDuration : 5000,);companion
Section titled “companion”companion: CompanionCardConfiguration card for the companion (same as companionAgent.companion).
libp2p
Section titled “libp2p”libp2p: Libp2p<Services>Instance of the libp2p node. This serves as the core object for P2P communication.
this.libp2p = await createLibp2p({ addresses: { listen: ["/ip4/0.0.0.0/tcp/0"] }, transports: [tcp()], peerDiscovery: [mdns()], connectionEncrypters: [noise()], streamMuxers: [yamux()], services: { pubsub: gossipsub({ allowPublishToZeroTopicPeers: true, emitSelf: true, }), identify: identify(), },});For details, see P2P Communication.
companionList
Section titled “companionList”companionList: Map<string, Metadata>Map managing metadata for connected companions.
companionList = new Map<string, Metadata>();During initialization:
First, register yourself, then add the other party’s metadata when connecting peers.
pendingQueries
Section titled “pendingQueries”pendingQueries = new Map< string, { resolve: (value: QueryResult) => void; reject: (reason: string) => void; }>();Map managing the state of pending queries to clients. For details, see Query.
libp2pConfig
Section titled “libp2pConfig”libp2pConfig?: Libp2pOptions<Services>Optional: Custom configuration for the libp2p node. If omitted, default settings are used.
Methods
Section titled “Methods”start()
Section titled “start()”Starts the server.
async start(): Promise<void>Processing Flow:
- Initialize the libp2p node (
setupLibp2p()) - Register event listeners
handleMessageReceived()
Section titled “handleMessageReceived()”Handles the reception of messages.
async handleMessageReceived(message: Message): Promise<void>Parameters:
message: The received message
Processing Flow:
- Register the message with the
TurnTakingManager - Generate your own
State - Publish the State to the
statestopic
This method is called from handlePubSubMessage.