Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

21.2 KB
Raw Blame
import type { MessageContext, ThreadSummary } from "./chat";
import type { AssistantDocument } from "./document";
import type { IconName } from "@/ui";
import { Trans, useLingui } from "@lingui/react/macro";
import { useRouter } from "expo-router";
import { useState } from "react";
import { ActivityIndicator, View } from "react-native";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import { useThreads } from "./chat";
import { ChatView, ContextChip } from "./chat-view";
import { ChatWelcome } from "./chat-welcome";
import { useLetterAssistantDocument, useResumeAssistantDocument } from "./document";
import { takeAssistantHandoff } from "./handoff";
import { publishProposals } from "./proposals";
import { ConnectionNotice, ModelButton, ModelSheet, ProviderGate, useConversationProvider } from "./provider";
import { PastConversations } from "./threads";
import { TranslateResume } from "./translate-resume";
import { client } from "@/api/client";
import { getAccountScope, useSession } from "@/auth/session";
import { useLetterStore, useResumeStore } from "@/draft";
import { useHasAIProvider } from "@/features/ai/providers";
import { useUnlockResume } from "@/features/write/unlock";
import { routes } from "@/routes";
import { Banner, Button, Icon, IconButton, Text, useTheme } from "@/ui";

export type AssistantScreenProps = {
	resumeId: string;
	/** A conversation to open (another document's row in Past conversations opens it here). */
	threadId?: string | undefined;
	/** A question to send in a new conversation as soon as the assistant can. */
	ask?: string | undefined;
	/** A question to review in the composer before sending. */
	draft?: string | undefined;
	/** Prepare for next step: the application's suggestions (fit, follow-up, interview). */
	prepare?: boolean | undefined;
	/** The application this resume was opened for. */
	applicationId?: string | undefined;
};

/** Another document's conversation opens that document, with the Assistant on it. */
function openElsewhere(router: ReturnType<typeof useRouter>, thread: ThreadSummary) {
	if (thread.workingResumeId && !thread.coverLetterId) {
		router.replace(routes.builder(thread.workingResumeId));
		router.push({ pathname: routes.assistant(thread.workingResumeId), params: { thread: thread.id } });
	} else if (thread.coverLetterId) {
		router.replace(routes.letter(thread.coverLetterId));
		router.push({ pathname: routes.letterAssistant(thread.coverLetterId), params: { thread: thread.id } });
	}
}

const belongsTo = (thread: ThreadSummary, document: AssistantDocument) =>
	document.kind === "letter"
		? thread.coverLetterId === document.id
		: thread.workingResumeId === document.id && !thread.coverLetterId;

/**
 * The builder's Assistant, full screen over the builder: it knows the open resume and the posting it's for, proposes
 * edits as cards (marked on Page), and asks before writing anything the resume doesn't say.
 */
export function AssistantScreen(props: AssistantScreenProps) {
	useSession();
	const document = useResumeAssistantDocument(props.resumeId, props.applicationId);
	const loadFailed = useResumeStore((state) => state.document === null && state.status === "error");
	return (
		<DocumentAssistantScreen
			{...props}
			key={`${getAccountScope()}:${props.resumeId}:${props.threadId}:${props.ask}:${props.draft}:${props.prepare}`}
			kind="resume"
			documentId={props.resumeId}
			document={document}
			loadFailed={loadFailed}
		/>
	);
}

export type LetterAssistantScreenProps = Omit<AssistantScreenProps, "resumeId"> & { letterId: string };

export function LetterAssistantScreen(props: LetterAssistantScreenProps) {
	useSession();
	const document = useLetterAssistantDocument(props.letterId, props.applicationId);
	const loadFailed = useLetterStore((state) => state.document === null && state.status === "error");
	return (
		<DocumentAssistantScreen
			{...props}
			key={`${getAccountScope()}:${props.letterId}:${props.threadId}:${props.ask}:${props.draft}`}
			kind="letter"
			documentId={props.letterId}
			document={document}
			loadFailed={loadFailed}
		/>
	);
}

type DocumentAssistantScreenProps = Omit<AssistantScreenProps, "resumeId"> & {
	kind: "resume" | "letter";
	documentId: string;
	document: AssistantDocument | null;
	loadFailed: boolean;
};

