Skip to content

Commit 38a2be0

Browse files
feat: add pagination config for list agent files
1 parent b557cb6 commit 38a2be0

File tree

7 files changed

+149
-92
lines changed

7 files changed

+149
-92
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 106
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/letta-ai%2Fletta-sdk-c9da12a5f16bb5f9039d2c783261913ea367f0e5d077b6fdb10e899fd8e6d7ca.yml
33
openapi_spec_hash: e14297a0306cb58ebfd7594c7d9d994b
4-
config_hash: fcdeacffedf80fe9b63275b738fbb8c3
4+
config_hash: 2f5889efe104e3e49d62c0322e07ff4f

api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Types:
157157

158158
Methods:
159159

160-
- <code title="get /v1/agents/{agent_id}/files">client.agents.files.<a href="./src/resources/agents/files.ts">list</a>(agentID, { ...params }) -> FileListResponse</code>
160+
- <code title="get /v1/agents/{agent_id}/files">client.agents.files.<a href="./src/resources/agents/files.ts">list</a>(agentID, { ...params }) -> FileListResponsesNextFilesPage</code>
161161
- <code title="patch /v1/agents/{agent_id}/files/{file_id}/close">client.agents.files.<a href="./src/resources/agents/files.ts">close</a>(fileID, { ...params }) -> unknown</code>
162162
- <code title="patch /v1/agents/{agent_id}/files/close-all">client.agents.files.<a href="./src/resources/agents/files.ts">closeAll</a>(agentID) -> FileCloseAllResponse</code>
163163
- <code title="patch /v1/agents/{agent_id}/files/{file_id}/open">client.agents.files.<a href="./src/resources/agents/files.ts">open</a>(fileID, { ...params }) -> FileOpenResponse</code>

src/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
AbstractPage,
2020
type ArrayPageParams,
2121
ArrayPageResponse,
22+
type NextFilesPageParams,
23+
NextFilesPageResponse,
2224
type ObjectPageParams,
2325
ObjectPageResponse,
2426
} from './core/pagination';
@@ -923,6 +925,12 @@ export declare namespace Letta {
923925
export import ObjectPage = Pagination.ObjectPage;
924926
export { type ObjectPageParams as ObjectPageParams, type ObjectPageResponse as ObjectPageResponse };
925927

928+
export import NextFilesPage = Pagination.NextFilesPage;
929+
export {
930+
type NextFilesPageParams as NextFilesPageParams,
931+
type NextFilesPageResponse as NextFilesPageResponse,
932+
};
933+
926934
export { type HealthResponse as HealthResponse };
927935

928936
export {

src/core/pagination.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,92 @@ export class ObjectPage<Item extends { id: string }>
243243
};
244244
}
245245
}
246+
247+
export interface NextFilesPageResponse<Item> {
248+
files: Array<Item>;
249+
250+
next_cursor: string | null;
251+
252+
has_more: boolean;
253+
}
254+
255+
export interface NextFilesPageParams {
256+
before?: string | null;
257+
258+
after?: string | null;
259+
260+
limit?: number | null;
261+
262+
order?: string | null;
263+
264+
order_by?: string | null;
265+
}
266+
267+
export class NextFilesPage<Item extends { next_cursor: string | null }>
268+
extends AbstractPage<Item>
269+
implements NextFilesPageResponse<Item>
270+
{
271+
files: Array<Item>;
272+
273+
next_cursor: string | null;
274+
275+
has_more: boolean;
276+
277+
constructor(
278+
client: Letta,
279+
response: Response,
280+
body: NextFilesPageResponse<Item>,
281+
options: FinalRequestOptions,
282+
) {
283+
super(client, response, body, options);
284+
285+
this.files = body.files || [];
286+
this.next_cursor = body.next_cursor || null;
287+
this.has_more = body.has_more || false;
288+
}
289+
290+
getPaginatedItems(): Item[] {
291+
return this.files ?? [];
292+
}
293+
294+
override hasNextPage(): boolean {
295+
if (this.has_more === false) {
296+
return false;
297+
}
298+
299+
return super.hasNextPage();
300+
}
301+
302+
nextPageRequestOptions(): PageRequestOptions | null {
303+
const files = this.getPaginatedItems();
304+
305+
const isForwards = !(typeof this.options.query === 'object' && 'before' in (this.options.query || {}));
306+
if (isForwards) {
307+
const nextCursor = files[files.length - 1]?.next_cursor;
308+
if (!nextCursor) {
309+
return null;
310+
}
311+
312+
return {
313+
...this.options,
314+
query: {
315+
...maybeObj(this.options.query),
316+
after: nextCursor,
317+
},
318+
};
319+
}
320+
321+
const nextCursor = files[0]?.next_cursor;
322+
if (!nextCursor) {
323+
return null;
324+
}
325+
326+
return {
327+
...this.options,
328+
query: {
329+
...maybeObj(this.options.query),
330+
before: nextCursor,
331+
},
332+
};
333+
}
334+
}

