@@ -7,6 +7,9 @@ import { Pty } from "@opencode-ai/schema/pty"
77import { Config } from "./config"
88import { EventV2 } from "./event"
99import { Location } from "./location"
10+ import { PtyCapture } from "./pty/capture"
11+ import { PtyIntegration } from "./pty/integration"
12+ import { PtyOsc } from "./pty/osc"
1013import { PtyID } from "./pty/schema"
1114import { Shell } from "./shell"
1215import { lazy } from "./util/lazy"
@@ -78,7 +81,7 @@ export class ExitedError extends Schema.TaggedErrorClass<ExitedError>()("Pty.Exi
7881} ) { }
7982
8083export interface Interface {
81- readonly list : ( ) => Effect . Effect < Info [ ] >
84+ readonly list : ( filter ?: { readonly sessionID ?: string } ) => Effect . Effect < Info [ ] >
8285 readonly get : ( id : PtyID ) => Effect . Effect < Info , NotFoundError >
8386 readonly create : ( input : CreateInput ) => Effect . Effect < Info >
8487 readonly update : ( id : PtyID , input : UpdateInput ) => Effect . Effect < Info , NotFoundError >
@@ -95,6 +98,7 @@ const layer = Layer.effect(
9598 const events = yield * EventV2 . Service
9699 const location = yield * Location . Service
97100 const config = yield * Config . Service
101+ const capture = yield * PtyCapture . Service
98102 const context = yield * Effect . context ( )
99103 const runFork = Effect . runForkWith ( context )
100104 const sessions = new Map < PtyID , Active > ( )
@@ -154,8 +158,10 @@ const layer = Layer.effect(
154158 yield * removeSession ( id )
155159 } )
156160
157- const list = Effect . fn ( "Pty.list" ) ( function * ( ) {
158- return Array . from ( sessions . values ( ) ) . map ( ( session ) => session . info )
161+ const list = Effect . fn ( "Pty.list" ) ( function * ( filter ?: { readonly sessionID ?: string } ) {
162+ const all = Array . from ( sessions . values ( ) ) . map ( ( session ) => session . info )
163+ if ( filter ?. sessionID === undefined ) return all
164+ return all . filter ( ( info ) => info . sessionID === filter . sessionID )
159165 } )
160166
161167 const get = Effect . fn ( "Pty.get" ) ( function * ( id : PtyID ) {
@@ -165,11 +171,15 @@ const layer = Layer.effect(
165171 const create = Effect . fn ( "Pty.create" ) ( function * ( input : CreateInput ) {
166172 const id = PtyID . ascending ( )
167173 const command = input . command || Shell . preferred ( Config . latest ( yield * config . entries ( ) , "shell" ) )
168- const args = Shell . login ( command ) ? [ ...( input . args ?? [ ] ) , "-l" ] : [ ...( input . args ?? [ ] ) ]
174+ const baseArgs = Shell . login ( command ) ? [ ...( input . args ?? [ ] ) , "-l" ] : [ ...( input . args ?? [ ] ) ]
175+ // Integration failure (e.g. tmpdir not writable) degrades to an untouched spawn.
176+ const integration = yield * Effect . promise ( ( ) => PtyIntegration . setup ( command , baseArgs ) . catch ( ( ) => undefined ) )
177+ const args = integration ? integration . args : baseArgs
169178 const cwd = input . cwd || location . directory
170179 const env = {
171180 ...process . env ,
172181 ...input . env ,
182+ ...integration ?. env ,
173183 TERM : "xterm-256color" ,
174184 OPENCODE_TERMINAL : "1" ,
175185 } as Record < string , string >
@@ -189,6 +199,7 @@ const layer = Layer.effect(
189199 cwd,
190200 status : "running" ,
191201 pid : proc . pid ,
202+ sessionID : input . sessionID ,
192203 }
193204 const session : Active = {
194205 info,
@@ -200,8 +211,50 @@ const layer = Layer.effect(
200211 listeners : [ ] ,
201212 }
202213 sessions . set ( id , session )
214+ // Command capture: observe the output stream for shell-integration markers.
215+ const parser = integration ? PtyOsc . createParser ( ) : undefined
216+ const observe = ( chunk : string ) => {
217+ if ( ! parser ) return
218+ for ( const event of parser . push ( chunk ) ) {
219+ if ( event . type === "cwd" ) {
220+ observed . cwd = event . cwd
221+ continue
222+ }
223+ if ( event . type === "command-line" ) {
224+ observed . command = event . command
225+ continue
226+ }
227+ if ( event . type === "pre-exec" ) {
228+ const begun = Effect . runSync (
229+ capture . begin ( {
230+ ptyID : id ,
231+ sessionID : session . info . sessionID ,
232+ terminalTitle : session . info . title ,
233+ command : observed . command ?? "(unknown)" ,
234+ cwd : observed . cwd ,
235+ } ) ,
236+ )
237+ observed . command = undefined
238+ observed . active = begun . id
239+ runFork ( events . publish ( Pty . CommandEvent . Started , { command : begun } ) )
240+ continue
241+ }
242+ if ( event . type === "output" ) {
243+ if ( observed . active ) Effect . runSync ( capture . append ( observed . active , PtyOsc . stripControl ( event . data ) ) )
244+ continue
245+ }
246+ if ( event . type === "finished" ) {
247+ if ( ! observed . active ) continue
248+ const finished = Effect . runSync ( capture . finish ( observed . active , event . exitCode ) )
249+ observed . active = undefined
250+ if ( finished ) runFork ( events . publish ( Pty . CommandEvent . Finished , { command : finished } ) )
251+ }
252+ }
253+ }
254+ const observed : { command ?: string ; cwd ?: string ; active ?: string } = { cwd }
203255 session . listeners . push (
204256 proc . onData ( ( chunk ) => {
257+ observe ( chunk )
205258 session . cursor += chunk . length
206259 for ( const [ token , subscriber ] of session . subscribers . entries ( ) ) {
207260 if ( ! subscriber . active ) {
@@ -224,6 +277,11 @@ const layer = Layer.effect(
224277 if ( session . info . status === "exited" ) return
225278 session . info . status = "exited"
226279 session . info . exitCode = exitCode
280+ if ( observed . active ) {
281+ const finished = Effect . runSync ( capture . finish ( observed . active ) )
282+ observed . active = undefined
283+ if ( finished ) runFork ( events . publish ( Pty . CommandEvent . Finished , { command : finished } ) )
284+ }
227285 notifyEnd ( session , { exitCode } )
228286 exitOrder . push ( id )
229287 runFork (
@@ -315,4 +373,8 @@ const layer = Layer.effect(
315373
316374export const locationLayer = layer . pipe ( Layer . provide ( Config . locationLayer ) )
317375
318- export const node = makeLocationNode ( { service : Service , layer, deps : [ EventV2 . node , Location . node , Config . node ] } )
376+ export const node = makeLocationNode ( {
377+ service : Service ,
378+ layer,
379+ deps : [ EventV2 . node , Location . node , Config . node , PtyCapture . node ] ,
380+ } )
0 commit comments