-
Notifications
You must be signed in to change notification settings - Fork 7
feat: execute automation run now #983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
55ee3c0
feat: add automation run context seam
Astro-Han 41ee735
feat: execute automation run now
Astro-Han 57a93fa
test: cover automation run now execution
Astro-Han 1150838
fix: address automation run review feedback
Astro-Han d5d233b
fix: guard automation run writer boundaries
Astro-Han b66d426
fix: cancel active automation on delete
Astro-Han cc73650
test: keep automation temp directory alive
Astro-Han eda2ac4
fix: cancel automation session prompts
Astro-Han decdbb2
fix: honor automation abort before prompt start
Astro-Han 39f7cee
test: strengthen automation delete coverage
Astro-Han 722a6f6
fix: avoid duplicate automation running events
Astro-Han 67dc4f0
test: clean up automation run listener
Astro-Han File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { Context, Effect } from "effect" | ||
| import type { Permission } from "@/permission" | ||
| import type { PermissionID } from "@/permission/schema" | ||
|
|
||
| export type AutomationRunAttendance = "attended" | "unattended" | ||
|
|
||
| export type AutomationRunBlocker = | ||
| | { kind: "permission"; requestID: PermissionID } | ||
| | { kind: "question"; callID: string } | ||
|
|
||
| export interface AutomationRunContext { | ||
| readonly attendance: AutomationRunAttendance | ||
| readonly stepCap?: number | ||
| readonly block: (blocker: AutomationRunBlocker) => Effect.Effect<void> | ||
| readonly clear: () => Effect.Effect<void> | ||
| } | ||
|
|
||
| export class AutomationStepCapError extends Error { | ||
| readonly _tag = "AutomationStepCapError" | ||
| constructor(readonly stepCap: number) { | ||
| super(`Automation run exceeded the hard step cap (${stepCap}).`) | ||
| this.name = "AutomationStepCapError" | ||
| } | ||
| } | ||
|
|
||
| export const AutomationRunContextService: Context.Reference<AutomationRunContext | undefined> = Context.Reference< | ||
| AutomationRunContext | undefined | ||
| >("@opencode/AutomationRunContext", { | ||
| defaultValue: () => undefined, | ||
| }) | ||
|
|
||
| export const AutomationRunContext = { | ||
| service: AutomationRunContextService, | ||
| current: AutomationRunContextService, | ||
| attended(input: Pick<AutomationRunContext, "block" | "clear" | "stepCap">): AutomationRunContext { | ||
| return { ...input, attendance: "attended" } | ||
| }, | ||
| unattended(input: Pick<AutomationRunContext, "block" | "clear" | "stepCap">): AutomationRunContext { | ||
| return { ...input, attendance: "unattended" } | ||
| }, | ||
|
Astro-Han marked this conversation as resolved.
|
||
| permissionOnPending( | ||
| context: AutomationRunContext | undefined, | ||
| ): ((request: Permission.Request) => Effect.Effect<void>) | undefined { | ||
| if (!context) return undefined | ||
| return (request) => context.block({ kind: "permission", requestID: request.id }) | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { Effect } from "effect" | ||
| import { Automation } from "." | ||
| import { Session } from "@/session" | ||
| import { SessionPrompt } from "@/session/prompt" | ||
| import { AutomationRunContext, type AutomationRunBlocker } from "./run-context" | ||
|
|
||
| export const sessionPromptExecutor: Automation.RunExecutor = async ({ definition, run, attendance, signal }) => { | ||
| signal.throwIfAborted() | ||
| const sessionID = | ||
| definition.context === "continue" && definition.automationSessionID | ||
| ? definition.automationSessionID | ||
| : (await Session.create({ title: `Automation: ${definition.title}` })).id | ||
| const cancelPrompt = () => { | ||
| void SessionPrompt.cancel(sessionID, { source: "automation.cancel" }).catch(() => undefined) | ||
| } | ||
| if (signal.aborted) cancelPrompt() | ||
| else signal.addEventListener("abort", cancelPrompt, { once: true }) | ||
| try { | ||
| signal.throwIfAborted() | ||
| let currentRun = Automation.markRunStarted(run, sessionID) | ||
| await Automation.publishRunUpdated(currentRun) | ||
| const handlers = { | ||
| stepCap: 50, | ||
| block: (blocker: AutomationRunBlocker) => | ||
| Effect.gen(function* () { | ||
| const previous = currentRun | ||
| currentRun = Automation.markRunBlocked(currentRun, blocker) | ||
| if (currentRun !== previous) yield* Effect.promise(() => Automation.publishRunUpdated(currentRun)) | ||
| }), | ||
| clear: () => | ||
| Effect.gen(function* () { | ||
| const previous = currentRun | ||
| currentRun = Automation.clearRunBlocker(currentRun) | ||
| if (currentRun !== previous) yield* Effect.promise(() => Automation.publishRunUpdated(currentRun)) | ||
| }), | ||
| } | ||
| const scoped = | ||
| attendance === "attended" ? AutomationRunContext.attended(handlers) : AutomationRunContext.unattended(handlers) | ||
| const message = await SessionPrompt.promptWithAutomationContext( | ||
| { | ||
| sessionID, | ||
| parts: [{ type: "text", text: definition.prompt }], | ||
| }, | ||
| scoped, | ||
| { abortSignal: signal }, | ||
| ) | ||
| signal.throwIfAborted() | ||
| return { | ||
| sessionID, | ||
| result: message.parts.find((part) => part.type === "text")?.text ?? null, | ||
| cost: message.info.role === "assistant" ? message.info.cost : null, | ||
| } | ||
| } finally { | ||
| signal.removeEventListener("abort", cancelPrompt) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.