## Problem `dispatchTask()` shells out to `openclaw gateway call sessions.patch` which times out when the gateway is busy, causing: 1. Notifications never fire (they're at the end of dispatchTask) 2. Worker state may not be recorded 3. Workers run silently ## Solution (3 changes) ### 1. Make `ensureSession` fire-and-forget Session key is deterministic, so we don't need to wait for confirmation. Health check catches orphaned state later. ### 2. Use runtime API for notifications instead of CLI Pass `runtime` through opts and use direct API calls: - `runtime.channel.telegram.sendMessageTelegram()` - `runtime.channel.whatsapp.sendMessageWhatsApp()` - etc. ### 3. Move notification before session dispatch Fire workerStart/workerComplete notifications early (after label transition) before the session calls that can timeout. ## Files Changed - lib/dispatch.ts — fire-and-forget ensureSession, early notification, accept runtime - lib/notify.ts — use runtime API for direct channel sends - lib/services/pipeline.ts — early notification, accept runtime - lib/services/tick.ts — pass runtime through to dispatchTask - lib/tool-helpers.ts — accept runtime in tickAndNotify - lib/tools/work-start.ts — pass api.runtime to dispatchTask - lib/tools/work-finish.ts — pass api.runtime to executeCompletion/tickAndNotify
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
/**
|
|
* tool-helpers.ts — Shared resolution helpers for tool execute() functions.
|
|
*
|
|
* Eliminates repeated boilerplate across tools: workspace validation,
|
|
* project resolution, provider creation.
|
|
*/
|
|
import type { OpenClawPluginApi, PluginRuntime } from "openclaw/plugin-sdk";
|
|
import type { ToolContext } from "./types.js";
|
|
import { readProjects, getProject, type Project, type ProjectsData } from "./projects.js";
|
|
import { createProvider, type ProviderWithType } from "./providers/index.js";
|
|
import { projectTick, type TickAction } from "./services/tick.js";
|
|
|
|
/**
|
|
* Require workspaceDir from context or throw a clear error.
|
|
*/
|
|
export function requireWorkspaceDir(ctx: ToolContext): string {
|
|
if (!ctx.workspaceDir) {
|
|
throw new Error("No workspace directory available in tool context");
|
|
}
|
|
return ctx.workspaceDir;
|
|
}
|
|
|
|
/**
|
|
* Resolve project by groupId, throw if not found.
|
|
*/
|
|
export async function resolveProject(
|
|
workspaceDir: string,
|
|
groupId: string,
|
|
): Promise<{ data: ProjectsData; project: Project }> {
|
|
const data = await readProjects(workspaceDir);
|
|
const project = getProject(data, groupId);
|
|
if (!project) {
|
|
throw new Error(`Project not found for groupId ${groupId}. Run project_register first.`);
|
|
}
|
|
return { data, project };
|
|
}
|
|
|
|
/**
|
|
* Create an issue provider for a project.
|
|
*/
|
|
export async function resolveProvider(project: Project): Promise<ProviderWithType> {
|
|
return createProvider({ repo: project.repo });
|
|
}
|
|
|
|
/**
|
|
* Get plugin config as a typed record (or undefined).
|
|
*/
|
|
export function getPluginConfig(api: OpenClawPluginApi): Record<string, unknown> | undefined {
|
|
return api.pluginConfig as Record<string, unknown> | undefined;
|
|
}
|
|
|
|
/**
|
|
* Run projectTick (non-fatal). Notifications are now handled by dispatchTask.
|
|
* Returns the pickups array (empty on failure).
|
|
*/
|
|
export async function tickAndNotify(opts: {
|
|
workspaceDir: string;
|
|
groupId: string;
|
|
agentId?: string;
|
|
pluginConfig?: Record<string, unknown>;
|
|
sessionKey?: string;
|
|
targetRole?: "dev" | "qa";
|
|
/** Plugin runtime for direct API access (avoids CLI subprocess timeouts) */
|
|
runtime?: PluginRuntime;
|
|
}): Promise<TickAction[]> {
|
|
try {
|
|
const result = await projectTick({
|
|
workspaceDir: opts.workspaceDir,
|
|
groupId: opts.groupId,
|
|
agentId: opts.agentId,
|
|
pluginConfig: opts.pluginConfig,
|
|
sessionKey: opts.sessionKey,
|
|
targetRole: opts.targetRole,
|
|
runtime: opts.runtime,
|
|
});
|
|
return result.pickups;
|
|
} catch {
|
|
/* non-fatal: tick failure shouldn't break the caller */
|
|
return [];
|
|
}
|
|
}
|