import type { ActivateContext, CommandTaskStatus } from "@idea/plugin-sdk"; import { STORAGE_KEYS } from "../constants.js"; export type HelloCommandFeedback = | { status: "skipped"; reason: "services-unavailable" | "no-focused-project" | "missing-owner-agent"; message: string; } | { status: "launched"; taskId: string; state: CommandTaskStatus["state"]; message: string; }; export async function useWorkspaceSdk(ctx: ActivateContext): Promise { const workspace = ctx.services?.workspace; if (!workspace) return; const project = await workspace.getCurrentProject(); if (!project) { ctx.logger.info("workspace service available without a focused project"); return; } const listing = await workspace.listDirectory(".ideai", project.id); const structure = await workspace.queryStructure({ projectId: project.id, maxDepth: 2, maxEntries: 100 }); ctx.logger.info("workspace project inspection complete", { projectId: project.id, ideaiEntries: listing.entries.length, conventions: structure.conventions.map((convention) => convention.id) }); const diagnostic = await ctx.services?.tooling.diagnose({ projectId: project.id, tools: [ { id: "echo", executable: "echo", versionArgs: ["hello-plugin-toolcheck"], required: true } ], env: [{ name: "PATH", required: true }], files: [{ path: "idea-plugin.json", kind: "file" }] }); ctx.logger.info("tooling diagnostic complete", { ok: diagnostic?.ok, echoVersion: diagnostic?.tools.find((tool) => tool.id === "echo")?.version, messages: diagnostic?.messages }); try { const watch = await workspace.watch(".ideai", (event) => { ctx.logger.info("workspace watch event", { path: event.path, kind: event.kind, operation: event.operation }); }, project.id); ctx.subscriptions.push(watch); } catch (error) { ctx.logger.info("workspace watch unavailable", { error }); } const events = await ctx.services?.events.subscribe( { projectId: project.id, eventTypes: ["backgroundTaskChanged"], pollIntervalMs: 2000, onDropped: (count) => ctx.logger.warn("plugin events dropped", { count }) }, (event) => { if (event.type === "backgroundTaskChanged") { ctx.logger.info("background task changed", { taskId: event.taskId, state: event.state }); } } ); if (events) ctx.subscriptions.push(events); } export async function runHelloCommandTask(ctx: ActivateContext): Promise { if (!ctx.services) { const feedback = { status: "skipped", reason: "services-unavailable", message: "The tooling service facade is unavailable for hello-plugin." } as const; ctx.logger.info("hello command skipped", feedback); return feedback; } const project = await ctx.services.workspace.getCurrentProject(); if (!project) { const feedback = { status: "skipped", reason: "no-focused-project", message: "Open a project before launching the hello-plugin task." } as const; ctx.logger.info("hello command skipped", feedback); return feedback; } const ownerAgentId = await ctx.storage?.get(STORAGE_KEYS.ownerAgentId); if (!ownerAgentId) { const feedback = { status: "skipped", reason: "missing-owner-agent", message: "Configure helloPlugin.ownerAgentId in plugin storage before launching the task." } as const; ctx.logger.info("hello command skipped", feedback); return feedback; } const pluginRoot = ctx.pluginRoot.replace(/[\\/]+$/, ""); const task = await ctx.services.tasks.runCommand({ projectId: project.id, ownerAgentId, label: "Hello plugin command", command: "node", args: [`${pluginRoot}/scripts/hello-task.mjs`], cwd: ".", recordOnly: true }); const status = await ctx.services.tasks.getCommandStatus(task.taskId); const feedback = { status: "launched", taskId: task.taskId, state: status?.state ?? task.state, message: "Started the hello-plugin background command." } as const; ctx.logger.info("hello command task launched", { ...feedback, exitCode: status?.exitCode ?? task.exitCode }); return feedback; }