@emilia-protocol/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ /**
2
+ * @emilia-protocol/sdk
3
+ *
4
+ * TypeScript SDK for EMILIA Protocol (EP).
5
+ * The trust layer for agentic commerce.
6
+ *
7
+ * Usage:
8
+ * import { EmiliaClient } from '@emilia-protocol/sdk';
9
+ * const ep = new EmiliaClient({ apiKey: 'ep_live_...' });
10
+ * const score = await ep.getScore('rex-booking-v1');
11
+ *
12
+ * @license Apache-2.0
13
+ */
14
+ export interface EmiliaConfig {
15
+ /** Base URL of the EP implementation. Default: https://emiliaprotocol.ai */
16
+ baseUrl?: string;
17
+ /** API key for write operations (ep_live_...) */
18
+ apiKey?: string;
19
+ /** Request timeout in ms. Default: 10000 */
20
+ timeout?: number;
21
+ }
22
+ export interface ScoreBreakdown {
23
+ delivery_accuracy: number | null;
24
+ product_accuracy: number | null;
25
+ price_integrity: number | null;
26
+ return_processing: number | null;
27
+ agent_satisfaction: number | null;
28
+ consistency: number | null;
29
+ }
30
+ export interface ScoreResult {
31
+ entity_id: string;
32
+ display_name: string;
33
+ entity_type: 'agent' | 'merchant' | 'service_provider';
34
+ description?: string;
35
+ category?: string;
36
+ capabilities?: string[];
37
+ emilia_score: number;
38
+ established: boolean;
39
+ total_receipts: number;
40
+ successful_receipts: number;
41
+ success_rate: number | null;
42
+ breakdown: ScoreBreakdown | null;
43
+ verified: boolean;
44
+ a2a_endpoint?: string;
45
+ ucp_profile_url?: string;
46
+ member_since: string;
47
+ }
48
+ export type TransactionType = 'purchase' | 'service' | 'task_completion' | 'delivery' | 'return';
49
+ export interface SubmitReceiptInput {
50
+ entity_id: string;
51
+ transaction_type: TransactionType;
52
+ transaction_ref?: string;
53
+ delivery_accuracy?: number;
54
+ product_accuracy?: number;
55
+ price_integrity?: number;
56
+ return_processing?: number;
57
+ agent_satisfaction?: number;
58
+ evidence?: Record<string, unknown>;
59
+ }
60
+ export interface ReceiptResult {
61
+ receipt: {
62
+ receipt_id: string;
63
+ entity_id: string;
64
+ composite_score: number;
65
+ receipt_hash: string;
66
+ created_at: string;
67
+ };
68
+ entity_score: {
69
+ emilia_score: number;
70
+ total_receipts: number;
71
+ };
72
+ }
73
+ export interface RegisterEntityInput {
74
+ entity_id: string;
75
+ display_name: string;
76
+ entity_type: 'agent' | 'merchant' | 'service_provider';
77
+ description: string;
78
+ capabilities?: string[];
79
+ website_url?: string;
80
+ category?: string;
81
+ service_area?: string;
82
+ a2a_endpoint?: string;
83
+ ucp_profile_url?: string;
84
+ }
85
+ export interface RegisterResult {
86
+ entity: {
87
+ id: string;
88
+ entity_id: string;
89
+ display_name: string;
90
+ entity_number?: number;
91
+ emilia_score: number;
92
+ };
93
+ api_key: string;
94
+ }
95
+ export interface VerifyResult {
96
+ receipt_id: string;
97
+ receipt_hash: string;
98
+ anchored: boolean;
99
+ batch?: {
100
+ id: string;
101
+ merkle_root: string;
102
+ leaf_count: number;
103
+ tx_hash: string | null;
104
+ block_number: number | null;
105
+ status: string;
106
+ created_at: string;
107
+ };
108
+ proof?: Array<{
109
+ hash: string;
110
+ position: 'left' | 'right';
111
+ }>;
112
+ verified: boolean;
113
+ how_to_verify?: Record<string, string | null>;
114
+ }
115
+ export interface SearchResult {
116
+ entities: Array<{
117
+ entity_id: string;
118
+ display_name: string;
119
+ entity_type: string;
120
+ description: string;
121
+ emilia_score: number;
122
+ total_receipts: number;
123
+ verified: boolean;
124
+ }>;
125
+ }
126
+ export interface LeaderboardResult {
127
+ entities: Array<{
128
+ entity_id: string;
129
+ display_name: string;
130
+ entity_type: string;
131
+ emilia_score: number;
132
+ total_receipts: number;
133
+ }>;
134
+ }
135
+ export declare class EmiliaError extends Error {
136
+ status: number;
137
+ constructor(message: string, status: number);
138
+ }
139
+ export declare class EmiliaClient {
140
+ private baseUrl;
141
+ private apiKey;
142
+ private timeout;
143
+ constructor(config?: EmiliaConfig);
144
+ private request;
145
+ /**
146
+ * Look up an entity's EMILIA Score.
147
+ * No authentication required — scores are public by design.
148
+ */
149
+ getScore(entityId: string): Promise<ScoreResult>;
150
+ /**
151
+ * Submit a transaction receipt.
152
+ * Requires API key.
153
+ */
154
+ submitReceipt(input: SubmitReceiptInput): Promise<ReceiptResult>;
155
+ /**
156
+ * Register a new entity.
157
+ * Requires API key.
158
+ */
159
+ registerEntity(input: RegisterEntityInput): Promise<RegisterResult>;
160
+ /**
161
+ * Verify a receipt against the on-chain Merkle root.
162
+ * No authentication required.
163
+ */
164
+ verifyReceipt(receiptId: string): Promise<VerifyResult>;
165
+ /**
166
+ * Search for entities.
167
+ * No authentication required.
168
+ */
169
+ searchEntities(query: string, options?: {
170
+ entityType?: 'agent' | 'merchant' | 'service_provider';
171
+ minScore?: number;
172
+ }): Promise<SearchResult>;
173
+ /**
174
+ * Get the leaderboard.
175
+ * No authentication required.
176
+ */
177
+ getLeaderboard(options?: {
178
+ limit?: number;
179
+ entityType?: 'agent' | 'merchant' | 'service_provider';
180
+ }): Promise<LeaderboardResult>;
181
+ /**
182
+ * Check if an entity meets a minimum trust threshold.
183
+ * Returns true if the entity's score >= minScore.
184
+ */
185
+ isTrusted(entityId: string, minScore?: number): Promise<boolean>;
186
+ /**
187
+ * Submit a receipt and verify the entity meets a threshold in one call.
188
+ * Returns the receipt result and whether the entity is still trusted.
189
+ */
190
+ submitAndCheck(input: SubmitReceiptInput, minScore?: number): Promise<ReceiptResult & {
191
+ still_trusted: boolean;
192
+ }>;
193
+ }
194
+ export default EmiliaClient;
package/dist/index.js ADDED
@@ -0,0 +1,159 @@
1
+ /**
2
+ * @emilia-protocol/sdk
3
+ *
4
+ * TypeScript SDK for EMILIA Protocol (EP).
5
+ * The trust layer for agentic commerce.
6
+ *
7
+ * Usage:
8
+ * import { EmiliaClient } from '@emilia-protocol/sdk';
9
+ * const ep = new EmiliaClient({ apiKey: 'ep_live_...' });
10
+ * const score = await ep.getScore('rex-booking-v1');
11
+ *
12
+ * @license Apache-2.0
13
+ */
14
+ export class EmiliaError extends Error {
15
+ status;
16
+ constructor(message, status) {
17
+ super(message);
18
+ this.name = 'EmiliaError';
19
+ this.status = status;
20
+ }
21
+ }
22
+ // =============================================================================
23
+ // Client
24
+ // =============================================================================
25
+ export class EmiliaClient {
26
+ baseUrl;
27
+ apiKey;
28
+ timeout;
29
+ constructor(config = {}) {
30
+ this.baseUrl = (config.baseUrl || 'https://emiliaprotocol.ai').replace(/\/$/, '');
31
+ this.apiKey = config.apiKey || '';
32
+ this.timeout = config.timeout || 10000;
33
+ }
34
+ // ---------------------------------------------------------------------------
35
+ // Internal fetch
36
+ // ---------------------------------------------------------------------------
37
+ async request(path, options = {}) {
38
+ const url = `${this.baseUrl}${path}`;
39
+ const headers = {
40
+ 'Content-Type': 'application/json',
41
+ };
42
+ if (options.auth) {
43
+ if (!this.apiKey) {
44
+ throw new EmiliaError('API key required for this operation. Pass apiKey in EmiliaClient config.', 401);
45
+ }
46
+ headers['Authorization'] = `Bearer ${this.apiKey}`;
47
+ }
48
+ const controller = new AbortController();
49
+ const timer = setTimeout(() => controller.abort(), this.timeout);
50
+ try {
51
+ const res = await fetch(url, {
52
+ method: options.method || 'GET',
53
+ headers,
54
+ body: options.body ? JSON.stringify(options.body) : undefined,
55
+ signal: controller.signal,
56
+ });
57
+ const data = await res.json();
58
+ if (!res.ok) {
59
+ throw new EmiliaError(data.error || `EP API error: ${res.status}`, res.status);
60
+ }
61
+ return data;
62
+ }
63
+ finally {
64
+ clearTimeout(timer);
65
+ }
66
+ }
67
+ // ---------------------------------------------------------------------------
68
+ // Public API
69
+ // ---------------------------------------------------------------------------
70
+ /**
71
+ * Look up an entity's EMILIA Score.
72
+ * No authentication required — scores are public by design.
73
+ */
74
+ async getScore(entityId) {
75
+ return this.request(`/api/score/${encodeURIComponent(entityId)}`);
76
+ }
77
+ /**
78
+ * Submit a transaction receipt.
79
+ * Requires API key.
80
+ */
81
+ async submitReceipt(input) {
82
+ return this.request('/api/receipts/submit', {
83
+ method: 'POST',
84
+ auth: true,
85
+ body: input,
86
+ });
87
+ }
88
+ /**
89
+ * Register a new entity.
90
+ * Requires API key.
91
+ */
92
+ async registerEntity(input) {
93
+ return this.request('/api/entities/register', {
94
+ method: 'POST',
95
+ auth: true,
96
+ body: input,
97
+ });
98
+ }
99
+ /**
100
+ * Verify a receipt against the on-chain Merkle root.
101
+ * No authentication required.
102
+ */
103
+ async verifyReceipt(receiptId) {
104
+ return this.request(`/api/verify/${encodeURIComponent(receiptId)}`);
105
+ }
106
+ /**
107
+ * Search for entities.
108
+ * No authentication required.
109
+ */
110
+ async searchEntities(query, options) {
111
+ const params = new URLSearchParams({ q: query });
112
+ if (options?.entityType)
113
+ params.set('type', options.entityType);
114
+ if (options?.minScore)
115
+ params.set('min_score', options.minScore.toString());
116
+ return this.request(`/api/entities/search?${params}`);
117
+ }
118
+ /**
119
+ * Get the leaderboard.
120
+ * No authentication required.
121
+ */
122
+ async getLeaderboard(options) {
123
+ const params = new URLSearchParams();
124
+ if (options?.limit)
125
+ params.set('limit', Math.min(options.limit, 50).toString());
126
+ if (options?.entityType)
127
+ params.set('type', options.entityType);
128
+ return this.request(`/api/leaderboard?${params}`);
129
+ }
130
+ // ---------------------------------------------------------------------------
131
+ // Convenience methods
132
+ // ---------------------------------------------------------------------------
133
+ /**
134
+ * Check if an entity meets a minimum trust threshold.
135
+ * Returns true if the entity's score >= minScore.
136
+ */
137
+ async isTrusted(entityId, minScore = 70) {
138
+ try {
139
+ const result = await this.getScore(entityId);
140
+ return result.emilia_score >= minScore && result.established;
141
+ }
142
+ catch {
143
+ return false;
144
+ }
145
+ }
146
+ /**
147
+ * Submit a receipt and verify the entity meets a threshold in one call.
148
+ * Returns the receipt result and whether the entity is still trusted.
149
+ */
150
+ async submitAndCheck(input, minScore = 70) {
151
+ const result = await this.submitReceipt(input);
152
+ return {
153
+ ...result,
154
+ still_trusted: result.entity_score.emilia_score >= minScore,
155
+ };
156
+ }
157
+ }
158
+ // Default export
159
+ export default EmiliaClient;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@emilia-protocol/sdk",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript SDK for EMILIA Protocol (EP) — the trust layer for agentic commerce.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": ["dist", "README.md"],
16
+ "scripts": {
17
+ "build": "tsc",
18
+ "prepublishOnly": "npm run build"
19
+ },
20
+ "devDependencies": {
21
+ "typescript": "^5.5.0"
22
+ },
23
+ "keywords": [
24
+ "emilia", "trust", "reputation", "ai-agents", "agentic-commerce",
25
+ "mcp", "a2a", "ucp", "receipts"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/emiliaprotocol/emilia-protocol",
30
+ "directory": "sdks/typescript"
31
+ },
32
+ "homepage": "https://emiliaprotocol.ai"
33
+ }