@agent-os-sdk/client 0.3.15 → 0.4.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.
- package/dist/client/config.d.ts +49 -0
- package/dist/client/config.d.ts.map +1 -0
- package/dist/client/config.js +62 -0
- package/dist/client/pagination.d.ts +105 -0
- package/dist/client/pagination.d.ts.map +1 -0
- package/dist/client/pagination.js +117 -0
- package/dist/client/raw.d.ts +65 -0
- package/dist/client/raw.d.ts.map +1 -1
- package/dist/client/raw.js +78 -17
- package/dist/client/retry.d.ts +37 -0
- package/dist/client/retry.d.ts.map +1 -0
- package/dist/client/retry.js +108 -0
- package/dist/client/timeout.d.ts +26 -0
- package/dist/client/timeout.d.ts.map +1 -0
- package/dist/client/timeout.js +51 -0
- package/dist/errors/factory.d.ts +20 -0
- package/dist/errors/factory.d.ts.map +1 -0
- package/dist/errors/factory.js +97 -0
- package/dist/errors/index.d.ts +210 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +283 -0
- package/dist/index.d.ts +11 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -0
- package/dist/modules/audit.d.ts +27 -4
- package/dist/modules/audit.d.ts.map +1 -1
- package/dist/modules/audit.js +58 -2
- package/dist/modules/checkpoints.d.ts +1 -1
- package/dist/modules/checkpoints.d.ts.map +1 -1
- package/dist/modules/info.d.ts +49 -0
- package/dist/modules/info.d.ts.map +1 -1
- package/dist/modules/info.js +66 -0
- package/dist/modules/runs.d.ts +103 -0
- package/dist/modules/runs.d.ts.map +1 -1
- package/dist/modules/runs.js +258 -0
- package/dist/modules/tenants.d.ts +4 -1
- package/dist/modules/tenants.d.ts.map +1 -1
- package/dist/modules/tenants.js +3 -0
- package/dist/modules/threads.d.ts +24 -0
- package/dist/modules/threads.d.ts.map +1 -1
- package/dist/modules/threads.js +48 -1
- package/dist/sse/client.d.ts.map +1 -1
- package/dist/sse/client.js +17 -5
- package/package.json +49 -50
- package/src/client/config.ts +100 -0
- package/src/client/pagination.ts +218 -0
- package/src/client/raw.ts +138 -17
- package/src/client/retry.ts +150 -0
- package/src/client/timeout.ts +59 -0
- package/src/errors/factory.ts +135 -0
- package/src/errors/index.ts +365 -0
- package/src/index.ts +72 -2
- package/src/modules/audit.ts +77 -6
- package/src/modules/checkpoints.ts +1 -1
- package/src/modules/info.ts +108 -0
- package/src/modules/runs.ts +333 -0
- package/src/modules/tenants.ts +5 -2
- package/src/modules/threads.ts +57 -1
- package/src/sse/client.ts +21 -5
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent OS SDK - Network Configuration
|
|
3
|
+
*
|
|
4
|
+
* Centralized configuration for timeouts, retries, and network policies.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Retry configuration for network requests.
|
|
8
|
+
*/
|
|
9
|
+
export interface RetryConfig {
|
|
10
|
+
/** Maximum retry attempts (default: 3) */
|
|
11
|
+
maxRetries: number;
|
|
12
|
+
/** Base delay between retries in ms (default: 1000) */
|
|
13
|
+
baseDelayMs: number;
|
|
14
|
+
/** Maximum delay between retries in ms (default: 30000) */
|
|
15
|
+
maxDelayMs: number;
|
|
16
|
+
/** Jitter factor to prevent thundering herd (0-1, default: 0.2) */
|
|
17
|
+
jitterFactor: number;
|
|
18
|
+
/** HTTP status codes that trigger retry (default: [408, 429, 500, 502, 503, 504]) */
|
|
19
|
+
retryableStatuses: number[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Full network configuration for the SDK client.
|
|
23
|
+
*/
|
|
24
|
+
export interface NetworkConfig {
|
|
25
|
+
/** Timeout per request attempt in milliseconds (default: 30000) */
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
/** Retry configuration */
|
|
28
|
+
retry: RetryConfig;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Default network configuration.
|
|
32
|
+
* Balanced for most use cases - 30s timeout, 3 retries with exponential backoff.
|
|
33
|
+
*/
|
|
34
|
+
export declare const DEFAULT_NETWORK_CONFIG: NetworkConfig;
|
|
35
|
+
/**
|
|
36
|
+
* Aggressive configuration for interactive UIs.
|
|
37
|
+
* Shorter timeouts, fewer retries.
|
|
38
|
+
*/
|
|
39
|
+
export declare const INTERACTIVE_NETWORK_CONFIG: NetworkConfig;
|
|
40
|
+
/**
|
|
41
|
+
* Patient configuration for background jobs.
|
|
42
|
+
* Longer timeouts, more retries.
|
|
43
|
+
*/
|
|
44
|
+
export declare const BACKGROUND_NETWORK_CONFIG: NetworkConfig;
|
|
45
|
+
/**
|
|
46
|
+
* Merges user config with defaults.
|
|
47
|
+
*/
|
|
48
|
+
export declare function mergeNetworkConfig(userConfig?: Partial<NetworkConfig>): NetworkConfig;
|
|
49
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/client/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,0CAA0C;IAC1C,UAAU,EAAE,MAAM,CAAC;IAEnB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;IAEpB,2DAA2D;IAC3D,UAAU,EAAE,MAAM,CAAC;IAEnB,mEAAmE;IACnE,YAAY,EAAE,MAAM,CAAC;IAErB,qFAAqF;IACrF,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC1B,mEAAmE;IACnE,SAAS,EAAE,MAAM,CAAC;IAElB,0BAA0B;IAC1B,KAAK,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,aASpC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,0BAA0B,EAAE,aASxC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,EAAE,aASvC,CAAC;AAEF;;GAEG;AACH,wBAAgB,kBAAkB,CAC9B,UAAU,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GACpC,aAAa,CAYf"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent OS SDK - Network Configuration
|
|
3
|
+
*
|
|
4
|
+
* Centralized configuration for timeouts, retries, and network policies.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Default network configuration.
|
|
8
|
+
* Balanced for most use cases - 30s timeout, 3 retries with exponential backoff.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_NETWORK_CONFIG = {
|
|
11
|
+
timeoutMs: 30_000,
|
|
12
|
+
retry: {
|
|
13
|
+
maxRetries: 3,
|
|
14
|
+
baseDelayMs: 1_000,
|
|
15
|
+
maxDelayMs: 30_000,
|
|
16
|
+
jitterFactor: 0.2,
|
|
17
|
+
retryableStatuses: [408, 429, 500, 502, 503, 504],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Aggressive configuration for interactive UIs.
|
|
22
|
+
* Shorter timeouts, fewer retries.
|
|
23
|
+
*/
|
|
24
|
+
export const INTERACTIVE_NETWORK_CONFIG = {
|
|
25
|
+
timeoutMs: 10_000,
|
|
26
|
+
retry: {
|
|
27
|
+
maxRetries: 1,
|
|
28
|
+
baseDelayMs: 500,
|
|
29
|
+
maxDelayMs: 5_000,
|
|
30
|
+
jitterFactor: 0.1,
|
|
31
|
+
retryableStatuses: [429, 503, 504],
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Patient configuration for background jobs.
|
|
36
|
+
* Longer timeouts, more retries.
|
|
37
|
+
*/
|
|
38
|
+
export const BACKGROUND_NETWORK_CONFIG = {
|
|
39
|
+
timeoutMs: 120_000,
|
|
40
|
+
retry: {
|
|
41
|
+
maxRetries: 5,
|
|
42
|
+
baseDelayMs: 2_000,
|
|
43
|
+
maxDelayMs: 60_000,
|
|
44
|
+
jitterFactor: 0.3,
|
|
45
|
+
retryableStatuses: [408, 429, 500, 502, 503, 504],
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Merges user config with defaults.
|
|
50
|
+
*/
|
|
51
|
+
export function mergeNetworkConfig(userConfig) {
|
|
52
|
+
if (!userConfig) {
|
|
53
|
+
return DEFAULT_NETWORK_CONFIG;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
timeoutMs: userConfig.timeoutMs ?? DEFAULT_NETWORK_CONFIG.timeoutMs,
|
|
57
|
+
retry: {
|
|
58
|
+
...DEFAULT_NETWORK_CONFIG.retry,
|
|
59
|
+
...userConfig.retry,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent OS SDK - Pagination Utilities
|
|
3
|
+
*
|
|
4
|
+
* Auto-paginating iterators that support both offset and cursor pagination.
|
|
5
|
+
* Designed to work with any list endpoint.
|
|
6
|
+
*/
|
|
7
|
+
import type { APIResponse } from "./raw.js";
|
|
8
|
+
/**
|
|
9
|
+
* Offset-based paginated response.
|
|
10
|
+
* Most common format for list endpoints.
|
|
11
|
+
*/
|
|
12
|
+
export interface OffsetPaginatedResponse<T> {
|
|
13
|
+
items: T[];
|
|
14
|
+
total: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Cursor-based paginated response.
|
|
18
|
+
* Better for large datasets and real-time consistency.
|
|
19
|
+
*/
|
|
20
|
+
export interface CursorPaginatedResponse<T> {
|
|
21
|
+
items: T[];
|
|
22
|
+
next_cursor?: string;
|
|
23
|
+
has_more: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Union type for both pagination styles.
|
|
27
|
+
*/
|
|
28
|
+
export type PaginatedResponse<T> = OffsetPaginatedResponse<T> | CursorPaginatedResponse<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Offset pagination parameters.
|
|
31
|
+
*/
|
|
32
|
+
export interface OffsetParams {
|
|
33
|
+
limit?: number;
|
|
34
|
+
offset?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Cursor pagination parameters.
|
|
38
|
+
*/
|
|
39
|
+
export interface CursorParams {
|
|
40
|
+
limit?: number;
|
|
41
|
+
after?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Combined pagination parameters.
|
|
45
|
+
*/
|
|
46
|
+
export type PaginationParams = OffsetParams | CursorParams;
|
|
47
|
+
/**
|
|
48
|
+
* Options for pagination behavior.
|
|
49
|
+
*/
|
|
50
|
+
export interface PaginateOptions {
|
|
51
|
+
/** Number of items per page (default: 100) */
|
|
52
|
+
pageSize?: number;
|
|
53
|
+
/** Maximum total items to fetch (default: unlimited) */
|
|
54
|
+
maxItems?: number;
|
|
55
|
+
/** AbortSignal for cancellation */
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates an async iterator that automatically paginates through results.
|
|
60
|
+
* Supports both offset and cursor pagination transparently.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Basic usage
|
|
64
|
+
* for await (const run of paginate(
|
|
65
|
+
* (p) => client.runs.list(p),
|
|
66
|
+
* { status: "completed" }
|
|
67
|
+
* )) {
|
|
68
|
+
* console.log(run.id);
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // With options
|
|
73
|
+
* for await (const agent of paginate(
|
|
74
|
+
* (p) => client.agents.list(p),
|
|
75
|
+
* { workspace_id: "ws_123" },
|
|
76
|
+
* { pageSize: 50, maxItems: 200 }
|
|
77
|
+
* )) {
|
|
78
|
+
* console.log(agent.name);
|
|
79
|
+
* }
|
|
80
|
+
*/
|
|
81
|
+
export declare function paginate<T, P extends PaginationParams>(fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>, baseParams: Omit<P, "limit" | "offset" | "after">, options?: PaginateOptions): AsyncGenerator<T, void, unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* Collects all items from a paginated endpoint into an array.
|
|
84
|
+
* Useful when you need all items at once.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const allRuns = await collectAll(
|
|
88
|
+
* (p) => client.runs.list(p),
|
|
89
|
+
* { status: "completed" },
|
|
90
|
+
* { maxItems: 1000 }
|
|
91
|
+
* );
|
|
92
|
+
*/
|
|
93
|
+
export declare function collectAll<T, P extends PaginationParams>(fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>, baseParams: Omit<P, "limit" | "offset" | "after">, options?: PaginateOptions): Promise<T[]>;
|
|
94
|
+
/**
|
|
95
|
+
* Gets the first item from a paginated endpoint.
|
|
96
|
+
* More efficient than collecting all items when you only need one.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* const latestRun = await getFirst(
|
|
100
|
+
* (p) => client.runs.list({ ...p, order: "desc" }),
|
|
101
|
+
* { agent_id: "agent_123" }
|
|
102
|
+
* );
|
|
103
|
+
*/
|
|
104
|
+
export declare function getFirst<T, P extends PaginationParams>(fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>, baseParams: Omit<P, "limit" | "offset" | "after">): Promise<T | undefined>;
|
|
105
|
+
//# sourceMappingURL=pagination.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../../src/client/pagination.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAM5C;;;GAGG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC;IACtC,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC;IACtC,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,WAAW,CAA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C,CAAC,CAAC,EACpE,UAAU,EAAE,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC,EACjD,OAAO,CAAC,EAAE,eAAe,GAC1B,OAAO,CAAC,CAAC,EAAE,CAAC,CAQd;AAED;;;;;;;;;GASG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,gBAAgB,EACxD,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EACpE,UAAU,EAAE,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC,GAClD,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAKxB"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent OS SDK - Pagination Utilities
|
|
3
|
+
*
|
|
4
|
+
* Auto-paginating iterators that support both offset and cursor pagination.
|
|
5
|
+
* Designed to work with any list endpoint.
|
|
6
|
+
*/
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Type Guards
|
|
9
|
+
// ============================================================================
|
|
10
|
+
/**
|
|
11
|
+
* Checks if response uses cursor pagination.
|
|
12
|
+
*/
|
|
13
|
+
function isCursorResponse(response) {
|
|
14
|
+
return "has_more" in response;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an async iterator that automatically paginates through results.
|
|
18
|
+
* Supports both offset and cursor pagination transparently.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Basic usage
|
|
22
|
+
* for await (const run of paginate(
|
|
23
|
+
* (p) => client.runs.list(p),
|
|
24
|
+
* { status: "completed" }
|
|
25
|
+
* )) {
|
|
26
|
+
* console.log(run.id);
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // With options
|
|
31
|
+
* for await (const agent of paginate(
|
|
32
|
+
* (p) => client.agents.list(p),
|
|
33
|
+
* { workspace_id: "ws_123" },
|
|
34
|
+
* { pageSize: 50, maxItems: 200 }
|
|
35
|
+
* )) {
|
|
36
|
+
* console.log(agent.name);
|
|
37
|
+
* }
|
|
38
|
+
*/
|
|
39
|
+
export async function* paginate(fetchPage, baseParams, options) {
|
|
40
|
+
const pageSize = options?.pageSize ?? 100;
|
|
41
|
+
const maxItems = options?.maxItems ?? Infinity;
|
|
42
|
+
const signal = options?.signal;
|
|
43
|
+
let offset = 0;
|
|
44
|
+
let cursor;
|
|
45
|
+
let hasMore = true;
|
|
46
|
+
let yielded = 0;
|
|
47
|
+
while (hasMore && yielded < maxItems) {
|
|
48
|
+
// Check for cancellation
|
|
49
|
+
if (signal?.aborted) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// Build params based on pagination style
|
|
53
|
+
const params = {
|
|
54
|
+
...baseParams,
|
|
55
|
+
limit: Math.min(pageSize, maxItems - yielded),
|
|
56
|
+
...(cursor !== undefined ? { after: cursor } : { offset }),
|
|
57
|
+
};
|
|
58
|
+
const response = await fetchPage(params);
|
|
59
|
+
if (response.error) {
|
|
60
|
+
throw response.error;
|
|
61
|
+
}
|
|
62
|
+
const data = response.data;
|
|
63
|
+
for (const item of data.items) {
|
|
64
|
+
if (yielded >= maxItems) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
yield item;
|
|
68
|
+
yielded++;
|
|
69
|
+
}
|
|
70
|
+
// Update pagination state based on response type
|
|
71
|
+
if (isCursorResponse(data)) {
|
|
72
|
+
cursor = data.next_cursor;
|
|
73
|
+
hasMore = data.has_more && data.items.length > 0;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
offset += data.items.length;
|
|
77
|
+
hasMore = offset < data.total && data.items.length > 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// Collect Utility
|
|
83
|
+
// ============================================================================
|
|
84
|
+
/**
|
|
85
|
+
* Collects all items from a paginated endpoint into an array.
|
|
86
|
+
* Useful when you need all items at once.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* const allRuns = await collectAll(
|
|
90
|
+
* (p) => client.runs.list(p),
|
|
91
|
+
* { status: "completed" },
|
|
92
|
+
* { maxItems: 1000 }
|
|
93
|
+
* );
|
|
94
|
+
*/
|
|
95
|
+
export async function collectAll(fetchPage, baseParams, options) {
|
|
96
|
+
const items = [];
|
|
97
|
+
for await (const item of paginate(fetchPage, baseParams, options)) {
|
|
98
|
+
items.push(item);
|
|
99
|
+
}
|
|
100
|
+
return items;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Gets the first item from a paginated endpoint.
|
|
104
|
+
* More efficient than collecting all items when you only need one.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const latestRun = await getFirst(
|
|
108
|
+
* (p) => client.runs.list({ ...p, order: "desc" }),
|
|
109
|
+
* { agent_id: "agent_123" }
|
|
110
|
+
* );
|
|
111
|
+
*/
|
|
112
|
+
export async function getFirst(fetchPage, baseParams) {
|
|
113
|
+
for await (const item of paginate(fetchPage, baseParams, { pageSize: 1, maxItems: 1 })) {
|
|
114
|
+
return item;
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
package/dist/client/raw.d.ts
CHANGED
|
@@ -11,7 +11,42 @@ export type ClientOptions = {
|
|
|
11
11
|
baseUrl: string;
|
|
12
12
|
headers?: Record<string, string>;
|
|
13
13
|
headerProvider?: () => Promise<Record<string, string>>;
|
|
14
|
+
/** Hooks for observability (OTEL, Sentry, etc.) */
|
|
15
|
+
hooks?: SDKHooks;
|
|
14
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* SDK hooks for external observability tools.
|
|
19
|
+
* These are called during the request lifecycle for instrumentation.
|
|
20
|
+
*/
|
|
21
|
+
export interface SDKHooks {
|
|
22
|
+
/** Called before each request. Return modified request context or void. */
|
|
23
|
+
onRequest?: (context: HookRequestContext) => void | Promise<void>;
|
|
24
|
+
/** Called after each successful response */
|
|
25
|
+
onResponse?: (context: HookResponseContext) => void | Promise<void>;
|
|
26
|
+
/** Called on any error (network, timeout, HTTP error) */
|
|
27
|
+
onError?: (context: HookErrorContext) => void | Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export interface HookRequestContext {
|
|
30
|
+
method: string;
|
|
31
|
+
url: string;
|
|
32
|
+
headers: Record<string, string>;
|
|
33
|
+
body?: unknown;
|
|
34
|
+
requestId?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface HookResponseContext {
|
|
37
|
+
method: string;
|
|
38
|
+
url: string;
|
|
39
|
+
status: number;
|
|
40
|
+
durationMs: number;
|
|
41
|
+
requestId?: string;
|
|
42
|
+
}
|
|
43
|
+
export interface HookErrorContext {
|
|
44
|
+
method: string;
|
|
45
|
+
url: string;
|
|
46
|
+
error: Error;
|
|
47
|
+
durationMs: number;
|
|
48
|
+
requestId?: string;
|
|
49
|
+
}
|
|
15
50
|
/**
|
|
16
51
|
* Standard API response wrapper.
|
|
17
52
|
* This matches the pattern used by openapi-fetch.
|
|
@@ -24,6 +59,11 @@ export interface APIResponse<T> {
|
|
|
24
59
|
details?: unknown;
|
|
25
60
|
};
|
|
26
61
|
response: Response;
|
|
62
|
+
/** Metadata for observability */
|
|
63
|
+
meta?: {
|
|
64
|
+
/** Request ID from x-request-id header */
|
|
65
|
+
requestId?: string;
|
|
66
|
+
};
|
|
27
67
|
}
|
|
28
68
|
/**
|
|
29
69
|
* Typed API client using openapi-fetch.
|
|
@@ -47,6 +87,11 @@ export declare function createRawClient(options: ClientOptions): {
|
|
|
47
87
|
};
|
|
48
88
|
body?: unknown;
|
|
49
89
|
headers?: Record<string, string>;
|
|
90
|
+
signal?: AbortSignal;
|
|
91
|
+
/** Set to false to disable retry for this request */
|
|
92
|
+
retry?: boolean;
|
|
93
|
+
/** Override timeout for this request */
|
|
94
|
+
timeoutMs?: number;
|
|
50
95
|
}) => Promise<APIResponse<T_1>>)>[2]) => Promise<APIResponse<T>>;
|
|
51
96
|
POST: <T>(path: string, opts?: Parameters<(<T_1>(method: string, path: string, opts?: {
|
|
52
97
|
params?: {
|
|
@@ -55,6 +100,11 @@ export declare function createRawClient(options: ClientOptions): {
|
|
|
55
100
|
};
|
|
56
101
|
body?: unknown;
|
|
57
102
|
headers?: Record<string, string>;
|
|
103
|
+
signal?: AbortSignal;
|
|
104
|
+
/** Set to false to disable retry for this request */
|
|
105
|
+
retry?: boolean;
|
|
106
|
+
/** Override timeout for this request */
|
|
107
|
+
timeoutMs?: number;
|
|
58
108
|
}) => Promise<APIResponse<T_1>>)>[2]) => Promise<APIResponse<T>>;
|
|
59
109
|
PUT: <T>(path: string, opts?: Parameters<(<T_1>(method: string, path: string, opts?: {
|
|
60
110
|
params?: {
|
|
@@ -63,6 +113,11 @@ export declare function createRawClient(options: ClientOptions): {
|
|
|
63
113
|
};
|
|
64
114
|
body?: unknown;
|
|
65
115
|
headers?: Record<string, string>;
|
|
116
|
+
signal?: AbortSignal;
|
|
117
|
+
/** Set to false to disable retry for this request */
|
|
118
|
+
retry?: boolean;
|
|
119
|
+
/** Override timeout for this request */
|
|
120
|
+
timeoutMs?: number;
|
|
66
121
|
}) => Promise<APIResponse<T_1>>)>[2]) => Promise<APIResponse<T>>;
|
|
67
122
|
PATCH: <T>(path: string, opts?: Parameters<(<T_1>(method: string, path: string, opts?: {
|
|
68
123
|
params?: {
|
|
@@ -71,6 +126,11 @@ export declare function createRawClient(options: ClientOptions): {
|
|
|
71
126
|
};
|
|
72
127
|
body?: unknown;
|
|
73
128
|
headers?: Record<string, string>;
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
/** Set to false to disable retry for this request */
|
|
131
|
+
retry?: boolean;
|
|
132
|
+
/** Override timeout for this request */
|
|
133
|
+
timeoutMs?: number;
|
|
74
134
|
}) => Promise<APIResponse<T_1>>)>[2]) => Promise<APIResponse<T>>;
|
|
75
135
|
DELETE: <T>(path: string, opts?: Parameters<(<T_1>(method: string, path: string, opts?: {
|
|
76
136
|
params?: {
|
|
@@ -79,6 +139,11 @@ export declare function createRawClient(options: ClientOptions): {
|
|
|
79
139
|
};
|
|
80
140
|
body?: unknown;
|
|
81
141
|
headers?: Record<string, string>;
|
|
142
|
+
signal?: AbortSignal;
|
|
143
|
+
/** Set to false to disable retry for this request */
|
|
144
|
+
retry?: boolean;
|
|
145
|
+
/** Override timeout for this request */
|
|
146
|
+
timeoutMs?: number;
|
|
82
147
|
}) => Promise<APIResponse<T_1>>)>[2]) => Promise<APIResponse<T>>;
|
|
83
148
|
/**
|
|
84
149
|
* Stream GET request - returns raw Response for SSE consumption.
|
package/dist/client/raw.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/client/raw.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAqB,EAAE,KAAK,MAAM,EAAqB,MAAM,eAAe,CAAC;AAC7E,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGjE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC;AAElC,MAAM,MAAM,aAAa,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/client/raw.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAqB,EAAE,KAAK,MAAM,EAAqB,MAAM,eAAe,CAAC;AAC7E,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGjE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,CAA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55
|
if (opts?.body) {
|
|
56
56
|
headers["Content-Type"] = "application/json";
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
// Detect idempotency from header or body
|
|
59
|
+
const hasIdempotencyKey = Boolean(headers["Idempotency-Key"] ||
|
|
60
|
+
headers["idempotency-key"] ||
|
|
61
|
+
(opts?.body && typeof opts.body === "object" && opts.body.idempotency_key));
|
|
62
|
+
// Generate client request ID for tracing
|
|
63
|
+
const clientRequestId = `sdk_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
64
|
+
// Call onRequest hook
|
|
65
|
+
const startTime = Date.now();
|
|
66
|
+
await options.hooks?.onRequest?.({
|
|
59
67
|
method,
|
|
68
|
+
url: fullUrl,
|
|
60
69
|
headers,
|
|
61
|
-
body: opts?.body
|
|
70
|
+
body: opts?.body,
|
|
71
|
+
requestId: clientRequestId,
|
|
62
72
|
});
|
|
73
|
+
// Execute the actual fetch
|
|
74
|
+
const doFetch = async (signal) => {
|
|
75
|
+
return fetch(fullUrl, {
|
|
76
|
+
method,
|
|
77
|
+
headers,
|
|
78
|
+
body: opts?.body ? JSON.stringify(opts.body) : undefined,
|
|
79
|
+
signal,
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
let response;
|
|
83
|
+
try {
|
|
84
|
+
response = await doFetch(opts?.signal);
|
|
85
|
+
}
|
|
86
|
+
catch (fetchError) {
|
|
87
|
+
const durationMs = Date.now() - startTime;
|
|
88
|
+
// Call onError hook
|
|
89
|
+
await options.hooks?.onError?.({
|
|
90
|
+
method,
|
|
91
|
+
url: fullUrl,
|
|
92
|
+
error: fetchError instanceof Error ? fetchError : new Error(String(fetchError)),
|
|
93
|
+
durationMs,
|
|
94
|
+
requestId: clientRequestId,
|
|
95
|
+
});
|
|
96
|
+
// Network error - return as error
|
|
97
|
+
return {
|
|
98
|
+
error: {
|
|
99
|
+
code: "NETWORK_ERROR",
|
|
100
|
+
message: fetchError instanceof Error ? fetchError.message : "Network request failed"
|
|
101
|
+
},
|
|
102
|
+
response: new Response(null, { status: 0 })
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const durationMs = Date.now() - startTime;
|
|
106
|
+
// Extract requestId for tracing (prefer server requestId if available)
|
|
107
|
+
const requestId = response.headers.get("x-request-id") ?? clientRequestId;
|
|
63
108
|
if (!response.ok) {
|
|
64
|
-
let
|
|
109
|
+
let errorBody;
|
|
65
110
|
try {
|
|
66
|
-
|
|
67
|
-
error = {
|
|
68
|
-
code: json.code || `HTTP_${response.status}`,
|
|
69
|
-
message: json.message || response.statusText,
|
|
70
|
-
details: json.details,
|
|
71
|
-
};
|
|
111
|
+
errorBody = await response.json();
|
|
72
112
|
}
|
|
73
113
|
catch {
|
|
74
|
-
|
|
75
|
-
code: `HTTP_${response.status}`,
|
|
76
|
-
message: response.statusText,
|
|
77
|
-
};
|
|
114
|
+
// Ignore JSON parse errors
|
|
78
115
|
}
|
|
79
|
-
|
|
116
|
+
// Call onError hook
|
|
117
|
+
await options.hooks?.onError?.({
|
|
118
|
+
method,
|
|
119
|
+
url: fullUrl,
|
|
120
|
+
error: new Error(errorBody?.message || response.statusText),
|
|
121
|
+
durationMs,
|
|
122
|
+
requestId,
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
error: {
|
|
126
|
+
code: errorBody?.code || `HTTP_${response.status}`,
|
|
127
|
+
message: errorBody?.message || response.statusText,
|
|
128
|
+
details: errorBody?.details,
|
|
129
|
+
},
|
|
130
|
+
response,
|
|
131
|
+
meta: { requestId },
|
|
132
|
+
};
|
|
80
133
|
}
|
|
134
|
+
// Call onResponse hook
|
|
135
|
+
await options.hooks?.onResponse?.({
|
|
136
|
+
method,
|
|
137
|
+
url: fullUrl,
|
|
138
|
+
status: response.status,
|
|
139
|
+
durationMs,
|
|
140
|
+
requestId,
|
|
141
|
+
});
|
|
81
142
|
// Handle no-content responses
|
|
82
143
|
if (response.status === 204) {
|
|
83
|
-
return { data: undefined, response };
|
|
144
|
+
return { data: undefined, response, meta: { requestId } };
|
|
84
145
|
}
|
|
85
146
|
try {
|
|
86
147
|
const data = await response.json();
|
|
87
|
-
return { data: data, response };
|
|
148
|
+
return { data: data, response, meta: { requestId } };
|
|
88
149
|
}
|
|
89
150
|
catch {
|
|
90
|
-
return { data: undefined, response };
|
|
151
|
+
return { data: undefined, response, meta: { requestId } };
|
|
91
152
|
}
|
|
92
153
|
}
|
|
93
154
|
return {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent OS SDK - Retry Logic
|
|
3
|
+
*
|
|
4
|
+
* Enterprise-grade retry with:
|
|
5
|
+
* - Exponential backoff with jitter
|
|
6
|
+
* - Respect for Retry-After headers
|
|
7
|
+
* - Idempotency-aware mutation retries
|
|
8
|
+
* - Proper abort signal handling (no listener leaks)
|
|
9
|
+
*/
|
|
10
|
+
import type { RetryConfig } from "./config.js";
|
|
11
|
+
/**
|
|
12
|
+
* Context for retry decision-making.
|
|
13
|
+
*/
|
|
14
|
+
export interface RetryContext {
|
|
15
|
+
/** HTTP method (GET, POST, etc.) */
|
|
16
|
+
method: string;
|
|
17
|
+
/** Whether the request has an idempotency key */
|
|
18
|
+
hasIdempotencyKey: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Wraps an async function with retry logic.
|
|
22
|
+
*
|
|
23
|
+
* IMPORTANT: Mutations (POST/PUT/PATCH/DELETE) are only retried if hasIdempotencyKey is true.
|
|
24
|
+
* This prevents duplicate side effects.
|
|
25
|
+
*
|
|
26
|
+
* @param fn - Function to execute (receives an AbortSignal)
|
|
27
|
+
* @param config - Retry configuration
|
|
28
|
+
* @param context - Request context for retry decisions
|
|
29
|
+
* @param parentSignal - Optional parent AbortSignal for cancellation
|
|
30
|
+
*/
|
|
31
|
+
export declare function withRetry<T>(fn: (signal: AbortSignal) => Promise<T>, config: RetryConfig, context: RetryContext, parentSignal?: AbortSignal): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Sleep utility.
|
|
34
|
+
*/
|
|
35
|
+
declare function sleep(ms: number): Promise<void>;
|
|
36
|
+
export { sleep };
|
|
37
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/client/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,oCAAoC;IACpC,MAAM,EAAE,MAAM,CAAC;IAEf,iDAAiD;IACjD,iBAAiB,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC7B,EAAE,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,EACvC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,WAAW,GAC3B,OAAO,CAAC,CAAC,CAAC,CAmDZ;AAmDD;;GAEG;AACH,iBAAS,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAExC;AAED,OAAO,EAAE,KAAK,EAAE,CAAC"}
|