@ghostspeak/sdk
Version:
TypeScript SDK for GhostSpeak AI Agent Commerce Protocol - Production Ready Beta
397 lines (393 loc) • 7.98 kB
TypeScript
import { Address } from '@solana/addresses';
/**
* 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.
*/
type Signature = string;
/**
* 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';
};
/**
* 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 ConfidentialBalance, type ConfidentialTransfer, type DeepPartial, ErrorCode, type Escrow, type EscrowCreationParams, type EscrowEvent, EscrowStatus, type Event, type FilterOptions, type Message, MessageType, type Milestone, type PaginationOptions, type QueryResult, type Rating, type Reaction, type Reputation, type Result, type SDKError, type SimulationResult, type Transaction, type TransferProof, type WithAddress, isError, isSuccess, unwrap };