@@ -2,7 +2,6 @@ import { EOL } from "os"
22import { Effect , Option } from "effect"
33import { Service } from "@opencode/client/effect/service"
44import { OpenCode } from "@opencode/client/promise"
5- import { base64Encode } from "@opencode/util/encode"
65import { renderUnicodeCompact } from "uqr"
76import { Commands } from "../commands"
87import { Runtime } from "../../framework/runtime"
@@ -12,43 +11,43 @@ export default Runtime.handler(
1211 Commands . commands . pair ,
1312 Effect . fn ( "cli.pair" ) ( function * ( input : Runtime . Input < typeof Commands . commands . pair > ) {
1413 const endpoint = yield * Service . ensure ( yield * ServiceConfig . options ( ) )
15- const password = yield * ServiceConfig . password ( )
14+ const client = OpenCode . make ( { baseUrl : endpoint . url , headers : Service . headers ( endpoint ) } )
1615 const urls = Option . isSome ( input . url )
1716 ? [ input . url . value ]
18- : ( yield * Effect . tryPromise ( ( ) =>
19- OpenCode . make ( { baseUrl : endpoint . url , headers : Service . headers ( endpoint ) } ) . server . info ( ) ,
20- ) ) . urls
21- const info = { urls, username : "opencode" , password }
22- const link = info . urls [ 0 ]
23- ? `${ new URL ( "/connect" , info . urls [ 0 ] ) } #${ base64Encode ( JSON . stringify ( { username : info . username , password } ) ) } `
24- : undefined
17+ : ( yield * Effect . tryPromise ( ( ) => client . server . info ( ) ) ) . urls
18+ const pairing = yield * Effect . tryPromise ( ( ) => client . server . pair ( ) )
19+ const links = urls . map ( ( url ) => new URL ( `/auth/connect/${ pairing . code } ` , url ) . href )
2520 process . stdout . write (
2621 [
2722 "" ,
28- ` URLs ${ info . urls [ 0 ] ?? "(none)" } ` ,
29- ...info . urls . slice ( 1 ) . map ( ( url ) => ` ${ url } ` ) ,
30- ` Username ${ info . username } ` ,
31- ` Password ${ info . password } ` ,
32- ...( link
23+ ` Open a link to connect. Links work once and expire in ${ Math . round ( pairing . expires_in / 60 ) } minutes.` ,
24+ "" ,
25+ ...( links . length ? links . map ( ( link ) => ` ${ link } ` ) : [ " (no server URLs)" ] ) ,
26+ ...( links [ 0 ]
3327 ? [
3428 "" ,
35- " Scan to pair" ,
36- "" ,
37- renderUnicodeCompact ( link , { border : 2 } )
29+ renderUnicodeCompact ( links [ 0 ] , { border : 2 } )
3830 . split ( EOL )
3931 . map ( ( line ) => " " + line )
4032 . join ( EOL ) ,
41- "" ,
42- ` Link ${ link } ` ,
4333 ]
4434 : [ ] ) ,
4535 "" ,
4636 ] . join ( EOL ) + EOL ,
4737 )
4838
4939 if ( Option . isSome ( input . url ) ) return
50- const hostname = new URL ( endpoint . url ) . hostname
51- if ( ! [ "localhost" , "127.0.0.1" , "[::1]" ] . includes ( hostname ) ) return
52- process . stderr . write ( ` Run \`opencode service set hostname 0.0.0.0\` to access the service remotely.${ EOL } ${ EOL } ` )
40+ const url = new URL ( endpoint . url )
41+ if ( ! [ "localhost" , "127.0.0.1" , "[::1]" ] . includes ( url . hostname ) ) return
42+ process . stderr . write (
43+ [
44+ ` Over SSH? Forward the port, then open the link on your machine:` ,
45+ ` ssh -L ${ url . port } :${ url . hostname } :${ url . port } <host>` ,
46+ ` If port ${ url . port } is busy locally, forward another port and use it in the link.` ,
47+ "" ,
48+ " To connect from other devices, run `opencode service set hostname 0.0.0.0`." ,
49+ "" ,
50+ ] . join ( EOL ) + EOL ,
51+ )
5352 } ) ,
5453)
0 commit comments