+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
369 lines
14 KiB
TypeScript
369 lines
14 KiB
TypeScript
import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS } from "@t3tools/contracts";
|
|
import * as Cause from "effect/Cause";
|
|
import * as Context from "effect/Context";
|
|
import type * as Duration from "effect/Duration";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Option from "effect/Option";
|
|
import * as Schema from "effect/Schema";
|
|
import * as Schedule from "effect/Schedule";
|
|
import * as Stream from "effect/Stream";
|
|
import * as SubscriptionRef from "effect/SubscriptionRef";
|
|
import { RpcClientError } from "effect/unstable/rpc";
|
|
|
|
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
|
|
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
|
|
import type { RpcSession } from "../rpc/session.ts";
|
|
|
|
export class EnvironmentRpcUnavailableError extends Schema.TaggedError<EnvironmentRpcUnavailableError>()(
|
|
"EnvironmentRpcUnavailableError",
|
|
{
|
|
environmentId: Schema.String,
|
|
message: Schema.String,
|
|
},
|
|
) {}
|
|
|
|
export interface EnvironmentRpcRequestObservation {
|
|
readonly environmentId: string;
|
|
readonly method: string;
|
|
}
|
|
|
|
export class EnvironmentRpcRequestObserver extends Context.Reference<{
|
|
readonly observe: (
|
|
request: EnvironmentRpcRequestObservation,
|
|
) => Effect.Effect<Effect.Effect<void>>;
|
|
}>("@t3tools/client-runtime/rpc/EnvironmentRpcRequestObserver", {
|
|
defaultValue: () => ({
|
|
observe: () => Effect.succeed(Effect.void),
|
|
}),
|
|
}) {}
|
|
|
|
export type EnvironmentRpcTag = keyof WsRpcProtocolClient & string;
|
|
type RpcMethod<TTag extends EnvironmentRpcTag> = WsRpcProtocolClient[TTag];
|
|
|
|
export type EnvironmentSubscriptionRpcTag =
|
|
| typeof WS_METHODS.codexAuthCallbackSubscribe
|
|
| typeof WS_METHODS.providerAuthSubscribe
|
|
| typeof WS_METHODS.providerInstallSubscribe
|
|
| typeof ORCHESTRATION_V2_WS_METHODS.subscribeShell
|
|
| typeof ORCHESTRATION_V2_WS_METHODS.subscribeThread
|
|
| typeof WS_METHODS.subscribeAuthAccess
|
|
| typeof WS_METHODS.subscribeServerConfig
|
|
| typeof WS_METHODS.subscribeServerLifecycle
|
|
| typeof WS_METHODS.scheduledTasksSubscribe
|
|
| typeof WS_METHODS.subscribeTerminalEvents
|
|
| typeof WS_METHODS.subscribeTerminalMetadata
|
|
| typeof WS_METHODS.subscribePreviewEvents
|
|
| typeof WS_METHODS.subscribeDiscoveredLocalServers
|
|
| typeof WS_METHODS.subscribeDeviceState
|
|
| typeof WS_METHODS.subscribeResourceTelemetry
|
|
| typeof WS_METHODS.pullRequestsSubscribeRefreshes
|
|
| typeof WS_METHODS.previewAutomationConnect
|
|
| typeof WS_METHODS.subscribeVcsStatus
|
|
| typeof WS_METHODS.subscribeWorktreeSetup
|
|
| typeof WS_METHODS.subscribeProjectClones
|
|
| typeof WS_METHODS.terminalAttach;
|
|
|
|
export type EnvironmentStreamCommandRpcTag =
|
|
| typeof WS_METHODS.chatGptHandoffSubscribe
|
|
| typeof WS_METHODS.cloudInstallRelayClient
|
|
| typeof WS_METHODS.serverUpdateServerWithProgress
|
|
| typeof WS_METHODS.gitRunStackedAction;
|
|
|
|
export type EnvironmentStreamRpcTag =
|
|
| EnvironmentSubscriptionRpcTag
|
|
| EnvironmentStreamCommandRpcTag;
|
|
|
|
export type EnvironmentUnaryRpcTag = Exclude<EnvironmentRpcTag, EnvironmentStreamRpcTag>;
|
|
|
|
export interface EnvironmentRpcSubscriptionObservation {
|
|
readonly environmentId: string;
|
|
readonly method: EnvironmentSubscriptionRpcTag;
|
|
readonly input: unknown;
|
|
}
|
|
|
|
export class EnvironmentRpcSubscriptionObserver extends Context.Reference<{
|
|
readonly observe: (
|
|
subscription: EnvironmentRpcSubscriptionObservation,
|
|
) => Effect.Effect<Effect.Effect<void>>;
|
|
}>("@t3tools/client-runtime/rpc/EnvironmentRpcSubscriptionObserver", {
|
|
defaultValue: () => ({
|
|
observe: () => Effect.succeed(Effect.void),
|
|
}),
|
|
}) {}
|
|
|
|
export const isRpcClientError = Schema.is(RpcClientError.RpcClientError);
|
|
|
|
export type EnvironmentRpcInput<TTag extends EnvironmentRpcTag> = Parameters<RpcMethod<TTag>>[0];
|
|
|
|
export type EnvironmentRpcSuccess<TTag extends EnvironmentUnaryRpcTag> =
|
|
RpcMethod<TTag> extends (input: any, options?: any) => Effect.Effect<infer A, any, any>
|
|
? A
|
|
: never;
|
|
|
|
export type EnvironmentRpcFailure<TTag extends EnvironmentUnaryRpcTag> =
|
|
RpcMethod<TTag> extends (input: any, options?: any) => Effect.Effect<any, infer E, any>
|
|
? E
|
|
: never;
|
|
|
|
export type EnvironmentRpcStreamValue<TTag extends EnvironmentStreamRpcTag> =
|
|
RpcMethod<TTag> extends (input: any, options?: any) => Stream.Stream<infer A, any, any>
|
|
? A
|
|
: never;
|
|
|
|
export type EnvironmentRpcStreamFailure<TTag extends EnvironmentStreamRpcTag> =
|
|
RpcMethod<TTag> extends (input: any, options?: any) => Stream.Stream<any, infer E, any>
|
|
? E
|
|
: never;
|
|
|
|
const currentSession = Effect.fn("EnvironmentRpc.currentSession")(function* () {
|
|
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
|
|
return yield* SubscriptionRef.get(supervisor.session).pipe(
|
|
Effect.flatMap(
|
|
Option.match({
|
|
onNone: () =>
|
|
Effect.fail(
|
|
new EnvironmentRpcUnavailableError({
|
|
environmentId: supervisor.target.environmentId,
|
|
message: `${supervisor.target.label} is not connected.`,
|
|
}),
|
|
),
|
|
onSome: Effect.succeed,
|
|
}),
|
|
),
|
|
);
|
|
});
|
|
|
|
export const getInitialServerConfig = Effect.fn("EnvironmentRpc.getInitialServerConfig")(
|
|
function* () {
|
|
const session = yield* currentSession();
|
|
return yield* session.initialConfig;
|
|
},
|
|
);
|
|
|
|
export const request = Effect.fn("EnvironmentRpc.request")(function* <
|
|
TTag extends EnvironmentUnaryRpcTag,
|
|
>(tag: TTag, input: EnvironmentRpcInput<TTag>) {
|
|
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
|
|
yield* Effect.annotateCurrentSpan({
|
|
"environment.id": supervisor.target.environmentId,
|
|
"rpc.method": tag,
|
|
});
|
|
const session = yield* currentSession();
|
|
const observer = yield* EnvironmentRpcRequestObserver;
|
|
const method = session.client[tag] as (
|
|
input: EnvironmentRpcInput<TTag>,
|
|
) => Effect.Effect<EnvironmentRpcSuccess<TTag>, EnvironmentRpcFailure<TTag>>;
|
|
const completeObservation = yield* observer.observe({
|
|
environmentId: supervisor.target.environmentId,
|
|
method: tag,
|
|
});
|
|
return yield* method(input).pipe(Effect.ensuring(completeObservation));
|
|
});
|
|
|
|
export function runStream<TTag extends EnvironmentStreamCommandRpcTag>(
|
|
tag: TTag,
|
|
input: EnvironmentRpcInput<TTag>,
|
|
): Stream.Stream<
|
|
EnvironmentRpcStreamValue<TTag>,
|
|
EnvironmentRpcStreamFailure<TTag> | EnvironmentRpcUnavailableError,
|
|
EnvironmentSupervisor.EnvironmentSupervisor
|
|
> {
|
|
return Stream.unwrap(
|
|
currentSession().pipe(
|
|
Effect.map((session) => {
|
|
const method = session.client[tag] as (
|
|
input: EnvironmentRpcInput<TTag>,
|
|
) => Stream.Stream<EnvironmentRpcStreamValue<TTag>, EnvironmentRpcStreamFailure<TTag>>;
|
|
return method(input);
|
|
}),
|
|
),
|
|
).pipe(
|
|
Stream.withSpan("EnvironmentRpc.runStream", {
|
|
attributes: { "rpc.method": tag },
|
|
}),
|
|
);
|
|
}
|
|
|
|
interface SubscriptionOptions<TTag extends EnvironmentSubscriptionRpcTag> {
|
|
/** Reports protocol or programming defects without changing their recovery policy. */
|
|
readonly onDefect?: (
|
|
cause: Cause.Cause<EnvironmentRpcStreamFailure<TTag>>,
|
|
) => Effect.Effect<void, never, never>;
|
|
readonly onExpectedFailure?: (
|
|
cause: Cause.Cause<EnvironmentRpcStreamFailure<TTag>>,
|
|
) => Effect.Effect<void, never, never>;
|
|
readonly retryExpectedFailureAfter?: Duration.Input;
|
|
readonly resubscribe?: Stream.Stream<unknown, never, never>;
|
|
}
|
|
|
|
function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
|
|
tag: TTag,
|
|
makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,
|
|
mapStream: (
|
|
session: RpcSession,
|
|
stream: Stream.Stream<EnvironmentRpcStreamValue<TTag>, EnvironmentRpcStreamFailure<TTag>>,
|
|
) => Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>>,
|
|
options?: SubscriptionOptions<TTag>,
|
|
): Stream.Stream<
|
|
A,
|
|
EnvironmentRpcStreamFailure<TTag>,
|
|
EnvironmentSupervisor.EnvironmentSupervisor
|
|
> {
|
|
return Stream.unwrap(
|
|
Effect.gen(function* () {
|
|
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
|
|
const observer = yield* EnvironmentRpcSubscriptionObserver;
|
|
const sessionChanges = SubscriptionRef.changes(supervisor.session);
|
|
const sessions =
|
|
options?.resubscribe === undefined
|
|
? sessionChanges
|
|
: Stream.merge(
|
|
sessionChanges,
|
|
options.resubscribe.pipe(
|
|
Stream.mapEffect(() => SubscriptionRef.get(supervisor.session)),
|
|
),
|
|
);
|
|
return sessions.pipe(
|
|
Stream.switchMap(
|
|
Option.match({
|
|
onNone: () => Stream.empty,
|
|
onSome: (session) => {
|
|
const method = (
|
|
tag === WS_METHODS.subscribeServerConfig
|
|
? session.subscribeServerConfig
|
|
: session.client[tag]
|
|
) as (
|
|
input: EnvironmentRpcInput<TTag>,
|
|
) => Stream.Stream<
|
|
EnvironmentRpcStreamValue<TTag>,
|
|
EnvironmentRpcStreamFailure<TTag>
|
|
>;
|
|
const subscribeToSession = (): Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>> =>
|
|
Stream.suspend(() =>
|
|
Stream.unwrap(
|
|
Effect.gen(function* () {
|
|
const input = yield* makeInput(session);
|
|
const completeObservation = yield* observer.observe({
|
|
environmentId: supervisor.target.environmentId,
|
|
method: tag,
|
|
input,
|
|
});
|
|
const stream = mapStream(session, method(input));
|
|
// An evicted preview host completes its registration stream.
|
|
// Re-register only after completion; failures still follow the
|
|
// session recovery policy and browser actions are never replayed.
|
|
return (
|
|
tag === WS_METHODS.previewAutomationConnect
|
|
? stream.pipe(Stream.repeat(Schedule.spaced("1 second")))
|
|
: stream
|
|
).pipe(Stream.ensuring(completeObservation));
|
|
}),
|
|
).pipe(
|
|
Stream.tapCause((cause) =>
|
|
options?.onDefect !== undefined &&
|
|
cause.reasons.some(
|
|
(reason) =>
|
|
reason._tag === "Die" ||
|
|
(reason._tag === "Fail" &&
|
|
isRpcClientError(reason.error) &&
|
|
reason.error.reason._tag === "RpcClientDefect"),
|
|
)
|
|
? options.onDefect(cause)
|
|
: Effect.void,
|
|
),
|
|
Stream.catchCause((cause) => {
|
|
const hasOnlyExpectedFailures =
|
|
cause.reasons.length > 0 &&
|
|
cause.reasons.every((reason) => reason._tag === "Fail");
|
|
const isTransportFailure =
|
|
hasOnlyExpectedFailures &&
|
|
cause.reasons.every(
|
|
(reason) => reason._tag === "Fail" && isRpcClientError(reason.error),
|
|
);
|
|
if (isTransportFailure) {
|
|
return Stream.fromEffect(
|
|
Effect.logWarning(
|
|
"Durable RPC subscription lost its transport; waiting for the next session.",
|
|
{
|
|
cause: Cause.pretty(cause),
|
|
method: tag,
|
|
environmentId: supervisor.target.environmentId,
|
|
},
|
|
),
|
|
).pipe(Stream.drain);
|
|
}
|
|
if (hasOnlyExpectedFailures && options?.onExpectedFailure !== undefined) {
|
|
const handled = Stream.fromEffect(options.onExpectedFailure(cause)).pipe(
|
|
Stream.drain,
|
|
);
|
|
if (options.retryExpectedFailureAfter === undefined) {
|
|
return handled;
|
|
}
|
|
return handled.pipe(
|
|
Stream.concat(
|
|
Stream.fromEffect(Effect.sleep(options.retryExpectedFailureAfter)).pipe(
|
|
Stream.drain,
|
|
),
|
|
),
|
|
Stream.concat(subscribeToSession()),
|
|
);
|
|
}
|
|
return Stream.failCause(cause);
|
|
}),
|
|
),
|
|
);
|
|
return subscribeToSession();
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
}),
|
|
).pipe(
|
|
Stream.withSpan("EnvironmentRpc.subscribe", {
|
|
attributes: { "rpc.method": tag },
|
|
}),
|
|
);
|
|
}
|
|
|
|
export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(
|
|
tag: TTag,
|
|
makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,
|
|
options?: SubscriptionOptions<TTag>,
|
|
): Stream.Stream<
|
|
EnvironmentRpcStreamValue<TTag>,
|
|
EnvironmentRpcStreamFailure<TTag>,
|
|
EnvironmentSupervisor.EnvironmentSupervisor
|
|
> {
|
|
return subscribeDynamicMapped(tag, makeInput, (_session, stream) => stream, options);
|
|
}
|
|
|
|
/** Tags each value before `switchMap` can buffer it across a session change. */
|
|
export function subscribeDynamicWithSession<TTag extends EnvironmentSubscriptionRpcTag>(
|
|
tag: TTag,
|
|
makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,
|
|
options?: SubscriptionOptions<TTag>,
|
|
): Stream.Stream<
|
|
readonly [session: RpcSession, value: EnvironmentRpcStreamValue<TTag>],
|
|
EnvironmentRpcStreamFailure<TTag>,
|
|
EnvironmentSupervisor.EnvironmentSupervisor
|
|
> {
|
|
return subscribeDynamicMapped(
|
|
tag,
|
|
makeInput,
|
|
(session, stream) => stream.pipe(Stream.map((value) => [session, value] as const)),
|
|
options,
|
|
);
|
|
}
|
|
|
|
export function subscribe<TTag extends EnvironmentSubscriptionRpcTag>(
|
|
tag: TTag,
|
|
input: EnvironmentRpcInput<TTag>,
|
|
options?: SubscriptionOptions<TTag>,
|
|
): Stream.Stream<
|
|
EnvironmentRpcStreamValue<TTag>,
|
|
EnvironmentRpcStreamFailure<TTag>,
|
|
EnvironmentSupervisor.EnvironmentSupervisor
|
|
> {
|
|
return subscribeDynamic(tag, () => Effect.succeed(input), options);
|
|
}
|