function DocumentAssistantScreen(props: DocumentAssistantScreenProps) {
	const router = useRouter();
	const { t } = useLingui();
	const { colors, space } = useTheme();
	const session = useSession();
	const { loadFailed } = props;
	const [savedPosting, setSavedPosting] = useState<AssistantDocument["posting"]>(null);
	const document =
		props.document && savedPosting
			? { ...props.document, posting: savedPosting, madeFor: savedPosting.id, jobCopy: true }
			: props.document;
	if (savedPosting && props.document?.posting?.id === savedPosting.id) setSavedPosting(null);
	const store = props.kind === "letter" ? useLetterStore : useResumeStore;
	const providers = useHasAIProvider();
	const threads = useThreads();
	// Text the coach handed over for this resume and account: it waits in a new conversation's box, unsent.
	const [handed] = useState(() =>
		props.kind === "resume" ? takeAssistantHandoff(getAccountScope(), props.documentId) : null,
	);
	// null: the document's latest conversation; "new": a new one.
	const [selected, setSelected] = useState<string | null>(
		props.ask || props.draft || props.prepare || handed ? "new" : (props.threadId ?? null),
	);
	const [ask, setAsk] = useState(props.ask ?? null);
	const [prepare, setPrepare] = useState(props.prepare ?? false);
	const [view, setView] = useState<"thread" | "history">("thread");
	const [modelOpen, setModelOpen] = useState(false);
	const [translating, setTranslating] = useState(false);
	const [context, setContext] = useState<MessageContext>({ document: true, posting: true });
	const [draftKey, setDraftKey] = useState(0);

	const mine = document ? (threads.data ?? []).filter((thread) => belongsTo(thread, document)) : [];
	const threadId = selected === "new" ? null : (selected ?? mine[0]?.id ?? null);
	const draft = draftKey === 0 ? (props.draft ?? handed) : null;
	const summary = mine.find((thread) => thread.id === threadId);
	const { provider, stranded, label, choose } = useConversationProvider(providers, summary, threadId);

	const newConversation = () => {
		if (props.kind === "resume") publishProposals(props.documentId, []);
		setDraftKey((key) => key + 1);
		setContext({ document: true, posting: true });
		setSelected("new");
		setView("thread");
	};

	const openThread = (thread: ThreadSummary) => {
		if (!document || !belongsTo(thread, document)) return openElsewhere(router, thread);
		setDraftKey((key) => key + 1);
		setContext({ document: true, posting: true });
		setSelected(thread.id);
		setView("thread");
	};

	// Explicit links must belong to this document. Never silently replace a missing or mismatched selection.
	const linked = selected !== null && selected === props.threadId ? selected : null;
	const linkInvalid = linked && threads.data && document && !mine.some((thread) => thread.id === linked);

	const waiting = (message: string, action?: { label: string; onPress: () => void }) => (
		<View style={{ flex: 1, alignItems: "center", justifyContent: "center", gap: space.s2, padding: space.s4 }}>
			<ActivityIndicator />
			<Text color="ink2" align="center">
				{message}
			</Text>
			{action ? <Button variant="link" label={action.label} onPress={action.onPress} /> : null}
		</View>
	);
	const notice = (tone: "warn" | "danger", message: string, action: { label: string; onPress: () => void }) => (
		<View style={{ padding: space.s4 }}>
			<Banner tone={tone} message={message} action={action} />
		</View>
	);

	// Chat (and Accept) mounts only after every check passes; each other state says what it waits for or offers a way on.
	const body =
		session.status !== "signed-in" ? (
			notice("warn", t`Sign in to use the assistant.`, {
				label: t`Sign in`,
				onPress: () => router.push(routes.signIn()),
			})
		) : loadFailed ? (
			notice("danger", props.kind === "letter" ? t`Couldn't load the cover letter.` : t`Couldn't load the resume.`, {
				label: t`Retry`,
				onPress: () =>
					void store
						.getState()
						.open(props.documentId)
						.catch(() => undefined),
			})
		) : !document ? (
			waiting(props.kind === "letter" ? t`Opening the letter…` : t`Opening the resume…`, {
				label: props.kind === "letter" ? t`Open letter` : t`Open resume`,
				onPress: () =>
					router.replace(props.kind === "letter" ? routes.letter(props.documentId) : routes.builder(props.documentId)),
			})
		) : threads.isError ? (
			notice("danger", t`Conversations couldn't load.`, { label: t`Retry`, onPress: () => void threads.refetch() })
		) : !threads.data ? (
			waiting(t`Loading conversations…`)
		) : linkInvalid && view !== "history" ? (
			notice("warn", t`This conversation couldn't be opened.`, {
				label: t`New conversation`,
				onPress: () => setSelected("new"),
			})
		) : view === "history" ? (
			<PastConversations
				threads={threads.data ?? []}
				isMine={(thread) => belongsTo(thread, document)}
				currentId={threadId}
				onOpen={openThread}
				onDeleted={(id) => {
					if (id === threadId) setSelected("new");
				}}
			/>
		) : (
			<ChatView
				key={draftKey}
				threadId={threadId}
				createThread={async (scope) =>
					(
						await client.agent.threads.start(
							{
								...(document.kind === "letter" ? { coverLetterId: document.id } : { resumeId: document.id }),
								...(provider ? { aiProviderId: provider.id } : {}),
							},
							{ context: { scope } },
						)
					).id
				}
				onThreadCreated={(id) => {
					setSelected(id);
					setPrepare(false);
					void threads.refetch();
				}}
				context={{ ...context, ...(document.posting ? { applicationId: document.posting.id } : {}) }}
				onPostingSaved={(posting) => {
					setSavedPosting(posting);
					setContext((previous) => ({ ...previous, posting: true, applicationId: posting.id }));
					router.setParams({ applicationId: posting.id });
				}}
				document={document}
				connection={label}
				privacy={(history) => disclosure(t, document, context, label, history.earlier > 0)}
				placeholder={t`Ask, or describe a change…`}
				assistantName={t`Assistant`}
				disabled={!provider}
				{...(draft && threadId === null ? { draft } : {})}
				notice={
					stranded ? (
						<ConnectionNotice
							label={summary?.providerLabel ?? null}
							fallback={providers.usableProviders[0]}
							onUse={(id) => void choose(id)}
							onChoose={() => setModelOpen(true)}
						/>
					) : document.locked ? (
						// A locked document still takes questions; edits wait for Unlock (web assistant-panel).
						<LockedNotice document={document} />
					) : null
				}
				prompt={ask}
				onPromptSent={() => setAsk(null)}
				onSwitchModel={() => setModelOpen(true)}
				chips={
					<>
						<ContextChip
							icon="brain"
							label={t`Knowledge`}
							pressed={context.memory === true}
							onPress={() => setContext({ ...context, memory: !context.memory })}
						/>
						{context.document ? (
							<ContextChip
								icon="file-text"
								label={document.name}
								removeLabel={t`Don't send ${document.name}`}
								onRemove={() => setContext({ ...context, document: false })}
							/>
						) : (
							// A chip that's off stays, so it can be sent again (web conversation.tsx).
							<ContextChip
								icon="plus"
								label={document.name}
								pressed={false}
								onPress={() => setContext({ ...context, document: true })}
							/>
						)}
						{document.posting ? (
							context.posting ? (
								<ContextChip
									icon="briefcase"
									label={t`${document.posting.company} posting`}
									removeLabel={t`Don't send ${document.posting.company} posting`}
									onRemove={() => setContext({ ...context, posting: false })}
								/>
							) : (
								<ContextChip
									icon="plus"
									label={t`${document.posting.company} posting`}
									pressed={false}
									onPress={() => setContext({ ...context, posting: true })}
								/>
							)
						) : null}
						{needsJobCopy(document, context) && document.posting ? (
							<Button
								size="sm"
								variant="secondary"
								label={t`Copy for this job`}
								onPress={() =>
									router.push({
										pathname: routes.new(),
										params: { step: "copy", applicationId: document.posting?.id, sourceResumeId: document.id },
									})
								}
							/>
						) : null}
					</>
				}
				empty={(send) => <Suggestions document={document} prepare={prepare} onPick={send} />}
			/>
		);

	const gate = (
		<ProviderGate
			providers={providers}
			readOnly={view === "history" || threadId !== null}
			threadsError={threads.error}
			loading={threads.isPending}
			onRetry={() => {
				providers.retry();
				void threads.refetch();
			}}
			notSetUp={t`The assistant isn't set up on this server. Whoever runs it needs to set ENCRYPTION_SECRET. Everything else works without it.`}
		>
			{body}
		</ProviderGate>
	);

	return (
		<SafeAreaProvider>
			<SafeAreaView edges={["top", "bottom", "left", "right"]} style={{ flex: 1, backgroundColor: colors.surface }}>
				<View
					style={{
						flexDirection: "row",
						alignItems: "center",
						minHeight: 52,
						paddingStart: space.s3,
						paddingEnd: space.s1,
						borderBottomWidth: 1,
						borderColor: colors.line,
					}}
				>
					<Icon name="sparkle" size={20} color={colors.accent} />
					<View style={{ flex: 1, paddingStart: space.s2 }}>
						<Text variant="title" accessibilityRole="header">
							<Trans>Assistant</Trans>
						</Text>
						{providers.hasAIProvider ? (
							<ModelButton current={provider} label={label} onPress={() => setModelOpen(true)} />
						) : null}
					</View>
					<IconButton icon="note-pencil" label={t`New conversation`} onPress={newConversation} />
					<IconButton
						icon="clock-counter-clockwise"
						label={t`Past conversations`}
						selected={view === "history"}
						onPress={() => setView(view === "history" ? "thread" : "history")}
					/>
					<IconButton icon="x" label={t`Close the assistant`} onPress={() => router.back()} />
				</View>

				{props.kind === "resume" ? (
					<View style={{ gap: space.s2, padding: space.s3, borderBottomWidth: 1, borderColor: colors.line }}>
						<Text variant="caption" color="ink2">
							<Trans>Review each set before accepting it together. Some fields still need Write.</Trans>
						</Text>
						<Button
							variant="link"
							label={t`Open Write`}
							onPress={() =>
								router.replace({
									pathname: routes.builder(props.documentId, "write"),
									params: props.applicationId ? { applicationId: props.applicationId } : {},
								})
							}
						/>
						<Button variant="link" label={t`Translate into…`} onPress={() => setTranslating(true)} />
					</View>
				) : null}
				{translating && props.kind === "resume" && (
					<TranslateResume resumeId={props.documentId} onClose={() => setTranslating(false)} />
				)}
				{gate}

				<ModelSheet
					open={modelOpen}
					onClose={() => setModelOpen(false)}
					providers={providers.usableProviders}
					currentId={provider?.id}
					onChoose={(id) => void choose(id)}
				/>
			</SafeAreaView>
		</SafeAreaProvider>
	);
}

