@ghostspeak/sdk
Version:
TypeScript SDK for GhostSpeak AI Agent Commerce Protocol - Production Ready Beta
500 lines (496 loc) • 11 kB
TypeScript
import { Address } from '@solana/addresses';
import { Signature } from '@solana/kit';
/**
* Comprehensive Type System for GhostSpeak SDK
*
* All types are exported as individual named exports using ES2015 module syntax
* for better tree-shaking and compatibility with ESLint rules.
*/
/**
* Transaction result with success/failure handling
*/
type Result<T> = {
success: true;
data: T;
signature: Signature;
explorer: string;
} | {
success: false;
error: SDKError;
};
/**
* Generic transaction interface
*/
interface Transaction<T> {
execute(): Promise<Result<T>>;
simulate(): Promise<SimulationResult>;
getCost(): Promise<bigint>;
debug(): this;
}
/**
* Simulation result
*/
interface SimulationResult {
success: boolean;
logs: string[];
unitsConsumed?: bigint;
error?: string;
}
/**
* Agent entity
*/
interface Agent {
address: Address;
type: AgentType;
owner: Address;
metadata: AgentMetadata;
reputation: Reputation;
isActive: boolean;
isVerified: boolean;
createdAt: Date;
}
/**
* Agent types
*/
declare enum AgentType {
General = 0,
Specialized = 1,
Oracle = 2,
Validator = 3
}
/**
* Agent metadata
*/
interface AgentMetadata {
name: string;
description: string;
capabilities: string[];
avatar?: string;
website?: string;
socialLinks?: Record<string, string>;
}
/**
* Reputation data
*/
interface Reputation {
score: number;
jobsCompleted: number;
successRate: number;
totalEarnings: bigint;
ratings: Rating[];
}
/**
* Rating entry
*/
interface Rating {
from: Address;
score: number;
comment?: string;
timestamp: Date;
}
/**
* Escrow entity
*/
interface Escrow {
address: Address;
buyer: Address;
seller: Address;
amount: bigint;
status: EscrowStatus;
description: string;
milestones?: Milestone[];
createdAt: Date;
completedAt?: Date;
}
/**
* Escrow status
*/
declare enum EscrowStatus {
Active = "active",
Completed = "completed",
Cancelled = "cancelled",
Disputed = "disputed",
Refunded = "refunded"
}
/**
* Milestone for escrow
*/
interface Milestone {
amount: bigint;
description: string;
completed: boolean;
completedAt?: Date;
}
/**
* Channel entity
*/
interface Channel {
address: Address;
name: string;
description: string;
type: ChannelType;
creator: Address;
members: Address[];
isPrivate: boolean;
maxMembers: number;
messageCount: number;
createdAt: Date;
}
/**
* Channel types
*/
declare enum ChannelType {
Public = "public",
Private = "private",
Direct = "direct",
Group = "group"
}
/**
* Message entity
*/
interface Message {
address: Address;
channel: Address;
sender: Address;
content: string;
type: MessageType;
attachments?: Attachment[];
reactions?: Reaction[];
replyTo?: Address;
editedAt?: Date;
timestamp: Date;
}
/**
* Message types
*/
declare enum MessageType {
Text = "text",
Image = "image",
File = "file",
Code = "code",
System = "system"
}
/**
* Message attachment
*/
interface Attachment {
uri: string;
mimeType: string;
size: number;
name: string;
}
/**
* Message reaction
*/
interface Reaction {
emoji: string;
users: Address[];
}
/**
* Confidential balance
*/
interface ConfidentialBalance {
encrypted: Uint8Array;
decrypted?: bigint;
}
/**
* Transfer with proof
*/
interface ConfidentialTransfer {
from: Address;
to: Address;
encryptedAmount: Uint8Array;
proof: TransferProof;
}
/**
* Transfer proof data
*/
interface TransferProof {
rangeProof: Uint8Array;
validityProof: Uint8Array;
equalityProof: Uint8Array;
}
/**
* SDK error with context
*/
interface SDKError {
code: ErrorCode;
message: string;
context?: Record<string, unknown>;
solution?: string;
instruction?: string;
}
/**
* Error codes
*/
declare enum ErrorCode {
NETWORK_ERROR = "NETWORK_ERROR",
RPC_ERROR = "RPC_ERROR",
TIMEOUT = "TIMEOUT",
INSUFFICIENT_BALANCE = "INSUFFICIENT_BALANCE",
TRANSACTION_FAILED = "TRANSACTION_FAILED",
SIMULATION_FAILED = "SIMULATION_FAILED",
ACCOUNT_NOT_FOUND = "ACCOUNT_NOT_FOUND",
INVALID_ACCOUNT = "INVALID_ACCOUNT",
UNAUTHORIZED = "UNAUTHORIZED",
INVALID_INPUT = "INVALID_INPUT",
INVALID_ADDRESS = "INVALID_ADDRESS",
INVALID_AMOUNT = "INVALID_AMOUNT",
PROGRAM_ERROR = "PROGRAM_ERROR",
INSTRUCTION_ERROR = "INSTRUCTION_ERROR",
UNKNOWN_ERROR = "UNKNOWN_ERROR"
}
/**
* Agent creation parameters
*/
interface AgentCreationParams {
name: string;
capabilities: string[];
type?: AgentType;
metadata?: Partial<AgentMetadata>;
compressed?: boolean;
}
/**
* Escrow creation parameters
*/
interface EscrowCreationParams {
buyer: Address;
seller: Address;
amount: bigint;
description: string;
milestones?: Milestone[];
}
/**
* Channel creation parameters
*/
interface ChannelCreationParams {
name: string;
description: string;
type?: ChannelType;
isPrivate?: boolean;
maxMembers?: number;
}
/**
* Pagination options
*/
interface PaginationOptions {
limit?: number;
offset?: number;
cursor?: string;
}
/**
* Filter options
*/
interface FilterOptions<T> {
where?: Partial<T>;
orderBy?: keyof T;
orderDirection?: 'asc' | 'desc';
}
/**
* Query result with pagination
*/
interface QueryResult<T> {
items: T[];
total: number;
hasMore: boolean;
nextCursor?: string;
}
/**
* Base event interface
*/
interface Event<T = unknown> {
type: string;
timestamp: Date;
data: T;
}
/**
* Agent events
*/
type AgentEvent = Event<{
agent: Agent;
}> & {
type: 'agent.created';
} | Event<{
agent: Agent;
changes: Partial<Agent>;
}> & {
type: 'agent.updated';
} | Event<{
agent: Agent;
}> & {
type: 'agent.verified';
};
/**
* Escrow events
*/
type EscrowEvent = Event<{
escrow: Escrow;
}> & {
type: 'escrow.created';
} | Event<{
escrow: Escrow;
}> & {
type: 'escrow.completed';
} | Event<{
escrow: Escrow;
reason: string;
}> & {
type: 'escrow.disputed';
};
/**
* Channel events
*/
type ChannelEvent = Event<{
channel: Channel;
message: Message;
}> & {
type: 'channel.message';
} | Event<{
channel: Channel;
member: Address;
}> & {
type: 'channel.member.joined';
} | Event<{
channel: Channel;
member: Address;
}> & {
type: 'channel.member.left';
};
/**
* Participant type enum for distinguishing between humans and agents
*/
declare enum ParticipantType {
Human = "Human",
Agent = "Agent"
}
/**
* Unified communication session supporting H2A, A2A, and future H2H
*/
interface CommunicationSession {
/** Session identifier */
sessionId: bigint;
/** Address of session initiator */
initiator: Address;
/** Type of initiator (human or agent) */
initiatorType: ParticipantType;
/** Address of session responder */
responder: Address;
/** Type of responder (human or agent) */
responderType: ParticipantType;
/** Type of communication session */
sessionType: string;
/** Additional session metadata */
metadata: string;
/** Whether session is currently active */
isActive: boolean;
/** Session creation timestamp */
createdAt: bigint;
/** Session expiration timestamp */
expiresAt: bigint;
}
/**
* Communication message in unified sessions
*/
interface CommunicationMessage {
/** Message identifier */
messageId: bigint;
/** Session this message belongs to */
session: Address;
/** Address of message sender */
sender: Address;
/** Type of sender (human or agent) */
senderType: ParticipantType;
/** Message content */
content: string;
/** Type of message */
messageType: string;
/** File attachments (IPFS hashes) */
attachments: string[];
/** Message timestamp */
sentAt: bigint;
}
/**
* Participant status for service discovery
*/
interface ParticipantStatus {
/** Participant address */
participant: Address;
/** Type of participant (human or agent) */
participantType: ParticipantType;
/** Services offered by this participant */
servicesOffered: string[];
/** Current availability status */
availability: boolean;
/** Reputation score (0-100) */
reputationScore: number;
/** Last status update timestamp */
lastUpdated: bigint;
}
/**
* Data for creating communication sessions
*/
interface CreateCommunicationSessionParams {
sessionId: bigint;
initiator: Address;
initiatorType: ParticipantType;
responder: Address;
responderType: ParticipantType;
sessionType: string;
metadata: string;
expiresAt: bigint;
}
/**
* Data for sending communication messages
*/
interface SendCommunicationMessageParams {
messageId: bigint;
senderType: ParticipantType;
content: string;
messageType: string;
attachments?: string[];
}
/**
* Data for updating participant status
*/
interface UpdateParticipantStatusParams {
participant: Address;
participantType: ParticipantType;
servicesOffered: string[];
availability: boolean;
reputationScore: number;
}
/**
* Check if result is successful
*/
declare function isSuccess<T>(result: Result<T>): result is Result<T> & {
success: true;
};
/**
* Check if result is error
*/
declare function isError<T>(result: Result<T>): result is Result<T> & {
success: false;
};
/**
* Extract data from successful result
*/
declare function unwrap<T>(result: Result<T>): T;
/**
* Make all properties optional recursively
*/
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};
/**
* Extract addresses from entities
*/
type AddressOf<T> = T extends {
address: Address;
} ? T['address'] : never;
/**
* Entity with address
*/
type WithAddress<T> = T & {
address: Address;
};
export { type AddressOf, type Agent, type AgentCreationParams, type AgentEvent, type AgentMetadata, AgentType, type Attachment, type Channel, type ChannelCreationParams, type ChannelEvent, ChannelType, type CommunicationMessage, type CommunicationSession, type ConfidentialBalance, type ConfidentialTransfer, type CreateCommunicationSessionParams, type DeepPartial, ErrorCode, type Escrow, type EscrowCreationParams, type EscrowEvent, EscrowStatus, type Event, type FilterOptions, type Message, MessageType, type Milestone, type PaginationOptions, type ParticipantStatus, ParticipantType, type QueryResult, type Rating, type Reaction, type Reputation, type Result, type SDKError, type SendCommunicationMessageParams, type SimulationResult, type Transaction, type TransferProof, type UpdateParticipantStatusParams, type WithAddress, isError, isSuccess, unwrap };