@agent-os-sdk/client 0.9.25 → 0.9.27
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/generated/openapi.d.ts +82 -0
- package/dist/generated/openapi.d.ts.map +1 -1
- package/dist/modules/runs.d.ts.map +1 -1
- package/dist/modules/templates.d.ts +23 -0
- package/dist/modules/templates.d.ts.map +1 -1
- package/dist/modules/templates.js +7 -0
- package/package.json +2 -2
- package/src/client/AgentOsClient.ts +0 -294
- package/src/client/HttpRequestBuilder.ts +0 -115
- package/src/client/OperationContext.ts +0 -22
- package/src/client/OperationContextProvider.ts +0 -89
- package/src/client/auth.ts +0 -136
- package/src/client/config.ts +0 -100
- package/src/client/helpers.ts +0 -98
- package/src/client/pagination.ts +0 -218
- package/src/client/raw.ts +0 -609
- package/src/client/retry.ts +0 -150
- package/src/client/sanitize.ts +0 -31
- package/src/client/timeout.ts +0 -59
- package/src/errors/factory.ts +0 -140
- package/src/errors/index.ts +0 -365
- package/src/generated/client.ts +0 -32
- package/src/generated/index.ts +0 -2
- package/src/generated/openapi.ts +0 -12302
- package/src/generated/swagger.json +0 -16851
- package/src/index.ts +0 -131
- package/src/modules/a2a.ts +0 -64
- package/src/modules/agents.ts +0 -604
- package/src/modules/apiTokens.ts +0 -101
- package/src/modules/approvals.ts +0 -151
- package/src/modules/audit.ts +0 -145
- package/src/modules/auth.ts +0 -33
- package/src/modules/catalog.ts +0 -241
- package/src/modules/chatwoot.ts +0 -242
- package/src/modules/checkpoints.ts +0 -87
- package/src/modules/contracts.ts +0 -80
- package/src/modules/credentials.ts +0 -216
- package/src/modules/crons.ts +0 -115
- package/src/modules/datasets.ts +0 -142
- package/src/modules/evaluation.ts +0 -269
- package/src/modules/files.ts +0 -208
- package/src/modules/improvements.ts +0 -71
- package/src/modules/info.ts +0 -143
- package/src/modules/me.ts +0 -74
- package/src/modules/members.ts +0 -199
- package/src/modules/memberships.ts +0 -42
- package/src/modules/metaAgent.ts +0 -131
- package/src/modules/metrics.ts +0 -34
- package/src/modules/observability.ts +0 -28
- package/src/modules/playground.ts +0 -68
- package/src/modules/presets.ts +0 -246
- package/src/modules/prompts.ts +0 -147
- package/src/modules/roles.ts +0 -112
- package/src/modules/runs.ts +0 -878
- package/src/modules/store.ts +0 -65
- package/src/modules/templates.ts +0 -40
- package/src/modules/tenants.ts +0 -79
- package/src/modules/threads.ts +0 -343
- package/src/modules/tools.ts +0 -91
- package/src/modules/traces.ts +0 -133
- package/src/modules/triggers.ts +0 -357
- package/src/modules/usage.ts +0 -117
- package/src/modules/vectorStores.ts +0 -257
- package/src/modules/workspaces.ts +0 -216
- package/src/sse/client.ts +0 -179
package/src/client/auth.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Provider Types for Agent OS SDK
|
|
3
|
-
*
|
|
4
|
-
* Two modes only:
|
|
5
|
-
* - JWT (browser): Uses Supabase JWT + X-Workspace-Id header
|
|
6
|
-
* - API Token (server): Uses aosk_* token with embedded claims
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { NetworkConfig } from "./config.js";
|
|
10
|
-
import type { SDKHooks } from "./raw.js";
|
|
11
|
-
import type { OperationContextProvider } from "./OperationContextProvider.js";
|
|
12
|
-
|
|
13
|
-
// ============================================================================
|
|
14
|
-
// Auth Provider Types
|
|
15
|
-
// ============================================================================
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* API Token authentication for server-to-server integrations.
|
|
19
|
-
* Token format: aosk_live_* or aosk_test_*
|
|
20
|
-
*
|
|
21
|
-
* SECURITY: API tokens contain embedded workspace/tenant claims.
|
|
22
|
-
* The SDK sends ONLY Authorization header (no X-Workspace-Id, no X-Tenant-Id).
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const client = new AgentOsClient({
|
|
27
|
-
* baseUrl: "https://api.example.com",
|
|
28
|
-
* auth: { type: "api_token", apiKey: "aosk_live_xxx" }
|
|
29
|
-
* })
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export type ApiTokenAuth = {
|
|
33
|
-
type: "api_token"
|
|
34
|
-
/** API key (aosk_*) or function that returns one */
|
|
35
|
-
apiKey: string | (() => string | Promise<string>)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* JWT authentication for browser/frontend clients.
|
|
40
|
-
* Uses Supabase JWT with X-Workspace-Id header.
|
|
41
|
-
*
|
|
42
|
-
* SECURITY:
|
|
43
|
-
* - X-Workspace-Id is REQUIRED (throws if missing)
|
|
44
|
-
* - X-Tenant-Id is NEVER sent (backend derives from workspace membership)
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* const client = new AgentOsClient({
|
|
49
|
-
* baseUrl: "https://api.example.com",
|
|
50
|
-
* auth: {
|
|
51
|
-
* type: "jwt",
|
|
52
|
-
* getToken: async () => (await supabase.auth.getSession()).data.session?.access_token,
|
|
53
|
-
* getWorkspaceId: () => localStorage.getItem("agentos.workspaceId")!,
|
|
54
|
-
* }
|
|
55
|
-
* })
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
export type JwtAuth = {
|
|
59
|
-
type: "jwt"
|
|
60
|
-
/** Function to get the JWT access token */
|
|
61
|
-
getToken: () => string | Promise<string>
|
|
62
|
-
/** Function to get the current workspace ID (REQUIRED) */
|
|
63
|
-
getWorkspaceId: () => string | Promise<string>
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Auth provider union type - only two modes supported
|
|
68
|
-
*/
|
|
69
|
-
export type AuthProvider = ApiTokenAuth | JwtAuth
|
|
70
|
-
|
|
71
|
-
// ============================================================================
|
|
72
|
-
// Client Options
|
|
73
|
-
// ============================================================================
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Options for AgentOsClient - auth is REQUIRED
|
|
77
|
-
*/
|
|
78
|
-
export type AgentOsClientOptions = {
|
|
79
|
-
/** Base URL of the Agent OS Control Plane */
|
|
80
|
-
baseUrl: string
|
|
81
|
-
/** Authentication provider (REQUIRED) */
|
|
82
|
-
auth: AuthProvider
|
|
83
|
-
/**
|
|
84
|
-
* Allow API token in browser environment.
|
|
85
|
-
* Default: false (throws error to prevent accidental exposure)
|
|
86
|
-
* Only set to true if you understand the security implications.
|
|
87
|
-
*/
|
|
88
|
-
allowApiTokenInBrowser?: boolean
|
|
89
|
-
/** Network policy (timeouts/retries). */
|
|
90
|
-
network?: Partial<NetworkConfig>
|
|
91
|
-
/** Optional request lifecycle hooks for observability */
|
|
92
|
-
hooks?: SDKHooks
|
|
93
|
-
/** Optional flow context provider (ALS/manual) for sticky correlation per operation. */
|
|
94
|
-
/** Recommended for long-lived server clients to avoid process-wide sticky fallback correlation. */
|
|
95
|
-
contextProvider?: OperationContextProvider
|
|
96
|
-
/** Custom headers to add to all requests */
|
|
97
|
-
headers?: Record<string, string>
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ============================================================================
|
|
101
|
-
// Type Guards
|
|
102
|
-
// ============================================================================
|
|
103
|
-
|
|
104
|
-
export function isApiTokenAuth(auth: AuthProvider): auth is ApiTokenAuth {
|
|
105
|
-
return auth.type === "api_token"
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function isJwtAuth(auth: AuthProvider): auth is JwtAuth {
|
|
109
|
-
return auth.type === "jwt"
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// ============================================================================
|
|
113
|
-
// Helpers
|
|
114
|
-
// ============================================================================
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Detect browser environment
|
|
118
|
-
*/
|
|
119
|
-
export function isBrowser(): boolean {
|
|
120
|
-
return typeof window !== "undefined" && typeof document !== "undefined"
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Check if token looks like an API token (aosk_*)
|
|
125
|
-
*/
|
|
126
|
-
export function isApiToken(token: string): boolean {
|
|
127
|
-
return token.startsWith("aosk_")
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Check if token looks like a JWT (three base64 segments)
|
|
132
|
-
*/
|
|
133
|
-
export function isJwtToken(token: string): boolean {
|
|
134
|
-
const parts = token.split(".")
|
|
135
|
-
return parts.length === 3 && parts.every(p => p.length > 0)
|
|
136
|
-
}
|
package/src/client/config.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent OS SDK - Network Configuration
|
|
3
|
-
*
|
|
4
|
-
* Centralized configuration for timeouts, retries, and network policies.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Retry configuration for network requests.
|
|
9
|
-
*/
|
|
10
|
-
export interface RetryConfig {
|
|
11
|
-
/** Maximum retry attempts (default: 3) */
|
|
12
|
-
maxRetries: number;
|
|
13
|
-
|
|
14
|
-
/** Base delay between retries in ms (default: 1000) */
|
|
15
|
-
baseDelayMs: number;
|
|
16
|
-
|
|
17
|
-
/** Maximum delay between retries in ms (default: 30000) */
|
|
18
|
-
maxDelayMs: number;
|
|
19
|
-
|
|
20
|
-
/** Jitter factor to prevent thundering herd (0-1, default: 0.2) */
|
|
21
|
-
jitterFactor: number;
|
|
22
|
-
|
|
23
|
-
/** HTTP status codes that trigger retry (default: [408, 429, 500, 502, 503, 504]) */
|
|
24
|
-
retryableStatuses: number[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Full network configuration for the SDK client.
|
|
29
|
-
*/
|
|
30
|
-
export interface NetworkConfig {
|
|
31
|
-
/** Timeout per request attempt in milliseconds (default: 30000) */
|
|
32
|
-
timeoutMs: number;
|
|
33
|
-
|
|
34
|
-
/** Retry configuration */
|
|
35
|
-
retry: RetryConfig;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Default network configuration.
|
|
40
|
-
* Balanced for most use cases - 30s timeout, 3 retries with exponential backoff.
|
|
41
|
-
*/
|
|
42
|
-
export const DEFAULT_NETWORK_CONFIG: NetworkConfig = {
|
|
43
|
-
timeoutMs: 30_000,
|
|
44
|
-
retry: {
|
|
45
|
-
maxRetries: 3,
|
|
46
|
-
baseDelayMs: 1_000,
|
|
47
|
-
maxDelayMs: 30_000,
|
|
48
|
-
jitterFactor: 0.2,
|
|
49
|
-
retryableStatuses: [408, 429, 500, 502, 503, 504],
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Aggressive configuration for interactive UIs.
|
|
55
|
-
* Shorter timeouts, fewer retries.
|
|
56
|
-
*/
|
|
57
|
-
export const INTERACTIVE_NETWORK_CONFIG: NetworkConfig = {
|
|
58
|
-
timeoutMs: 10_000,
|
|
59
|
-
retry: {
|
|
60
|
-
maxRetries: 1,
|
|
61
|
-
baseDelayMs: 500,
|
|
62
|
-
maxDelayMs: 5_000,
|
|
63
|
-
jitterFactor: 0.1,
|
|
64
|
-
retryableStatuses: [429, 503, 504],
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Patient configuration for background jobs.
|
|
70
|
-
* Longer timeouts, more retries.
|
|
71
|
-
*/
|
|
72
|
-
export const BACKGROUND_NETWORK_CONFIG: NetworkConfig = {
|
|
73
|
-
timeoutMs: 120_000,
|
|
74
|
-
retry: {
|
|
75
|
-
maxRetries: 5,
|
|
76
|
-
baseDelayMs: 2_000,
|
|
77
|
-
maxDelayMs: 60_000,
|
|
78
|
-
jitterFactor: 0.3,
|
|
79
|
-
retryableStatuses: [408, 429, 500, 502, 503, 504],
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Merges user config with defaults.
|
|
85
|
-
*/
|
|
86
|
-
export function mergeNetworkConfig(
|
|
87
|
-
userConfig?: Partial<NetworkConfig>
|
|
88
|
-
): NetworkConfig {
|
|
89
|
-
if (!userConfig) {
|
|
90
|
-
return DEFAULT_NETWORK_CONFIG;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
timeoutMs: userConfig.timeoutMs ?? DEFAULT_NETWORK_CONFIG.timeoutMs,
|
|
95
|
-
retry: {
|
|
96
|
-
...DEFAULT_NETWORK_CONFIG.retry,
|
|
97
|
-
...userConfig.retry,
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
package/src/client/helpers.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SDK Helper Types and Functions
|
|
3
|
-
*
|
|
4
|
-
* Provides utilities for working with API responses.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { APIResponse } from "./raw.js";
|
|
8
|
-
|
|
9
|
-
// ============================================================================
|
|
10
|
-
// Pagination
|
|
11
|
-
// ============================================================================
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Standard pagination parameters used across all list endpoints.
|
|
15
|
-
* Extends Record to be compatible with query parameter types.
|
|
16
|
-
*/
|
|
17
|
-
export interface PaginationParams extends Record<string, unknown> {
|
|
18
|
-
/** Maximum number of items to return (default: 20) */
|
|
19
|
-
limit?: number;
|
|
20
|
-
/** Number of items to skip (default: 0) */
|
|
21
|
-
offset?: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Paginated list response wrapper.
|
|
26
|
-
*/
|
|
27
|
-
export interface PaginatedResponse<T> {
|
|
28
|
-
items: T[];
|
|
29
|
-
total: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// ============================================================================
|
|
33
|
-
// Error Handling
|
|
34
|
-
// ============================================================================
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* SDK-specific error class with structured error data.
|
|
38
|
-
*/
|
|
39
|
-
export class SDKError extends Error {
|
|
40
|
-
readonly code: string;
|
|
41
|
-
readonly status: number;
|
|
42
|
-
readonly details?: unknown;
|
|
43
|
-
|
|
44
|
-
constructor(response: APIResponse<unknown>) {
|
|
45
|
-
const message = response.error?.message || `HTTP ${response.response.status}`;
|
|
46
|
-
super(message);
|
|
47
|
-
this.name = "SDKError";
|
|
48
|
-
this.code = response.error?.code || `HTTP_${response.response.status}`;
|
|
49
|
-
this.status = response.response.status;
|
|
50
|
-
this.details = response.error?.details;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Unwraps an APIResponse, returning the data directly or throwing on error.
|
|
56
|
-
* Use this when you want the cleaner "throws on error" pattern.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* // Instead of:
|
|
61
|
-
* const { data, error } = await client.agents.list();
|
|
62
|
-
* if (error) throw new Error(error.message);
|
|
63
|
-
*
|
|
64
|
-
* // Use:
|
|
65
|
-
* const agents = await unwrap(client.agents.list());
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
export async function unwrap<T>(promise: Promise<APIResponse<T>>): Promise<T> {
|
|
69
|
-
const result = await promise;
|
|
70
|
-
if (result.error || !result.data) {
|
|
71
|
-
throw new SDKError(result);
|
|
72
|
-
}
|
|
73
|
-
return result.data;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Type helper for unwrapped responses.
|
|
78
|
-
*/
|
|
79
|
-
export type Unwrapped<T> = T extends APIResponse<infer U> ? U : never;
|
|
80
|
-
|
|
81
|
-
// ============================================================================
|
|
82
|
-
// Result Pattern (Alternative to exceptions)
|
|
83
|
-
// ============================================================================
|
|
84
|
-
|
|
85
|
-
export type Result<T, E = SDKError> =
|
|
86
|
-
| { success: true; data: T }
|
|
87
|
-
| { success: false; error: E };
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Converts an APIResponse to a Result type for pattern matching.
|
|
91
|
-
*/
|
|
92
|
-
export async function toResult<T>(promise: Promise<APIResponse<T>>): Promise<Result<T>> {
|
|
93
|
-
const result = await promise;
|
|
94
|
-
if (result.error || !result.data) {
|
|
95
|
-
return { success: false, error: new SDKError(result) };
|
|
96
|
-
}
|
|
97
|
-
return { success: true, data: result.data };
|
|
98
|
-
}
|
package/src/client/pagination.ts
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
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
|
-
import type { APIResponse } from "./raw.js";
|
|
9
|
-
|
|
10
|
-
// ============================================================================
|
|
11
|
-
// Response Types
|
|
12
|
-
// ============================================================================
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Offset-based paginated response.
|
|
16
|
-
* Most common format for list endpoints.
|
|
17
|
-
*/
|
|
18
|
-
export interface OffsetPaginatedResponse<T> {
|
|
19
|
-
items: T[];
|
|
20
|
-
total: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Cursor-based paginated response.
|
|
25
|
-
* Better for large datasets and real-time consistency.
|
|
26
|
-
*/
|
|
27
|
-
export interface CursorPaginatedResponse<T> {
|
|
28
|
-
items: T[];
|
|
29
|
-
next_cursor?: string;
|
|
30
|
-
has_more: boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Union type for both pagination styles.
|
|
35
|
-
*/
|
|
36
|
-
export type PaginatedResponse<T> =
|
|
37
|
-
| OffsetPaginatedResponse<T>
|
|
38
|
-
| CursorPaginatedResponse<T>;
|
|
39
|
-
|
|
40
|
-
// ============================================================================
|
|
41
|
-
// Parameter Types
|
|
42
|
-
// ============================================================================
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Offset pagination parameters.
|
|
46
|
-
*/
|
|
47
|
-
export interface OffsetParams {
|
|
48
|
-
limit?: number;
|
|
49
|
-
offset?: number;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Cursor pagination parameters.
|
|
54
|
-
*/
|
|
55
|
-
export interface CursorParams {
|
|
56
|
-
limit?: number;
|
|
57
|
-
after?: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Combined pagination parameters.
|
|
62
|
-
*/
|
|
63
|
-
export type PaginationParams = OffsetParams | CursorParams;
|
|
64
|
-
|
|
65
|
-
// ============================================================================
|
|
66
|
-
// Type Guards
|
|
67
|
-
// ============================================================================
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Checks if response uses cursor pagination.
|
|
71
|
-
*/
|
|
72
|
-
function isCursorResponse<T>(response: PaginatedResponse<T>): response is CursorPaginatedResponse<T> {
|
|
73
|
-
return "has_more" in response;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ============================================================================
|
|
77
|
-
// Paginate Function
|
|
78
|
-
// ============================================================================
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Options for pagination behavior.
|
|
82
|
-
*/
|
|
83
|
-
export interface PaginateOptions {
|
|
84
|
-
/** Number of items per page (default: 100) */
|
|
85
|
-
pageSize?: number;
|
|
86
|
-
|
|
87
|
-
/** Maximum total items to fetch (default: unlimited) */
|
|
88
|
-
maxItems?: number;
|
|
89
|
-
|
|
90
|
-
/** AbortSignal for cancellation */
|
|
91
|
-
signal?: AbortSignal;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Creates an async iterator that automatically paginates through results.
|
|
96
|
-
* Supports both offset and cursor pagination transparently.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* // Basic usage
|
|
100
|
-
* for await (const run of paginate(
|
|
101
|
-
* (p) => client.runs.list(p),
|
|
102
|
-
* { status: "completed" }
|
|
103
|
-
* )) {
|
|
104
|
-
* console.log(run.id);
|
|
105
|
-
* }
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* // With options
|
|
109
|
-
* for await (const agent of paginate(
|
|
110
|
-
* (p) => client.agents.list(p),
|
|
111
|
-
* { workspace_id: "ws_123" },
|
|
112
|
-
* { pageSize: 50, maxItems: 200 }
|
|
113
|
-
* )) {
|
|
114
|
-
* console.log(agent.name);
|
|
115
|
-
* }
|
|
116
|
-
*/
|
|
117
|
-
export async function* paginate<T, P extends PaginationParams>(
|
|
118
|
-
fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>,
|
|
119
|
-
baseParams: Omit<P, "limit" | "offset" | "after">,
|
|
120
|
-
options?: PaginateOptions
|
|
121
|
-
): AsyncGenerator<T, void, unknown> {
|
|
122
|
-
const pageSize = options?.pageSize ?? 100;
|
|
123
|
-
const maxItems = options?.maxItems ?? Infinity;
|
|
124
|
-
const signal = options?.signal;
|
|
125
|
-
|
|
126
|
-
let offset = 0;
|
|
127
|
-
let cursor: string | undefined;
|
|
128
|
-
let hasMore = true;
|
|
129
|
-
let yielded = 0;
|
|
130
|
-
|
|
131
|
-
while (hasMore && yielded < maxItems) {
|
|
132
|
-
// Check for cancellation
|
|
133
|
-
if (signal?.aborted) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Build params based on pagination style
|
|
138
|
-
const params = {
|
|
139
|
-
...baseParams,
|
|
140
|
-
limit: Math.min(pageSize, maxItems - yielded),
|
|
141
|
-
...(cursor !== undefined ? { after: cursor } : { offset }),
|
|
142
|
-
} as P;
|
|
143
|
-
|
|
144
|
-
const response = await fetchPage(params);
|
|
145
|
-
|
|
146
|
-
if (response.error) {
|
|
147
|
-
throw response.error;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const data = response.data!;
|
|
151
|
-
|
|
152
|
-
for (const item of data.items) {
|
|
153
|
-
if (yielded >= maxItems) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
yield item;
|
|
157
|
-
yielded++;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Update pagination state based on response type
|
|
161
|
-
if (isCursorResponse(data)) {
|
|
162
|
-
cursor = data.next_cursor;
|
|
163
|
-
hasMore = data.has_more && data.items.length > 0;
|
|
164
|
-
} else {
|
|
165
|
-
offset += data.items.length;
|
|
166
|
-
hasMore = offset < data.total && data.items.length > 0;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// ============================================================================
|
|
172
|
-
// Collect Utility
|
|
173
|
-
// ============================================================================
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Collects all items from a paginated endpoint into an array.
|
|
177
|
-
* Useful when you need all items at once.
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* const allRuns = await collectAll(
|
|
181
|
-
* (p) => client.runs.list(p),
|
|
182
|
-
* { status: "completed" },
|
|
183
|
-
* { maxItems: 1000 }
|
|
184
|
-
* );
|
|
185
|
-
*/
|
|
186
|
-
export async function collectAll<T, P extends PaginationParams>(
|
|
187
|
-
fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>,
|
|
188
|
-
baseParams: Omit<P, "limit" | "offset" | "after">,
|
|
189
|
-
options?: PaginateOptions
|
|
190
|
-
): Promise<T[]> {
|
|
191
|
-
const items: T[] = [];
|
|
192
|
-
|
|
193
|
-
for await (const item of paginate(fetchPage, baseParams, options)) {
|
|
194
|
-
items.push(item);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return items;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Gets the first item from a paginated endpoint.
|
|
202
|
-
* More efficient than collecting all items when you only need one.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* const latestRun = await getFirst(
|
|
206
|
-
* (p) => client.runs.list({ ...p, order: "desc" }),
|
|
207
|
-
* { agent_id: "agent_123" }
|
|
208
|
-
* );
|
|
209
|
-
*/
|
|
210
|
-
export async function getFirst<T, P extends PaginationParams>(
|
|
211
|
-
fetchPage: (params: P) => Promise<APIResponse<PaginatedResponse<T>>>,
|
|
212
|
-
baseParams: Omit<P, "limit" | "offset" | "after">
|
|
213
|
-
): Promise<T | undefined> {
|
|
214
|
-
for await (const item of paginate(fetchPage, baseParams, { pageSize: 1, maxItems: 1 })) {
|
|
215
|
-
return item;
|
|
216
|
-
}
|
|
217
|
-
return undefined;
|
|
218
|
-
}
|