Files
neuron/packages/opencode/src/session/reminders.ts
T

63 lines
2.4 KiB
TypeScript

import path from "path"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { Effect } from "effect"
import { Agent } from "@/agent/agent"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { InstanceState } from "@/effect/instance-state"
import { PartID } from "./schema"
import { Session } from "./session"
import BUILD_SWITCH from "./prompt/build-switch.txt"
import PLAN_MODE from "./prompt/plan-mode.txt"
export const apply = Effect.fn("SessionReminders.apply")(function* (input: {
messages: SessionV1.WithParts[]
agent: Agent.Info
session: Session.Info
}) {
const fsys = yield* FSUtil.Service
const sessions = yield* Session.Service
const userMessage = input.messages.findLast((msg) => msg.info.role === "user")
if (!userMessage) return input.messages
const ctx = yield* InstanceState.context
const plan = Session.plan(input.session, ctx)
// leaving plan mode: remind build to execute on the plan file if one exists
const assistantMessage = input.messages.findLast((msg) => msg.info.role === "assistant")
if (input.agent.name !== "plan" && assistantMessage?.info.agent === "plan") {
const exists = yield* fsys.existsSafe(plan)
const part = yield* sessions.updatePart({
id: PartID.ascending(),
messageID: userMessage.info.id,
sessionID: userMessage.info.sessionID,
type: "text",
text: exists ? `${BUILD_SWITCH}\n\nA plan file exists at ${plan}. Execute on the plan defined within it.` : BUILD_SWITCH,
synthetic: true,
})
userMessage.parts.push(part)
return input.messages
}
// entering plan mode: hand over the plan file location and workflow
if (input.agent.name !== "plan" || assistantMessage?.info.agent === "plan") return input.messages
const exists = yield* fsys.existsSafe(plan)
if (!exists) yield* fsys.ensureDir(path.dirname(plan)).pipe(Effect.catch(Effect.die))
const part = yield* sessions.updatePart({
id: PartID.ascending(),
messageID: userMessage.info.id,
sessionID: userMessage.info.sessionID,
type: "text",
text: PLAN_MODE.replace("${planInfo}", () =>
exists
? `A plan file already exists at ${plan}. You can read it and make incremental edits using the edit tool.`
: `No plan file exists yet. You should create your plan at ${plan} using the write tool.`,
),
synthetic: true,
})
userMessage.parts.push(part)
return input.messages
})
export * as SessionReminders from "./reminders"