Showing the changes to apps/host-selfhost/src/apps-resolver.ts (1 of 19 files). Show all
Unified · Split
| @@ -0,0 +1,130 @@ | ||
| 1 | +import { Effect } from "effect"; | |
| 2 | + | |
| 3 | +import { | |
| 4 | + ConnectionName, | |
| 5 | + IntegrationSlug, | |
| 6 | + ToolAddress, | |
| 7 | + isToolResult, | |
| 8 | + type ConnectionRef, | |
| 9 | + type ExecuteError, | |
| 10 | + type Owner, | |
| 11 | + type PluginCtx, | |
| 12 | +} from "@executor-js/sdk"; | |
| 13 | +import { | |
| 14 | + BindingError, | |
| 15 | + type ClientResolver, | |
| 16 | + type ConnectionCandidate, | |
| 17 | +} from "@executor-js/plugin-apps/api"; | |
| 18 | + | |
| 19 | +const parseConnectionAddress = (address: string): ConnectionRef | null => { | |
| 20 | + const parts = address.split("."); | |
| 21 | + if (parts.length !== 4 || parts[0] !== "tools") return null; | |
| 22 | + const [, integration, owner, name] = parts; | |
| 23 | + if (!integration || !name) return null; | |
| 24 | + if (owner !== "org" && owner !== "user") return null; | |
| 25 | + return { | |
| 26 | + owner: owner as Owner, | |
| 27 | + integration: IntegrationSlug.make(integration), | |
| 28 | + name: ConnectionName.make(name), | |
| 29 | + }; | |
| 30 | +}; | |
| 31 | + | |
| 32 | +const bindingError = (input: { | |
| 33 | + readonly integration: string; | |
| 34 | + readonly connection?: string; | |
| 35 | + readonly message: string; | |
| 36 | + readonly cause?: unknown; | |
| 37 | +}): BindingError => | |
| 38 | + new BindingError({ | |
| 39 | + role: input.integration, | |
| 40 | + integration: input.integration, | |
| 41 | + requestedConnection: input.connection, | |
| 42 | + message: input.message, | |
| 43 | + }); | |
| 44 | + | |
| 45 | +const executeErrorMessage = (cause: ExecuteError): string => | |
| 46 | + typeof cause.message === "string" && cause.message.length > 0 ? cause.message : cause._tag; | |
| 47 | + | |
| 48 | +const toCandidate = (connection: { | |
| 49 | + readonly address: unknown; | |
| 50 | + readonly integration: unknown; | |
| 51 | + readonly name?: unknown; | |
| 52 | + readonly owner?: unknown; | |
| 53 | +}): ConnectionCandidate => ({ | |
| 54 | + address: String(connection.address), | |
| 55 | + integration: String(connection.integration), | |
| 56 | + ...(connection.name !== undefined ? { name: String(connection.name) } : {}), | |
| 57 | + ...(connection.owner !== undefined ? { owner: String(connection.owner) } : {}), | |
| 58 | +}); | |
| 59 | + | |
| 60 | +export const makeSelfHostAppsResolver = (input: { readonly ctx: PluginCtx }): ClientResolver => ({ | |
| 61 | + listConnections: ({ integration }) => | |
| 62 | + input.ctx.connections.list({ integration: IntegrationSlug.make(integration) }).pipe( | |
| 63 | + Effect.map((connections) => connections.map(toCandidate)), | |
| 64 | + Effect.mapError((cause) => | |
| 65 | + bindingError({ | |
| 66 | + integration, | |
| 67 | + message: `failed to list ${integration} connections`, | |
| 68 | + cause, | |
| 69 | + }), | |
| 70 | + ), | |
| 71 | + ), | |
| 72 | + | |
| 73 | + resolveConnection: ({ connection }) => | |
| 74 | + Effect.gen(function* () { | |
| 75 | + const ref = parseConnectionAddress(connection); | |
| 76 | + if (!ref) return null; | |
| 77 | + const row = yield* input.ctx.connections.get(ref).pipe( | |
| 78 | + Effect.mapError((cause) => | |
| 79 | + bindingError({ | |
| 80 | + integration: String(ref.integration), | |
| 81 | + connection, | |
| 82 | + message: `failed to resolve connection ${connection}`, | |
| 83 | + cause, | |
| 84 | + }), | |
| 85 | + ), | |
| 86 | + ); | |
| 87 | + return row ? toCandidate(row) : null; | |
| 88 | + }), | |
| 89 | + | |
| 90 | + call: ({ integration, connection, path, args }) => | |
| 91 | + Effect.gen(function* () { | |
| 92 | + const ref = parseConnectionAddress(connection); | |
| 93 | + if (!ref) { | |
| 94 | + return yield* bindingError({ | |
| 95 | + integration, | |
| 96 | + connection, | |
| 97 | + message: `invalid connection address ${connection}`, | |
| 98 | + }); | |
| 99 | + } | |
| 100 | + if (String(ref.integration) !== integration) { | |
| 101 | + return yield* bindingError({ | |
| 102 | + integration, | |
| 103 | + connection, | |
| 104 | + message: `connection "${connection}" belongs to integration "${ref.integration}", not "${integration}"`, | |
| 105 | + }); | |
| 106 | + } | |
| 107 | + const tool = path.join("."); | |
| 108 | + const address = ToolAddress.make(`${connection}.${tool}`); | |
| 109 | + const payload = args[0] ?? {}; | |
| 110 | + const result = yield* input.ctx.execute(address, payload).pipe( | |
| 111 | + Effect.mapError((cause) => | |
| 112 | + bindingError({ | |
| 113 | + integration, | |
| 114 | + connection, | |
| 115 | + message: executeErrorMessage(cause), | |
| 116 | + cause, | |
| 117 | + }), | |
| 118 | + ), | |
| 119 | + ); | |
| 120 | + if (isToolResult(result)) { | |
| 121 | + if (result.ok) return result.data; | |
| 122 | + return yield* bindingError({ | |
| 123 | + integration, | |
| 124 | + connection, | |
| 125 | + message: result.error.message, | |
| 126 | + }); | |
| 127 | + } | |
| 128 | + return result; | |
| 129 | + }), | |
| 130 | +}); | |