Skip to content

Commit d55a7f6

Browse files
committed
fix: adapt automations to dev rebase
1 parent dbd03b5 commit d55a7f6

7 files changed

Lines changed: 1375 additions & 152 deletions

File tree

packages/app/src/pages/automations.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export default function AutomationsPage() {
292292
}
293293

294294
const loadRuns = async () => {
295-
const result = await sdk.client.automation.runs({ limit: 100 })
295+
const result = await sdk.client.automation.runs({ limit: "100" })
296296
return result.data ?? []
297297
}
298298

packages/opencode/src/automation/automation.ts

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Log from "@opencode-ai/core/util/log"
55
import { and, desc, eq, inArray, lte, sql } from "drizzle-orm"
66
import { Database } from "@/storage/db"
77
import { Instance } from "@/project/instance"
8+
import { InstanceRef } from "@/effect/instance-ref"
89
import { EffectBridge } from "@/effect/bridge"
910
import { InstanceState } from "@/effect/instance-state"
1011
import { ProjectID } from "@/project/schema"
@@ -18,8 +19,8 @@ import { SessionSummary } from "@/session/summary"
1819
import { SessionStatus } from "@/session/status"
1920
import type { FileDiff } from "@/snapshot"
2021
import { Worktree } from "@/worktree"
21-
import { zod } from "@/util/effect-zod"
22-
import { withStatics } from "@/util/schema"
22+
import { zod } from "@opencode-ai/core/effect-zod"
23+
import { withStatics } from "@opencode-ai/core/schema"
2324
import { Cause, Context, Effect, Exit, Layer, Option, Schema, Types } from "effect"
2425
import { AutomationFindingTable, AutomationRunTable, AutomationTable } from "./automation.sql"
2526
import { AutomationFindingID, AutomationID, AutomationRunID } from "./schema"
@@ -34,7 +35,7 @@ export type Weekday = Schema.Schema.Type<typeof Weekday>
3435
export const ScheduleConfig = Schema.Union([
3536
Schema.Struct({
3637
type: Schema.Literal("interval"),
37-
everyMinutes: Schema.Number,
38+
everyMinutes: Schema.Finite,
3839
}),
3940
Schema.Struct({
4041
type: Schema.Literal("daily"),
@@ -77,18 +78,18 @@ export const RunStatus = Schema.Literals([
7778
export type RunStatus = Schema.Schema.Type<typeof RunStatus>
7879

7980
const DiffStats = Schema.Struct({
80-
additions: Schema.Number,
81-
deletions: Schema.Number,
82-
files: Schema.Number,
81+
additions: Schema.Finite,
82+
deletions: Schema.Finite,
83+
files: Schema.Finite,
8384
})
8485

8586
const Time = Schema.Struct({
86-
created: Schema.Number,
87-
updated: Schema.Number,
88-
lastRun: Schema.optional(Schema.Number),
89-
nextRun: Schema.optional(Schema.Number),
90-
starts: Schema.optional(Schema.Number),
91-
ends: Schema.optional(Schema.Number),
87+
created: Schema.Finite,
88+
updated: Schema.Finite,
89+
lastRun: Schema.optional(Schema.Finite),
90+
nextRun: Schema.optional(Schema.Finite),
91+
starts: Schema.optional(Schema.Finite),
92+
ends: Schema.optional(Schema.Finite),
9293
})
9394

9495
export const Finding = Schema.Struct({
@@ -100,8 +101,8 @@ export const Finding = Schema.Struct({
100101
filesChanged: Schema.mutable(Schema.Array(Schema.String)),
101102
recommendedNextAction: Schema.optional(Schema.String),
102103
time: Schema.Struct({
103-
created: Schema.Number,
104-
updated: Schema.Number,
104+
created: Schema.Finite,
105+
updated: Schema.Finite,
105106
}),
106107
})
107108
.annotate({ identifier: "AutomationFinding" })
@@ -123,7 +124,7 @@ export const Info = Schema.Struct({
123124
reasoningEffort: Schema.optional(Schema.Literals(["none", "low", "medium", "high"])),
124125
permissionProfile: PermissionProfile,
125126
notificationBehavior: Schema.Literals(["inbox", "auto_archive_no_findings"]),
126-
maxRuntimeMinutes: Schema.optional(Schema.Number),
127+
maxRuntimeMinutes: Schema.optional(Schema.Finite),
127128
time: Time,
128129
})
129130
.annotate({ identifier: "Automation" })
@@ -145,17 +146,17 @@ export const RunInfo = Schema.Struct({
145146
branchName: Schema.optional(Schema.String),
146147
summary: Schema.optional(Schema.String),
147148
result: Schema.optional(Schema.Literals(["findings", "no_findings", "needs_approval", "failed"])),
148-
findingsCount: Schema.Number,
149+
findingsCount: Schema.Finite,
149150
diffStats: Schema.optional(DiffStats),
150151
error: Schema.optional(Schema.String),
151152
time: Schema.Struct({
152-
created: Schema.Number,
153-
updated: Schema.Number,
154-
queued: Schema.Number,
155-
started: Schema.optional(Schema.Number),
156-
completed: Schema.optional(Schema.Number),
157-
read: Schema.optional(Schema.Number),
158-
archived: Schema.optional(Schema.Number),
153+
created: Schema.Finite,
154+
updated: Schema.Finite,
155+
queued: Schema.Finite,
156+
started: Schema.optional(Schema.Finite),
157+
completed: Schema.optional(Schema.Finite),
158+
read: Schema.optional(Schema.Finite),
159+
archived: Schema.optional(Schema.Finite),
159160
}),
160161
})
161162
.annotate({ identifier: "AutomationRun" })
@@ -174,9 +175,9 @@ export const CreateInput = Schema.Struct({
174175
reasoningEffort: Schema.optional(Schema.Literals(["none", "low", "medium", "high"])),
175176
permissionProfile: Schema.optional(PermissionProfile),
176177
notificationBehavior: Schema.optional(Schema.Literals(["inbox", "auto_archive_no_findings"])),
177-
maxRuntimeMinutes: Schema.optional(Schema.Number),
178-
startsAt: Schema.optional(Schema.Number),
179-
endsAt: Schema.optional(Schema.Number),
178+
maxRuntimeMinutes: Schema.optional(Schema.Finite),
179+
startsAt: Schema.optional(Schema.Finite),
180+
endsAt: Schema.optional(Schema.Finite),
180181
})
181182
.annotate({ identifier: "AutomationCreateInput" })
182183
.pipe(withStatics((s) => ({ zod: zod(s) })))
@@ -194,9 +195,9 @@ export const UpdateInput = Schema.Struct({
194195
reasoningEffort: Schema.optional(Schema.Literals(["none", "low", "medium", "high"])),
195196
permissionProfile: Schema.optional(PermissionProfile),
196197
notificationBehavior: Schema.optional(Schema.Literals(["inbox", "auto_archive_no_findings"])),
197-
maxRuntimeMinutes: Schema.optional(Schema.Number),
198-
startsAt: Schema.optional(Schema.Number),
199-
endsAt: Schema.optional(Schema.Number),
198+
maxRuntimeMinutes: Schema.optional(Schema.Finite),
199+
startsAt: Schema.optional(Schema.Finite),
200+
endsAt: Schema.optional(Schema.Finite),
200201
})
201202
.annotate({ identifier: "AutomationUpdateInput" })
202203
.pipe(withStatics((s) => ({ zod: zod(s) })))
@@ -206,7 +207,7 @@ export const ListRunsInput = Schema.Struct({
206207
automationID: Schema.optional(AutomationID),
207208
inbox: Schema.optional(Schema.Boolean),
208209
archived: Schema.optional(Schema.Boolean),
209-
limit: Schema.optional(Schema.Number),
210+
limit: Schema.optional(Schema.Finite),
210211
})
211212
.annotate({ identifier: "AutomationListRunsInput" })
212213
.pipe(withStatics((s) => ({ zod: zod(s) })))
@@ -1035,7 +1036,7 @@ export const layer = Layer.effect(
10351036
details:
10361037
[finalOutput.summary, finalOutput.diffSummary].filter(Boolean).join("\n\n") ||
10371038
"Automation completed.",
1038-
filesChanged: diffs.map((diff) => diff.file),
1039+
filesChanged: diffs.map((diff) => diff.file).filter((file): file is string => file !== undefined),
10391040
},
10401041
]
10411042
const nextRunAt = computeNextRun({
@@ -1217,12 +1218,16 @@ export const layer = Layer.effect(
12171218
)
12181219
: runPromptEffect(automation, runID)
12191220
const result = worktreeInfo
1220-
? yield* Effect.promise(async () => {
1221-
const nested = await Instance.provide({
1222-
directory: worktreeInfo!.directory,
1223-
fn: () => Effect.runPromise(task.pipe(Effect.provide(WorktreeRunLayer))),
1224-
})
1225-
return await nested
1221+
? yield* Effect.gen(function* () {
1222+
const current = yield* InstanceState.context
1223+
const ctx = { ...current, directory: worktreeInfo!.directory }
1224+
return yield* Effect.promise(() =>
1225+
Instance.restore(ctx, () =>
1226+
Effect.runPromise(
1227+
task.pipe(Effect.provideService(InstanceRef, ctx), Effect.provide(WorktreeRunLayer)),
1228+
),
1229+
),
1230+
)
12261231
})
12271232
: yield* task
12281233
const structured =

packages/opencode/src/automation/schema.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import { Schema } from "effect"
22
import { Identifier } from "@/id/id"
3-
import { zod, ZodOverride } from "@/util/effect-zod"
4-
import { withStatics } from "@/util/schema"
3+
import { zod } from "@opencode-ai/core/effect-zod"
4+
import { withStatics } from "@opencode-ai/core/schema"
55

6-
export const AutomationID = Schema.String.annotate({ [ZodOverride]: Identifier.schema("automation") }).pipe(
7-
Schema.brand("AutomationID"),
8-
withStatics((s) => ({
9-
ascending: (id?: string) => s.make(Identifier.ascending("automation", id)),
10-
zod: zod(s),
6+
const automationIdSchema = Schema.String.check(Schema.isStartsWith("aut")).pipe(Schema.brand("AutomationID"))
7+
export type AutomationID = typeof automationIdSchema.Type
8+
9+
export const AutomationID = automationIdSchema.pipe(
10+
withStatics((schema: typeof automationIdSchema) => ({
11+
ascending: (id?: string) => schema.make(Identifier.ascending("automation", id)),
12+
zod: zod(schema),
1113
})),
1214
)
13-
export type AutomationID = Schema.Schema.Type<typeof AutomationID>
1415

15-
export const AutomationRunID = Schema.String.annotate({ [ZodOverride]: Identifier.schema("automationRun") }).pipe(
16-
Schema.brand("AutomationRunID"),
17-
withStatics((s) => ({
18-
ascending: (id?: string) => s.make(Identifier.ascending("automationRun", id)),
19-
zod: zod(s),
16+
const automationRunIdSchema = Schema.String.check(Schema.isStartsWith("arn")).pipe(Schema.brand("AutomationRunID"))
17+
export type AutomationRunID = typeof automationRunIdSchema.Type
18+
19+
export const AutomationRunID = automationRunIdSchema.pipe(
20+
withStatics((schema: typeof automationRunIdSchema) => ({
21+
ascending: (id?: string) => schema.make(Identifier.ascending("automationRun", id)),
22+
zod: zod(schema),
2023
})),
2124
)
22-
export type AutomationRunID = Schema.Schema.Type<typeof AutomationRunID>
2325

24-
export const AutomationFindingID = Schema.String.annotate({
25-
[ZodOverride]: Identifier.schema("automationFinding"),
26-
}).pipe(
26+
const automationFindingIdSchema = Schema.String.check(Schema.isStartsWith("afn")).pipe(
2727
Schema.brand("AutomationFindingID"),
28-
withStatics((s) => ({
29-
ascending: (id?: string) => s.make(Identifier.ascending("automationFinding", id)),
30-
zod: zod(s),
28+
)
29+
export type AutomationFindingID = typeof automationFindingIdSchema.Type
30+
31+
export const AutomationFindingID = automationFindingIdSchema.pipe(
32+
withStatics((schema: typeof automationFindingIdSchema) => ({
33+
ascending: (id?: string) => schema.make(Identifier.ascending("automationFinding", id)),
34+
zod: zod(schema),
3135
})),
3236
)
33-
export type AutomationFindingID = Schema.Schema.Type<typeof AutomationFindingID>

packages/opencode/src/server/routes/instance/httpapi/groups/global.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const LiveAutomationRun = Schema.Struct({
4545
}).annotate({ identifier: "LiveAutomationRun" })
4646

4747
const GlobalAutomationRunning = Schema.Struct({
48-
count: Schema.Number,
48+
count: Schema.Finite,
4949
runs: Schema.Array(LiveAutomationRun),
5050
})
5151

packages/opencode/src/server/routes/instance/httpapi/handlers/automation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export const automationHandlers = HttpApiBuilder.group(InstanceHttpApi, "automat
4444

4545
const read = Effect.fn("AutomationHttpApi.read")(function* (ctx: {
4646
params: { runID: AutomationRunID }
47-
payload?: typeof RunReadPayload.Type
47+
payload: typeof RunReadPayload.Type | void
4848
}) {
4949
return yield* mapNotFound(automation.markRunRead(ctx.params.runID, ctx.payload?.read))
5050
})
5151

5252
const archive = Effect.fn("AutomationHttpApi.archive")(function* (ctx: {
5353
params: { runID: AutomationRunID }
54-
payload?: typeof RunArchivePayload.Type
54+
payload: typeof RunArchivePayload.Type | void
5555
}) {
5656
return yield* mapNotFound(automation.archiveRun(ctx.params.runID, ctx.payload?.archived))
5757
})

0 commit comments

Comments
 (0)