-
Notifications
You must be signed in to change notification settings - Fork 442
feat: add Griptape plugin #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThePhantom007
wants to merge
9
commits into
corsairdev:main
Choose a base branch
from
ThePhantom007:feat/griptape-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33e727b
feat: add Griptape plugin
ThePhantom007 91f61b7
Apply suggestion from @greptile-apps[bot]
ThePhantom007 34f2c6c
chore: update lockfile
ThePhantom007 326d226
Merge branch 'feat/griptape-plugin' of https://github.com/thephantom0…
ThePhantom007 8ead34b
fix(griptape): rethrow ApiError unchanged so rate-limit and auth hand…
Mayank-saraswal 33ecd1c
fix(griptape): remove webhook boilerplate, fix UUID validation, harde…
Mayank-saraswal 6eb5068
Merge branch 'main' into feat/griptape-plugin
ThePhantom007 58daa07
Merge remote-tracking branch 'upstream/main' into pr-947-fixes
Mayank-saraswal 68d14d9
Merge remote-tracking branch 'phantom/feat/griptape-plugin' into pr-9…
Mayank-saraswal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # @corsair-dev/griptape | ||
|
|
||
| Corsair integration for the Griptape Cloud API. | ||
|
|
||
| ## Authentication | ||
|
|
||
| Griptape Cloud uses HTTP Bearer authentication. | ||
|
|
||
| Provide a Griptape Cloud API key when creating the plugin: | ||
|
|
||
| ```ts | ||
| import { griptape } from '@corsair-dev/griptape'; | ||
|
|
||
| const plugin = griptape({ | ||
| key: process.env.GT_CLOUD_API_KEY, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { ApiRequestOptions, ApiResult } from 'corsair/http'; | ||
| import { ApiError, request } from 'corsair/http'; | ||
| import { GriptapeAPIError, makeGriptapeRequest } from './client'; | ||
|
|
||
| jest.mock('corsair/http', () => ({ | ||
| ...jest.requireActual('corsair/http'), | ||
| request: jest.fn(), | ||
| })); | ||
|
|
||
| const mockRequest = request as jest.MockedFunction<typeof request>; | ||
|
|
||
| const sampleRequest: ApiRequestOptions = { | ||
| method: 'GET', | ||
| url: 'assistants', | ||
| }; | ||
|
|
||
| function apiErrorOf( | ||
| status: number, | ||
| statusText: string, | ||
| retryAfterMs?: number, | ||
| ): ApiError { | ||
| const result: ApiResult = { | ||
| url: 'https://cloud.griptape.ai/api/assistants', | ||
| ok: false, | ||
| status, | ||
| statusText, | ||
| body: { message: statusText }, | ||
| }; | ||
| return new ApiError( | ||
| sampleRequest, | ||
| result, | ||
| statusText, | ||
| retryAfterMs === undefined ? undefined : { retryAfter: retryAfterMs }, | ||
| ); | ||
| } | ||
|
|
||
| describe('makeGriptapeRequest error handling', () => { | ||
| beforeEach(() => { | ||
| mockRequest.mockReset(); | ||
| }); | ||
|
|
||
| it('rethrows ApiError unchanged so status-based handlers keep working', async () => { | ||
| const rateLimitError = apiErrorOf(429, 'Too Many Requests', 30000); | ||
| mockRequest.mockRejectedValueOnce(rateLimitError); | ||
|
|
||
| await expect( | ||
| makeGriptapeRequest('assistants', 'test-api-key'), | ||
| ).rejects.toBe(rateLimitError); | ||
| }); | ||
|
|
||
| it('keeps status and Retry-After readable on the rethrown rate-limit error', async () => { | ||
| mockRequest.mockRejectedValueOnce( | ||
| apiErrorOf(429, 'Too Many Requests', 45000), | ||
| ); | ||
|
|
||
| const error = await makeGriptapeRequest('assistants', 'test-api-key').then( | ||
| () => null, | ||
| (error: unknown) => error, | ||
| ); | ||
|
|
||
| expect(error).toBeInstanceOf(ApiError); | ||
| expect(error).toMatchObject({ | ||
| name: 'ApiError', | ||
| status: 429, | ||
| retryAfter: 45000, | ||
| }); | ||
| }); | ||
|
|
||
| it('propagates authentication errors with their status code', async () => { | ||
| const authError = apiErrorOf(401, 'Unauthorized'); | ||
| mockRequest.mockRejectedValueOnce(authError); | ||
|
|
||
| await expect(makeGriptapeRequest('assistants', 'invalid-key')).rejects.toBe( | ||
| authError, | ||
| ); | ||
| }); | ||
|
|
||
| it('wraps non-API network failures as GriptapeAPIError', async () => { | ||
| mockRequest.mockRejectedValueOnce(new Error('socket hang up')); | ||
|
|
||
| const rejection = makeGriptapeRequest('assistants', 'test-api-key'); | ||
|
|
||
| await expect(rejection).rejects.toBeInstanceOf(GriptapeAPIError); | ||
| await expect(rejection).rejects.toThrow('socket hang up'); | ||
| }); | ||
|
|
||
| it('maps non-Error rejections to GriptapeAPIError with a generic message', async () => { | ||
| mockRequest.mockRejectedValueOnce('boom'); | ||
|
|
||
| await expect( | ||
| makeGriptapeRequest('assistants', 'test-api-key'), | ||
| ).rejects.toEqual(new GriptapeAPIError('Unknown error')); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { ApiRequestOptions, OpenAPIConfig } from 'corsair/http'; | ||
| import { ApiError, request } from 'corsair/http'; | ||
|
|
||
| export class GriptapeAPIError extends Error { | ||
| constructor( | ||
| message: string, | ||
| public readonly code?: string, | ||
| ) { | ||
| super(message); | ||
| this.name = 'GriptapeAPIError'; | ||
| } | ||
| } | ||
|
|
||
| const GRIPTAPE_API_BASE = 'https://cloud.griptape.ai/api'; | ||
|
|
||
| export async function makeGriptapeRequest<T>( | ||
| endpoint: string, | ||
| apiKey: string, | ||
| options: { | ||
| method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
| body?: Record<string, unknown>; | ||
| query?: Record<string, string | number | boolean | undefined>; | ||
| } = {}, | ||
| ): Promise<T> { | ||
| const { method = 'GET', body, query } = options; | ||
|
|
||
| const config: OpenAPIConfig = { | ||
| BASE: GRIPTAPE_API_BASE, | ||
| VERSION: '1.0.0', | ||
| WITH_CREDENTIALS: false, | ||
| CREDENTIALS: 'omit', | ||
| TOKEN: apiKey, | ||
| HEADERS: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| }; | ||
|
|
||
| const requestOptions: ApiRequestOptions = { | ||
| method, | ||
| url: endpoint, | ||
| body: | ||
| method === 'POST' || method === 'PUT' || method === 'PATCH' | ||
| ? body | ||
| : undefined, | ||
| mediaType: 'application/json; charset=utf-8', | ||
| query: method === 'GET' ? query : undefined, | ||
| }; | ||
|
|
||
| try { | ||
| return await request<T>(config, requestOptions); | ||
| } catch (error) { | ||
| // Re-thrown as-is: ApiError already carries the HTTP status code and | ||
| // Retry-After info that error-handlers.ts inspects. Wrapping it here | ||
| // would hide those fields behind a message string. | ||
| if (error instanceof ApiError) { | ||
| throw error; | ||
| } | ||
| if (error instanceof Error) { | ||
| throw new GriptapeAPIError(error.message); | ||
| } | ||
| throw new GriptapeAPIError('Unknown error'); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.