@@ -11,9 +11,11 @@ import { Session } from "../../session"
1111import { Config } from "../../config/config"
1212import { ConsoleState } from "../../config/console-state"
1313import { Account , AccountID , OrgID } from "../../account"
14+ import { AppRuntime } from "../../effect/app-runtime"
1415import { zodToJsonSchema } from "zod-to-json-schema"
1516import { errors } from "../error"
1617import { lazy } from "../../util/lazy"
18+ import { Effect , Option } from "effect"
1719import { WorkspaceRoutes } from "./workspace"
1820import { Agent } from "@/agent/agent"
1921
@@ -55,11 +57,18 @@ export const ExperimentalRoutes = lazy(() =>
5557 } ,
5658 } ) ,
5759 async ( c ) => {
58- const [ consoleState , groups ] = await Promise . all ( [ Config . getConsoleState ( ) , Account . orgsByAccount ( ) ] )
59- return c . json ( {
60- ...consoleState ,
61- switchableOrgCount : groups . reduce ( ( count , group ) => count + group . orgs . length , 0 ) ,
62- } )
60+ const result = await AppRuntime . runPromise (
61+ Effect . gen ( function * ( ) {
62+ const config = yield * Config . Service
63+ const account = yield * Account . Service
64+ const [ state , groups ] = yield * Effect . all ( [ config . getConsoleState ( ) , account . orgsByAccount ( ) ] , { concurrency : "unbounded" } )
65+ return {
66+ ...state ,
67+ switchableOrgCount : groups . reduce ( ( count , group ) => count + group . orgs . length , 0 ) ,
68+ }
69+ } ) ,
70+ )
71+ return c . json ( result )
6372 } ,
6473 )
6574 . get (
@@ -80,17 +89,22 @@ export const ExperimentalRoutes = lazy(() =>
8089 } ,
8190 } ) ,
8291 async ( c ) => {
83- const [ groups , active ] = await Promise . all ( [ Account . orgsByAccount ( ) , Account . active ( ) ] )
84-
85- const orgs = groups . flatMap ( ( group ) =>
86- group . orgs . map ( ( org ) => ( {
87- accountID : group . account . id ,
88- accountEmail : group . account . email ,
89- accountUrl : group . account . url ,
90- orgID : org . id ,
91- orgName : org . name ,
92- active : ! ! active && active . id === group . account . id && active . active_org_id === org . id ,
93- } ) ) ,
92+ const orgs = await AppRuntime . runPromise (
93+ Effect . gen ( function * ( ) {
94+ const account = yield * Account . Service
95+ const [ groups , active ] = yield * Effect . all ( [ account . orgsByAccount ( ) , account . active ( ) ] , { concurrency : "unbounded" } )
96+ const info = Option . getOrUndefined ( active )
97+ return groups . flatMap ( ( group ) =>
98+ group . orgs . map ( ( org ) => ( {
99+ accountID : group . account . id ,
100+ accountEmail : group . account . email ,
101+ accountUrl : group . account . url ,
102+ orgID : org . id ,
103+ orgName : org . name ,
104+ active : ! ! info && info . id === group . account . id && info . active_org_id === org . id ,
105+ } ) ) ,
106+ )
107+ } ) ,
94108 )
95109 return c . json ( { orgs } )
96110 } ,
@@ -115,7 +129,12 @@ export const ExperimentalRoutes = lazy(() =>
115129 validator ( "json" , ConsoleSwitchBody ) ,
116130 async ( c ) => {
117131 const body = c . req . valid ( "json" )
118- await Account . switchOrg ( AccountID . make ( body . accountID ) , OrgID . make ( body . orgID ) )
132+ await AppRuntime . runPromise (
133+ Effect . gen ( function * ( ) {
134+ const account = yield * Account . Service
135+ yield * account . use ( AccountID . make ( body . accountID ) , Option . some ( OrgID . make ( body . orgID ) ) )
136+ } ) ,
137+ )
119138 return c . json ( true )
120139 } ,
121140 )
0 commit comments