type Translate = ReturnType<typeof useLingui>["t"];

/** What a send shares, and with whom (web's disclosure line). */
function disclosure(
	t: Translate,
	document: AssistantDocument,
	context: MessageContext,
	provider: string,
	hasHistory: boolean,
) {
	const posting = context.posting && document.posting;
	const sends = context.document
		? document.kind === "letter"
			? posting
				? t`Sends this letter and the posting to ${provider} with your key, only when you tap send.`
				: t`Sends this letter to ${provider} with your key, only when you tap send.`
			: posting
				? t`Sends this resume and the posting to ${provider} with your key, only when you tap send.`
				: t`Sends this resume to ${provider} with your key, only when you tap send.`
		: posting
			? t`Sends your message and the posting to ${provider} with your key, only when you tap send.`
			: t`Sends your message to ${provider} with your key, only when you tap send.`;
	// Said only once there is history to send or to leave out.
	const history = !hasHistory
		? ""
		: !context.document || !context.posting
			? ` ${t`Each send starts fresh with this message and selected files. Previous conversation history stays here.`}`
			: ` ${t`Previous messages, including document details, are also sent.`}`;
	const knowledge =
		context.memory === true ? ` ${t`Knowledge facts and stories the assistant looks up are sent too.`}` : "";
	const copy =
		needsJobCopy(document, context) && document.posting
			? ` ${t`Edits that use the ${document.posting.company} posting go to a copy made for that job, so this resume stays unchanged.`}`
			: "";
	return `${sends}${knowledge}${history}${copy}`;
}

