| ... | ... | @@ -22,7 +22,7 @@ import { |
| 22 | 22 | type ProviderRuntimeEvent, |
| 23 | 23 | type ProviderSession, |
| 24 | 24 | } from "@t3tools/contracts"; |
| 25 | | import { Effect, Layer, Option, PubSub, Queue, Ref, Schema, Stream } from "effect"; |
| 25 | import { Effect, Layer, Option, PubSub, Queue, Ref, Schema, SchemaIssue, Stream } from "effect"; |
| 26 | 26 | |
| 27 | 27 | import { ProviderSessionNotFoundError, ProviderValidationError } from "../Errors.ts"; |
| 28 | 28 | import { ProviderAdapterRegistry } from "../Services/ProviderAdapterRegistry.ts"; |
| ... | ... | @@ -54,30 +54,21 @@ function toValidationError( |
| 54 | 54 | }); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | | function decodeIssueMessage(cause: unknown): string { |
| 58 | | if (cause instanceof Error && cause.message.length > 0) { |
| 59 | | return cause.message; |
| 60 | | } |
| 61 | | try { |
| 62 | | return JSON.stringify(cause); |
| 63 | | } catch { |
| 64 | | return "Invalid provider input."; |
| 65 | | } |
| 66 | | } |
| 67 | | |
| 68 | | function decodeInputOrValidationError<S extends Schema.Top>(input: { |
| 57 | const decodeInputOrValidationError = <S extends Schema.Top>(input: { |
| 69 | 58 | readonly operation: string; |
| 70 | 59 | readonly schema: S; |
| 71 | 60 | readonly payload: unknown; |
| 72 | | }): Effect.Effect<Schema.Schema.Type<S>, ProviderValidationError> { |
| 73 | | try { |
| 74 | | return Effect.succeed( |
| 75 | | Schema.decodeUnknownSync(input.schema as never)(input.payload) as Schema.Schema.Type<S>, |
| 76 | | ); |
| 77 | | } catch (cause) { |
| 78 | | return Effect.fail(toValidationError(input.operation, decodeIssueMessage(cause), cause)); |
| 79 | | } |
| 80 | | } |
| 61 | }) => |
| 62 | Schema.decodeUnknownEffect(input.schema)(input.payload).pipe( |
| 63 | Effect.mapError( |
| 64 | (schemaError) => |
| 65 | new ProviderValidationError({ |
| 66 | operation: input.operation, |
| 67 | issue: SchemaIssue.makeFormatterDefault()(schemaError.issue), |
| 68 | cause: schemaError, |
| 69 | }), |
| 70 | ), |
| 71 | ); |
| 81 | 72 | |
| 82 | 73 | function toRuntimeStatus(session: ProviderSession): "starting" | "running" | "stopped" | "error" { |
| 83 | 74 | switch (session.status) { |
| ... | ... | @@ -265,9 +256,7 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) => |
| 265 | 256 | provider: existing.provider, |
| 266 | 257 | threadId: input.binding.threadId, |
| 267 | 258 | providerThreadId: existingProviderThreadId, |
| 268 | | ...(existing.resumeCursor !== undefined |
| 269 | | ? { resumeCursor: existing.resumeCursor } |
| 270 | | : {}), |
| 259 | ...(existing.resumeCursor !== undefined ? { resumeCursor: existing.resumeCursor } : {}), |
| 271 | 260 | }); |
| 272 | 261 | if (existing.sessionId !== input.staleSessionId) { |
| 273 | 262 | yield* setAlias(input.staleSessionId, existing.sessionId); |
| ... | ... | @@ -310,9 +299,7 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) => |
| 310 | 299 | provider: resumed.provider, |
| 311 | 300 | threadId: input.binding.threadId, |
| 312 | 301 | providerThreadId: resumedProviderThreadId, |
| 313 | | ...(resumed.resumeCursor !== undefined |
| 314 | | ? { resumeCursor: resumed.resumeCursor } |
| 315 | | : {}), |
| 302 | ...(resumed.resumeCursor !== undefined ? { resumeCursor: resumed.resumeCursor } : {}), |
| 316 | 303 | }); |
| 317 | 304 | |
| 318 | 305 | if (resumed.sessionId !== input.staleSessionId) { |