refactor(core): split index.ts and config-handler.ts into focused modules
Main entry point: - create-hooks.ts, create-tools.ts, create-managers.ts - plugin-interface.ts: plugin interface types - plugin/ directory: plugin lifecycle modules Config handler: - agent-config-handler.ts, command-config-handler.ts - tool-config-handler.ts, mcp-config-handler.ts - provider-config-handler.ts, category-config-resolver.ts - agent-priority-order.ts, prometheus-agent-config-builder.ts - plugin-components-loader.ts
This commit is contained in:
61
src/create-hooks.ts
Normal file
61
src/create-hooks.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { AvailableSkill } from "./agents/dynamic-agent-prompt-builder"
|
||||
import type { HookName, OhMyOpenCodeConfig } from "./config"
|
||||
import type { LoadedSkill } from "./features/opencode-skill-loader/types"
|
||||
import type { BackgroundManager } from "./features/background-agent"
|
||||
import type { PluginContext } from "./plugin/types"
|
||||
|
||||
import { createCoreHooks } from "./plugin/hooks/create-core-hooks"
|
||||
import { createContinuationHooks } from "./plugin/hooks/create-continuation-hooks"
|
||||
import { createSkillHooks } from "./plugin/hooks/create-skill-hooks"
|
||||
|
||||
export type CreatedHooks = ReturnType<typeof createHooks>
|
||||
|
||||
export function createHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
backgroundManager: BackgroundManager
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
mergedSkills: LoadedSkill[]
|
||||
availableSkills: AvailableSkill[]
|
||||
}) {
|
||||
const {
|
||||
ctx,
|
||||
pluginConfig,
|
||||
backgroundManager,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
mergedSkills,
|
||||
availableSkills,
|
||||
} = args
|
||||
|
||||
const core = createCoreHooks({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
})
|
||||
|
||||
const continuation = createContinuationHooks({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
backgroundManager,
|
||||
sessionRecovery: core.sessionRecovery,
|
||||
})
|
||||
|
||||
const skill = createSkillHooks({
|
||||
ctx,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
mergedSkills,
|
||||
availableSkills,
|
||||
})
|
||||
|
||||
return {
|
||||
...core,
|
||||
...continuation,
|
||||
...skill,
|
||||
}
|
||||
}
|
||||
79
src/create-managers.ts
Normal file
79
src/create-managers.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { OhMyOpenCodeConfig } from "./config"
|
||||
import type { ModelCacheState } from "./plugin-state"
|
||||
import type { PluginContext, TmuxConfig } from "./plugin/types"
|
||||
|
||||
import type { SubagentSessionCreatedEvent } from "./features/background-agent"
|
||||
import { BackgroundManager } from "./features/background-agent"
|
||||
import { SkillMcpManager } from "./features/skill-mcp-manager"
|
||||
import { initTaskToastManager } from "./features/task-toast-manager"
|
||||
import { TmuxSessionManager } from "./features/tmux-subagent"
|
||||
import { createConfigHandler } from "./plugin-handlers"
|
||||
import { log } from "./shared"
|
||||
|
||||
export type Managers = {
|
||||
tmuxSessionManager: TmuxSessionManager
|
||||
backgroundManager: BackgroundManager
|
||||
skillMcpManager: SkillMcpManager
|
||||
configHandler: ReturnType<typeof createConfigHandler>
|
||||
}
|
||||
|
||||
export function createManagers(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
tmuxConfig: TmuxConfig
|
||||
modelCacheState: ModelCacheState
|
||||
}): Managers {
|
||||
const { ctx, pluginConfig, tmuxConfig, modelCacheState } = args
|
||||
|
||||
const tmuxSessionManager = new TmuxSessionManager(ctx, tmuxConfig)
|
||||
|
||||
const backgroundManager = new BackgroundManager(
|
||||
ctx,
|
||||
pluginConfig.background_task,
|
||||
{
|
||||
tmuxConfig,
|
||||
onSubagentSessionCreated: async (event: SubagentSessionCreatedEvent) => {
|
||||
log("[index] onSubagentSessionCreated callback received", {
|
||||
sessionID: event.sessionID,
|
||||
parentID: event.parentID,
|
||||
title: event.title,
|
||||
})
|
||||
|
||||
await tmuxSessionManager.onSessionCreated({
|
||||
type: "session.created",
|
||||
properties: {
|
||||
info: {
|
||||
id: event.sessionID,
|
||||
parentID: event.parentID,
|
||||
title: event.title,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
log("[index] onSubagentSessionCreated callback completed")
|
||||
},
|
||||
onShutdown: () => {
|
||||
tmuxSessionManager.cleanup().catch((error) => {
|
||||
log("[index] tmux cleanup error during shutdown:", error)
|
||||
})
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
initTaskToastManager(ctx.client)
|
||||
|
||||
const skillMcpManager = new SkillMcpManager()
|
||||
|
||||
const configHandler = createConfigHandler({
|
||||
ctx: { directory: ctx.directory, client: ctx.client },
|
||||
pluginConfig,
|
||||
modelCacheState,
|
||||
})
|
||||
|
||||
return {
|
||||
tmuxSessionManager,
|
||||
backgroundManager,
|
||||
skillMcpManager,
|
||||
configHandler,
|
||||
}
|
||||
}
|
||||
53
src/create-tools.ts
Normal file
53
src/create-tools.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { AvailableCategory, AvailableSkill } from "./agents/dynamic-agent-prompt-builder"
|
||||
import type { OhMyOpenCodeConfig } from "./config"
|
||||
import type { BrowserAutomationProvider } from "./config/schema/browser-automation"
|
||||
import type { LoadedSkill } from "./features/opencode-skill-loader/types"
|
||||
import type { PluginContext, ToolsRecord } from "./plugin/types"
|
||||
import type { Managers } from "./create-managers"
|
||||
|
||||
import { createAvailableCategories } from "./plugin/available-categories"
|
||||
import { createSkillContext } from "./plugin/skill-context"
|
||||
import { createToolRegistry } from "./plugin/tool-registry"
|
||||
|
||||
export type CreateToolsResult = {
|
||||
filteredTools: ToolsRecord
|
||||
mergedSkills: LoadedSkill[]
|
||||
availableSkills: AvailableSkill[]
|
||||
availableCategories: AvailableCategory[]
|
||||
browserProvider: BrowserAutomationProvider
|
||||
disabledSkills: Set<string>
|
||||
taskSystemEnabled: boolean
|
||||
}
|
||||
|
||||
export async function createTools(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
managers: Pick<Managers, "backgroundManager" | "tmuxSessionManager" | "skillMcpManager">
|
||||
}): Promise<CreateToolsResult> {
|
||||
const { ctx, pluginConfig, managers } = args
|
||||
|
||||
const skillContext = await createSkillContext({
|
||||
directory: ctx.directory,
|
||||
pluginConfig,
|
||||
})
|
||||
|
||||
const availableCategories = createAvailableCategories(pluginConfig)
|
||||
|
||||
const { filteredTools, taskSystemEnabled } = createToolRegistry({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
managers,
|
||||
skillContext,
|
||||
availableCategories,
|
||||
})
|
||||
|
||||
return {
|
||||
filteredTools,
|
||||
mergedSkills: skillContext.mergedSkills,
|
||||
availableSkills: skillContext.availableSkills,
|
||||
availableCategories,
|
||||
browserProvider: skillContext.browserProvider,
|
||||
disabledSkills: skillContext.disabledSkills,
|
||||
taskSystemEnabled,
|
||||
}
|
||||
}
|
||||
996
src/index.ts
996
src/index.ts
File diff suppressed because it is too large
Load Diff
188
src/plugin-handlers/agent-config-handler.ts
Normal file
188
src/plugin-handlers/agent-config-handler.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
import { createBuiltinAgents } from "../agents";
|
||||
import { createSisyphusJuniorAgentWithOverrides } from "../agents/sisyphus-junior";
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import { log, migrateAgentConfig } from "../shared";
|
||||
import { AGENT_NAME_MAP } from "../shared/migration";
|
||||
import {
|
||||
discoverOpencodeGlobalSkills,
|
||||
discoverOpencodeProjectSkills,
|
||||
discoverProjectClaudeSkills,
|
||||
discoverUserClaudeSkills,
|
||||
} from "../features/opencode-skill-loader";
|
||||
import { loadProjectAgents, loadUserAgents } from "../features/claude-code-agent-loader";
|
||||
import type { PluginComponents } from "./plugin-components-loader";
|
||||
import { reorderAgentsByPriority } from "./agent-priority-order";
|
||||
import { buildPrometheusAgentConfig } from "./prometheus-agent-config-builder";
|
||||
|
||||
type AgentConfigRecord = Record<string, Record<string, unknown> | undefined> & {
|
||||
build?: Record<string, unknown>;
|
||||
plan?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export async function applyAgentConfig(params: {
|
||||
config: Record<string, unknown>;
|
||||
pluginConfig: OhMyOpenCodeConfig;
|
||||
ctx: { directory: string; client?: any };
|
||||
pluginComponents: PluginComponents;
|
||||
}): Promise<Record<string, unknown>> {
|
||||
const migratedDisabledAgents = (params.pluginConfig.disabled_agents ?? []).map(
|
||||
(agent) => {
|
||||
return AGENT_NAME_MAP[agent.toLowerCase()] ?? AGENT_NAME_MAP[agent] ?? agent;
|
||||
},
|
||||
) as typeof params.pluginConfig.disabled_agents;
|
||||
|
||||
const includeClaudeSkillsForAwareness = params.pluginConfig.claude_code?.skills ?? true;
|
||||
const [
|
||||
discoveredUserSkills,
|
||||
discoveredProjectSkills,
|
||||
discoveredOpencodeGlobalSkills,
|
||||
discoveredOpencodeProjectSkills,
|
||||
] = await Promise.all([
|
||||
includeClaudeSkillsForAwareness ? discoverUserClaudeSkills() : Promise.resolve([]),
|
||||
includeClaudeSkillsForAwareness
|
||||
? discoverProjectClaudeSkills()
|
||||
: Promise.resolve([]),
|
||||
discoverOpencodeGlobalSkills(),
|
||||
discoverOpencodeProjectSkills(),
|
||||
]);
|
||||
|
||||
const allDiscoveredSkills = [
|
||||
...discoveredOpencodeProjectSkills,
|
||||
...discoveredProjectSkills,
|
||||
...discoveredOpencodeGlobalSkills,
|
||||
...discoveredUserSkills,
|
||||
];
|
||||
|
||||
const browserProvider =
|
||||
params.pluginConfig.browser_automation_engine?.provider ?? "playwright";
|
||||
const currentModel = params.config.model as string | undefined;
|
||||
const disabledSkills = new Set<string>(params.pluginConfig.disabled_skills ?? []);
|
||||
|
||||
const builtinAgents = await createBuiltinAgents(
|
||||
migratedDisabledAgents,
|
||||
params.pluginConfig.agents,
|
||||
params.ctx.directory,
|
||||
undefined,
|
||||
params.pluginConfig.categories,
|
||||
params.pluginConfig.git_master,
|
||||
allDiscoveredSkills,
|
||||
params.ctx.client,
|
||||
browserProvider,
|
||||
currentModel,
|
||||
disabledSkills,
|
||||
);
|
||||
|
||||
const includeClaudeAgents = params.pluginConfig.claude_code?.agents ?? true;
|
||||
const userAgents = includeClaudeAgents ? loadUserAgents() : {};
|
||||
const projectAgents = includeClaudeAgents ? loadProjectAgents() : {};
|
||||
|
||||
const rawPluginAgents = params.pluginComponents.agents;
|
||||
const pluginAgents = Object.fromEntries(
|
||||
Object.entries(rawPluginAgents).map(([key, value]) => [
|
||||
key,
|
||||
value ? migrateAgentConfig(value as Record<string, unknown>) : value,
|
||||
]),
|
||||
);
|
||||
|
||||
const isSisyphusEnabled = params.pluginConfig.sisyphus_agent?.disabled !== true;
|
||||
const builderEnabled =
|
||||
params.pluginConfig.sisyphus_agent?.default_builder_enabled ?? false;
|
||||
const plannerEnabled = params.pluginConfig.sisyphus_agent?.planner_enabled ?? true;
|
||||
const replacePlan = params.pluginConfig.sisyphus_agent?.replace_plan ?? true;
|
||||
const shouldDemotePlan = plannerEnabled && replacePlan;
|
||||
|
||||
const configAgent = params.config.agent as AgentConfigRecord | undefined;
|
||||
|
||||
if (isSisyphusEnabled && builtinAgents.sisyphus) {
|
||||
(params.config as { default_agent?: string }).default_agent = "sisyphus";
|
||||
|
||||
const agentConfig: Record<string, unknown> = {
|
||||
sisyphus: builtinAgents.sisyphus,
|
||||
};
|
||||
|
||||
agentConfig["sisyphus-junior"] = createSisyphusJuniorAgentWithOverrides(
|
||||
params.pluginConfig.agents?.["sisyphus-junior"],
|
||||
undefined,
|
||||
);
|
||||
|
||||
if (builderEnabled) {
|
||||
const { name: _buildName, ...buildConfigWithoutName } =
|
||||
configAgent?.build ?? {};
|
||||
const migratedBuildConfig = migrateAgentConfig(
|
||||
buildConfigWithoutName as Record<string, unknown>,
|
||||
);
|
||||
const override = params.pluginConfig.agents?.["OpenCode-Builder"];
|
||||
const base = {
|
||||
...migratedBuildConfig,
|
||||
description: `${(configAgent?.build?.description as string) ?? "Build agent"} (OpenCode default)`,
|
||||
};
|
||||
agentConfig["OpenCode-Builder"] = override ? { ...base, ...override } : base;
|
||||
}
|
||||
|
||||
if (plannerEnabled) {
|
||||
const prometheusOverride = params.pluginConfig.agents?.["prometheus"] as
|
||||
| (Record<string, unknown> & { prompt_append?: string })
|
||||
| undefined;
|
||||
|
||||
agentConfig["prometheus"] = await buildPrometheusAgentConfig({
|
||||
configAgentPlan: configAgent?.plan,
|
||||
pluginPrometheusOverride: prometheusOverride,
|
||||
userCategories: params.pluginConfig.categories,
|
||||
currentModel,
|
||||
});
|
||||
}
|
||||
|
||||
const filteredConfigAgents = configAgent
|
||||
? Object.fromEntries(
|
||||
Object.entries(configAgent)
|
||||
.filter(([key]) => {
|
||||
if (key === "build") return false;
|
||||
if (key === "plan" && shouldDemotePlan) return false;
|
||||
if (key in builtinAgents) return false;
|
||||
return true;
|
||||
})
|
||||
.map(([key, value]) => [
|
||||
key,
|
||||
value ? migrateAgentConfig(value as Record<string, unknown>) : value,
|
||||
]),
|
||||
)
|
||||
: {};
|
||||
|
||||
const migratedBuild = configAgent?.build
|
||||
? migrateAgentConfig(configAgent.build as Record<string, unknown>)
|
||||
: {};
|
||||
|
||||
const planDemoteConfig = shouldDemotePlan ? { mode: "subagent" as const } : undefined;
|
||||
|
||||
params.config.agent = {
|
||||
...agentConfig,
|
||||
...Object.fromEntries(
|
||||
Object.entries(builtinAgents).filter(([key]) => key !== "sisyphus"),
|
||||
),
|
||||
...userAgents,
|
||||
...projectAgents,
|
||||
...pluginAgents,
|
||||
...filteredConfigAgents,
|
||||
build: { ...migratedBuild, mode: "subagent", hidden: true },
|
||||
...(planDemoteConfig ? { plan: planDemoteConfig } : {}),
|
||||
};
|
||||
} else {
|
||||
params.config.agent = {
|
||||
...builtinAgents,
|
||||
...userAgents,
|
||||
...projectAgents,
|
||||
...pluginAgents,
|
||||
...configAgent,
|
||||
};
|
||||
}
|
||||
|
||||
if (params.config.agent) {
|
||||
params.config.agent = reorderAgentsByPriority(
|
||||
params.config.agent as Record<string, unknown>,
|
||||
);
|
||||
}
|
||||
|
||||
const agentResult = params.config.agent as Record<string, unknown>;
|
||||
log("[config-handler] agents loaded", { agentKeys: Object.keys(agentResult) });
|
||||
return agentResult;
|
||||
}
|
||||
23
src/plugin-handlers/agent-priority-order.ts
Normal file
23
src/plugin-handlers/agent-priority-order.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
const CORE_AGENT_ORDER = ["sisyphus", "hephaestus", "prometheus", "atlas"] as const;
|
||||
|
||||
export function reorderAgentsByPriority(
|
||||
agents: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const ordered: Record<string, unknown> = {};
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const key of CORE_AGENT_ORDER) {
|
||||
if (Object.prototype.hasOwnProperty.call(agents, key)) {
|
||||
ordered[key] = agents[key];
|
||||
seen.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(agents)) {
|
||||
if (!seen.has(key)) {
|
||||
ordered[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return ordered;
|
||||
}
|
||||
9
src/plugin-handlers/category-config-resolver.ts
Normal file
9
src/plugin-handlers/category-config-resolver.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { CategoryConfig } from "../config/schema";
|
||||
import { DEFAULT_CATEGORIES } from "../tools/delegate-task/constants";
|
||||
|
||||
export function resolveCategoryConfig(
|
||||
categoryName: string,
|
||||
userCategories?: Record<string, CategoryConfig>,
|
||||
): CategoryConfig | undefined {
|
||||
return userCategories?.[categoryName] ?? DEFAULT_CATEGORIES[categoryName];
|
||||
}
|
||||
62
src/plugin-handlers/command-config-handler.ts
Normal file
62
src/plugin-handlers/command-config-handler.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import {
|
||||
loadUserCommands,
|
||||
loadProjectCommands,
|
||||
loadOpencodeGlobalCommands,
|
||||
loadOpencodeProjectCommands,
|
||||
} from "../features/claude-code-command-loader";
|
||||
import { loadBuiltinCommands } from "../features/builtin-commands";
|
||||
import {
|
||||
loadUserSkills,
|
||||
loadProjectSkills,
|
||||
loadOpencodeGlobalSkills,
|
||||
loadOpencodeProjectSkills,
|
||||
} from "../features/opencode-skill-loader";
|
||||
import type { PluginComponents } from "./plugin-components-loader";
|
||||
|
||||
export async function applyCommandConfig(params: {
|
||||
config: Record<string, unknown>;
|
||||
pluginConfig: OhMyOpenCodeConfig;
|
||||
pluginComponents: PluginComponents;
|
||||
}): Promise<void> {
|
||||
const builtinCommands = loadBuiltinCommands(params.pluginConfig.disabled_commands);
|
||||
const systemCommands = (params.config.command as Record<string, unknown>) ?? {};
|
||||
|
||||
const includeClaudeCommands = params.pluginConfig.claude_code?.commands ?? true;
|
||||
const includeClaudeSkills = params.pluginConfig.claude_code?.skills ?? true;
|
||||
|
||||
const [
|
||||
userCommands,
|
||||
projectCommands,
|
||||
opencodeGlobalCommands,
|
||||
opencodeProjectCommands,
|
||||
userSkills,
|
||||
projectSkills,
|
||||
opencodeGlobalSkills,
|
||||
opencodeProjectSkills,
|
||||
] = await Promise.all([
|
||||
includeClaudeCommands ? loadUserCommands() : Promise.resolve({}),
|
||||
includeClaudeCommands ? loadProjectCommands() : Promise.resolve({}),
|
||||
loadOpencodeGlobalCommands(),
|
||||
loadOpencodeProjectCommands(),
|
||||
includeClaudeSkills ? loadUserSkills() : Promise.resolve({}),
|
||||
includeClaudeSkills ? loadProjectSkills() : Promise.resolve({}),
|
||||
loadOpencodeGlobalSkills(),
|
||||
loadOpencodeProjectSkills(),
|
||||
]);
|
||||
|
||||
params.config.command = {
|
||||
...builtinCommands,
|
||||
...userCommands,
|
||||
...userSkills,
|
||||
...opencodeGlobalCommands,
|
||||
...opencodeGlobalSkills,
|
||||
...systemCommands,
|
||||
...projectCommands,
|
||||
...projectSkills,
|
||||
...opencodeProjectCommands,
|
||||
...opencodeProjectSkills,
|
||||
...params.pluginComponents.commands,
|
||||
...params.pluginComponents.skills,
|
||||
};
|
||||
}
|
||||
@@ -1,39 +1,14 @@
|
||||
import { createBuiltinAgents } from "../agents";
|
||||
import { createSisyphusJuniorAgentWithOverrides } from "../agents/sisyphus-junior";
|
||||
import {
|
||||
loadUserCommands,
|
||||
loadProjectCommands,
|
||||
loadOpencodeGlobalCommands,
|
||||
loadOpencodeProjectCommands,
|
||||
} from "../features/claude-code-command-loader";
|
||||
import { loadBuiltinCommands } from "../features/builtin-commands";
|
||||
import {
|
||||
loadUserSkills,
|
||||
loadProjectSkills,
|
||||
loadOpencodeGlobalSkills,
|
||||
loadOpencodeProjectSkills,
|
||||
discoverUserClaudeSkills,
|
||||
discoverProjectClaudeSkills,
|
||||
discoverOpencodeGlobalSkills,
|
||||
discoverOpencodeProjectSkills,
|
||||
} from "../features/opencode-skill-loader";
|
||||
import {
|
||||
loadUserAgents,
|
||||
loadProjectAgents,
|
||||
} from "../features/claude-code-agent-loader";
|
||||
import { loadMcpConfigs } from "../features/claude-code-mcp-loader";
|
||||
import { loadAllPluginComponents } from "../features/claude-code-plugin-loader";
|
||||
import { createBuiltinMcps } from "../mcp";
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import { log, fetchAvailableModels, readConnectedProvidersCache, resolveModelPipeline, addConfigLoadError } from "../shared";
|
||||
import { getOpenCodeConfigPaths } from "../shared/opencode-config-dir";
|
||||
import { migrateAgentConfig } from "../shared/permission-compat";
|
||||
import { AGENT_NAME_MAP } from "../shared/migration";
|
||||
import { AGENT_MODEL_REQUIREMENTS } from "../shared/model-requirements";
|
||||
import { PROMETHEUS_SYSTEM_PROMPT, PROMETHEUS_PERMISSION } from "../agents/prometheus";
|
||||
import { DEFAULT_CATEGORIES } from "../tools/delegate-task/constants";
|
||||
import type { ModelCacheState } from "../plugin-state";
|
||||
import type { CategoryConfig } from "../config/schema";
|
||||
import { log } from "../shared";
|
||||
import { applyAgentConfig } from "./agent-config-handler";
|
||||
import { applyCommandConfig } from "./command-config-handler";
|
||||
import { applyMcpConfig } from "./mcp-config-handler";
|
||||
import { applyProviderConfig } from "./provider-config-handler";
|
||||
import { loadPluginComponents } from "./plugin-components-loader";
|
||||
import { applyToolConfig } from "./tool-config-handler";
|
||||
|
||||
export { resolveCategoryConfig } from "./category-config-resolver";
|
||||
|
||||
export interface ConfigHandlerDeps {
|
||||
ctx: { directory: string; client?: any };
|
||||
@@ -41,486 +16,29 @@ export interface ConfigHandlerDeps {
|
||||
modelCacheState: ModelCacheState;
|
||||
}
|
||||
|
||||
export function resolveCategoryConfig(
|
||||
categoryName: string,
|
||||
userCategories?: Record<string, CategoryConfig>
|
||||
): CategoryConfig | undefined {
|
||||
return userCategories?.[categoryName] ?? DEFAULT_CATEGORIES[categoryName];
|
||||
}
|
||||
|
||||
const CORE_AGENT_ORDER = ["sisyphus", "hephaestus", "prometheus", "atlas"] as const;
|
||||
|
||||
function reorderAgentsByPriority(agents: Record<string, unknown>): Record<string, unknown> {
|
||||
const ordered: Record<string, unknown> = {};
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const key of CORE_AGENT_ORDER) {
|
||||
if (Object.prototype.hasOwnProperty.call(agents, key)) {
|
||||
ordered[key] = agents[key];
|
||||
seen.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(agents)) {
|
||||
if (!seen.has(key)) {
|
||||
ordered[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return ordered;
|
||||
}
|
||||
|
||||
export function createConfigHandler(deps: ConfigHandlerDeps) {
|
||||
const { ctx, pluginConfig, modelCacheState } = deps;
|
||||
|
||||
return async (config: Record<string, unknown>) => {
|
||||
type ProviderConfig = {
|
||||
options?: { headers?: Record<string, string> };
|
||||
models?: Record<string, { limit?: { context?: number } }>;
|
||||
};
|
||||
const providers = config.provider as
|
||||
| Record<string, ProviderConfig>
|
||||
| undefined;
|
||||
applyProviderConfig({ config, modelCacheState });
|
||||
|
||||
const anthropicBeta =
|
||||
providers?.anthropic?.options?.headers?.["anthropic-beta"];
|
||||
modelCacheState.anthropicContext1MEnabled =
|
||||
anthropicBeta?.includes("context-1m") ?? false;
|
||||
const pluginComponents = await loadPluginComponents({ pluginConfig });
|
||||
|
||||
if (providers) {
|
||||
for (const [providerID, providerConfig] of Object.entries(providers)) {
|
||||
const models = providerConfig?.models;
|
||||
if (models) {
|
||||
for (const [modelID, modelConfig] of Object.entries(models)) {
|
||||
const contextLimit = modelConfig?.limit?.context;
|
||||
if (contextLimit) {
|
||||
modelCacheState.modelContextLimitsCache.set(
|
||||
`${providerID}/${modelID}`,
|
||||
contextLimit
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const agentResult = await applyAgentConfig({
|
||||
config,
|
||||
pluginConfig,
|
||||
ctx,
|
||||
pluginComponents,
|
||||
});
|
||||
|
||||
const emptyPluginDefaults = {
|
||||
commands: {},
|
||||
skills: {},
|
||||
agents: {},
|
||||
mcpServers: {},
|
||||
hooksConfigs: [] as { hooks?: Record<string, unknown> }[],
|
||||
plugins: [] as { name: string; version: string }[],
|
||||
errors: [] as { pluginKey: string; installPath: string; error: string }[],
|
||||
};
|
||||
applyToolConfig({ config, pluginConfig, agentResult });
|
||||
await applyMcpConfig({ config, pluginConfig, pluginComponents });
|
||||
await applyCommandConfig({ config, pluginConfig, pluginComponents });
|
||||
|
||||
let pluginComponents: typeof emptyPluginDefaults;
|
||||
const pluginsEnabled = pluginConfig.claude_code?.plugins ?? true;
|
||||
|
||||
if (pluginsEnabled) {
|
||||
const timeoutMs = pluginConfig.experimental?.plugin_load_timeout_ms ?? 10000;
|
||||
try {
|
||||
let timeoutId: ReturnType<typeof setTimeout>;
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
timeoutId = setTimeout(
|
||||
() => reject(new Error(`Plugin loading timed out after ${timeoutMs}ms`)),
|
||||
timeoutMs,
|
||||
);
|
||||
});
|
||||
pluginComponents = await Promise.race([
|
||||
loadAllPluginComponents({
|
||||
enabledPluginsOverride: pluginConfig.claude_code?.plugins_override,
|
||||
}),
|
||||
timeoutPromise,
|
||||
]).finally(() => clearTimeout(timeoutId));
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
log("[config-handler] Plugin loading failed", { error: errorMessage });
|
||||
addConfigLoadError({ path: "plugin-loading", error: errorMessage });
|
||||
pluginComponents = emptyPluginDefaults;
|
||||
}
|
||||
} else {
|
||||
pluginComponents = emptyPluginDefaults;
|
||||
}
|
||||
|
||||
if (pluginComponents.plugins.length > 0) {
|
||||
log(`Loaded ${pluginComponents.plugins.length} Claude Code plugins`, {
|
||||
plugins: pluginComponents.plugins.map((p) => `${p.name}@${p.version}`),
|
||||
});
|
||||
}
|
||||
|
||||
if (pluginComponents.errors.length > 0) {
|
||||
log(`Plugin load errors`, { errors: pluginComponents.errors });
|
||||
}
|
||||
|
||||
// Migrate disabled_agents from old names to new names
|
||||
const migratedDisabledAgents = (pluginConfig.disabled_agents ?? []).map(agent => {
|
||||
return AGENT_NAME_MAP[agent.toLowerCase()] ?? AGENT_NAME_MAP[agent] ?? agent
|
||||
}) as typeof pluginConfig.disabled_agents
|
||||
|
||||
const includeClaudeSkillsForAwareness = pluginConfig.claude_code?.skills ?? true;
|
||||
const [
|
||||
discoveredUserSkills,
|
||||
discoveredProjectSkills,
|
||||
discoveredOpencodeGlobalSkills,
|
||||
discoveredOpencodeProjectSkills,
|
||||
] = await Promise.all([
|
||||
includeClaudeSkillsForAwareness ? discoverUserClaudeSkills() : Promise.resolve([]),
|
||||
includeClaudeSkillsForAwareness ? discoverProjectClaudeSkills() : Promise.resolve([]),
|
||||
discoverOpencodeGlobalSkills(),
|
||||
discoverOpencodeProjectSkills(),
|
||||
]);
|
||||
|
||||
const allDiscoveredSkills = [
|
||||
...discoveredOpencodeProjectSkills,
|
||||
...discoveredProjectSkills,
|
||||
...discoveredOpencodeGlobalSkills,
|
||||
...discoveredUserSkills,
|
||||
];
|
||||
|
||||
const browserProvider = pluginConfig.browser_automation_engine?.provider ?? "playwright";
|
||||
// config.model represents the currently active model in OpenCode (including UI selection)
|
||||
// Pass it as uiSelectedModel so it takes highest priority in model resolution
|
||||
const currentModel = config.model as string | undefined;
|
||||
const disabledSkills = new Set<string>(pluginConfig.disabled_skills ?? []);
|
||||
const builtinAgents = await createBuiltinAgents(
|
||||
migratedDisabledAgents,
|
||||
pluginConfig.agents,
|
||||
ctx.directory,
|
||||
undefined, // systemDefaultModel - let fallback chain handle this
|
||||
pluginConfig.categories,
|
||||
pluginConfig.git_master,
|
||||
allDiscoveredSkills,
|
||||
ctx.client,
|
||||
browserProvider,
|
||||
currentModel, // uiSelectedModel - takes highest priority
|
||||
disabledSkills
|
||||
);
|
||||
|
||||
// Claude Code agents: Do NOT apply permission migration
|
||||
// Claude Code uses whitelist-based tools format which is semantically different
|
||||
// from OpenCode's denylist-based permission system
|
||||
const userAgents = (pluginConfig.claude_code?.agents ?? true)
|
||||
? loadUserAgents()
|
||||
: {};
|
||||
const projectAgents = (pluginConfig.claude_code?.agents ?? true)
|
||||
? loadProjectAgents()
|
||||
: {};
|
||||
|
||||
// Plugin agents: Apply permission migration for compatibility
|
||||
const rawPluginAgents = pluginComponents.agents;
|
||||
const pluginAgents = Object.fromEntries(
|
||||
Object.entries(rawPluginAgents).map(([k, v]) => [
|
||||
k,
|
||||
v ? migrateAgentConfig(v as Record<string, unknown>) : v,
|
||||
])
|
||||
);
|
||||
|
||||
const isSisyphusEnabled = pluginConfig.sisyphus_agent?.disabled !== true;
|
||||
const builderEnabled =
|
||||
pluginConfig.sisyphus_agent?.default_builder_enabled ?? false;
|
||||
const plannerEnabled =
|
||||
pluginConfig.sisyphus_agent?.planner_enabled ?? true;
|
||||
const replacePlan = pluginConfig.sisyphus_agent?.replace_plan ?? true;
|
||||
const shouldDemotePlan = plannerEnabled && replacePlan;
|
||||
|
||||
type AgentConfig = Record<
|
||||
string,
|
||||
Record<string, unknown> | undefined
|
||||
> & {
|
||||
build?: Record<string, unknown>;
|
||||
plan?: Record<string, unknown>;
|
||||
explore?: { tools?: Record<string, unknown> };
|
||||
librarian?: { tools?: Record<string, unknown> };
|
||||
"multimodal-looker"?: { tools?: Record<string, unknown> };
|
||||
atlas?: { tools?: Record<string, unknown> };
|
||||
sisyphus?: { tools?: Record<string, unknown> };
|
||||
};
|
||||
const configAgent = config.agent as AgentConfig | undefined;
|
||||
|
||||
if (isSisyphusEnabled && builtinAgents.sisyphus) {
|
||||
(config as { default_agent?: string }).default_agent = "sisyphus";
|
||||
|
||||
const agentConfig: Record<string, unknown> = {
|
||||
sisyphus: builtinAgents.sisyphus,
|
||||
};
|
||||
|
||||
agentConfig["sisyphus-junior"] = createSisyphusJuniorAgentWithOverrides(
|
||||
pluginConfig.agents?.["sisyphus-junior"],
|
||||
undefined
|
||||
);
|
||||
|
||||
if (builderEnabled) {
|
||||
const { name: _buildName, ...buildConfigWithoutName } =
|
||||
configAgent?.build ?? {};
|
||||
const migratedBuildConfig = migrateAgentConfig(
|
||||
buildConfigWithoutName as Record<string, unknown>
|
||||
);
|
||||
const openCodeBuilderOverride =
|
||||
pluginConfig.agents?.["OpenCode-Builder"];
|
||||
const openCodeBuilderBase = {
|
||||
...migratedBuildConfig,
|
||||
description: `${configAgent?.build?.description ?? "Build agent"} (OpenCode default)`,
|
||||
};
|
||||
|
||||
agentConfig["OpenCode-Builder"] = openCodeBuilderOverride
|
||||
? { ...openCodeBuilderBase, ...openCodeBuilderOverride }
|
||||
: openCodeBuilderBase;
|
||||
}
|
||||
|
||||
if (plannerEnabled) {
|
||||
const prometheusOverride =
|
||||
pluginConfig.agents?.["prometheus"] as
|
||||
| (Record<string, unknown> & {
|
||||
category?: string
|
||||
model?: string
|
||||
variant?: string
|
||||
reasoningEffort?: string
|
||||
textVerbosity?: string
|
||||
thinking?: { type: string; budgetTokens?: number }
|
||||
temperature?: number
|
||||
top_p?: number
|
||||
maxTokens?: number
|
||||
})
|
||||
| undefined;
|
||||
|
||||
const categoryConfig = prometheusOverride?.category
|
||||
? resolveCategoryConfig(
|
||||
prometheusOverride.category,
|
||||
pluginConfig.categories
|
||||
)
|
||||
: undefined;
|
||||
|
||||
const prometheusRequirement = AGENT_MODEL_REQUIREMENTS["prometheus"];
|
||||
const connectedProviders = readConnectedProvidersCache();
|
||||
// IMPORTANT: Do NOT pass ctx.client to fetchAvailableModels during plugin initialization.
|
||||
// Calling client API (e.g., client.provider.list()) from config handler causes deadlock:
|
||||
// - Plugin init waits for server response
|
||||
// - Server waits for plugin init to complete before handling requests
|
||||
// Use cache-only mode instead. If cache is unavailable, fallback chain uses first model.
|
||||
// See: https://github.com/code-yeongyu/oh-my-opencode/issues/1301
|
||||
const availableModels = await fetchAvailableModels(undefined, {
|
||||
connectedProviders: connectedProviders ?? undefined,
|
||||
});
|
||||
|
||||
const modelResolution = resolveModelPipeline({
|
||||
intent: {
|
||||
uiSelectedModel: currentModel,
|
||||
userModel: prometheusOverride?.model ?? categoryConfig?.model,
|
||||
},
|
||||
constraints: { availableModels },
|
||||
policy: {
|
||||
fallbackChain: prometheusRequirement?.fallbackChain,
|
||||
systemDefaultModel: undefined,
|
||||
},
|
||||
});
|
||||
const resolvedModel = modelResolution?.model;
|
||||
const resolvedVariant = modelResolution?.variant;
|
||||
|
||||
const variantToUse = prometheusOverride?.variant ?? resolvedVariant;
|
||||
const reasoningEffortToUse = prometheusOverride?.reasoningEffort ?? categoryConfig?.reasoningEffort;
|
||||
const textVerbosityToUse = prometheusOverride?.textVerbosity ?? categoryConfig?.textVerbosity;
|
||||
const thinkingToUse = prometheusOverride?.thinking ?? categoryConfig?.thinking;
|
||||
const temperatureToUse = prometheusOverride?.temperature ?? categoryConfig?.temperature;
|
||||
const topPToUse = prometheusOverride?.top_p ?? categoryConfig?.top_p;
|
||||
const maxTokensToUse = prometheusOverride?.maxTokens ?? categoryConfig?.maxTokens;
|
||||
const prometheusBase = {
|
||||
name: "prometheus",
|
||||
...(resolvedModel ? { model: resolvedModel } : {}),
|
||||
...(variantToUse ? { variant: variantToUse } : {}),
|
||||
mode: "all" as const,
|
||||
prompt: PROMETHEUS_SYSTEM_PROMPT,
|
||||
permission: PROMETHEUS_PERMISSION,
|
||||
description: `${configAgent?.plan?.description ?? "Plan agent"} (Prometheus - OhMyOpenCode)`,
|
||||
color: (configAgent?.plan?.color as string) ?? "#FF5722", // Deep Orange - Fire/Flame theme
|
||||
...(temperatureToUse !== undefined ? { temperature: temperatureToUse } : {}),
|
||||
...(topPToUse !== undefined ? { top_p: topPToUse } : {}),
|
||||
...(maxTokensToUse !== undefined ? { maxTokens: maxTokensToUse } : {}),
|
||||
...(categoryConfig?.tools ? { tools: categoryConfig.tools } : {}),
|
||||
...(thinkingToUse ? { thinking: thinkingToUse } : {}),
|
||||
...(reasoningEffortToUse !== undefined
|
||||
? { reasoningEffort: reasoningEffortToUse }
|
||||
: {}),
|
||||
...(textVerbosityToUse !== undefined
|
||||
? { textVerbosity: textVerbosityToUse }
|
||||
: {}),
|
||||
};
|
||||
|
||||
// Properly handle prompt_append for Prometheus
|
||||
// Extract prompt_append and append it to prompt instead of shallow spread
|
||||
// Fixes: https://github.com/code-yeongyu/oh-my-opencode/issues/723
|
||||
if (prometheusOverride) {
|
||||
const { prompt_append, ...restOverride } = prometheusOverride as Record<string, unknown> & { prompt_append?: string };
|
||||
const merged = { ...prometheusBase, ...restOverride };
|
||||
if (prompt_append && merged.prompt) {
|
||||
merged.prompt = merged.prompt + "\n" + prompt_append;
|
||||
}
|
||||
agentConfig["prometheus"] = merged;
|
||||
} else {
|
||||
agentConfig["prometheus"] = prometheusBase;
|
||||
}
|
||||
}
|
||||
|
||||
const filteredConfigAgents = configAgent
|
||||
? Object.fromEntries(
|
||||
Object.entries(configAgent)
|
||||
.filter(([key]) => {
|
||||
if (key === "build") return false;
|
||||
if (key === "plan" && shouldDemotePlan) return false;
|
||||
// Filter out agents that oh-my-opencode provides to prevent
|
||||
// OpenCode defaults from overwriting user config in oh-my-opencode.json
|
||||
// See: https://github.com/code-yeongyu/oh-my-opencode/issues/472
|
||||
if (key in builtinAgents) return false;
|
||||
return true;
|
||||
})
|
||||
.map(([key, value]) => [
|
||||
key,
|
||||
value ? migrateAgentConfig(value as Record<string, unknown>) : value,
|
||||
])
|
||||
)
|
||||
: {};
|
||||
|
||||
const migratedBuild = configAgent?.build
|
||||
? migrateAgentConfig(configAgent.build as Record<string, unknown>)
|
||||
: {};
|
||||
|
||||
const planDemoteConfig = shouldDemotePlan
|
||||
? { mode: "subagent" as const
|
||||
}
|
||||
: undefined;
|
||||
|
||||
config.agent = {
|
||||
...agentConfig,
|
||||
...Object.fromEntries(
|
||||
Object.entries(builtinAgents).filter(([k]) => k !== "sisyphus")
|
||||
),
|
||||
...userAgents,
|
||||
...projectAgents,
|
||||
...pluginAgents,
|
||||
...filteredConfigAgents,
|
||||
build: { ...migratedBuild, mode: "subagent", hidden: true },
|
||||
...(planDemoteConfig ? { plan: planDemoteConfig } : {}),
|
||||
};
|
||||
} else {
|
||||
config.agent = {
|
||||
...builtinAgents,
|
||||
...userAgents,
|
||||
...projectAgents,
|
||||
...pluginAgents,
|
||||
...configAgent,
|
||||
};
|
||||
}
|
||||
|
||||
if (config.agent) {
|
||||
config.agent = reorderAgentsByPriority(config.agent as Record<string, unknown>);
|
||||
}
|
||||
|
||||
const agentResult = config.agent as AgentConfig;
|
||||
|
||||
config.tools = {
|
||||
...(config.tools as Record<string, unknown>),
|
||||
"grep_app_*": false,
|
||||
LspHover: false,
|
||||
LspCodeActions: false,
|
||||
LspCodeActionResolve: false,
|
||||
"task_*": false,
|
||||
teammate: false,
|
||||
...(pluginConfig.experimental?.task_system ? { todowrite: false, todoread: false } : {}),
|
||||
};
|
||||
|
||||
type AgentWithPermission = { permission?: Record<string, unknown> };
|
||||
|
||||
// In CLI run mode, deny Question tool for all agents (no TUI to answer questions)
|
||||
const isCliRunMode = process.env.OPENCODE_CLI_RUN_MODE === "true";
|
||||
const questionPermission = isCliRunMode ? "deny" : "allow";
|
||||
|
||||
if (agentResult.librarian) {
|
||||
const agent = agentResult.librarian as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, "grep_app_*": "allow" };
|
||||
}
|
||||
if (agentResult["multimodal-looker"]) {
|
||||
const agent = agentResult["multimodal-looker"] as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, task: "deny", look_at: "deny" };
|
||||
}
|
||||
if (agentResult["atlas"]) {
|
||||
const agent = agentResult["atlas"] as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, task: "allow", call_omo_agent: "deny", "task_*": "allow", teammate: "allow" };
|
||||
}
|
||||
if (agentResult.sisyphus) {
|
||||
const agent = agentResult.sisyphus as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, call_omo_agent: "deny", task: "allow", question: questionPermission, "task_*": "allow", teammate: "allow" };
|
||||
}
|
||||
if (agentResult.hephaestus) {
|
||||
const agent = agentResult.hephaestus as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, call_omo_agent: "deny", task: "allow", question: questionPermission };
|
||||
}
|
||||
if (agentResult["prometheus"]) {
|
||||
const agent = agentResult["prometheus"] as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, call_omo_agent: "deny", task: "allow", question: questionPermission, "task_*": "allow", teammate: "allow" };
|
||||
}
|
||||
if (agentResult["sisyphus-junior"]) {
|
||||
const agent = agentResult["sisyphus-junior"] as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, task: "allow", "task_*": "allow", teammate: "allow" };
|
||||
}
|
||||
|
||||
config.permission = {
|
||||
...(config.permission as Record<string, unknown>),
|
||||
webfetch: "allow",
|
||||
external_directory: "allow",
|
||||
task: "deny",
|
||||
};
|
||||
|
||||
const mcpResult = (pluginConfig.claude_code?.mcp ?? true)
|
||||
? await loadMcpConfigs()
|
||||
: { servers: {} };
|
||||
|
||||
config.mcp = {
|
||||
...createBuiltinMcps(pluginConfig.disabled_mcps, pluginConfig),
|
||||
...(config.mcp as Record<string, unknown>),
|
||||
...mcpResult.servers,
|
||||
...pluginComponents.mcpServers,
|
||||
};
|
||||
|
||||
const builtinCommands = loadBuiltinCommands(pluginConfig.disabled_commands);
|
||||
const systemCommands = (config.command as Record<string, unknown>) ?? {};
|
||||
|
||||
// Parallel loading of all commands and skills for faster startup
|
||||
const includeClaudeCommands = pluginConfig.claude_code?.commands ?? true;
|
||||
const includeClaudeSkills = pluginConfig.claude_code?.skills ?? true;
|
||||
|
||||
const [
|
||||
userCommands,
|
||||
projectCommands,
|
||||
opencodeGlobalCommands,
|
||||
opencodeProjectCommands,
|
||||
userSkills,
|
||||
projectSkills,
|
||||
opencodeGlobalSkills,
|
||||
opencodeProjectSkills,
|
||||
] = await Promise.all([
|
||||
includeClaudeCommands ? loadUserCommands() : Promise.resolve({}),
|
||||
includeClaudeCommands ? loadProjectCommands() : Promise.resolve({}),
|
||||
loadOpencodeGlobalCommands(),
|
||||
loadOpencodeProjectCommands(),
|
||||
includeClaudeSkills ? loadUserSkills() : Promise.resolve({}),
|
||||
includeClaudeSkills ? loadProjectSkills() : Promise.resolve({}),
|
||||
loadOpencodeGlobalSkills(),
|
||||
loadOpencodeProjectSkills(),
|
||||
]);
|
||||
|
||||
config.command = {
|
||||
...builtinCommands,
|
||||
...userCommands,
|
||||
...userSkills,
|
||||
...opencodeGlobalCommands,
|
||||
...opencodeGlobalSkills,
|
||||
...systemCommands,
|
||||
...projectCommands,
|
||||
...projectSkills,
|
||||
...opencodeProjectCommands,
|
||||
...opencodeProjectSkills,
|
||||
...pluginComponents.commands,
|
||||
...pluginComponents.skills,
|
||||
};
|
||||
log("[config-handler] config handler applied", {
|
||||
agentCount: Object.keys(agentResult).length,
|
||||
commandCount: Object.keys((config.command as Record<string, unknown>) ?? {})
|
||||
.length,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1 +1,10 @@
|
||||
export { createConfigHandler, type ConfigHandlerDeps } from "./config-handler";
|
||||
export * from "./provider-config-handler";
|
||||
export * from "./agent-config-handler";
|
||||
export * from "./tool-config-handler";
|
||||
export * from "./mcp-config-handler";
|
||||
export * from "./command-config-handler";
|
||||
export * from "./plugin-components-loader";
|
||||
export * from "./category-config-resolver";
|
||||
export * from "./prometheus-agent-config-builder";
|
||||
export * from "./agent-priority-order";
|
||||
|
||||
21
src/plugin-handlers/mcp-config-handler.ts
Normal file
21
src/plugin-handlers/mcp-config-handler.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import { loadMcpConfigs } from "../features/claude-code-mcp-loader";
|
||||
import { createBuiltinMcps } from "../mcp";
|
||||
import type { PluginComponents } from "./plugin-components-loader";
|
||||
|
||||
export async function applyMcpConfig(params: {
|
||||
config: Record<string, unknown>;
|
||||
pluginConfig: OhMyOpenCodeConfig;
|
||||
pluginComponents: PluginComponents;
|
||||
}): Promise<void> {
|
||||
const mcpResult = params.pluginConfig.claude_code?.mcp ?? true
|
||||
? await loadMcpConfigs()
|
||||
: { servers: {} };
|
||||
|
||||
params.config.mcp = {
|
||||
...createBuiltinMcps(params.pluginConfig.disabled_mcps, params.pluginConfig),
|
||||
...(params.config.mcp as Record<string, unknown>),
|
||||
...mcpResult.servers,
|
||||
...params.pluginComponents.mcpServers,
|
||||
};
|
||||
}
|
||||
70
src/plugin-handlers/plugin-components-loader.ts
Normal file
70
src/plugin-handlers/plugin-components-loader.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import { loadAllPluginComponents } from "../features/claude-code-plugin-loader";
|
||||
import { addConfigLoadError, log } from "../shared";
|
||||
|
||||
export type PluginComponents = {
|
||||
commands: Record<string, unknown>;
|
||||
skills: Record<string, unknown>;
|
||||
agents: Record<string, unknown>;
|
||||
mcpServers: Record<string, unknown>;
|
||||
hooksConfigs: Array<{ hooks?: Record<string, unknown> }>;
|
||||
plugins: Array<{ name: string; version: string }>;
|
||||
errors: Array<{ pluginKey: string; installPath: string; error: string }>;
|
||||
};
|
||||
|
||||
const EMPTY_PLUGIN_COMPONENTS: PluginComponents = {
|
||||
commands: {},
|
||||
skills: {},
|
||||
agents: {},
|
||||
mcpServers: {},
|
||||
hooksConfigs: [],
|
||||
plugins: [],
|
||||
errors: [],
|
||||
};
|
||||
|
||||
export async function loadPluginComponents(params: {
|
||||
pluginConfig: OhMyOpenCodeConfig;
|
||||
}): Promise<PluginComponents> {
|
||||
const pluginsEnabled = params.pluginConfig.claude_code?.plugins ?? true;
|
||||
if (!pluginsEnabled) {
|
||||
return EMPTY_PLUGIN_COMPONENTS;
|
||||
}
|
||||
|
||||
const timeoutMs = params.pluginConfig.experimental?.plugin_load_timeout_ms ?? 10000;
|
||||
|
||||
try {
|
||||
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
timeoutId = setTimeout(
|
||||
() => reject(new Error(`Plugin loading timed out after ${timeoutMs}ms`)),
|
||||
timeoutMs,
|
||||
);
|
||||
});
|
||||
|
||||
const pluginComponents = (await Promise.race([
|
||||
loadAllPluginComponents({
|
||||
enabledPluginsOverride: params.pluginConfig.claude_code?.plugins_override,
|
||||
}),
|
||||
timeoutPromise,
|
||||
]).finally(() => {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
})) as PluginComponents;
|
||||
|
||||
if (pluginComponents.plugins.length > 0) {
|
||||
log(`Loaded ${pluginComponents.plugins.length} Claude Code plugins`, {
|
||||
plugins: pluginComponents.plugins.map((p) => `${p.name}@${p.version}`),
|
||||
});
|
||||
}
|
||||
|
||||
if (pluginComponents.errors.length > 0) {
|
||||
log(`Plugin load errors`, { errors: pluginComponents.errors });
|
||||
}
|
||||
|
||||
return pluginComponents;
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
log("[config-handler] Plugin loading failed", { error: errorMessage });
|
||||
addConfigLoadError({ path: "plugin-loading", error: errorMessage });
|
||||
return EMPTY_PLUGIN_COMPONENTS;
|
||||
}
|
||||
}
|
||||
98
src/plugin-handlers/prometheus-agent-config-builder.ts
Normal file
98
src/plugin-handlers/prometheus-agent-config-builder.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import type { CategoryConfig } from "../config/schema";
|
||||
import { PROMETHEUS_PERMISSION, PROMETHEUS_SYSTEM_PROMPT } from "../agents/prometheus";
|
||||
import { AGENT_MODEL_REQUIREMENTS } from "../shared/model-requirements";
|
||||
import {
|
||||
fetchAvailableModels,
|
||||
readConnectedProvidersCache,
|
||||
resolveModelPipeline,
|
||||
} from "../shared";
|
||||
import { resolveCategoryConfig } from "./category-config-resolver";
|
||||
|
||||
type PrometheusOverride = Record<string, unknown> & {
|
||||
category?: string;
|
||||
model?: string;
|
||||
variant?: string;
|
||||
reasoningEffort?: string;
|
||||
textVerbosity?: string;
|
||||
thinking?: { type: string; budgetTokens?: number };
|
||||
temperature?: number;
|
||||
top_p?: number;
|
||||
maxTokens?: number;
|
||||
prompt_append?: string;
|
||||
};
|
||||
|
||||
export async function buildPrometheusAgentConfig(params: {
|
||||
configAgentPlan: Record<string, unknown> | undefined;
|
||||
pluginPrometheusOverride: PrometheusOverride | undefined;
|
||||
userCategories: Record<string, CategoryConfig> | undefined;
|
||||
currentModel: string | undefined;
|
||||
}): Promise<Record<string, unknown>> {
|
||||
const categoryConfig = params.pluginPrometheusOverride?.category
|
||||
? resolveCategoryConfig(params.pluginPrometheusOverride.category, params.userCategories)
|
||||
: undefined;
|
||||
|
||||
const requirement = AGENT_MODEL_REQUIREMENTS["prometheus"];
|
||||
const connectedProviders = readConnectedProvidersCache();
|
||||
const availableModels = await fetchAvailableModels(undefined, {
|
||||
connectedProviders: connectedProviders ?? undefined,
|
||||
});
|
||||
|
||||
const modelResolution = resolveModelPipeline({
|
||||
intent: {
|
||||
uiSelectedModel: params.currentModel,
|
||||
userModel: params.pluginPrometheusOverride?.model ?? categoryConfig?.model,
|
||||
},
|
||||
constraints: { availableModels },
|
||||
policy: {
|
||||
fallbackChain: requirement?.fallbackChain,
|
||||
systemDefaultModel: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
const resolvedModel = modelResolution?.model;
|
||||
const resolvedVariant = modelResolution?.variant;
|
||||
|
||||
const variantToUse = params.pluginPrometheusOverride?.variant ?? resolvedVariant;
|
||||
const reasoningEffortToUse =
|
||||
params.pluginPrometheusOverride?.reasoningEffort ?? categoryConfig?.reasoningEffort;
|
||||
const textVerbosityToUse =
|
||||
params.pluginPrometheusOverride?.textVerbosity ?? categoryConfig?.textVerbosity;
|
||||
const thinkingToUse = params.pluginPrometheusOverride?.thinking ?? categoryConfig?.thinking;
|
||||
const temperatureToUse =
|
||||
params.pluginPrometheusOverride?.temperature ?? categoryConfig?.temperature;
|
||||
const topPToUse = params.pluginPrometheusOverride?.top_p ?? categoryConfig?.top_p;
|
||||
const maxTokensToUse =
|
||||
params.pluginPrometheusOverride?.maxTokens ?? categoryConfig?.maxTokens;
|
||||
|
||||
const base: Record<string, unknown> = {
|
||||
name: "prometheus",
|
||||
...(resolvedModel ? { model: resolvedModel } : {}),
|
||||
...(variantToUse ? { variant: variantToUse } : {}),
|
||||
mode: "all",
|
||||
prompt: PROMETHEUS_SYSTEM_PROMPT,
|
||||
permission: PROMETHEUS_PERMISSION,
|
||||
description: `${(params.configAgentPlan?.description as string) ?? "Plan agent"} (Prometheus - OhMyOpenCode)`,
|
||||
color: (params.configAgentPlan?.color as string) ?? "#FF5722",
|
||||
...(temperatureToUse !== undefined ? { temperature: temperatureToUse } : {}),
|
||||
...(topPToUse !== undefined ? { top_p: topPToUse } : {}),
|
||||
...(maxTokensToUse !== undefined ? { maxTokens: maxTokensToUse } : {}),
|
||||
...(categoryConfig?.tools ? { tools: categoryConfig.tools } : {}),
|
||||
...(thinkingToUse ? { thinking: thinkingToUse } : {}),
|
||||
...(reasoningEffortToUse !== undefined
|
||||
? { reasoningEffort: reasoningEffortToUse }
|
||||
: {}),
|
||||
...(textVerbosityToUse !== undefined
|
||||
? { textVerbosity: textVerbosityToUse }
|
||||
: {}),
|
||||
};
|
||||
|
||||
const override = params.pluginPrometheusOverride;
|
||||
if (!override) return base;
|
||||
|
||||
const { prompt_append, ...restOverride } = override;
|
||||
const merged = { ...base, ...restOverride };
|
||||
if (prompt_append && typeof merged.prompt === "string") {
|
||||
merged.prompt = merged.prompt + "\n" + prompt_append;
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
36
src/plugin-handlers/provider-config-handler.ts
Normal file
36
src/plugin-handlers/provider-config-handler.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { ModelCacheState } from "../plugin-state";
|
||||
|
||||
type ProviderConfig = {
|
||||
options?: { headers?: Record<string, string> };
|
||||
models?: Record<string, { limit?: { context?: number } }>;
|
||||
};
|
||||
|
||||
export function applyProviderConfig(params: {
|
||||
config: Record<string, unknown>;
|
||||
modelCacheState: ModelCacheState;
|
||||
}): void {
|
||||
const providers = params.config.provider as
|
||||
| Record<string, ProviderConfig>
|
||||
| undefined;
|
||||
|
||||
const anthropicBeta = providers?.anthropic?.options?.headers?.["anthropic-beta"];
|
||||
params.modelCacheState.anthropicContext1MEnabled =
|
||||
anthropicBeta?.includes("context-1m") ?? false;
|
||||
|
||||
if (!providers) return;
|
||||
|
||||
for (const [providerID, providerConfig] of Object.entries(providers)) {
|
||||
const models = providerConfig?.models;
|
||||
if (!models) continue;
|
||||
|
||||
for (const [modelID, modelConfig] of Object.entries(models)) {
|
||||
const contextLimit = modelConfig?.limit?.context;
|
||||
if (!contextLimit) continue;
|
||||
|
||||
params.modelCacheState.modelContextLimitsCache.set(
|
||||
`${providerID}/${modelID}`,
|
||||
contextLimit,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
91
src/plugin-handlers/tool-config-handler.ts
Normal file
91
src/plugin-handlers/tool-config-handler.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
|
||||
type AgentWithPermission = { permission?: Record<string, unknown> };
|
||||
|
||||
export function applyToolConfig(params: {
|
||||
config: Record<string, unknown>;
|
||||
pluginConfig: OhMyOpenCodeConfig;
|
||||
agentResult: Record<string, unknown>;
|
||||
}): void {
|
||||
params.config.tools = {
|
||||
...(params.config.tools as Record<string, unknown>),
|
||||
"grep_app_*": false,
|
||||
LspHover: false,
|
||||
LspCodeActions: false,
|
||||
LspCodeActionResolve: false,
|
||||
"task_*": false,
|
||||
teammate: false,
|
||||
...(params.pluginConfig.experimental?.task_system
|
||||
? { todowrite: false, todoread: false }
|
||||
: {}),
|
||||
};
|
||||
|
||||
const isCliRunMode = process.env.OPENCODE_CLI_RUN_MODE === "true";
|
||||
const questionPermission = isCliRunMode ? "deny" : "allow";
|
||||
|
||||
if (params.agentResult.librarian) {
|
||||
const agent = params.agentResult.librarian as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, "grep_app_*": "allow" };
|
||||
}
|
||||
if (params.agentResult["multimodal-looker"]) {
|
||||
const agent = params.agentResult["multimodal-looker"] as AgentWithPermission;
|
||||
agent.permission = { ...agent.permission, task: "deny", look_at: "deny" };
|
||||
}
|
||||
if (params.agentResult["atlas"]) {
|
||||
const agent = params.agentResult["atlas"] as AgentWithPermission;
|
||||
agent.permission = {
|
||||
...agent.permission,
|
||||
task: "allow",
|
||||
call_omo_agent: "deny",
|
||||
"task_*": "allow",
|
||||
teammate: "allow",
|
||||
};
|
||||
}
|
||||
if (params.agentResult.sisyphus) {
|
||||
const agent = params.agentResult.sisyphus as AgentWithPermission;
|
||||
agent.permission = {
|
||||
...agent.permission,
|
||||
call_omo_agent: "deny",
|
||||
task: "allow",
|
||||
question: questionPermission,
|
||||
"task_*": "allow",
|
||||
teammate: "allow",
|
||||
};
|
||||
}
|
||||
if (params.agentResult.hephaestus) {
|
||||
const agent = params.agentResult.hephaestus as AgentWithPermission;
|
||||
agent.permission = {
|
||||
...agent.permission,
|
||||
call_omo_agent: "deny",
|
||||
task: "allow",
|
||||
question: questionPermission,
|
||||
};
|
||||
}
|
||||
if (params.agentResult["prometheus"]) {
|
||||
const agent = params.agentResult["prometheus"] as AgentWithPermission;
|
||||
agent.permission = {
|
||||
...agent.permission,
|
||||
call_omo_agent: "deny",
|
||||
task: "allow",
|
||||
question: questionPermission,
|
||||
"task_*": "allow",
|
||||
teammate: "allow",
|
||||
};
|
||||
}
|
||||
if (params.agentResult["sisyphus-junior"]) {
|
||||
const agent = params.agentResult["sisyphus-junior"] as AgentWithPermission;
|
||||
agent.permission = {
|
||||
...agent.permission,
|
||||
task: "allow",
|
||||
"task_*": "allow",
|
||||
teammate: "allow",
|
||||
};
|
||||
}
|
||||
|
||||
params.config.permission = {
|
||||
...(params.config.permission as Record<string, unknown>),
|
||||
webfetch: "allow",
|
||||
external_directory: "allow",
|
||||
task: "deny",
|
||||
};
|
||||
}
|
||||
65
src/plugin-interface.ts
Normal file
65
src/plugin-interface.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { PluginContext, PluginInterface, ToolsRecord } from "./plugin/types"
|
||||
import type { OhMyOpenCodeConfig } from "./config"
|
||||
|
||||
import { createChatParamsHandler } from "./plugin/chat-params"
|
||||
import { createChatMessageHandler } from "./plugin/chat-message"
|
||||
import { createMessagesTransformHandler } from "./plugin/messages-transform"
|
||||
import { createEventHandler } from "./plugin/event"
|
||||
import { createToolExecuteAfterHandler } from "./plugin/tool-execute-after"
|
||||
import { createToolExecuteBeforeHandler } from "./plugin/tool-execute-before"
|
||||
|
||||
import type { CreatedHooks } from "./create-hooks"
|
||||
import type { Managers } from "./create-managers"
|
||||
|
||||
export function createPluginInterface(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
firstMessageVariantGate: {
|
||||
shouldOverride: (sessionID: string) => boolean
|
||||
markApplied: (sessionID: string) => void
|
||||
markSessionCreated: (sessionInfo: { id?: string; title?: string; parentID?: string } | undefined) => void
|
||||
clear: (sessionID: string) => void
|
||||
}
|
||||
managers: Managers
|
||||
hooks: CreatedHooks
|
||||
tools: ToolsRecord
|
||||
}): PluginInterface {
|
||||
const { ctx, pluginConfig, firstMessageVariantGate, managers, hooks, tools } =
|
||||
args
|
||||
|
||||
return {
|
||||
tool: tools,
|
||||
|
||||
"chat.params": createChatParamsHandler({ anthropicEffort: hooks.anthropicEffort }),
|
||||
|
||||
"chat.message": createChatMessageHandler({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
firstMessageVariantGate,
|
||||
hooks,
|
||||
}),
|
||||
|
||||
"experimental.chat.messages.transform": createMessagesTransformHandler({
|
||||
hooks,
|
||||
}),
|
||||
|
||||
config: managers.configHandler,
|
||||
|
||||
event: createEventHandler({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
firstMessageVariantGate,
|
||||
managers,
|
||||
hooks,
|
||||
}),
|
||||
|
||||
"tool.execute.before": createToolExecuteBeforeHandler({
|
||||
ctx,
|
||||
hooks,
|
||||
}),
|
||||
|
||||
"tool.execute.after": createToolExecuteAfterHandler({
|
||||
hooks,
|
||||
}),
|
||||
}
|
||||
}
|
||||
29
src/plugin/available-categories.ts
Normal file
29
src/plugin/available-categories.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { AvailableCategory } from "../agents/dynamic-agent-prompt-builder"
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
|
||||
import {
|
||||
CATEGORY_DESCRIPTIONS,
|
||||
DEFAULT_CATEGORIES,
|
||||
} from "../tools/delegate-task/constants"
|
||||
|
||||
export function createAvailableCategories(
|
||||
pluginConfig: OhMyOpenCodeConfig,
|
||||
): AvailableCategory[] {
|
||||
const mergedCategories = pluginConfig.categories
|
||||
? { ...DEFAULT_CATEGORIES, ...pluginConfig.categories }
|
||||
: DEFAULT_CATEGORIES
|
||||
|
||||
return Object.entries(mergedCategories).map(([name, categoryConfig]) => {
|
||||
const model =
|
||||
typeof categoryConfig.model === "string" ? categoryConfig.model : undefined
|
||||
|
||||
return {
|
||||
name,
|
||||
description:
|
||||
pluginConfig.categories?.[name]?.description ??
|
||||
CATEGORY_DESCRIPTIONS[name] ??
|
||||
"General tasks",
|
||||
model,
|
||||
}
|
||||
})
|
||||
}
|
||||
139
src/plugin/chat-message.ts
Normal file
139
src/plugin/chat-message.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { PluginContext } from "./types"
|
||||
|
||||
import {
|
||||
applyAgentVariant,
|
||||
resolveAgentVariant,
|
||||
resolveVariantForModel,
|
||||
} from "../shared/agent-variant"
|
||||
import { hasConnectedProvidersCache } from "../shared"
|
||||
import {
|
||||
setSessionAgent,
|
||||
} from "../features/claude-code-session-state"
|
||||
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
|
||||
type FirstMessageVariantGate = {
|
||||
shouldOverride: (sessionID: string) => boolean
|
||||
markApplied: (sessionID: string) => void
|
||||
}
|
||||
|
||||
type ChatMessagePart = { type: string; text?: string; [key: string]: unknown }
|
||||
type ChatMessageHandlerOutput = { message: Record<string, unknown>; parts: ChatMessagePart[] }
|
||||
type StartWorkHookOutput = { parts: Array<{ type: string; text?: string }> }
|
||||
|
||||
function isStartWorkHookOutput(value: unknown): value is StartWorkHookOutput {
|
||||
if (typeof value !== "object" || value === null) return false
|
||||
const record = value as Record<string, unknown>
|
||||
const partsValue = record["parts"]
|
||||
if (!Array.isArray(partsValue)) return false
|
||||
return partsValue.every((part) => {
|
||||
if (typeof part !== "object" || part === null) return false
|
||||
const partRecord = part as Record<string, unknown>
|
||||
return typeof partRecord["type"] === "string"
|
||||
})
|
||||
}
|
||||
|
||||
export function createChatMessageHandler(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
firstMessageVariantGate: FirstMessageVariantGate
|
||||
hooks: CreatedHooks
|
||||
}): (
|
||||
input: { sessionID: string; agent?: string; model?: { providerID: string; modelID: string } },
|
||||
output: ChatMessageHandlerOutput
|
||||
) => Promise<void> {
|
||||
const { ctx, pluginConfig, firstMessageVariantGate, hooks } = args
|
||||
|
||||
return async (
|
||||
input: { sessionID: string; agent?: string; model?: { providerID: string; modelID: string } },
|
||||
output: ChatMessageHandlerOutput
|
||||
): Promise<void> => {
|
||||
if (input.agent) {
|
||||
setSessionAgent(input.sessionID, input.agent)
|
||||
}
|
||||
|
||||
const message = output.message
|
||||
|
||||
if (firstMessageVariantGate.shouldOverride(input.sessionID)) {
|
||||
const variant =
|
||||
input.model && input.agent
|
||||
? resolveVariantForModel(pluginConfig, input.agent, input.model)
|
||||
: resolveAgentVariant(pluginConfig, input.agent)
|
||||
if (variant !== undefined) {
|
||||
message["variant"] = variant
|
||||
}
|
||||
firstMessageVariantGate.markApplied(input.sessionID)
|
||||
} else {
|
||||
if (input.model && input.agent && message["variant"] === undefined) {
|
||||
const variant = resolveVariantForModel(pluginConfig, input.agent, input.model)
|
||||
if (variant !== undefined) {
|
||||
message["variant"] = variant
|
||||
}
|
||||
} else {
|
||||
applyAgentVariant(pluginConfig, input.agent, message)
|
||||
}
|
||||
}
|
||||
|
||||
await hooks.stopContinuationGuard?.["chat.message"]?.(input)
|
||||
await hooks.keywordDetector?.["chat.message"]?.(input, output)
|
||||
await hooks.claudeCodeHooks?.["chat.message"]?.(input, output)
|
||||
await hooks.autoSlashCommand?.["chat.message"]?.(input, output)
|
||||
if (hooks.startWork && isStartWorkHookOutput(output)) {
|
||||
await hooks.startWork["chat.message"]?.(input, output)
|
||||
}
|
||||
|
||||
if (!hasConnectedProvidersCache()) {
|
||||
ctx.client.tui
|
||||
.showToast({
|
||||
body: {
|
||||
title: "⚠️ Provider Cache Missing",
|
||||
message:
|
||||
"Model filtering disabled. RESTART OpenCode to enable full functionality.",
|
||||
variant: "warning" as const,
|
||||
duration: 6000,
|
||||
},
|
||||
})
|
||||
.catch(() => {})
|
||||
}
|
||||
|
||||
if (hooks.ralphLoop) {
|
||||
const parts = output.parts
|
||||
const promptText =
|
||||
parts
|
||||
?.filter((p) => p.type === "text" && p.text)
|
||||
.map((p) => p.text)
|
||||
.join("\n")
|
||||
.trim() || ""
|
||||
|
||||
const isRalphLoopTemplate =
|
||||
promptText.includes("You are starting a Ralph Loop") &&
|
||||
promptText.includes("<user-task>")
|
||||
const isCancelRalphTemplate = promptText.includes(
|
||||
"Cancel the currently active Ralph Loop",
|
||||
)
|
||||
|
||||
if (isRalphLoopTemplate) {
|
||||
const taskMatch = promptText.match(/<user-task>\s*([\s\S]*?)\s*<\/user-task>/i)
|
||||
const rawTask = taskMatch?.[1]?.trim() || ""
|
||||
const quotedMatch = rawTask.match(/^["'](.+?)["']/)
|
||||
const prompt =
|
||||
quotedMatch?.[1] ||
|
||||
rawTask.split(/\s+--/)[0]?.trim() ||
|
||||
"Complete the task as instructed"
|
||||
|
||||
const maxIterMatch = rawTask.match(/--max-iterations=(\d+)/i)
|
||||
const promiseMatch = rawTask.match(
|
||||
/--completion-promise=["']?([^"'\s]+)["']?/i,
|
||||
)
|
||||
|
||||
hooks.ralphLoop.startLoop(input.sessionID, prompt, {
|
||||
maxIterations: maxIterMatch ? parseInt(maxIterMatch[1], 10) : undefined,
|
||||
completionPromise: promiseMatch?.[1],
|
||||
})
|
||||
} else if (isCancelRalphTemplate) {
|
||||
hooks.ralphLoop.cancelLoop(input.sessionID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
71
src/plugin/chat-params.ts
Normal file
71
src/plugin/chat-params.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
type ChatParamsInput = {
|
||||
sessionID: string
|
||||
agent: { name?: string }
|
||||
model: { providerID: string; modelID: string }
|
||||
provider: { id: string }
|
||||
message: { variant?: string }
|
||||
}
|
||||
|
||||
type ChatParamsOutput = {
|
||||
temperature?: number
|
||||
topP?: number
|
||||
topK?: number
|
||||
options: Record<string, unknown>
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null
|
||||
}
|
||||
|
||||
function buildChatParamsInput(raw: unknown): ChatParamsInput | null {
|
||||
if (!isRecord(raw)) return null
|
||||
|
||||
const sessionID = raw.sessionID
|
||||
const agent = raw.agent
|
||||
const model = raw.model
|
||||
const provider = raw.provider
|
||||
const message = raw.message
|
||||
|
||||
if (typeof sessionID !== "string") return null
|
||||
if (typeof agent !== "string") return null
|
||||
if (!isRecord(model)) return null
|
||||
if (!isRecord(provider)) return null
|
||||
if (!isRecord(message)) return null
|
||||
|
||||
const providerID = model.providerID
|
||||
const modelID = model.modelID
|
||||
const providerId = provider.id
|
||||
const variant = message.variant
|
||||
|
||||
if (typeof providerID !== "string") return null
|
||||
if (typeof modelID !== "string") return null
|
||||
if (typeof providerId !== "string") return null
|
||||
|
||||
return {
|
||||
sessionID,
|
||||
agent: { name: agent },
|
||||
model: { providerID, modelID },
|
||||
provider: { id: providerId },
|
||||
message: typeof variant === "string" ? { variant } : {},
|
||||
}
|
||||
}
|
||||
|
||||
function isChatParamsOutput(raw: unknown): raw is ChatParamsOutput {
|
||||
if (!isRecord(raw)) return false
|
||||
if (!isRecord(raw.options)) {
|
||||
raw.options = {}
|
||||
}
|
||||
return isRecord(raw.options)
|
||||
}
|
||||
|
||||
export function createChatParamsHandler(args: {
|
||||
anthropicEffort: { "chat.params"?: (input: ChatParamsInput, output: ChatParamsOutput) => Promise<void> } | null
|
||||
}): (input: unknown, output: unknown) => Promise<void> {
|
||||
return async (input, output): Promise<void> => {
|
||||
const normalizedInput = buildChatParamsInput(input)
|
||||
if (!normalizedInput) return
|
||||
if (!isChatParamsOutput(output)) return
|
||||
|
||||
await args.anthropicEffort?.["chat.params"]?.(normalizedInput, output)
|
||||
}
|
||||
}
|
||||
133
src/plugin/event.ts
Normal file
133
src/plugin/event.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { PluginContext } from "./types"
|
||||
|
||||
import {
|
||||
clearSessionAgent,
|
||||
getMainSessionID,
|
||||
setMainSession,
|
||||
updateSessionAgent,
|
||||
} from "../features/claude-code-session-state"
|
||||
import { resetMessageCursor } from "../shared"
|
||||
import { lspManager } from "../tools"
|
||||
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
import type { Managers } from "../create-managers"
|
||||
|
||||
type FirstMessageVariantGate = {
|
||||
markSessionCreated: (sessionInfo: { id?: string; title?: string; parentID?: string } | undefined) => void
|
||||
clear: (sessionID: string) => void
|
||||
}
|
||||
|
||||
export function createEventHandler(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
firstMessageVariantGate: FirstMessageVariantGate
|
||||
managers: Managers
|
||||
hooks: CreatedHooks
|
||||
}): (input: { event: { type: string; properties?: Record<string, unknown> } }) => Promise<void> {
|
||||
const { ctx, firstMessageVariantGate, managers, hooks } = args
|
||||
|
||||
return async (input): Promise<void> => {
|
||||
await hooks.autoUpdateChecker?.event?.(input)
|
||||
await hooks.claudeCodeHooks?.event?.(input)
|
||||
await hooks.backgroundNotificationHook?.event?.(input)
|
||||
await hooks.sessionNotification?.(input)
|
||||
await hooks.todoContinuationEnforcer?.handler?.(input)
|
||||
await hooks.unstableAgentBabysitter?.event?.(input)
|
||||
await hooks.contextWindowMonitor?.event?.(input)
|
||||
await hooks.directoryAgentsInjector?.event?.(input)
|
||||
await hooks.directoryReadmeInjector?.event?.(input)
|
||||
await hooks.rulesInjector?.event?.(input)
|
||||
await hooks.thinkMode?.event?.(input)
|
||||
await hooks.anthropicContextWindowLimitRecovery?.event?.(input)
|
||||
await hooks.agentUsageReminder?.event?.(input)
|
||||
await hooks.categorySkillReminder?.event?.(input)
|
||||
await hooks.interactiveBashSession?.event?.(input)
|
||||
await hooks.ralphLoop?.event?.(input)
|
||||
await hooks.stopContinuationGuard?.event?.(input)
|
||||
await hooks.compactionTodoPreserver?.event?.(input)
|
||||
await hooks.atlasHook?.handler?.(input)
|
||||
|
||||
const { event } = input
|
||||
const props = event.properties as Record<string, unknown> | undefined
|
||||
|
||||
if (event.type === "session.created") {
|
||||
const sessionInfo = props?.info as
|
||||
| { id?: string; title?: string; parentID?: string }
|
||||
| undefined
|
||||
|
||||
if (!sessionInfo?.parentID) {
|
||||
setMainSession(sessionInfo?.id)
|
||||
}
|
||||
|
||||
firstMessageVariantGate.markSessionCreated(sessionInfo)
|
||||
|
||||
await managers.tmuxSessionManager.onSessionCreated(
|
||||
event as {
|
||||
type: string
|
||||
properties?: {
|
||||
info?: { id?: string; parentID?: string; title?: string }
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if (event.type === "session.deleted") {
|
||||
const sessionInfo = props?.info as { id?: string } | undefined
|
||||
if (sessionInfo?.id === getMainSessionID()) {
|
||||
setMainSession(undefined)
|
||||
}
|
||||
|
||||
if (sessionInfo?.id) {
|
||||
clearSessionAgent(sessionInfo.id)
|
||||
resetMessageCursor(sessionInfo.id)
|
||||
firstMessageVariantGate.clear(sessionInfo.id)
|
||||
await managers.skillMcpManager.disconnectSession(sessionInfo.id)
|
||||
await lspManager.cleanupTempDirectoryClients()
|
||||
await managers.tmuxSessionManager.onSessionDeleted({
|
||||
sessionID: sessionInfo.id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "message.updated") {
|
||||
const info = props?.info as Record<string, unknown> | undefined
|
||||
const sessionID = info?.sessionID as string | undefined
|
||||
const agent = info?.agent as string | undefined
|
||||
const role = info?.role as string | undefined
|
||||
if (sessionID && agent && role === "user") {
|
||||
updateSessionAgent(sessionID, agent)
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "session.error") {
|
||||
const sessionID = props?.sessionID as string | undefined
|
||||
const error = props?.error
|
||||
|
||||
if (hooks.sessionRecovery?.isRecoverableError(error)) {
|
||||
const messageInfo = {
|
||||
id: props?.messageID as string | undefined,
|
||||
role: "assistant" as const,
|
||||
sessionID,
|
||||
error,
|
||||
}
|
||||
const recovered = await hooks.sessionRecovery.handleSessionRecovery(messageInfo)
|
||||
|
||||
if (
|
||||
recovered &&
|
||||
sessionID &&
|
||||
sessionID === getMainSessionID() &&
|
||||
!hooks.stopContinuationGuard?.isStopped(sessionID)
|
||||
) {
|
||||
await ctx.client.session
|
||||
.prompt({
|
||||
path: { id: sessionID },
|
||||
body: { parts: [{ type: "text", text: "continue" }] },
|
||||
query: { directory: ctx.directory },
|
||||
})
|
||||
.catch(() => {})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
104
src/plugin/hooks/create-continuation-hooks.ts
Normal file
104
src/plugin/hooks/create-continuation-hooks.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import type { HookName, OhMyOpenCodeConfig } from "../../config"
|
||||
import type { BackgroundManager } from "../../features/background-agent"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import {
|
||||
createTodoContinuationEnforcer,
|
||||
createBackgroundNotificationHook,
|
||||
createStopContinuationGuardHook,
|
||||
createCompactionContextInjector,
|
||||
createCompactionTodoPreserverHook,
|
||||
createAtlasHook,
|
||||
} from "../../hooks"
|
||||
import { safeCreateHook } from "../../shared/safe-create-hook"
|
||||
import { createUnstableAgentBabysitter } from "../unstable-agent-babysitter"
|
||||
|
||||
export type ContinuationHooks = {
|
||||
stopContinuationGuard: ReturnType<typeof createStopContinuationGuardHook> | null
|
||||
compactionContextInjector: ReturnType<typeof createCompactionContextInjector> | null
|
||||
compactionTodoPreserver: ReturnType<typeof createCompactionTodoPreserverHook> | null
|
||||
todoContinuationEnforcer: ReturnType<typeof createTodoContinuationEnforcer> | null
|
||||
unstableAgentBabysitter: ReturnType<typeof createUnstableAgentBabysitter> | null
|
||||
backgroundNotificationHook: ReturnType<typeof createBackgroundNotificationHook> | null
|
||||
atlasHook: ReturnType<typeof createAtlasHook> | null
|
||||
}
|
||||
|
||||
type SessionRecovery = {
|
||||
setOnAbortCallback: (callback: (sessionID: string) => void) => void
|
||||
setOnRecoveryCompleteCallback: (callback: (sessionID: string) => void) => void
|
||||
} | null
|
||||
|
||||
export function createContinuationHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
backgroundManager: BackgroundManager
|
||||
sessionRecovery: SessionRecovery
|
||||
}): ContinuationHooks {
|
||||
const {
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
backgroundManager,
|
||||
sessionRecovery,
|
||||
} = args
|
||||
|
||||
const safeHook = <T>(hookName: HookName, factory: () => T): T | null =>
|
||||
safeCreateHook(hookName, factory, { enabled: safeHookEnabled })
|
||||
|
||||
const stopContinuationGuard = isHookEnabled("stop-continuation-guard")
|
||||
? safeHook("stop-continuation-guard", () => createStopContinuationGuardHook(ctx))
|
||||
: null
|
||||
|
||||
const compactionContextInjector = isHookEnabled("compaction-context-injector")
|
||||
? safeHook("compaction-context-injector", () => createCompactionContextInjector())
|
||||
: null
|
||||
|
||||
const compactionTodoPreserver = isHookEnabled("compaction-todo-preserver")
|
||||
? safeHook("compaction-todo-preserver", () => createCompactionTodoPreserverHook(ctx))
|
||||
: null
|
||||
|
||||
const todoContinuationEnforcer = isHookEnabled("todo-continuation-enforcer")
|
||||
? safeHook("todo-continuation-enforcer", () =>
|
||||
createTodoContinuationEnforcer(ctx, {
|
||||
backgroundManager,
|
||||
isContinuationStopped: stopContinuationGuard?.isStopped,
|
||||
}))
|
||||
: null
|
||||
|
||||
const unstableAgentBabysitter = isHookEnabled("unstable-agent-babysitter")
|
||||
? safeHook("unstable-agent-babysitter", () =>
|
||||
createUnstableAgentBabysitter({ ctx, backgroundManager, pluginConfig }))
|
||||
: null
|
||||
|
||||
if (sessionRecovery && todoContinuationEnforcer) {
|
||||
sessionRecovery.setOnAbortCallback(todoContinuationEnforcer.markRecovering)
|
||||
sessionRecovery.setOnRecoveryCompleteCallback(todoContinuationEnforcer.markRecoveryComplete)
|
||||
}
|
||||
|
||||
const backgroundNotificationHook = isHookEnabled("background-notification")
|
||||
? safeHook("background-notification", () => createBackgroundNotificationHook(backgroundManager))
|
||||
: null
|
||||
|
||||
const atlasHook = isHookEnabled("atlas")
|
||||
? safeHook("atlas", () =>
|
||||
createAtlasHook(ctx, {
|
||||
directory: ctx.directory,
|
||||
backgroundManager,
|
||||
isContinuationStopped: (sessionID: string) =>
|
||||
stopContinuationGuard?.isStopped(sessionID) ?? false,
|
||||
}))
|
||||
: null
|
||||
|
||||
return {
|
||||
stopContinuationGuard,
|
||||
compactionContextInjector,
|
||||
compactionTodoPreserver,
|
||||
todoContinuationEnforcer,
|
||||
unstableAgentBabysitter,
|
||||
backgroundNotificationHook,
|
||||
atlasHook,
|
||||
}
|
||||
}
|
||||
42
src/plugin/hooks/create-core-hooks.ts
Normal file
42
src/plugin/hooks/create-core-hooks.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { HookName, OhMyOpenCodeConfig } from "../../config"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import { createSessionHooks } from "./create-session-hooks"
|
||||
import { createToolGuardHooks } from "./create-tool-guard-hooks"
|
||||
import { createTransformHooks } from "./create-transform-hooks"
|
||||
|
||||
export function createCoreHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
}) {
|
||||
const { ctx, pluginConfig, isHookEnabled, safeHookEnabled } = args
|
||||
|
||||
const session = createSessionHooks({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
})
|
||||
|
||||
const tool = createToolGuardHooks({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled,
|
||||
safeHookEnabled,
|
||||
})
|
||||
|
||||
const transform = createTransformHooks({
|
||||
ctx,
|
||||
pluginConfig,
|
||||
isHookEnabled: (name) => isHookEnabled(name as HookName),
|
||||
safeHookEnabled,
|
||||
})
|
||||
|
||||
return {
|
||||
...session,
|
||||
...tool,
|
||||
...transform,
|
||||
}
|
||||
}
|
||||
181
src/plugin/hooks/create-session-hooks.ts
Normal file
181
src/plugin/hooks/create-session-hooks.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import type { OhMyOpenCodeConfig, HookName } from "../../config"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import {
|
||||
createContextWindowMonitorHook,
|
||||
createSessionRecoveryHook,
|
||||
createSessionNotification,
|
||||
createThinkModeHook,
|
||||
createAnthropicContextWindowLimitRecoveryHook,
|
||||
createAutoUpdateCheckerHook,
|
||||
createAgentUsageReminderHook,
|
||||
createNonInteractiveEnvHook,
|
||||
createInteractiveBashSessionHook,
|
||||
createRalphLoopHook,
|
||||
createEditErrorRecoveryHook,
|
||||
createDelegateTaskRetryHook,
|
||||
createTaskResumeInfoHook,
|
||||
createStartWorkHook,
|
||||
createPrometheusMdOnlyHook,
|
||||
createSisyphusJuniorNotepadHook,
|
||||
createQuestionLabelTruncatorHook,
|
||||
createSubagentQuestionBlockerHook,
|
||||
createPreemptiveCompactionHook,
|
||||
} from "../../hooks"
|
||||
import { createAnthropicEffortHook } from "../../hooks/anthropic-effort"
|
||||
import {
|
||||
detectExternalNotificationPlugin,
|
||||
getNotificationConflictWarning,
|
||||
log,
|
||||
} from "../../shared"
|
||||
import { safeCreateHook } from "../../shared/safe-create-hook"
|
||||
import { sessionExists } from "../../tools"
|
||||
|
||||
export type SessionHooks = {
|
||||
contextWindowMonitor: ReturnType<typeof createContextWindowMonitorHook> | null
|
||||
preemptiveCompaction: ReturnType<typeof createPreemptiveCompactionHook> | null
|
||||
sessionRecovery: ReturnType<typeof createSessionRecoveryHook> | null
|
||||
sessionNotification: ReturnType<typeof createSessionNotification> | null
|
||||
thinkMode: ReturnType<typeof createThinkModeHook> | null
|
||||
anthropicContextWindowLimitRecovery: ReturnType<typeof createAnthropicContextWindowLimitRecoveryHook> | null
|
||||
autoUpdateChecker: ReturnType<typeof createAutoUpdateCheckerHook> | null
|
||||
agentUsageReminder: ReturnType<typeof createAgentUsageReminderHook> | null
|
||||
nonInteractiveEnv: ReturnType<typeof createNonInteractiveEnvHook> | null
|
||||
interactiveBashSession: ReturnType<typeof createInteractiveBashSessionHook> | null
|
||||
ralphLoop: ReturnType<typeof createRalphLoopHook> | null
|
||||
editErrorRecovery: ReturnType<typeof createEditErrorRecoveryHook> | null
|
||||
delegateTaskRetry: ReturnType<typeof createDelegateTaskRetryHook> | null
|
||||
startWork: ReturnType<typeof createStartWorkHook> | null
|
||||
prometheusMdOnly: ReturnType<typeof createPrometheusMdOnlyHook> | null
|
||||
sisyphusJuniorNotepad: ReturnType<typeof createSisyphusJuniorNotepadHook> | null
|
||||
questionLabelTruncator: ReturnType<typeof createQuestionLabelTruncatorHook>
|
||||
subagentQuestionBlocker: ReturnType<typeof createSubagentQuestionBlockerHook>
|
||||
taskResumeInfo: ReturnType<typeof createTaskResumeInfoHook>
|
||||
anthropicEffort: ReturnType<typeof createAnthropicEffortHook> | null
|
||||
}
|
||||
|
||||
export function createSessionHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
}): SessionHooks {
|
||||
const { ctx, pluginConfig, isHookEnabled, safeHookEnabled } = args
|
||||
const safeHook = <T>(hookName: HookName, factory: () => T): T | null =>
|
||||
safeCreateHook(hookName, factory, { enabled: safeHookEnabled })
|
||||
|
||||
const contextWindowMonitor = isHookEnabled("context-window-monitor")
|
||||
? safeHook("context-window-monitor", () => createContextWindowMonitorHook(ctx))
|
||||
: null
|
||||
|
||||
const preemptiveCompaction =
|
||||
isHookEnabled("preemptive-compaction") &&
|
||||
pluginConfig.experimental?.preemptive_compaction
|
||||
? safeHook("preemptive-compaction", () => createPreemptiveCompactionHook(ctx))
|
||||
: null
|
||||
|
||||
const sessionRecovery = isHookEnabled("session-recovery")
|
||||
? safeHook("session-recovery", () =>
|
||||
createSessionRecoveryHook(ctx, { experimental: pluginConfig.experimental }))
|
||||
: null
|
||||
|
||||
let sessionNotification: ReturnType<typeof createSessionNotification> | null = null
|
||||
if (isHookEnabled("session-notification")) {
|
||||
const forceEnable = pluginConfig.notification?.force_enable ?? false
|
||||
const externalNotifier = detectExternalNotificationPlugin(ctx.directory)
|
||||
if (externalNotifier.detected && !forceEnable) {
|
||||
log(getNotificationConflictWarning(externalNotifier.pluginName!))
|
||||
} else {
|
||||
sessionNotification = safeHook("session-notification", () => createSessionNotification(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
const thinkMode = isHookEnabled("think-mode")
|
||||
? safeHook("think-mode", () => createThinkModeHook())
|
||||
: null
|
||||
|
||||
const anthropicContextWindowLimitRecovery = isHookEnabled("anthropic-context-window-limit-recovery")
|
||||
? safeHook("anthropic-context-window-limit-recovery", () =>
|
||||
createAnthropicContextWindowLimitRecoveryHook(ctx, { experimental: pluginConfig.experimental }))
|
||||
: null
|
||||
|
||||
const autoUpdateChecker = isHookEnabled("auto-update-checker")
|
||||
? safeHook("auto-update-checker", () =>
|
||||
createAutoUpdateCheckerHook(ctx, {
|
||||
showStartupToast: isHookEnabled("startup-toast"),
|
||||
isSisyphusEnabled: pluginConfig.sisyphus_agent?.disabled !== true,
|
||||
autoUpdate: pluginConfig.auto_update ?? true,
|
||||
}))
|
||||
: null
|
||||
|
||||
const agentUsageReminder = isHookEnabled("agent-usage-reminder")
|
||||
? safeHook("agent-usage-reminder", () => createAgentUsageReminderHook(ctx))
|
||||
: null
|
||||
|
||||
const nonInteractiveEnv = isHookEnabled("non-interactive-env")
|
||||
? safeHook("non-interactive-env", () => createNonInteractiveEnvHook(ctx))
|
||||
: null
|
||||
|
||||
const interactiveBashSession = isHookEnabled("interactive-bash-session")
|
||||
? safeHook("interactive-bash-session", () => createInteractiveBashSessionHook(ctx))
|
||||
: null
|
||||
|
||||
const ralphLoop = isHookEnabled("ralph-loop")
|
||||
? safeHook("ralph-loop", () =>
|
||||
createRalphLoopHook(ctx, {
|
||||
config: pluginConfig.ralph_loop,
|
||||
checkSessionExists: async (sessionId) => sessionExists(sessionId),
|
||||
}))
|
||||
: null
|
||||
|
||||
const editErrorRecovery = isHookEnabled("edit-error-recovery")
|
||||
? safeHook("edit-error-recovery", () => createEditErrorRecoveryHook(ctx))
|
||||
: null
|
||||
|
||||
const delegateTaskRetry = isHookEnabled("delegate-task-retry")
|
||||
? safeHook("delegate-task-retry", () => createDelegateTaskRetryHook(ctx))
|
||||
: null
|
||||
|
||||
const startWork = isHookEnabled("start-work")
|
||||
? safeHook("start-work", () => createStartWorkHook(ctx))
|
||||
: null
|
||||
|
||||
const prometheusMdOnly = isHookEnabled("prometheus-md-only")
|
||||
? safeHook("prometheus-md-only", () => createPrometheusMdOnlyHook(ctx))
|
||||
: null
|
||||
|
||||
const sisyphusJuniorNotepad = isHookEnabled("sisyphus-junior-notepad")
|
||||
? safeHook("sisyphus-junior-notepad", () => createSisyphusJuniorNotepadHook(ctx))
|
||||
: null
|
||||
|
||||
const questionLabelTruncator = createQuestionLabelTruncatorHook()
|
||||
const subagentQuestionBlocker = createSubagentQuestionBlockerHook()
|
||||
const taskResumeInfo = createTaskResumeInfoHook()
|
||||
|
||||
const anthropicEffort = isHookEnabled("anthropic-effort")
|
||||
? safeHook("anthropic-effort", () => createAnthropicEffortHook())
|
||||
: null
|
||||
|
||||
return {
|
||||
contextWindowMonitor,
|
||||
preemptiveCompaction,
|
||||
sessionRecovery,
|
||||
sessionNotification,
|
||||
thinkMode,
|
||||
anthropicContextWindowLimitRecovery,
|
||||
autoUpdateChecker,
|
||||
agentUsageReminder,
|
||||
nonInteractiveEnv,
|
||||
interactiveBashSession,
|
||||
ralphLoop,
|
||||
editErrorRecovery,
|
||||
delegateTaskRetry,
|
||||
startWork,
|
||||
prometheusMdOnly,
|
||||
sisyphusJuniorNotepad,
|
||||
questionLabelTruncator,
|
||||
subagentQuestionBlocker,
|
||||
taskResumeInfo,
|
||||
anthropicEffort,
|
||||
}
|
||||
}
|
||||
37
src/plugin/hooks/create-skill-hooks.ts
Normal file
37
src/plugin/hooks/create-skill-hooks.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { AvailableSkill } from "../../agents/dynamic-agent-prompt-builder"
|
||||
import type { HookName } from "../../config"
|
||||
import type { LoadedSkill } from "../../features/opencode-skill-loader/types"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import { createAutoSlashCommandHook, createCategorySkillReminderHook } from "../../hooks"
|
||||
import { safeCreateHook } from "../../shared/safe-create-hook"
|
||||
|
||||
export type SkillHooks = {
|
||||
categorySkillReminder: ReturnType<typeof createCategorySkillReminderHook> | null
|
||||
autoSlashCommand: ReturnType<typeof createAutoSlashCommandHook> | null
|
||||
}
|
||||
|
||||
export function createSkillHooks(args: {
|
||||
ctx: PluginContext
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
mergedSkills: LoadedSkill[]
|
||||
availableSkills: AvailableSkill[]
|
||||
}): SkillHooks {
|
||||
const { ctx, isHookEnabled, safeHookEnabled, mergedSkills, availableSkills } = args
|
||||
|
||||
const safeHook = <T>(hookName: HookName, factory: () => T): T | null =>
|
||||
safeCreateHook(hookName, factory, { enabled: safeHookEnabled })
|
||||
|
||||
const categorySkillReminder = isHookEnabled("category-skill-reminder")
|
||||
? safeHook("category-skill-reminder", () =>
|
||||
createCategorySkillReminderHook(ctx, availableSkills))
|
||||
: null
|
||||
|
||||
const autoSlashCommand = isHookEnabled("auto-slash-command")
|
||||
? safeHook("auto-slash-command", () =>
|
||||
createAutoSlashCommandHook({ skills: mergedSkills }))
|
||||
: null
|
||||
|
||||
return { categorySkillReminder, autoSlashCommand }
|
||||
}
|
||||
98
src/plugin/hooks/create-tool-guard-hooks.ts
Normal file
98
src/plugin/hooks/create-tool-guard-hooks.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import type { HookName, OhMyOpenCodeConfig } from "../../config"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import {
|
||||
createCommentCheckerHooks,
|
||||
createToolOutputTruncatorHook,
|
||||
createDirectoryAgentsInjectorHook,
|
||||
createDirectoryReadmeInjectorHook,
|
||||
createEmptyTaskResponseDetectorHook,
|
||||
createRulesInjectorHook,
|
||||
createTasksTodowriteDisablerHook,
|
||||
createWriteExistingFileGuardHook,
|
||||
} from "../../hooks"
|
||||
import {
|
||||
getOpenCodeVersion,
|
||||
isOpenCodeVersionAtLeast,
|
||||
log,
|
||||
OPENCODE_NATIVE_AGENTS_INJECTION_VERSION,
|
||||
} from "../../shared"
|
||||
import { safeCreateHook } from "../../shared/safe-create-hook"
|
||||
|
||||
export type ToolGuardHooks = {
|
||||
commentChecker: ReturnType<typeof createCommentCheckerHooks> | null
|
||||
toolOutputTruncator: ReturnType<typeof createToolOutputTruncatorHook> | null
|
||||
directoryAgentsInjector: ReturnType<typeof createDirectoryAgentsInjectorHook> | null
|
||||
directoryReadmeInjector: ReturnType<typeof createDirectoryReadmeInjectorHook> | null
|
||||
emptyTaskResponseDetector: ReturnType<typeof createEmptyTaskResponseDetectorHook> | null
|
||||
rulesInjector: ReturnType<typeof createRulesInjectorHook> | null
|
||||
tasksTodowriteDisabler: ReturnType<typeof createTasksTodowriteDisablerHook> | null
|
||||
writeExistingFileGuard: ReturnType<typeof createWriteExistingFileGuardHook> | null
|
||||
}
|
||||
|
||||
export function createToolGuardHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
isHookEnabled: (hookName: HookName) => boolean
|
||||
safeHookEnabled: boolean
|
||||
}): ToolGuardHooks {
|
||||
const { ctx, pluginConfig, isHookEnabled, safeHookEnabled } = args
|
||||
const safeHook = <T>(hookName: HookName, factory: () => T): T | null =>
|
||||
safeCreateHook(hookName, factory, { enabled: safeHookEnabled })
|
||||
|
||||
const commentChecker = isHookEnabled("comment-checker")
|
||||
? safeHook("comment-checker", () => createCommentCheckerHooks(pluginConfig.comment_checker))
|
||||
: null
|
||||
|
||||
const toolOutputTruncator = isHookEnabled("tool-output-truncator")
|
||||
? safeHook("tool-output-truncator", () =>
|
||||
createToolOutputTruncatorHook(ctx, { experimental: pluginConfig.experimental }))
|
||||
: null
|
||||
|
||||
let directoryAgentsInjector: ReturnType<typeof createDirectoryAgentsInjectorHook> | null = null
|
||||
if (isHookEnabled("directory-agents-injector")) {
|
||||
const currentVersion = getOpenCodeVersion()
|
||||
const hasNativeSupport =
|
||||
currentVersion !== null && isOpenCodeVersionAtLeast(OPENCODE_NATIVE_AGENTS_INJECTION_VERSION)
|
||||
if (hasNativeSupport) {
|
||||
log("directory-agents-injector auto-disabled due to native OpenCode support", {
|
||||
currentVersion,
|
||||
nativeVersion: OPENCODE_NATIVE_AGENTS_INJECTION_VERSION,
|
||||
})
|
||||
} else {
|
||||
directoryAgentsInjector = safeHook("directory-agents-injector", () => createDirectoryAgentsInjectorHook(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
const directoryReadmeInjector = isHookEnabled("directory-readme-injector")
|
||||
? safeHook("directory-readme-injector", () => createDirectoryReadmeInjectorHook(ctx))
|
||||
: null
|
||||
|
||||
const emptyTaskResponseDetector = isHookEnabled("empty-task-response-detector")
|
||||
? safeHook("empty-task-response-detector", () => createEmptyTaskResponseDetectorHook(ctx))
|
||||
: null
|
||||
|
||||
const rulesInjector = isHookEnabled("rules-injector")
|
||||
? safeHook("rules-injector", () => createRulesInjectorHook(ctx))
|
||||
: null
|
||||
|
||||
const tasksTodowriteDisabler = isHookEnabled("tasks-todowrite-disabler")
|
||||
? safeHook("tasks-todowrite-disabler", () =>
|
||||
createTasksTodowriteDisablerHook({ experimental: pluginConfig.experimental }))
|
||||
: null
|
||||
|
||||
const writeExistingFileGuard = isHookEnabled("write-existing-file-guard")
|
||||
? safeHook("write-existing-file-guard", () => createWriteExistingFileGuardHook(ctx))
|
||||
: null
|
||||
|
||||
return {
|
||||
commentChecker,
|
||||
toolOutputTruncator,
|
||||
directoryAgentsInjector,
|
||||
directoryReadmeInjector,
|
||||
emptyTaskResponseDetector,
|
||||
rulesInjector,
|
||||
tasksTodowriteDisabler,
|
||||
writeExistingFileGuard,
|
||||
}
|
||||
}
|
||||
65
src/plugin/hooks/create-transform-hooks.ts
Normal file
65
src/plugin/hooks/create-transform-hooks.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { OhMyOpenCodeConfig } from "../../config"
|
||||
import type { PluginContext } from "../types"
|
||||
|
||||
import {
|
||||
createClaudeCodeHooksHook,
|
||||
createKeywordDetectorHook,
|
||||
createThinkingBlockValidatorHook,
|
||||
} from "../../hooks"
|
||||
import {
|
||||
contextCollector,
|
||||
createContextInjectorMessagesTransformHook,
|
||||
} from "../../features/context-injector"
|
||||
import { safeCreateHook } from "../../shared/safe-create-hook"
|
||||
|
||||
export type TransformHooks = {
|
||||
claudeCodeHooks: ReturnType<typeof createClaudeCodeHooksHook>
|
||||
keywordDetector: ReturnType<typeof createKeywordDetectorHook> | null
|
||||
contextInjectorMessagesTransform: ReturnType<typeof createContextInjectorMessagesTransformHook>
|
||||
thinkingBlockValidator: ReturnType<typeof createThinkingBlockValidatorHook> | null
|
||||
}
|
||||
|
||||
export function createTransformHooks(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
isHookEnabled: (hookName: string) => boolean
|
||||
safeHookEnabled?: boolean
|
||||
}): TransformHooks {
|
||||
const { ctx, pluginConfig, isHookEnabled } = args
|
||||
const safeHookEnabled = args.safeHookEnabled ?? true
|
||||
|
||||
const claudeCodeHooks = createClaudeCodeHooksHook(
|
||||
ctx,
|
||||
{
|
||||
disabledHooks: (pluginConfig.claude_code?.hooks ?? true) ? undefined : true,
|
||||
keywordDetectorDisabled: !isHookEnabled("keyword-detector"),
|
||||
},
|
||||
contextCollector,
|
||||
)
|
||||
|
||||
const keywordDetector = isHookEnabled("keyword-detector")
|
||||
? safeCreateHook(
|
||||
"keyword-detector",
|
||||
() => createKeywordDetectorHook(ctx, contextCollector),
|
||||
{ enabled: safeHookEnabled },
|
||||
)
|
||||
: null
|
||||
|
||||
const contextInjectorMessagesTransform =
|
||||
createContextInjectorMessagesTransformHook(contextCollector)
|
||||
|
||||
const thinkingBlockValidator = isHookEnabled("thinking-block-validator")
|
||||
? safeCreateHook(
|
||||
"thinking-block-validator",
|
||||
() => createThinkingBlockValidatorHook(),
|
||||
{ enabled: safeHookEnabled },
|
||||
)
|
||||
: null
|
||||
|
||||
return {
|
||||
claudeCodeHooks,
|
||||
keywordDetector,
|
||||
contextInjectorMessagesTransform,
|
||||
thinkingBlockValidator,
|
||||
}
|
||||
}
|
||||
24
src/plugin/messages-transform.ts
Normal file
24
src/plugin/messages-transform.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { Message, Part } from "@opencode-ai/sdk"
|
||||
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
|
||||
type MessageWithParts = {
|
||||
info: Message
|
||||
parts: Part[]
|
||||
}
|
||||
|
||||
type MessagesTransformOutput = { messages: MessageWithParts[] }
|
||||
|
||||
export function createMessagesTransformHandler(args: {
|
||||
hooks: CreatedHooks
|
||||
}): (input: Record<string, never>, output: MessagesTransformOutput) => Promise<void> {
|
||||
return async (input, output): Promise<void> => {
|
||||
await args.hooks.contextInjectorMessagesTransform?.[
|
||||
"experimental.chat.messages.transform"
|
||||
]?.(input, output)
|
||||
|
||||
await args.hooks.thinkingBlockValidator?.[
|
||||
"experimental.chat.messages.transform"
|
||||
]?.(input, output)
|
||||
}
|
||||
}
|
||||
87
src/plugin/skill-context.ts
Normal file
87
src/plugin/skill-context.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { AvailableSkill } from "../agents/dynamic-agent-prompt-builder"
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { BrowserAutomationProvider } from "../config/schema/browser-automation"
|
||||
import type {
|
||||
LoadedSkill,
|
||||
SkillScope,
|
||||
} from "../features/opencode-skill-loader/types"
|
||||
|
||||
import {
|
||||
discoverUserClaudeSkills,
|
||||
discoverProjectClaudeSkills,
|
||||
discoverOpencodeGlobalSkills,
|
||||
discoverOpencodeProjectSkills,
|
||||
mergeSkills,
|
||||
} from "../features/opencode-skill-loader"
|
||||
import { createBuiltinSkills } from "../features/builtin-skills"
|
||||
import { getSystemMcpServerNames } from "../features/claude-code-mcp-loader"
|
||||
|
||||
export type SkillContext = {
|
||||
mergedSkills: LoadedSkill[]
|
||||
availableSkills: AvailableSkill[]
|
||||
browserProvider: BrowserAutomationProvider
|
||||
disabledSkills: Set<string>
|
||||
}
|
||||
|
||||
function mapScopeToLocation(scope: SkillScope): AvailableSkill["location"] {
|
||||
if (scope === "user" || scope === "opencode") return "user"
|
||||
if (scope === "project" || scope === "opencode-project") return "project"
|
||||
return "plugin"
|
||||
}
|
||||
|
||||
export async function createSkillContext(args: {
|
||||
directory: string
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
}): Promise<SkillContext> {
|
||||
const { directory, pluginConfig } = args
|
||||
|
||||
const browserProvider: BrowserAutomationProvider =
|
||||
pluginConfig.browser_automation_engine?.provider ?? "playwright"
|
||||
|
||||
const disabledSkills = new Set<string>(pluginConfig.disabled_skills ?? [])
|
||||
const systemMcpNames = getSystemMcpServerNames()
|
||||
|
||||
const builtinSkills = createBuiltinSkills({
|
||||
browserProvider,
|
||||
disabledSkills,
|
||||
}).filter((skill) => {
|
||||
if (skill.mcpConfig) {
|
||||
for (const mcpName of Object.keys(skill.mcpConfig)) {
|
||||
if (systemMcpNames.has(mcpName)) return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
const includeClaudeSkills = pluginConfig.claude_code?.skills !== false
|
||||
const [userSkills, globalSkills, projectSkills, opencodeProjectSkills] =
|
||||
await Promise.all([
|
||||
includeClaudeSkills ? discoverUserClaudeSkills() : Promise.resolve([]),
|
||||
discoverOpencodeGlobalSkills(),
|
||||
includeClaudeSkills ? discoverProjectClaudeSkills() : Promise.resolve([]),
|
||||
discoverOpencodeProjectSkills(),
|
||||
])
|
||||
|
||||
const mergedSkills = mergeSkills(
|
||||
builtinSkills,
|
||||
pluginConfig.skills,
|
||||
userSkills,
|
||||
globalSkills,
|
||||
projectSkills,
|
||||
opencodeProjectSkills,
|
||||
{ configDir: directory },
|
||||
)
|
||||
|
||||
const availableSkills: AvailableSkill[] = mergedSkills.map((skill) => ({
|
||||
name: skill.name,
|
||||
description: skill.definition.description ?? "",
|
||||
location: mapScopeToLocation(skill.scope),
|
||||
}))
|
||||
|
||||
return {
|
||||
mergedSkills,
|
||||
availableSkills,
|
||||
browserProvider,
|
||||
disabledSkills,
|
||||
}
|
||||
}
|
||||
47
src/plugin/tool-execute-after.ts
Normal file
47
src/plugin/tool-execute-after.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { consumeToolMetadata } from "../features/tool-metadata-store"
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
|
||||
export function createToolExecuteAfterHandler(args: {
|
||||
hooks: CreatedHooks
|
||||
}): (
|
||||
input: { tool: string; sessionID: string; callID: string },
|
||||
output:
|
||||
| { title: string; output: string; metadata: Record<string, unknown> }
|
||||
| undefined,
|
||||
) => Promise<void> {
|
||||
const { hooks } = args
|
||||
|
||||
return async (
|
||||
input: { tool: string; sessionID: string; callID: string },
|
||||
output: { title: string; output: string; metadata: Record<string, unknown> } | undefined,
|
||||
): Promise<void> => {
|
||||
if (!output) return
|
||||
|
||||
const stored = consumeToolMetadata(input.sessionID, input.callID)
|
||||
if (stored) {
|
||||
if (stored.title) {
|
||||
output.title = stored.title
|
||||
}
|
||||
if (stored.metadata) {
|
||||
output.metadata = { ...output.metadata, ...stored.metadata }
|
||||
}
|
||||
}
|
||||
|
||||
await hooks.claudeCodeHooks?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.toolOutputTruncator?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.preemptiveCompaction?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.contextWindowMonitor?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.commentChecker?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.directoryAgentsInjector?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.directoryReadmeInjector?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.rulesInjector?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.emptyTaskResponseDetector?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.agentUsageReminder?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.categorySkillReminder?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.interactiveBashSession?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.editErrorRecovery?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.delegateTaskRetry?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.atlasHook?.["tool.execute.after"]?.(input, output)
|
||||
await hooks.taskResumeInfo?.["tool.execute.after"]?.(input, output)
|
||||
}
|
||||
}
|
||||
99
src/plugin/tool-execute-before.ts
Normal file
99
src/plugin/tool-execute-before.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type { PluginContext } from "./types"
|
||||
|
||||
import { getMainSessionID } from "../features/claude-code-session-state"
|
||||
import { clearBoulderState } from "../features/boulder-state"
|
||||
import { log } from "../shared"
|
||||
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
|
||||
export function createToolExecuteBeforeHandler(args: {
|
||||
ctx: PluginContext
|
||||
hooks: CreatedHooks
|
||||
}): (
|
||||
input: { tool: string; sessionID: string; callID: string },
|
||||
output: { args: Record<string, unknown> },
|
||||
) => Promise<void> {
|
||||
const { ctx, hooks } = args
|
||||
|
||||
return async (input, output): Promise<void> => {
|
||||
await hooks.subagentQuestionBlocker?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.writeExistingFileGuard?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.questionLabelTruncator?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.claudeCodeHooks?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.nonInteractiveEnv?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.commentChecker?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.directoryAgentsInjector?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.directoryReadmeInjector?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.rulesInjector?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.tasksTodowriteDisabler?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.prometheusMdOnly?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.sisyphusJuniorNotepad?.["tool.execute.before"]?.(input, output)
|
||||
await hooks.atlasHook?.["tool.execute.before"]?.(input, output)
|
||||
|
||||
if (input.tool === "task") {
|
||||
const argsObject = output.args
|
||||
const category = typeof argsObject.category === "string" ? argsObject.category : undefined
|
||||
const subagentType = typeof argsObject.subagent_type === "string" ? argsObject.subagent_type : undefined
|
||||
if (category && !subagentType) {
|
||||
argsObject.subagent_type = "sisyphus-junior"
|
||||
}
|
||||
}
|
||||
|
||||
if (hooks.ralphLoop && input.tool === "slashcommand") {
|
||||
const rawCommand = typeof output.args.command === "string" ? output.args.command : undefined
|
||||
const command = rawCommand?.replace(/^\//, "").toLowerCase()
|
||||
const sessionID = input.sessionID || getMainSessionID()
|
||||
|
||||
if (command === "ralph-loop" && sessionID) {
|
||||
const rawArgs = rawCommand?.replace(/^\/?(ralph-loop)\s*/i, "") || ""
|
||||
const taskMatch = rawArgs.match(/^["'](.+?)["']/)
|
||||
const prompt =
|
||||
taskMatch?.[1] ||
|
||||
rawArgs.split(/\s+--/)[0]?.trim() ||
|
||||
"Complete the task as instructed"
|
||||
|
||||
const maxIterMatch = rawArgs.match(/--max-iterations=(\d+)/i)
|
||||
const promiseMatch = rawArgs.match(/--completion-promise=["']?([^"'\s]+)["']?/i)
|
||||
|
||||
hooks.ralphLoop.startLoop(sessionID, prompt, {
|
||||
maxIterations: maxIterMatch ? parseInt(maxIterMatch[1], 10) : undefined,
|
||||
completionPromise: promiseMatch?.[1],
|
||||
})
|
||||
} else if (command === "cancel-ralph" && sessionID) {
|
||||
hooks.ralphLoop.cancelLoop(sessionID)
|
||||
} else if (command === "ulw-loop" && sessionID) {
|
||||
const rawArgs = rawCommand?.replace(/^\/?(ulw-loop)\s*/i, "") || ""
|
||||
const taskMatch = rawArgs.match(/^["'](.+?)["']/)
|
||||
const prompt =
|
||||
taskMatch?.[1] ||
|
||||
rawArgs.split(/\s+--/)[0]?.trim() ||
|
||||
"Complete the task as instructed"
|
||||
|
||||
const maxIterMatch = rawArgs.match(/--max-iterations=(\d+)/i)
|
||||
const promiseMatch = rawArgs.match(/--completion-promise=["']?([^"'\s]+)["']?/i)
|
||||
|
||||
hooks.ralphLoop.startLoop(sessionID, prompt, {
|
||||
ultrawork: true,
|
||||
maxIterations: maxIterMatch ? parseInt(maxIterMatch[1], 10) : undefined,
|
||||
completionPromise: promiseMatch?.[1],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (input.tool === "slashcommand") {
|
||||
const rawCommand = typeof output.args.command === "string" ? output.args.command : undefined
|
||||
const command = rawCommand?.replace(/^\//, "").toLowerCase()
|
||||
const sessionID = input.sessionID || getMainSessionID()
|
||||
|
||||
if (command === "stop-continuation" && sessionID) {
|
||||
hooks.stopContinuationGuard?.stop(sessionID)
|
||||
hooks.todoContinuationEnforcer?.cancelAllCountdowns()
|
||||
hooks.ralphLoop?.cancelLoop(sessionID)
|
||||
clearBoulderState(ctx.directory)
|
||||
log("[stop-continuation] All continuation mechanisms stopped", {
|
||||
sessionID,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
143
src/plugin/tool-registry.ts
Normal file
143
src/plugin/tool-registry.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import type { ToolDefinition } from "@opencode-ai/plugin"
|
||||
|
||||
import type {
|
||||
AvailableCategory,
|
||||
} from "../agents/dynamic-agent-prompt-builder"
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { PluginContext, ToolsRecord } from "./types"
|
||||
|
||||
import {
|
||||
builtinTools,
|
||||
createBackgroundTools,
|
||||
createCallOmoAgent,
|
||||
createLookAt,
|
||||
createSkillTool,
|
||||
createSkillMcpTool,
|
||||
createSlashcommandTool,
|
||||
createGrepTools,
|
||||
createGlobTools,
|
||||
createAstGrepTools,
|
||||
createSessionManagerTools,
|
||||
createDelegateTask,
|
||||
discoverCommandsSync,
|
||||
interactive_bash,
|
||||
createTaskCreateTool,
|
||||
createTaskGetTool,
|
||||
createTaskList,
|
||||
createTaskUpdateTool,
|
||||
} from "../tools"
|
||||
import { getMainSessionID } from "../features/claude-code-session-state"
|
||||
import { filterDisabledTools } from "../shared/disabled-tools"
|
||||
import { log } from "../shared"
|
||||
|
||||
import type { Managers } from "../create-managers"
|
||||
import type { SkillContext } from "./skill-context"
|
||||
|
||||
export type ToolRegistryResult = {
|
||||
filteredTools: ToolsRecord
|
||||
taskSystemEnabled: boolean
|
||||
}
|
||||
|
||||
export function createToolRegistry(args: {
|
||||
ctx: PluginContext
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
managers: Pick<Managers, "backgroundManager" | "tmuxSessionManager" | "skillMcpManager">
|
||||
skillContext: SkillContext
|
||||
availableCategories: AvailableCategory[]
|
||||
}): ToolRegistryResult {
|
||||
const { ctx, pluginConfig, managers, skillContext, availableCategories } = args
|
||||
|
||||
const backgroundTools = createBackgroundTools(managers.backgroundManager, ctx.client)
|
||||
const callOmoAgent = createCallOmoAgent(ctx, managers.backgroundManager)
|
||||
|
||||
const isMultimodalLookerEnabled = !(pluginConfig.disabled_agents ?? []).some(
|
||||
(agent) => agent.toLowerCase() === "multimodal-looker",
|
||||
)
|
||||
const lookAt = isMultimodalLookerEnabled ? createLookAt(ctx) : null
|
||||
|
||||
const delegateTask = createDelegateTask({
|
||||
manager: managers.backgroundManager,
|
||||
client: ctx.client,
|
||||
directory: ctx.directory,
|
||||
userCategories: pluginConfig.categories,
|
||||
gitMasterConfig: pluginConfig.git_master,
|
||||
sisyphusJuniorModel: pluginConfig.agents?.["sisyphus-junior"]?.model,
|
||||
browserProvider: skillContext.browserProvider,
|
||||
disabledSkills: skillContext.disabledSkills,
|
||||
availableCategories,
|
||||
availableSkills: skillContext.availableSkills,
|
||||
onSyncSessionCreated: async (event) => {
|
||||
log("[index] onSyncSessionCreated callback", {
|
||||
sessionID: event.sessionID,
|
||||
parentID: event.parentID,
|
||||
title: event.title,
|
||||
})
|
||||
await managers.tmuxSessionManager.onSessionCreated({
|
||||
type: "session.created",
|
||||
properties: {
|
||||
info: {
|
||||
id: event.sessionID,
|
||||
parentID: event.parentID,
|
||||
title: event.title,
|
||||
},
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const getSessionIDForMcp = (): string => getMainSessionID() || ""
|
||||
|
||||
const skillTool = createSkillTool({
|
||||
skills: skillContext.mergedSkills,
|
||||
mcpManager: managers.skillMcpManager,
|
||||
getSessionID: getSessionIDForMcp,
|
||||
gitMasterConfig: pluginConfig.git_master,
|
||||
disabledSkills: skillContext.disabledSkills,
|
||||
})
|
||||
|
||||
const skillMcpTool = createSkillMcpTool({
|
||||
manager: managers.skillMcpManager,
|
||||
getLoadedSkills: () => skillContext.mergedSkills,
|
||||
getSessionID: getSessionIDForMcp,
|
||||
})
|
||||
|
||||
const commands = discoverCommandsSync()
|
||||
const slashcommandTool = createSlashcommandTool({
|
||||
commands,
|
||||
skills: skillContext.mergedSkills,
|
||||
})
|
||||
|
||||
const taskSystemEnabled = pluginConfig.experimental?.task_system ?? false
|
||||
const taskToolsRecord: Record<string, ToolDefinition> = taskSystemEnabled
|
||||
? {
|
||||
task_create: createTaskCreateTool(pluginConfig, ctx),
|
||||
task_get: createTaskGetTool(pluginConfig),
|
||||
task_list: createTaskList(pluginConfig),
|
||||
task_update: createTaskUpdateTool(pluginConfig, ctx),
|
||||
}
|
||||
: {}
|
||||
|
||||
const allTools: Record<string, ToolDefinition> = {
|
||||
...builtinTools,
|
||||
...createGrepTools(ctx),
|
||||
...createGlobTools(ctx),
|
||||
...createAstGrepTools(ctx),
|
||||
...createSessionManagerTools(ctx),
|
||||
...backgroundTools,
|
||||
call_omo_agent: callOmoAgent,
|
||||
...(lookAt ? { look_at: lookAt } : {}),
|
||||
task: delegateTask,
|
||||
skill: skillTool,
|
||||
skill_mcp: skillMcpTool,
|
||||
slashcommand: slashcommandTool,
|
||||
interactive_bash,
|
||||
...taskToolsRecord,
|
||||
}
|
||||
|
||||
const filteredTools = filterDisabledTools(allTools, pluginConfig.disabled_tools)
|
||||
|
||||
return {
|
||||
filteredTools,
|
||||
taskSystemEnabled,
|
||||
}
|
||||
}
|
||||
15
src/plugin/types.ts
Normal file
15
src/plugin/types.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { Plugin, ToolDefinition } from "@opencode-ai/plugin"
|
||||
|
||||
export type PluginContext = Parameters<Plugin>[0]
|
||||
export type PluginInstance = Awaited<ReturnType<Plugin>>
|
||||
export type PluginInterface = Omit<PluginInstance, "experimental.session.compacting">
|
||||
|
||||
export type ToolsRecord = Record<string, ToolDefinition>
|
||||
|
||||
export type TmuxConfig = {
|
||||
enabled: boolean
|
||||
layout: "main-horizontal" | "main-vertical" | "tiled" | "even-horizontal" | "even-vertical"
|
||||
main_pane_size: number
|
||||
main_pane_min_width: number
|
||||
agent_pane_min_width: number
|
||||
}
|
||||
41
src/plugin/unstable-agent-babysitter.ts
Normal file
41
src/plugin/unstable-agent-babysitter.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { PluginContext } from "./types"
|
||||
|
||||
import { createUnstableAgentBabysitterHook } from "../hooks"
|
||||
import type { BackgroundManager } from "../features/background-agent"
|
||||
|
||||
export function createUnstableAgentBabysitter(args: {
|
||||
ctx: PluginContext
|
||||
backgroundManager: BackgroundManager
|
||||
pluginConfig: OhMyOpenCodeConfig
|
||||
}) {
|
||||
const { ctx, backgroundManager, pluginConfig } = args
|
||||
|
||||
return createUnstableAgentBabysitterHook(
|
||||
{
|
||||
directory: ctx.directory,
|
||||
client: {
|
||||
session: {
|
||||
messages: async ({ path }) => {
|
||||
const result = await ctx.client.session.messages({ path })
|
||||
if (Array.isArray(result)) return result
|
||||
if (typeof result === "object" && result !== null) {
|
||||
return result
|
||||
}
|
||||
return []
|
||||
},
|
||||
prompt: async (promptArgs) => {
|
||||
await ctx.client.session.promptAsync(promptArgs)
|
||||
},
|
||||
promptAsync: async (promptArgs) => {
|
||||
await ctx.client.session.promptAsync(promptArgs)
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
backgroundManager,
|
||||
config: pluginConfig.babysitting,
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user