Files
archived-orca/mobile/src/session/mobile-structured-transcript-subscription.ts
T
Neil 89f9ce08de refactor(shared): organize session contracts and projections (#36979)
* refactor(shared): organize session contracts and projections

* chore: tighten dropped-path flaky pattern allowance

* docs: preserve current agent instructions with valid formatting
2026-11-01 03:14:46 -08:00

37 lines
1.5 KiB
TypeScript

import type { AgentSessionSubscribeEvent } from '../../../src/shared/agent-session/agent-session-wire'
import type { StructuredAgentSessionLiveSubscribe } from '../../../src/shared/agent-session/structured-agent-session-live-read'
import type { RpcClient } from '../transport/rpc-client'
function isSubscribeEvent(value: unknown): value is AgentSessionSubscribeEvent {
if (typeof value !== 'object' || value === null || !('type' in value)) {
return false
}
const type = value.type
return type === 'snapshot' || type === 'batch' || type === 'reset' || type === 'end'
}
// Subscribe after the hold settles even on refusal: older hosts can start on the next send, so their transcript must remain visible.
export function subscribeTranscriptAfterHold(
client: RpcClient,
held: Promise<unknown>,
isEnded: () => boolean
): StructuredAgentSessionLiveSubscribe {
return async (params, onEvent, onError) => {
await held.catch(() => undefined)
if (isEnded()) {
return { unsubscribe: () => {} }
}
const close = client.subscribe('agentSession.subscribe', params, (raw) => {
if (typeof raw === 'object' && raw !== null && 'type' in raw && raw.type === 'error') {
// A stream's error frame carries the host's error, refusal and all, under `error`.
onError('error' in raw && raw.error !== undefined ? raw.error : raw)
return
}
if (isSubscribeEvent(raw)) {
onEvent(raw)
}
})
return { unsubscribe: close }
}
}