/** Tailoring needs the job's own copy: the server refuses edits that use the posting on a resume other jobs share. */
const needsJobCopy = (document: AssistantDocument, context: MessageContext) =>
	document.kind === "resume" && Boolean(context.posting && document.posting) && !document.jobCopy;

/** `edits: false` for tasks that answer or draft rather than edit, which a locked document still takes. */
type Suggestion = { icon: IconName; label: string; hint: string; edits?: false };

/** Above the composer on a locked document: questions still work, edits wait for Unlock. */
function LockedNotice({ document }: { document: AssistantDocument }) {
	const { t } = useLingui();
	return document.kind === "resume" ? (
		<UnlockBanner resumeId={document.id} />
	) : (
		<Banner tone="info" icon="lock" message={t`Locked. I can answer questions; unlock to get edits.`} />
	);
}

function UnlockBanner({ resumeId }: { resumeId: string }) {
	const { t } = useLingui();
	const unlock = useUnlockResume(resumeId);
	return (
		<Banner
			tone="info"
			icon="lock"
			message={t`Locked. I can answer questions; unlock to get edits.`}
			action={{ label: unlock.isPending ? t`Unlocking…` : t`Unlock`, onPress: () => unlock.mutate() }}
		/>
	);
}

type SuggestionsProps = { document: AssistantDocument; prepare: boolean; onPick: (text: string) => void };

