(input: {
workspaceID?: WorkspaceV2.ID
local: () => Effect.Effect<A, E, R>
remote: (input: {
workspace: Info
target: Extract<Target, { type: "remote" }>
}) => HttpClientRequest.HttpClientRequest
fallback: A
response?: "json" | "text"
})
| 251 | }) |
| 252 | |
| 253 | const runInWorkspace = <A, E, R>(input: { |
| 254 | workspaceID?: WorkspaceV2.ID |
| 255 | local: () => Effect.Effect<A, E, R> |
| 256 | remote: (input: { |
| 257 | workspace: Info |
| 258 | target: Extract<Target, { type: "remote" }> |
| 259 | }) => HttpClientRequest.HttpClientRequest |
| 260 | fallback: A |
| 261 | response?: "json" | "text" |
| 262 | }) => |
| 263 | Effect.gen(function* () { |
| 264 | if (!input.workspaceID) return yield* input.local() |
| 265 | |
| 266 | const workspace = yield* get(input.workspaceID) |
| 267 | if (!workspace) return input.fallback |
| 268 | |
| 269 | const target = yield* WorkspaceAdapterRuntime.target(workspace) |
| 270 | |
| 271 | if (target.type === "local") { |
| 272 | const store = yield* InstanceStore.Service |
| 273 | return yield* store.provide({ directory: target.directory }, input.local()) |
| 274 | } |
| 275 | |
| 276 | const response = yield* http.execute(input.remote({ workspace, target })).pipe( |
| 277 | Effect.catch((error) => |
| 278 | Effect.logWarning("workspace target request failed", { |
| 279 | workspaceID: workspace.id, |
| 280 | error: errorData(error), |
| 281 | }).pipe(Effect.as(undefined)), |
| 282 | ), |
| 283 | ) |
| 284 | if (!response) return input.fallback |
| 285 | if (response.status < 200 || response.status >= 300) { |
| 286 | const body = yield* response.text.pipe(Effect.catch(() => Effect.succeed(""))) |
| 287 | yield* Effect.logWarning("workspace target request failed", { |
| 288 | workspaceID: workspace.id, |
| 289 | status: response.status, |
| 290 | body, |
| 291 | }) |
| 292 | return input.fallback |
| 293 | } |
| 294 | |
| 295 | const body = input.response === "text" ? response.text : response.json |
| 296 | return yield* body.pipe( |
| 297 | Effect.map((result) => result as A), |
| 298 | Effect.catch((error) => |
| 299 | Effect.logWarning("workspace target response decode failed", { |
| 300 | workspaceID: workspace.id, |
| 301 | error: errorData(error), |
| 302 | }).pipe(Effect.as(input.fallback)), |
| 303 | ), |
| 304 | ) |
| 305 | }) |
| 306 | |
| 307 | const syncHistory = Effect.fn("Workspace.syncHistory")(function* ( |
| 308 | space: Info, |
no test coverage detected