Skip to content

Commit

Permalink
Allow defects to pass through checks in AiPlan (#4537)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Feb 28, 2025
1 parent 367bb35 commit 975c20e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-shoes-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/ai": patch
---

Allow defects to pass through predicates in `AiPlan`
17 changes: 8 additions & 9 deletions packages/ai/ai/src/internal/aiPlan.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as Cause from "effect/Cause"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { CommitPrototype } from "effect/Effectable"
import * as Exit from "effect/Exit"
import * as Either from "effect/Either"
import { dual, identity } from "effect/Function"
import * as Option from "effect/Option"
import { pipeArguments } from "effect/Pipeable"
Expand Down Expand Up @@ -46,15 +45,15 @@ const buildPlan = <Error, Provides, Requires>(
Effect.map(Effect.context<AiModels | Requires>(), (context) => {
const models = Context.get(context, AiModels)
return Effect.fnUntraced(function*<A, E, R>(effect: Effect.Effect<A, E, R>) {
let exit: Exit.Exit<A, E> | undefined = undefined
let result: Either.Either<A, E> | undefined = undefined
for (const step of plan.steps) {
if (exit !== undefined && Exit.isFailure(exit) && Option.isSome(step.check)) {
const check = step.check.value(Cause.squash(exit.cause) as Error)
if (result !== undefined && Either.isLeft(result) && Option.isSome(step.check)) {
const check = step.check.value(result.left as any)
const isFatalError = !(Effect.isEffect(check) ? yield* check : check)
if (isFatalError) break
}
const retryOptions = getRetryOptions(step)
exit = yield* Effect.scopedWith((scope) =>
result = yield* Effect.scopedWith((scope) =>
models.build(step.model, context).pipe(
Scope.extend(scope),
Effect.flatMap((context) =>
Expand All @@ -65,12 +64,12 @@ const buildPlan = <Error, Provides, Requires>(
Effect.provide(context)
)
),
Effect.exit
Effect.either
)
)
if (Exit.isSuccess(exit)) break
if (Either.isRight(result)) break
}
return yield* exit!
return yield* result!
})
})

Expand Down

0 comments on commit 975c20e

Please # to comment.