Skip to content

Commit

Permalink
feat(api): Add cloning feature (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Nov 7, 2024
1 parent 7ffa4fd commit e1c2a1a
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 39
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-3b94ec668fd95755a2447237b113b61d5c82e62960832d065e8a6f06a1916286.yml
configured_endpoints: 40
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-96679f6cf67d77d1e9587886a8cdbb6e2a132b9f8abd22c056e479fe3ff89a6f.yml
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ On timeout, an `APIConnectionTimeoutError` is thrown.

Note that requests which time out will be [retried twice by default](#retries).

## Auto-pagination

List methods in the Honcho API are paginated.
You can use the `for await … of` syntax to iterate through items across all pages:

```ts
async function fetchAllAppsUsers(params) {
const allAppsUsers = [];
// Automatically fetches more pages as needed.
for await (const user of client.apps.users.list('REPLACE_ME')) {
allAppsUsers.push(user);
}
return allAppsUsers;
}
```

Alternatively, you can request a single page at a time:

```ts
let page = await client.apps.users.list('REPLACE_ME');
for (const user of page.items) {
console.log(user);
}

// Convenience methods are provided for manually paginating:
while (page.hasNextPage()) {
page = page.getNextPage();
// ...
}
```

## Advanced Usage

### Accessing raw Response data (e.g., headers)
Expand Down
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Methods:
- <code title="post /v1/apps/{app_id}/users/{user_id}/sessions/list">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">list</a>(appId, userId, { ...params }) -> SessionsPage</code>
- <code title="delete /v1/apps/{app_id}/users/{user_id}/sessions/{session_id}">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">delete</a>(appId, userId, sessionId) -> unknown</code>
- <code title="post /v1/apps/{app_id}/users/{user_id}/sessions/{session_id}/chat">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">chat</a>(appId, userId, sessionId, { ...params }) -> AgentChat</code>
- <code title="get /v1/apps/{app_id}/users/{user_id}/sessions/{session_id}/clone">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">clone</a>(appId, userId, sessionId, { ...params }) -> Session</code>
- <code title="get /v1/apps/{app_id}/users/{user_id}/sessions/{session_id}">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">get</a>(appId, userId, sessionId) -> Session</code>
- <code title="post /v1/apps/{app_id}/users/{user_id}/sessions/{session_id}/chat/stream">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">stream</a>(appId, userId, sessionId, { ...params }) -> unknown</code>

Expand Down
1 change: 1 addition & 0 deletions src/resources/apps/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
type SessionUpdateParams,
type SessionListParams,
type SessionChatParams,
type SessionCloneParams,
type SessionStreamParams,
} from './sessions/index';
export {
Expand Down
1 change: 1 addition & 0 deletions src/resources/apps/users/sessions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export {
type SessionUpdateParams,
type SessionListParams,
type SessionChatParams,
type SessionCloneParams,
type SessionStreamParams,
} from './sessions';
40 changes: 40 additions & 0 deletions src/resources/apps/users/sessions/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import { APIResource } from '../../../../resource';
import { isRequestOptions } from '../../../../core';
import * as Core from '../../../../core';
import * as MessagesAPI from './messages';
import {
Expand Down Expand Up @@ -126,6 +127,38 @@ export class Sessions extends APIResource {
});
}

/**
* Clone Session
*/
clone(
appId: string,
userId: string,
sessionId: string,
query?: SessionCloneParams,
options?: Core.RequestOptions,
): Core.APIPromise<Session>;
clone(
appId: string,
userId: string,
sessionId: string,
options?: Core.RequestOptions,
): Core.APIPromise<Session>;
clone(
appId: string,
userId: string,
sessionId: string,
query: SessionCloneParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.APIPromise<Session> {
if (isRequestOptions(query)) {
return this.clone(appId, userId, sessionId, {}, query);
}
return this._client.get(`/v1/apps/${appId}/users/${userId}/sessions/${sessionId}/clone`, {
query,
...options,
});
}

/**
* Get a specific session for a user by ID
*
Expand Down Expand Up @@ -226,6 +259,12 @@ export interface SessionChatParams {
queries: string | Array<string>;
}

export interface SessionCloneParams {
deep_copy?: boolean;

message_id?: string | null;
}

export interface SessionStreamParams {
queries: string | Array<string>;
}
Expand All @@ -248,6 +287,7 @@ export declare namespace Sessions {
type SessionUpdateParams as SessionUpdateParams,
type SessionListParams as SessionListParams,
type SessionChatParams as SessionChatParams,
type SessionCloneParams as SessionCloneParams,
type SessionStreamParams as SessionStreamParams,
};

Expand Down
2 changes: 2 additions & 0 deletions src/resources/apps/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
PageSession,
Session,
SessionChatParams,
SessionCloneParams,
SessionCreateParams,
SessionDeleteResponse,
SessionListParams,
Expand Down Expand Up @@ -207,6 +208,7 @@ export declare namespace Users {
type SessionUpdateParams as SessionUpdateParams,
type SessionListParams as SessionListParams,
type SessionChatParams as SessionChatParams,
type SessionCloneParams as SessionCloneParams,
type SessionStreamParams as SessionStreamParams,
};

Expand Down
33 changes: 33 additions & 0 deletions tests/api-resources/apps/users/sessions/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ describe('resource sessions', () => {
});
});

test('clone', async () => {
const responsePromise = client.apps.users.sessions.clone('app_id', 'user_id', 'session_id');
const rawResponse = await responsePromise.asResponse();
expect(rawResponse).toBeInstanceOf(Response);
const response = await responsePromise;
expect(response).not.toBeInstanceOf(Response);
const dataAndResponse = await responsePromise.withResponse();
expect(dataAndResponse.data).toBe(response);
expect(dataAndResponse.response).toBe(rawResponse);
});

test('clone: request options instead of params are passed correctly', async () => {
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
await expect(
client.apps.users.sessions.clone('app_id', 'user_id', 'session_id', {
path: '/_stainless_unknown_path',
}),
).rejects.toThrow(Honcho.NotFoundError);
});

test('clone: request options and params are passed correctly', async () => {
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
await expect(
client.apps.users.sessions.clone(
'app_id',
'user_id',
'session_id',
{ deep_copy: true, message_id: 'message_id' },
{ path: '/_stainless_unknown_path' },
),
).rejects.toThrow(Honcho.NotFoundError);
});

test('get', async () => {
const responsePromise = client.apps.users.sessions.get('app_id', 'user_id', 'session_id');
const rawResponse = await responsePromise.asResponse();
Expand Down

0 comments on commit e1c2a1a

Please sign in to comment.