src/resources/agents/agents.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
FileCloseResponse,
2424
FileListParams,
2525
FileListResponse,
26+
FileListResponsesNextFilesPage,
2627
FileOpenParams,
2728
FileOpenResponse,
2829
Files,
@@ -1718,6 +1719,7 @@ export declare namespace Agents {
17181719
type FileCloseResponse as FileCloseResponse,
17191720
type FileCloseAllResponse as FileCloseAllResponse,
17201721
type FileOpenResponse as FileOpenResponse,
1722+
type FileListResponsesNextFilesPage as FileListResponsesNextFilesPage,
17211723
type FileListParams as FileListParams,
17221724
type FileCloseParams as FileCloseParams,
17231725
type FileOpenParams as FileOpenParams,

src/resources/agents/files.ts

Lines changed: 47 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { APIResource } from '../../core/resource';
44
import { APIPromise } from '../../core/api-promise';
5+
import { NextFilesPage, type NextFilesPageParams, PagePromise } from '../../core/pagination';
56
import { RequestOptions } from '../../internal/request-options';
67
import { path } from '../../internal/utils/path';
78

@@ -13,8 +14,11 @@ export class Files extends APIResource {
1314
agentID: string,
1415
query: FileListParams | null | undefined = {},
1516
options?: RequestOptions,
16-
): APIPromise<FileListResponse> {
17-
return this._client.get(path`/v1/agents/${agentID}/files`, { query, ...options });
17+
): PagePromise<FileListResponsesNextFilesPage, FileListResponse> {
18+
return this._client.getAPIList(path`/v1/agents/${agentID}/files`, NextFilesPage<FileListResponse>, {
19+
query,
20+
...options,
21+
});
1822
}
1923

2024
/**
@@ -51,128 +55,80 @@ export class Files extends APIResource {
5155
}
5256
}
5357

58+
export type FileListResponsesNextFilesPage = NextFilesPage<FileListResponse>;
59+
5460
/**
55-
* Paginated response for agent files
61+
* Response model for agent file attachments showing file status in agent context
5662
*/
5763
export interface FileListResponse {
5864
/**
59-
* List of file attachments for the agent
65+
* Unique identifier of the file-agent relationship
6066
*/
61-
files: Array<FileListResponse.File>;
67+
id: string;
6268

6369
/**
64-
* Whether more results exist after this page
70+
* Unique identifier of the file
6571
*/
66-
has_more: boolean;
72+
file_id: string;
6773

6874
/**
69-
* Cursor for fetching the next page (file-agent relationship ID)
75+
* Name of the file
7076
*/
71-
next_cursor?: string | null;
72-
}
73-
74-
export namespace FileListResponse {
75-
/**
76-
* Response model for agent file attachments showing file status in agent context
77-
*/
78-
export interface File {
79-
/**
80-
* Unique identifier of the file-agent relationship
81-
*/
82-
id: string;
83-
84-
/**
85-
* Unique identifier of the file
86-
*/
87-
file_id: string;
88-
89-
/**
90-
* Name of the file
91-
*/
92-
file_name: string;
93-
94-
/**
95-
* Unique identifier of the folder/source
96-
*/
97-
folder_id: string;
98-
99-
/**
100-
* Name of the folder/source
101-
*/
102-
folder_name: string;
103-
104-
/**
105-
* Whether the file is currently open in the agent's context
106-
*/
107-
is_open: boolean;
108-
109-
/**
110-
* Ending line number if file was opened with line range
111-
*/
112-
end_line?: number | null;
113-
114-
/**
115-
* Timestamp of last access by the agent
116-
*/
117-
last_accessed_at?: string | null;
118-
119-
/**
120-
* Starting line number if file was opened with line range
121-
*/
122-
start_line?: number | null;
123-
124-
/**
125-
* Portion of the file visible to the agent if open
126-
*/
127-
visible_content?: string | null;
128-
}
129-
}
130-
131-
export type FileCloseResponse = unknown;
77+
file_name: string;
13278

133-
export type FileCloseAllResponse = Array<string>;
79+
/**
80+
* Unique identifier of the folder/source
81+
*/
82+
folder_id: string;
13483

135-
export type FileOpenResponse = Array<string>;
84+
/**
85+
* Name of the folder/source
86+
*/
87+
folder_name: string;
13688

137-
export interface FileListParams {
13889
/**
139-
* File ID cursor for pagination. Returns files that come after this file ID in the
140-
* specified sort order
90+
* Whether the file is currently open in the agent's context
14191
*/
142-
after?: string | null;
92+
is_open: boolean;
14393

14494
/**
145-
* File ID cursor for pagination. Returns files that come before this file ID in
146-
* the specified sort order
95+
* Ending line number if file was opened with line range
14796
*/
148-
before?: string | null;
97+
end_line?: number | null;
14998

15099
/**
151-
* @deprecated Pagination cursor from previous response (deprecated, use
152-
* before/after)
100+
* Timestamp of last access by the agent
153101
*/
154-
cursor?: string | null;
102+
last_accessed_at?: string | null;
155103

156104
/**
157-
* Filter by open status (true for open files, false for closed files)
105+
* Starting line number if file was opened with line range
158106
*/
159-
is_open?: boolean | null;
107+
start_line?: number | null;
160108

161109
/**
162-
* Maximum number of files to return
110+
* Portion of the file visible to the agent if open
163111
*/
164-
limit?: number | null;
112+
visible_content?: string | null;
113+
}
114+
115+
export type FileCloseResponse = unknown;
116+
117+
export type FileCloseAllResponse = Array<string>;
165118

119+
export type FileOpenResponse = Array<string>;
120+
121+
export interface FileListParams extends NextFilesPageParams {
166122
/**
167-
* Sort order for files by creation time. 'asc' for oldest first, 'desc' for newest
168-
* first
123+
* @deprecated Pagination cursor from previous response (deprecated, use
124+
* before/after)
169125
*/
170-
order?: 'asc' | 'desc';
126+
cursor?: string | null;
171127

172128
/**
173-
* Field to sort by
129+
* Filter by open status (true for open files, false for closed files)
174130
*/
175-
order_by?: 'created_at';
131+
is_open?: boolean | null;
176132
}
177133

178134
export interface FileCloseParams {
@@ -195,6 +151,7 @@ export declare namespace Files {
195151
type FileCloseResponse as FileCloseResponse,
196152
type FileCloseAllResponse as FileCloseAllResponse,
197153
type FileOpenResponse as FileOpenResponse,
154+
type FileListResponsesNextFilesPage as FileListResponsesNextFilesPage,
198155
type FileListParams as FileListParams,
199156
type FileCloseParams as FileCloseParams,
200157
type FileOpenParams as FileOpenParams,

src/resources/agents/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
type FileListParams,
5252
type FileCloseParams,
5353
type FileOpenParams,
54+
type FileListResponsesNextFilesPage,
5455
} from './files';
5556
export {
5657
Folders,

0 commit comments

Comments
 (0)