-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathflowRuntimeDeps.ts
More file actions
118 lines (105 loc) · 3.46 KB
/
Copy pathflowRuntimeDeps.ts
File metadata and controls
118 lines (105 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { resolveApiKey } from "~/domains/auth/resolve.js";
import { configureEmails } from "~/domains/emails/configureEmails.js";
import type { FlowRuntimeDeps } from "~/domains/runner/flowRuntimeDeps.js";
import type { CommandContext } from "~/shell/commandContext.js";
import { createPlatformClient } from "~/shell/platform/createPlatformClient.js";
import type { PlatformClient } from "~/shell/platform/createPlatformClient.js";
import { makeEnvVarDeps } from "./envVarDeps.js";
type CreateFlowRuntimeDepsArgs = {
readonly envDir: string;
readonly ctx: Pick<CommandContext, "apiBaseUrl" | "configDir" | "fs"> &
Partial<Pick<CommandContext, "log">>;
readonly platform?: PlatformClient;
readonly env?: Record<string, string | undefined>;
readonly resolveApiKeyFn?: typeof resolveApiKey;
readonly configureEmailsFn?: typeof configureEmails;
readonly createPlatform?: typeof createPlatformClient;
};
type GetInbox = NonNullable<FlowRuntimeDeps["getInbox"]>;
type LazyGetInboxArgs = Omit<
Required<CreateFlowRuntimeDepsArgs>,
"platform"
> & {
readonly platform: PlatformClient | undefined;
};
async function maybeGetIdentityTeamId(
platform: PlatformClient,
): Promise<string | undefined> {
const identity = await platform.getIdentity();
return identity.ok ? identity.value.team.id : undefined;
}
function lazyGetInbox({
envDir,
ctx,
platform,
env,
resolveApiKeyFn,
configureEmailsFn,
createPlatform,
}: LazyGetInboxArgs): GetInbox {
let getInboxPromise: Promise<GetInbox> | undefined;
async function configure(): Promise<GetInbox> {
const explicitEmailerUrl = env["EMAILER_URL"];
let teamId = env["CLOUD_AGENTS_INBOX_TEAM_ID"];
const apiKeyResult =
explicitEmailerUrl === undefined
? await resolveApiKeyFn(ctx.configDir, ctx.fs)
: undefined;
if (teamId === undefined && platform !== undefined) {
teamId = await maybeGetIdentityTeamId(platform);
}
if (teamId === undefined && apiKeyResult !== undefined) {
const identityPlatform = createPlatform(apiKeyResult.key, {
baseUrl: ctx.apiBaseUrl,
fetch: globalThis.fetch,
...(ctx.log ? { logger: ctx.log("trpc") } : {}),
});
teamId = await maybeGetIdentityTeamId(identityPlatform);
}
const teamIdPart = teamId !== undefined ? { teamId } : {};
if (explicitEmailerUrl !== undefined) {
const client = await configureEmailsFn(
{ emailerUrl: explicitEmailerUrl, ...teamIdPart },
envDir,
);
return client.getInbox;
}
if (apiKeyResult === undefined) {
throw new Error(
"getInbox requires EMAILER_URL, QAWOLF_API_KEY, or stored QA Wolf credentials. Run 'qawolf auth login'.",
);
}
const client = await configureEmailsFn(
{ apiKey: apiKeyResult.key, url: `${ctx.apiBaseUrl}/api`, ...teamIdPart },
envDir,
);
return client.getInbox;
}
return async (...args) => {
getInboxPromise ??= configure();
const getInbox = await getInboxPromise;
return getInbox(...args);
};
}
export function createFlowRuntimeDeps({
envDir,
ctx,
platform,
env = process.env,
resolveApiKeyFn = resolveApiKey,
configureEmailsFn = configureEmails,
createPlatform = createPlatformClient,
}: CreateFlowRuntimeDepsArgs): FlowRuntimeDeps {
return {
...makeEnvVarDeps(envDir, ctx.fs),
getInbox: lazyGetInbox({
envDir,
ctx,
platform,
env,
resolveApiKeyFn,
configureEmailsFn,
createPlatform,
}),
};
}