Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

refactor: generalize the review stream part for a second nested tool

Khuram Niazb57674fd33b2
diff --git a/__tests__/AgentChatMessages.spec.tsx b/__tests__/AgentChatMessages.spec.tsx
index cd01181..170fe8b 100644
--- a/__tests__/AgentChatMessages.spec.tsx
+++ b/__tests__/AgentChatMessages.spec.tsx
@@ -10,7 +10,7 @@ const chat = {
addToolApprovalResponse: vi.fn(),
interruptedTurn: false,
dismissInterrupted: vi.fn(),
- reviewStreams: {} as Record<string, string>,
+ toolStreams: {} as Record<string, string>,
};
vi.mock("@/components/agent/AgentChatProvider", () => ({
useAgentChat: () => chat,
@@ -42,7 +42,7 @@ describe("AgentChatMessages", () => {
chat.messages = [];
chat.status = "ready";
chat.interruptedTurn = false;
- chat.reviewStreams = {};
+ chat.toolStreams = {};
});
it("renders the running card while the tool input is still arriving", () => {
@@ -280,7 +280,7 @@ describe("AgentChatMessages", () => {
it("renders the review as it streams, before the tool result exists", () => {
chat.status = "streaming";
- chat.reviewStreams = {
+ chat.toolStreams = {
rv1: "SCORES: overall=78 impact=72 clarity=81 ats=69\n\n## Summary\n\nSolid.",
};
chat.messages = [assistant([reviewRunning])];
@@ -294,7 +294,7 @@ describe("AgentChatMessages", () => {
it("shows the running card until the first review token arrives", () => {
chat.status = "streaming";
- chat.reviewStreams = {};
+ chat.toolStreams = {};
chat.messages = [assistant([reviewRunning])];
render(<AgentChatMessages />);
expect(screen.getByText(/reviewing your resume/i)).toBeInTheDocument();
diff --git a/__tests__/AgentChatProvider.spec.tsx b/__tests__/AgentChatProvider.spec.tsx
index 22cc2dd..d65b55e 100644
--- a/__tests__/AgentChatProvider.spec.tsx
+++ b/__tests__/AgentChatProvider.spec.tsx
@@ -77,7 +77,7 @@ function Probe() {
<span data-testid="state">{`${c.isOpen}|${c.approvalPending}|${c.interruptedTurn}|${holder}|${c.preflight.ok}`}</span>
<span data-testid="composer-nonce">{c.composerNonce}</span>
<span data-testid="panel-expanded">{String(c.isPanelExpanded)}</span>
- <span data-testid="review-stream">{c.reviewStreams["rv1"] ?? ""}</span>
+ <span data-testid="review-stream">{c.toolStreams["rv1"] ?? ""}</span>
</div>
);
}
@@ -467,8 +467,16 @@ describe("AgentChatProvider", () => {
it("accumulates transient review deltas keyed by tool call id", async () => {
setup();
await act(async () => {
- chatInit().onData({ type: "data-review", id: "rv1", data: { delta: "SCO" } });
- chatInit().onData({ type: "data-review", id: "rv1", data: { delta: "RES:" } });
+ chatInit().onData({
+ type: "data-nested-stream",
+ id: "rv1",
+ data: { delta: "SCO" },
+ });
+ chatInit().onData({
+ type: "data-nested-stream",
+ id: "rv1",
+ data: { delta: "RES:" },
+ });
chatInit().onData({ type: "data-paste", id: "rv1", data: { delta: "nope" } });
});
expect(screen.getByTestId("review-stream")).toHaveTextContent("SCORES:");
diff --git a/__tests__/agentNestedStreamSpike.spec.ts b/__tests__/agentNestedStreamSpike.spec.ts
index b69625e..b1c96af 100644
--- a/__tests__/agentNestedStreamSpike.spec.ts
+++ b/__tests__/agentNestedStreamSpike.spec.ts
@@ -59,7 +59,7 @@ describe("nested generation stream plumbing", () => {
execute: async () => {
for (const delta of ["one ", "two ", "three"]) {
writer.write({
- type: "data-review",
+ type: "data-nested-stream",
id: "call-1",
data: { delta },
transient: true,
@@ -77,7 +77,7 @@ describe("nested generation stream plumbing", () => {
const chunks = await collect(stream);
const types = chunks.map((c) => c.type);
- const firstTransient = types.indexOf("data-review");
+ const firstTransient = types.indexOf("data-nested-stream");
const output = types.indexOf("tool-output-available");
expect(firstTransient).toBeGreaterThanOrEqual(0);
@@ -86,7 +86,7 @@ describe("nested generation stream plumbing", () => {
expect(firstTransient).toBeLessThan(output);
expect(
chunks
- .filter((c) => c.type === "data-review")
+ .filter((c) => c.type === "data-nested-stream")
.map((c) => c.data.delta)
.join(""),
).toBe("one two three");
diff --git a/__tests__/agentReviewResumeTool.spec.ts b/__tests__/agentReviewResumeTool.spec.ts
index c3c5a4e..53159dc 100644
--- a/__tests__/agentReviewResumeTool.spec.ts
+++ b/__tests__/agentReviewResumeTool.spec.ts
@@ -122,7 +122,7 @@ describe("review_resume agent tool", () => {
await execute(buildReviewResumeTool(ctx()), {});
expect(writer.write).toHaveBeenCalledTimes(3);
const first = writer.write.mock.calls[0][0];
- expect(first.type).toBe("data-review");
+ expect(first.type).toBe("data-nested-stream");
expect(first.id).toBe("call-1");
expect(first.transient).toBe(true);
expect(
diff --git a/src/components/agent/AgentChatMessages.tsx b/src/components/agent/AgentChatMessages.tsx
index 058ccb9..15d4c52 100644
--- a/src/components/agent/AgentChatMessages.tsx
+++ b/src/components/agent/AgentChatMessages.tsx
@@ -71,7 +71,7 @@ function showThinking(messages: UIMessage[], status: string): boolean {
export function AgentChatMessages() {
const {
messages,
- reviewStreams,
+ toolStreams,
status,
regenerate,
addToolApprovalResponse,
@@ -127,7 +127,7 @@ export function AgentChatMessages() {
case "input-available": {
const streamed =
getToolName(part) === "review_resume"
- ? reviewStreams[part.toolCallId]
+ ? toolStreams[part.toolCallId]
: undefined;
if (!streamed) return <AgentToolRunningCard key={i} part={part} />;
const parsed = parseResumeReview(streamed);
diff --git a/src/components/agent/AgentChatProvider.tsx b/src/components/agent/AgentChatProvider.tsx
index 849e1cf..1b141e6 100644
--- a/src/components/agent/AgentChatProvider.tsx
+++ b/src/components/agent/AgentChatProvider.tsx
@@ -29,9 +29,9 @@ import { getUserSettings } from "@/actions/userSettings.actions";
import { checkOllamaConnection } from "@/utils/ai.utils";
import { AiProvider } from "@/models/ai.model";
import {
- AGENT_REVIEW_PART_TYPE,
+ AGENT_NESTED_STREAM_PART_TYPE,
type AgentAddJobResult,
- type AgentReviewStreamData,
+ type AgentNestedStreamData,
type PageContext,
} from "@/models/agent.model";
@@ -110,9 +110,9 @@ function useAgentChatValue(initialMessages: UIMessage[]) {
}, [pathname]);
// Transient data parts never land in message.parts — the SDK hands them to
- // onData and drops them — so the review's streaming text lives here, keyed
- // by toolCallId so two reviews in one conversation cannot collide.
- const [reviewStreams, setReviewStreams] = useState<Record<string, string>>({});
+ // onData and drops them — so a nested tool's streaming text lives here,
+ // keyed by toolCallId so two nested calls cannot collide.
+ const [toolStreams, setToolStreams] = useState<Record<string, string>>({});
const chat = useChat({
messages: initialMessages,
@@ -126,10 +126,10 @@ function useAgentChatValue(initialMessages: UIMessage[]) {
// POSTs back to execute the tool. It is the mechanism, not a nicety.
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
onData: (part) => {
- if (part.type !== AGENT_REVIEW_PART_TYPE || !part.id) return;
+ if (part.type !== AGENT_NESTED_STREAM_PART_TYPE || !part.id) return;
const id = part.id;
- const { delta } = part.data as AgentReviewStreamData;
- setReviewStreams((prev) => ({ ...prev, [id]: (prev[id] ?? "") + delta }));
+ const { delta } = part.data as AgentNestedStreamData;
+ setToolStreams((prev) => ({ ...prev, [id]: (prev[id] ?? "") + delta }));
},
onFinish: ({ message, isAbort, isError }) => {
// onFinish runs from a finally block, so it fires on abort too.
@@ -249,7 +249,7 @@ function useAgentChatValue(initialMessages: UIMessage[]) {
chat.clearError();
setInterruptedTurn(false);
setComposerNonce((n) => n + 1);
- setReviewStreams({});
+ setToolStreams({});
setQueued(undefined);
// Clearing an idle conversation aborted nothing worth reporting.
if (wasStreaming) toastInfo("Conversation cleared and generation stopped.");
@@ -314,7 +314,7 @@ function useAgentChatValue(initialMessages: UIMessage[]) {
open,
close,
messages: chat.messages,
- reviewStreams,
+ toolStreams,
status: chat.status,
error: chat.error,
clearError: chat.clearError,
diff --git a/src/lib/agent/tools/reviewResume.ts b/src/lib/agent/tools/reviewResume.ts
index 9e475f1..a69a011 100644
--- a/src/lib/agent/tools/reviewResume.ts
+++ b/src/lib/agent/tools/reviewResume.ts
@@ -13,7 +13,7 @@ import {
} from "@/lib/ai/prompts/resume-review";
import { parseResumeReview } from "@/lib/ai/resumeReview/parse";
import { saveResumeReviewResult } from "@/actions/profile.actions";
-import { AGENT_REVIEW_PART_TYPE } from "@/models/agent.model";
+import { AGENT_NESTED_STREAM_PART_TYPE } from "@/models/agent.model";
import type { AgentReviewResumeResult } from "@/models/agent.model";
import type { ResumeReviewData } from "@/models/ai.schemas";
@@ -86,7 +86,7 @@ export function buildReviewResumeTool(ctx: ReviewResumeContext) {
for await (const delta of sub.textStream) {
generated += delta;
ctx.writer.write({
- type: AGENT_REVIEW_PART_TYPE,
+ type: AGENT_NESTED_STREAM_PART_TYPE,
id: toolCallId,
data: { delta },
transient: true,
diff --git a/src/models/agent.model.ts b/src/models/agent.model.ts
index 70353c2..f8fd510 100644
--- a/src/models/agent.model.ts
+++ b/src/models/agent.model.ts
@@ -79,12 +79,13 @@ export type AgentGetResumeResult =
| { status: "no_resumes" }
| { status: "unreadable"; title: string; reason: string };
-// Transient stream part carrying the nested review generation's tokens. It is
-// never persisted: the finished review lands in the tool result, and keeping
-// both would duplicate it in storage and in model context.
-export const AGENT_REVIEW_PART_TYPE = "data-review";
+// Transient stream part carrying a nested generation's tokens. It is never
+// persisted: the finished artifact lands in the tool result, and keeping both
+// would duplicate it in storage and in model context. Shared by review_resume
+// and match_job — the toolCallId key is what keeps two of them apart.
+export const AGENT_NESTED_STREAM_PART_TYPE = "data-nested-stream";
-export type AgentReviewStreamData = { delta: string };
+export type AgentNestedStreamData = { delta: string };
// What review_resume returns to the model AND to the result card. The resume
// text is deliberately absent — follow-ups answer from `body`, not from the