-
Notifications
You must be signed in to change notification settings - Fork 137
refactor: extract shared core logic into src/tools/core/ modules #466
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
jirispilka
merged 1 commit into
feat/tool-mode-separation-plan
from
feat/tool-mode-core-extraction
Feb 26, 2026
Merged
Changes from all commits
Commits
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
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,130 @@ | ||
| import type { ActorCallOptions, ActorRun } from 'apify-client'; | ||
|
|
||
| import log from '@apify/log'; | ||
|
|
||
| import type { ApifyClient } from '../../apify-client.js'; | ||
| import { TOOL_MAX_OUTPUT_CHARS } from '../../const.js'; | ||
| import type { ActorDefinitionStorage, DatasetItem } from '../../types.js'; | ||
| import { ensureOutputWithinCharLimit, getActorDefinitionStorageFieldNames } from '../../utils/actor.js'; | ||
| import { logHttpError, redactSkyfirePayId } from '../../utils/logging.js'; | ||
| import type { ProgressTracker } from '../../utils/progress.js'; | ||
| import type { JsonSchemaProperty } from '../../utils/schema-generation.js'; | ||
| import { generateSchemaFromItems } from '../../utils/schema-generation.js'; | ||
|
|
||
| // Define a named return type for callActorGetDataset | ||
| export type CallActorGetDatasetResult = { | ||
| runId: string; | ||
| datasetId: string; | ||
| itemCount: number; | ||
| schema: JsonSchemaProperty; | ||
| previewItems: DatasetItem[]; | ||
| usageTotalUsd?: number; | ||
| usageUsd?: Record<string, number>; | ||
| }; | ||
|
|
||
| /** | ||
| * Calls an Apify Actor and retrieves metadata about the dataset results. | ||
| * | ||
| * This function executes an Actor and returns summary information instead with a result items preview of the full dataset | ||
| * to prevent overwhelming responses. The actual data can be retrieved using the get-actor-output tool. | ||
| * | ||
| * It requires the `APIFY_TOKEN` environment variable to be set. | ||
| * If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset. | ||
| * | ||
| * @param {string} actorName - The name of the Actor to call. | ||
| * @param {unknown} input - The input to pass to the actor. | ||
| * @param {ApifyClient} apifyClient - The Apify client to use for authentication. | ||
| * @returns {Promise<CallActorGetDatasetResult | null>} - A promise that resolves to an object containing the actor run and dataset items. | ||
| * @throws {Error} - Throws an error if the `APIFY_TOKEN` is not set | ||
| */ | ||
| export async function callActorGetDataset(options: { | ||
| actorName: string; | ||
| input: unknown; | ||
| apifyClient: ApifyClient; | ||
| callOptions?: ActorCallOptions; | ||
| progressTracker?: ProgressTracker | null; | ||
| abortSignal?: AbortSignal; | ||
| previewOutput?: boolean; | ||
| mcpSessionId?: string; | ||
| }): Promise<CallActorGetDatasetResult | null> { | ||
| const { | ||
| actorName, | ||
| input, | ||
| apifyClient, | ||
| callOptions, | ||
| progressTracker, | ||
| abortSignal, | ||
| previewOutput = true, | ||
| mcpSessionId, | ||
| } = options; | ||
| const CLIENT_ABORT = Symbol('CLIENT_ABORT'); // Just internal symbol to identify client abort | ||
| const actorClient = apifyClient.actor(actorName); | ||
|
|
||
| // Start the actor run | ||
| const actorRun: ActorRun = await actorClient.start(input, callOptions); | ||
|
|
||
| // Start progress tracking if tracker is provided | ||
| if (progressTracker) { | ||
| progressTracker.startActorRunUpdates(actorRun.id, apifyClient, actorName); | ||
| } | ||
|
|
||
| // Create abort promise that handles both API abort and race rejection | ||
| const abortPromise = async () => new Promise<typeof CLIENT_ABORT>((resolve) => { | ||
| abortSignal?.addEventListener('abort', async () => { | ||
| // Abort the actor run via API | ||
| try { | ||
| await apifyClient.run(actorRun.id).abort({ gracefully: false }); | ||
| } catch (e) { | ||
| logHttpError(e, 'Error aborting Actor run', { runId: actorRun.id }); | ||
| } | ||
| // Reject to stop waiting | ||
| resolve(CLIENT_ABORT); | ||
| }, { once: true }); | ||
| }); | ||
|
|
||
| // Wait for completion or cancellation | ||
| const potentialAbortedRun = await Promise.race([ | ||
| apifyClient.run(actorRun.id).waitForFinish(), | ||
| ...(abortSignal ? [abortPromise()] : []), | ||
| ]); | ||
|
|
||
| if (potentialAbortedRun === CLIENT_ABORT) { | ||
| log.info('Actor run aborted by client', { actorName, mcpSessionId, input: redactSkyfirePayId(input) }); | ||
| return null; | ||
| } | ||
| const completedRun = potentialAbortedRun as ActorRun; | ||
|
|
||
| // Process the completed run | ||
| const dataset = apifyClient.dataset(completedRun.defaultDatasetId); | ||
| const [datasetItems, defaultBuild] = await Promise.all([ | ||
| dataset.listItems(), | ||
| (await actorClient.defaultBuild()).get(), | ||
| ]); | ||
|
|
||
| // Generate schema using the shared utility | ||
| const generatedSchema = generateSchemaFromItems(datasetItems.items, { | ||
| clean: true, | ||
| arrayMode: 'all', | ||
| }); | ||
| const schema = generatedSchema || { type: 'object', properties: {} }; | ||
|
|
||
| /** | ||
| * Get important fields that are using in any dataset view as they MAY be used in filtering to ensure the output fits | ||
| * the tool output limits. Client has to use the get-actor-output tool to retrieve the full dataset or filtered out fields. | ||
| */ | ||
| const storageDefinition = defaultBuild?.actorDefinition?.storages?.dataset as ActorDefinitionStorage | undefined; | ||
| const importantProperties = getActorDefinitionStorageFieldNames(storageDefinition || {}); | ||
| const previewItems = previewOutput | ||
| ? ensureOutputWithinCharLimit(datasetItems.items, importantProperties, TOOL_MAX_OUTPUT_CHARS) | ||
| : []; | ||
|
|
||
| return { | ||
| runId: actorRun.id, | ||
| datasetId: completedRun.defaultDatasetId, | ||
| itemCount: datasetItems.count, | ||
| schema, | ||
| previewItems, | ||
| usageTotalUsd: completedRun.usageTotalUsd, | ||
| usageUsd: completedRun.usageUsd as Record<string, number> | undefined, | ||
| }; | ||
| } |
4 changes: 2 additions & 2 deletions
4
src/utils/actor-response.ts → src/tools/core/actor-response.ts
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.
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.