@pingagent/sdk 0.1.7 → 0.1.9
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.
- package/bin/pingagent.js +24 -3
- package/dist/chunk-3OEFISNL.js +2433 -0
- package/dist/chunk-5Z6HZWDA.js +2603 -0
- package/dist/chunk-BSDY6AKB.js +2918 -0
- package/dist/chunk-PFABO4C7.js +2961 -0
- package/dist/chunk-QK2GMSWC.js +2959 -0
- package/dist/chunk-TCYDOFRQ.js +2085 -0
- package/dist/chunk-V7HHUQT6.js +1962 -0
- package/dist/index.d.ts +403 -5
- package/dist/index.js +41 -3
- package/dist/web-server.js +1151 -16
- package/package.json +11 -3
- package/__tests__/cli.test.ts +0 -225
- package/__tests__/identity.test.ts +0 -47
- package/__tests__/store.test.ts +0 -332
- package/src/a2a-adapter.ts +0 -159
- package/src/auth.ts +0 -50
- package/src/client.ts +0 -582
- package/src/contacts.ts +0 -210
- package/src/history.ts +0 -269
- package/src/identity.ts +0 -86
- package/src/index.ts +0 -25
- package/src/paths.ts +0 -52
- package/src/store.ts +0 -62
- package/src/transport.ts +0 -141
- package/src/web-server.ts +0 -1148
- package/src/ws-subscription.ts +0 -428
- package/tsconfig.json +0 -8
package/src/transport.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import type { ApiResponse, ErrorCodeValue } from '@pingagent/schemas';
|
|
2
|
-
import { ErrorCode } from '@pingagent/schemas';
|
|
3
|
-
|
|
4
|
-
export interface TransportOptions {
|
|
5
|
-
serverUrl: string;
|
|
6
|
-
accessToken: string;
|
|
7
|
-
maxRetries?: number;
|
|
8
|
-
onTokenRefreshed?: (newToken: string, expiresAt: number) => void;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class HttpTransport {
|
|
12
|
-
private serverUrl: string;
|
|
13
|
-
private accessToken: string;
|
|
14
|
-
private maxRetries: number;
|
|
15
|
-
private onTokenRefreshed?: (newToken: string, expiresAt: number) => void;
|
|
16
|
-
|
|
17
|
-
constructor(opts: TransportOptions) {
|
|
18
|
-
this.serverUrl = opts.serverUrl.replace(/\/$/, '');
|
|
19
|
-
this.accessToken = opts.accessToken;
|
|
20
|
-
this.maxRetries = opts.maxRetries ?? 3;
|
|
21
|
-
this.onTokenRefreshed = opts.onTokenRefreshed;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
setToken(token: string) {
|
|
25
|
-
this.accessToken = token;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Fetch a URL with Bearer auth (e.g. artifact upload/download). */
|
|
29
|
-
async fetchWithAuth(
|
|
30
|
-
url: string,
|
|
31
|
-
options: { method?: string; body?: ArrayBuffer | Buffer; contentType?: string } = {},
|
|
32
|
-
): Promise<ArrayBuffer> {
|
|
33
|
-
const headers: Record<string, string> = { Authorization: `Bearer ${this.accessToken}` };
|
|
34
|
-
if (options.contentType) headers['Content-Type'] = options.contentType;
|
|
35
|
-
const res = await fetch(url, {
|
|
36
|
-
method: options.method ?? 'GET',
|
|
37
|
-
headers,
|
|
38
|
-
body: options.body ?? undefined,
|
|
39
|
-
});
|
|
40
|
-
if (!res.ok) {
|
|
41
|
-
const text = await res.text();
|
|
42
|
-
throw new Error(`Request failed (${res.status}): ${text.slice(0, 200)}`);
|
|
43
|
-
}
|
|
44
|
-
return res.arrayBuffer();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async request<T = any>(
|
|
48
|
-
method: string,
|
|
49
|
-
path: string,
|
|
50
|
-
body?: any,
|
|
51
|
-
skipAuth = false,
|
|
52
|
-
): Promise<ApiResponse<T>> {
|
|
53
|
-
const url = `${this.serverUrl}${path}`;
|
|
54
|
-
const headers: Record<string, string> = {
|
|
55
|
-
'Content-Type': 'application/json',
|
|
56
|
-
};
|
|
57
|
-
if (!skipAuth) {
|
|
58
|
-
headers['Authorization'] = `Bearer ${this.accessToken}`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
let lastError: Error | null = null;
|
|
62
|
-
const delays = [1000, 2000, 4000];
|
|
63
|
-
|
|
64
|
-
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
65
|
-
try {
|
|
66
|
-
const res = await fetch(url, {
|
|
67
|
-
method,
|
|
68
|
-
headers,
|
|
69
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const text = await res.text();
|
|
73
|
-
let data: ApiResponse<T>;
|
|
74
|
-
try {
|
|
75
|
-
data = (text ? JSON.parse(text) : {}) as ApiResponse<T>;
|
|
76
|
-
} catch {
|
|
77
|
-
throw new Error(`Server returned non-JSON (${res.status}): ${text.slice(0, 200)}`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (res.status === 401 && data.error?.code === ErrorCode.TOKEN_EXPIRED && attempt === 0) {
|
|
81
|
-
const refreshed = await this.refreshToken();
|
|
82
|
-
if (refreshed) {
|
|
83
|
-
headers['Authorization'] = `Bearer ${this.accessToken}`;
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (res.status === 429 && data.error?.retry_after_ms) {
|
|
89
|
-
await sleep(data.error.retry_after_ms);
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (res.status >= 500 && attempt < this.maxRetries) {
|
|
94
|
-
await sleep(delays[attempt] ?? 4000);
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return data;
|
|
99
|
-
} catch (e: any) {
|
|
100
|
-
lastError = e;
|
|
101
|
-
if (attempt < this.maxRetries) {
|
|
102
|
-
await sleep(delays[attempt] ?? 4000);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
throw lastError ?? new Error('Request failed after retries');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private async refreshToken(): Promise<boolean> {
|
|
111
|
-
try {
|
|
112
|
-
const res = await fetch(`${this.serverUrl}/v1/auth/refresh`, {
|
|
113
|
-
method: 'POST',
|
|
114
|
-
headers: { 'Content-Type': 'application/json' },
|
|
115
|
-
body: JSON.stringify({ access_token: this.accessToken }),
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
if (!res.ok) return false;
|
|
119
|
-
|
|
120
|
-
const text = await res.text();
|
|
121
|
-
let data: ApiResponse<{ access_token: string; expires_ms: number }>;
|
|
122
|
-
try {
|
|
123
|
-
data = (text ? JSON.parse(text) : {}) as ApiResponse<{ access_token: string; expires_ms: number }>;
|
|
124
|
-
} catch {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
if (!data.ok || !data.data) return false;
|
|
128
|
-
|
|
129
|
-
this.accessToken = data.data.access_token;
|
|
130
|
-
const expiresAt = Date.now() + data.data.expires_ms;
|
|
131
|
-
this.onTokenRefreshed?.(data.data.access_token, expiresAt);
|
|
132
|
-
return true;
|
|
133
|
-
} catch {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function sleep(ms: number): Promise<void> {
|
|
140
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
141
|
-
}
|