-
Notifications
You must be signed in to change notification settings - Fork 280
feat: add Apify plugin #326
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f797a05
feat: add Apify plugin
huamanraj c77dcf2
test: wire Apify into demo testing
huamanraj 9c96465
fix: address Apify review feedback
huamanraj 9271f9b
Merge branch 'main' into feat/apify-plugin
ambikeesshh 71cbfb1
revert(apify): drop demo/testing wiring out of plugin scope
ambikeesshh 214c95e
fix(apify): correct browserInfoDelete risk metadata
ambikeesshh 978a978
fix(apify): export EndpointInputSchemas/EndpointOutputSchemas
ambikeesshh cef9bec
test(apify): add jest wiring and operation suite
ambikeesshh 88bc8fb
fix(apify): sync pnpm-lock with main after merge
ambikeesshh c28c9bf
fix(apify): nest charge/resurrect ops under actorRun
ambikeesshh 39ab998
test(apify): exercise endpoint invocation and request building
ambikeesshh 600181a
test(apify): cover endpoint invocation across all namespaces
ambikeesshh ba8b6b5
merge(main): sync apify plugin branch with upstream main
ambikeesshh 71ec835
fix(apify): guard logging failures and keep DEFAULT error handler last
ambikeesshh fe5a81f
fix(apify): repair pnpm-lock after main merge (ts-jest importer)
ambikeesshh d28ca3d
Merge branch 'main' into feat/apify-plugin
ambikeesshh a106074
fix(apify): trim ops to OSS listing (113) and drop comments
ambikeesshh 8021afe
fix(apify): restore 429 retries and document unknown types
ambikeesshh 590d1a1
Merge main into feat/apify-plugin
Dhirenderchoudhary 57cb810
merge(main): keep apify MCP and add REST ops
ambikeesshh 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const ApifyOperationInputSchema = z | ||
| .object({ | ||
| // Apify request bodies differ per endpoint and are passed through as-is. | ||
| body: z.unknown().optional(), | ||
| // Query values are endpoint-specific and not shared across operations. | ||
| query: z.record(z.string(), z.unknown()).optional(), | ||
| // Custom headers are forwarded without a fixed provider-wide shape. | ||
| headers: z.record(z.string(), z.unknown()).optional(), | ||
| contentType: z.string().optional(), | ||
| mediaType: z.string().optional(), | ||
| }) | ||
| .loose(); | ||
|
|
||
| // Apify responses are endpoint-specific and are not normalized here. | ||
| export const ApifyOperationOutputSchema = z.unknown(); | ||
|
|
||
| export type ApifyOperationInput = z.infer<typeof ApifyOperationInputSchema>; | ||
| export type ApifyOperationOutput = z.infer<typeof ApifyOperationOutputSchema>; | ||
|
|
||
| export type ApifyEndpointInputs = Record<string, ApifyOperationInput>; | ||
| export type ApifyEndpointOutputs = Record<string, ApifyOperationOutput>; | ||
|
|
||
| export const EndpointInputSchemas = ApifyOperationInputSchema; | ||
| export const EndpointOutputSchemas = ApifyOperationOutputSchema; |
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,146 @@ | ||
| import type { | ||
| CorsairEndpoint, | ||
| EndpointMetaEntry, | ||
| RequiredPluginEndpointMeta, | ||
| RequiredPluginEndpointSchemas, | ||
| } from 'corsair/core'; | ||
| import { logEventFromContext } from 'corsair/core'; | ||
| import { z } from 'zod'; | ||
| import type { ApifyMcpContext } from '../index'; | ||
| import { makeApifyRequest } from '../rest-client'; | ||
| import type { | ||
| ApifyOperationDefinition, | ||
| ApifyOperationTree, | ||
| } from './operations'; | ||
| import { apifyOperations } from './operations'; | ||
| import type { ApifyOperationInput, ApifyOperationOutput } from './rest-types'; | ||
| import { ApifyOperationOutputSchema } from './rest-types'; | ||
|
|
||
| type ApifyEndpoint = CorsairEndpoint< | ||
| ApifyMcpContext, | ||
| ApifyOperationInput, | ||
| ApifyOperationOutput | ||
| >; | ||
|
|
||
| export type ApifyEndpointTree<T extends ApifyOperationTree> = { | ||
| [K in keyof T]: T[K] extends ApifyOperationDefinition | ||
| ? ApifyEndpoint | ||
| : T[K] extends ApifyOperationTree | ||
| ? ApifyEndpointTree<T[K]> | ||
| : never; | ||
| }; | ||
|
|
||
| function isOperationDefinition( | ||
| value: ApifyOperationDefinition | ApifyOperationTree, | ||
| ): value is ApifyOperationDefinition { | ||
| return 'method' in value && 'path' in value; | ||
| } | ||
|
|
||
| function buildEndpointTree<T extends ApifyOperationTree>( | ||
| tree: T, | ||
| segments: string[] = [], | ||
| ): ApifyEndpointTree<T> { | ||
| // Accumulator holds mixed endpoint fns / nested trees before the final cast. | ||
| const endpoints: Record<string, unknown> = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(tree)) { | ||
| if (isOperationDefinition(value)) { | ||
| const operationPath = [...segments, key].join('.'); | ||
| endpoints[key] = async ( | ||
| ctx: ApifyMcpContext, | ||
| input: ApifyOperationInput, | ||
| ) => { | ||
| const response = await makeApifyRequest(value, ctx.key, input ?? {}); | ||
| try { | ||
| await logEventFromContext( | ||
| ctx, | ||
| `apify.${operationPath}`, | ||
| { | ||
| method: value.method, | ||
| path: value.path, | ||
| }, | ||
| 'completed', | ||
| ); | ||
| } catch {} | ||
| return response; | ||
| }; | ||
| } else { | ||
| endpoints[key] = buildEndpointTree(value, [...segments, key]); | ||
| } | ||
| } | ||
|
|
||
| return endpoints as ApifyEndpointTree<T>; | ||
| } | ||
|
|
||
| function createOperationInputSchema(operation: ApifyOperationDefinition) { | ||
| const shape: Record<string, z.ZodTypeAny> = { | ||
| // Apify request bodies differ per endpoint and are passed through as-is. | ||
| body: z.unknown().optional(), | ||
| // Query values are endpoint-specific and not shared across operations. | ||
| query: z.record(z.string(), z.unknown()).optional(), | ||
| // Custom headers are forwarded without a fixed provider-wide shape. | ||
| headers: z.record(z.string(), z.unknown()).optional(), | ||
| contentType: z.string().optional(), | ||
| mediaType: z.string().optional(), | ||
| }; | ||
|
|
||
| for (const param of operation.pathParams) { | ||
| shape[param] = z.union([z.string(), z.number()]); | ||
| } | ||
|
|
||
| for (const param of operation.queryParams ?? []) { | ||
| // Query metadata names the param but does not constrain its value type. | ||
| shape[param] = z.unknown().optional(); | ||
| } | ||
|
|
||
| return z.object(shape).loose(); | ||
| } | ||
|
|
||
| export function buildApifyEndpointSchemas<T extends ApifyOperationTree>( | ||
| tree: T, | ||
| segments: string[] = [], | ||
| ): RequiredPluginEndpointSchemas<ApifyEndpointTree<T>> { | ||
| // Schema map is keyed by dotted path before the typed cast. | ||
| const schemas: Record<string, unknown> = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(tree)) { | ||
| if (isOperationDefinition(value)) { | ||
| schemas[[...segments, key].join('.')] = { | ||
| input: createOperationInputSchema(value), | ||
| output: ApifyOperationOutputSchema, | ||
| }; | ||
| } else { | ||
| Object.assign( | ||
| schemas, | ||
| buildApifyEndpointSchemas(value, [...segments, key]), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return schemas as RequiredPluginEndpointSchemas<ApifyEndpointTree<T>>; | ||
| } | ||
|
|
||
| export function buildApifyEndpointMeta<T extends ApifyOperationTree>( | ||
| tree: T, | ||
| segments: string[] = [], | ||
| ): RequiredPluginEndpointMeta<ApifyEndpointTree<T>> { | ||
| const meta: Record<string, EndpointMetaEntry> = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(tree)) { | ||
| if (isOperationDefinition(value)) { | ||
| meta[[...segments, key].join('.')] = { | ||
| riskLevel: value.riskLevel, | ||
| irreversible: value.irreversible, | ||
| description: value.description, | ||
| }; | ||
| } else { | ||
| Object.assign(meta, buildApifyEndpointMeta(value, [...segments, key])); | ||
| } | ||
| } | ||
|
|
||
| return meta as RequiredPluginEndpointMeta<ApifyEndpointTree<T>>; | ||
| } | ||
|
|
||
| export const ApifyRestEndpoints = buildEndpointTree(apifyOperations); | ||
|
|
||
| export * from './operations'; |
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
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.