-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
86 lines (78 loc) · 3.22 KB
/
Copy pathindex.ts
File metadata and controls
86 lines (78 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
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";
/**
* Apify Pi Plugin - Universal Apify Actor integration for the Pi agent.
* Exposes a single 'apify' tool with discover/start/collect primitives
* that wraps all 20,000+ Actors on the Apify Store.
*/
export default function (pi: ExtensionAPI) {
// Register the universal apify tool
pi.registerTool(apifyTool);
// Register the /apify command with subcommands
pi.registerCommand("apify", {
description: "Apify Actor integration - manage API key and test connectivity",
handler: async (args, ctx) => {
const parts = args.trim().split(/\s+/);
const subcommand = parts[0] || "";
const subArgs = parts.slice(1).join(" ");
switch (subcommand) {
case "login":
return apifyLoginCommand(ctx);
case "status":
return apifyStatusCommand(subArgs, ctx);
case "test":
return apifyTestCommand(subArgs, ctx);
case "":
case "help":
ctx.ui.notify("Apify integration commands:");
ctx.ui.notify(" /apify login - Configure your Apify API key");
ctx.ui.notify(" /apify status - Check authentication and configuration");
ctx.ui.notify(" /apify test - Test connectivity and run a simple Actor");
ctx.ui.notify("");
ctx.ui.notify("To use Apify Actors in your prompts, use the 'apify' tool with actions:");
ctx.ui.notify(" - discover: Search for Actors or get an Actor's schema");
ctx.ui.notify(" - start: Launch an Actor run");
ctx.ui.notify(" - collect: Poll runs and fetch results");
break;
default:
ctx.ui.notify(`Unknown subcommand: ${subcommand}`);
ctx.ui.notify("Usage: /apify login | status | test | help");
}
},
});
// Status check on session start
pi.on("session_start", async (_event, ctx) => {
try {
const config = loadConfig();
// If explicitly disabled, stay silent
if (config.enabled === false) {
return;
}
// Check if API key is configured
const apiKey = resolveApiKey(config);
if (!apiKey) {
ctx.ui.notify("Apify: not configured. Run /apify login to set up your API key.");
return;
}
// Try to verify the key
const client = createClient(config);
if (!client) {
ctx.ui.notify("Apify: failed to initialize client. Check your configuration.");
return;
}
const result = await testConnectivity(client);
if (result.success) {
ctx.ui.notify(`Apify: authenticated as ${result.userId} (key ${fingerprintKey(apiKey)})`);
} else {
ctx.ui.notify("Apify: authentication failed. Run /apify login to reconfigure.");
}
} catch (error) {
// Silently ignore errors on session start to avoid spamming
console.error("Apify session_start error:", error);
}
});
}