/** "What should we work on?" with starters that send at once. */
function Suggestions({ document, prepare, onPick }: SuggestionsProps) {
	const { t } = useLingui();
	const company = document.posting?.company;
	const tailor: Suggestion[] = company
		? [{ icon: "briefcase", label: t`Tailor to the ${company} posting`, hint: t`Uses the linked application` }]
		: [];
	let suggestions: Suggestion[] =
		prepare && company
			? [
					{
						icon: "briefcase",
						label: t`How well do I fit this role?`,
						hint: t`Compares your resume with the posting`,
						edits: false,
					},
					{
						icon: "envelope-simple",
						label: t`Draft a follow-up email`,
						hint: t`Short and polite, for the recruiter`,
						edits: false,
					},
					{
						icon: "chat-text",
						label: t`Prepare me for the interview`,
						hint: t`Likely questions, from the posting and your resume`,
						edits: false,
					},
					...tailor,
				]
			: [
					...tailor,
					{
						icon: "scissors",
						label: t`Find weak bullets`,
						hint: t`Checks every bullet for action and result`,
						edits: false,
					},
					{
						icon: "arrows-in-line-vertical",
						label: t`Tighten to one page`,
						hint: t`Suggests cuts, never deletes on its own`,
					},
					{ icon: "text-align-left", label: t`Draft a summary`, hint: t`From your experience entries` },
				];
	if (document.kind === "letter")
		suggestions = [
			...tailor,
			// After a draft was accepted, the one way back to a fresh draft: proposals for the whole letter.
			...(company
				? [
						{
							icon: "sparkle" as const,
							label: t`Redraft from the posting`,
							hint: t`A new version of the whole letter, as edits to accept`,
						},
					]
				: []),
			{ icon: "lightning", label: t`Make the opening stronger`, hint: t`Leads with why you fit` },
			{ icon: "arrows-in-line-vertical", label: t`Make it shorter`, hint: t`Keeps the most specific parts` },
			{ icon: "text-align-left", label: t`Make it more specific`, hint: t`Uses facts from your resume` },
		];

	return (
		<ChatWelcome
			description={
				<>
					{document.kind === "letter" ? (
						company ? (
							<Trans>
								I can see this letter and the {company} posting. Suggestions come back as edits you can accept one by
								one.
							</Trans>
						) : (
							<Trans>I can see this letter. Suggestions come back as edits you can review before accepting.</Trans>
						)
					) : company && prepare ? (
						// These tasks answer and draft; none of them edits the resume (web assistant-panel).
						<Trans>
							I can see this resume and the {company} posting. Ask how you fit, or get a follow-up email or interview
							prep.
						</Trans>
					) : company ? (
						<Trans>
							I can see this resume and the {company} posting. Suggestions come back as edits you can review before
							accepting.
						</Trans>
					) : (
						<Trans>I can see this resume. Suggestions come back as edits you can review before accepting.</Trans>
					)}
				</>
			}
			starters={suggestions.map((suggestion) => ({
				...suggestion,
				disabled: document.locked && suggestion.edits !== false,
				onPick: () => onPick(suggestion.label),
			}))}
		/>
	);
}