Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

shared.ts

105 lines3.2 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import { RpcClientError } from "effect/unstable/rpc";
4​
5import * as AcpSchema from "../_generated/schema.gen.ts";
6import * as AcpError from "../errors.ts";
7const isError = Schema.is(AcpSchema.Error);
8​
9export const callRpc = <A>(
10 method: string,
11 effect: Effect.Effect<A, RpcClientError.RpcClientError | AcpSchema.Error>,
12): Effect.Effect<A, AcpError.AcpError> =>
13 effect.pipe(
14 Effect.catchIf(isError, (error) =>
15 Effect.fail(AcpError.AcpRequestError.fromProtocolError(error, { method })),
16 ),
17 Effect.catchTags({
18 RpcClientError: (cause) =>
19 Effect.fail(
20 new AcpError.AcpTransportError({
21 operation: "call-rpc",
22 method,
23 cause,
24 }),
25 ),
26 }),
27 );
28​
29export const runHandler = Effect.fnUntraced(function* <A, B>(
30 handler: ((payload: A) => Effect.Effect<B, AcpError.AcpError>) | undefined,
31 payload: A,
32 method: string,
33) {
34 if (!handler) {
35 return yield* Effect.fail(AcpError.AcpRequestError.methodNotFound(method).toProtocolError());
36 }
37 return yield* handler(payload).pipe(
38 Effect.mapError((error) =>
39 AcpError.AcpRequestError.fromCoreHandlerError(error, method).toProtocolError(),
40 ),
41 );
42});
43​
44export function decodeExtRequestRegistration<A, I>(
45 method: string,
46 payload: Schema.Codec<A, I>,
47 handler: (payload: A) => Effect.Effect<unknown, AcpError.AcpError>,
48) {
49 return (params: unknown): Effect.Effect<unknown, AcpError.AcpError> =>
50 Schema.decodeUnknownEffect(payload)(params).pipe(
51 Effect.mapError((error) => AcpError.AcpRequestError.invalidExtensionPayload(method, error)),
52 Effect.flatMap((decoded) => handler(decoded)),
53 );
55​
56export function decodeExtNotificationRegistration<A, I>(
57 method: string,
58 payload: Schema.Codec<A, I>,
59 handler: (payload: A) => Effect.Effect<void, AcpError.AcpError>,
60) {
61 return (params: unknown): Effect.Effect<void, AcpError.AcpError> =>
62 Schema.decodeUnknownEffect(payload)(params).pipe(
63 Effect.mapError((error) =>
64 AcpError.AcpProtocolParseError.fromSchemaError(
65 "decode-notification-payload",
66 method,
67 error,
68 ),
69 ),
70 Effect.flatMap((decoded) => handler(decoded)),
71 );
73​
74const encoder = new TextEncoder();
75​
76const JsonRpcId = Schema.Union([Schema.Number, Schema.String]);
77const JsonRpcHeaders = Schema.Array(Schema.Unknown);
78​
79export const jsonRpcRequest = <A, I>(method: string, params: Schema.Codec<A, I>) =>
80 Schema.Struct({
81 jsonrpc: Schema.Literal("2.0"),
82 id: JsonRpcId,
83 method: Schema.Literal(method),
84 params,
85 headers: JsonRpcHeaders,
86 });
87​
88export const jsonRpcNotification = <A, I>(method: string, params: Schema.Codec<A, I>) =>
89 Schema.Struct({
90 jsonrpc: Schema.Literal("2.0"),
91 method: Schema.Literal(method),
92 params,
93 });
94​
95export const jsonRpcResponse = <A, I>(result: Schema.Codec<A, I>) =>
96 Schema.Struct({
97 jsonrpc: Schema.Literal("2.0"),
98 id: JsonRpcId,
99 result,
100 });
101​
102export const encodeJsonl = <A, I>(schema: Schema.Codec<A, I>, value: A) =>
103 Effect.map(Schema.encodeEffect(Schema.fromJsonString(schema))(value), (encoded) =>
104 encoder.encode(`${encoded}\n`),
105 );