Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ npm run check
npm test
```

## Project structure

The plugin is organized into purpose-named feature folders under `src/`, each
concern in its own module with a barrel `index.ts` where a folder groups several
files:

```
index.ts # extension entry point (registers tool, command, hook)
src/
tool.ts # the universal `apify` tool definition
actions/ # tool action layer
index.ts # barrel re-exporting the router + handlers
execute.ts # apifyExecute router (config gate → handler dispatch)
discover.ts # discover action (search / schema)
start.ts # start action (launch a run)
collect.ts # collect action (poll runs & fetch datasets)
commands/ # /apify slash-command handlers
index.ts # barrel re-exporting the three commands
login.ts # /apify login
status.ts # /apify status
test.ts # /apify test
security/ # input/output safety helpers
index.ts # barrel re-exporting normalize + wrap
normalize.ts # secret normalization, slug validation, fingerprint
wrap.ts # untrusted-content wrapping (prompt-injection defense)
utils/ # single-concern helpers, one flat file each
config.ts # config load / merge / resolve
client.ts # ApifyClient creation + connectivity check
constants.ts # shared constants (limits, markers, known actors)
types.ts # shared TypeScript interfaces
```

## License

ISC
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apifyTool } from "./src/tool.ts";
import { apifyLoginCommand, apifyStatusCommand, apifyTestCommand } from "./src/commands/index.ts";
import { loadConfig, resolveApiKey } from "./src/utils/config.ts";
import { createClient, testConnectivity } from "./src/utils/client.ts";
import { fingerprintKey } from "./src/utils/normalize.ts";
import { fingerprintKey } from "./src/security/index.ts";

/**
* Apify Pi Plugin - Universal Apify Actor integration for the Pi agent.
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"scripts": {
"check": "tsc --noEmit",
"test": "node test-structure.js",
"setup:pi": "bash scripts/setup.sh",
"cleanup:pi": "rm -rf .pi-install ~/.pi/agent/auth.json ~/.pi/agent/apify.json 2>/dev/null; true"
},
Expand Down
4 changes: 2 additions & 2 deletions src/actions/collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
import type { ApifyToolParams, CollectDetails } from "../types/index.ts";
import { wrapUntrustedContent } from "../utils/wrap.ts";
import type { ApifyToolParams, CollectDetails } from "../utils/types.ts";
import { wrapUntrustedContent } from "../security/index.ts";
import { MAX_RESULT_CHARS } from "../utils/constants.ts";

// Maximum number of items per dataset to prevent context overflow
Expand Down
4 changes: 2 additions & 2 deletions src/actions/discover.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
import type { ApifyToolParams, DiscoverDetails } from "../types/index.ts";
import { validateSlug } from "../utils/normalize.ts";
import type { ApifyToolParams, DiscoverDetails } from "../utils/types.ts";
import { validateSlug } from "../security/index.ts";

/**
* Handle discover action (search or schema mode).
Expand Down
77 changes: 77 additions & 0 deletions src/actions/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { AgentToolResult, AgentToolUpdateCallback, ExtensionContext } from "@earendil-works/pi-coding-agent";
import type { ApifyToolParams, ApifyToolDetails, DiscoverDetails } from "../utils/types.ts";
import { createClient } from "../utils/client.ts";
import { loadConfig, resolveApiKey } from "../utils/config.ts";
import { handleDiscover } from "./discover.ts";
import { handleStart } from "./start.ts";
import { handleCollect } from "./collect.ts";

/**
* Main execute function for the apify tool.
*/
export async function apifyExecute(
_toolCallId: string,
params: ApifyToolParams,
_signal: AbortSignal | undefined,
_onUpdate: AgentToolUpdateCallback<ApifyToolDetails> | undefined,
ctx: ExtensionContext
): Promise<AgentToolResult<ApifyToolDetails>> {
// Load config
const config = loadConfig();

// Check if enabled
if (config.enabled === false) {
return {
content: [{ type: "text", text: "Apify integration is disabled. Enable it in config or run /apify login." }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Check enabledTools
if (config.enabledTools && !config.enabledTools.includes(params.action)) {
return {
content: [{ type: "text", text: `Action "${params.action}" is not enabled. Enabled actions: ${config.enabledTools.join(", ")}` }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Resolve API key
const apiKey = resolveApiKey(config);
if (!apiKey) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: "missing_credential",
message: "No Apify API key configured. Run /apify login or set APIFY_API_KEY env var.",
docs: "https://docs.apify.com/api-reference/v2"
}, null, 2)
}],
details: { mode: "search" } as DiscoverDetails,
};
}

// Create client
const client = createClient(config, apiKey);
if (!client) {
return {
content: [{ type: "text", text: "Failed to create Apify client" }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Route to appropriate handler
switch (params.action) {
case "discover":
return handleDiscover(client, params, config);
case "start":
return handleStart(client, params, config);
case "collect":
return handleCollect(client, params, config);
default:
return {
content: [{ type: "text", text: `Unknown action: ${params.action}` }],
details: { mode: "search" } as DiscoverDetails,
};
}
}
81 changes: 4 additions & 77 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,4 @@
import type { AgentToolResult, AgentToolUpdateCallback, ExtensionContext } from "@earendil-works/pi-coding-agent";
import type { ApifyToolParams, ApifyToolDetails, DiscoverDetails } from "../types/index.ts";
import { createClient } from "../utils/client.ts";
import { loadConfig, resolveApiKey } from "../utils/config.ts";
import { handleDiscover } from "./discover.ts";
import { handleStart } from "./start.ts";
import { handleCollect } from "./collect.ts";

/**
* Main execute function for the apify tool.
*/
export async function apifyExecute(
_toolCallId: string,
params: ApifyToolParams,
_signal: AbortSignal | undefined,
_onUpdate: AgentToolUpdateCallback<ApifyToolDetails> | undefined,
ctx: ExtensionContext
): Promise<AgentToolResult<ApifyToolDetails>> {
// Load config
const config = loadConfig();

// Check if enabled
if (config.enabled === false) {
return {
content: [{ type: "text", text: "Apify integration is disabled. Enable it in config or run /apify login." }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Check enabledTools
if (config.enabledTools && !config.enabledTools.includes(params.action)) {
return {
content: [{ type: "text", text: `Action "${params.action}" is not enabled. Enabled actions: ${config.enabledTools.join(", ")}` }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Resolve API key
const apiKey = resolveApiKey(config);
if (!apiKey) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: "missing_credential",
message: "No Apify API key configured. Run /apify login or set APIFY_API_KEY env var.",
docs: "https://docs.apify.com/api-reference/v2"
}, null, 2)
}],
details: { mode: "search" } as DiscoverDetails,
};
}

// Create client
const client = createClient(config, apiKey);
if (!client) {
return {
content: [{ type: "text", text: "Failed to create Apify client" }],
details: { mode: "search" } as DiscoverDetails,
};
}

// Route to appropriate handler
switch (params.action) {
case "discover":
return handleDiscover(client, params, config);
case "start":
return handleStart(client, params, config);
case "collect":
return handleCollect(client, params, config);
default:
return {
content: [{ type: "text", text: `Unknown action: ${params.action}` }],
details: { mode: "search" } as DiscoverDetails,
};
}
}
export * from "./execute.ts";
export * from "./discover.ts";
export * from "./start.ts";
export * from "./collect.ts";
4 changes: 2 additions & 2 deletions src/actions/start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
import type { ApifyToolParams, StartDetails } from "../types/index.ts";
import { validateSlug } from "../utils/normalize.ts";
import type { ApifyToolParams, StartDetails } from "../utils/types.ts";
import { validateSlug } from "../security/index.ts";

/**
* Handle start action (launch actor run).
Expand Down
Loading