-
Notifications
You must be signed in to change notification settings - Fork 0
feat(editor): complete Wiki Compose P2 entry points and chat seed (#950) #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6e11568
feat(editor): complete Wiki Compose P2 entry points and chat seed (#950)
cursoragent af6299b
fix(wiki-compose): hydrate reload, defer seed clear, bilingual docs (…
cursoragent 22663a6
fix(wiki-compose): address CodeRabbit review on projection and seed (…
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
server/api/src/__tests__/routes/composeSessionProjection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * `composeSessionProjection` のユニットテスト (#950)。 | ||
| * Unit tests for `composeSessionProjection`. | ||
| */ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { projectComposeStateValues } from "../../routes/composeSessionProjection.js"; | ||
|
|
||
| describe("projectComposeStateValues", () => { | ||
| it("projects a Brief interrupt from __interrupt__", () => { | ||
| const projection = projectComposeStateValues({ | ||
| __interrupt__: [ | ||
| { | ||
| value: { | ||
| kind: "human_review_brief", | ||
| questions: [{ id: "q1", question: "Scope?", required: false, options: [] }], | ||
| pageSnapshot: { pageId: "p1", title: "T", body: "", hasContent: false }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(projection.phase).toBe("brief"); | ||
| expect(projection.briefQuestions).toHaveLength(1); | ||
| expect(projection.pageSnapshot).toMatchObject({ title: "T" }); | ||
| }); | ||
|
|
||
| it("keeps interrupt-derived phase when row phase is also present", () => { | ||
| const projection = projectComposeStateValues({ | ||
| phase: "brief:await_user", | ||
| __interrupt__: [ | ||
| { | ||
| value: { | ||
| kind: "human_review_research", | ||
| batch: null, | ||
| pendingSources: [], | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(projection.phase).toBe("research"); | ||
| }); | ||
|
|
||
| it("projects completion markdown from checkpoint values", () => { | ||
| const projection = projectComposeStateValues({ | ||
| phase: "completed", | ||
| completion: { | ||
| markdown: "## A\n\nBody", | ||
| sections: [ | ||
| { | ||
| sectionId: "sec-1", | ||
| heading: "A", | ||
| body: "Body", | ||
| citedSourceIds: [], | ||
| completedAt: "2026-01-01T00:00:00.000Z", | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| expect(projection.completedMarkdown).toBe("## A\n\nBody"); | ||
| expect(projection.draftedSections).toHaveLength(1); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /** | ||
| * Project LangGraph checkpoint state into Compose UI slices (#950). | ||
| * | ||
| * `GET /compose-sessions/:id` が interrupted / completed 行を再開するとき、 | ||
| * チェックポイントから Brief 質問・アウトライン等を復元する。 | ||
| * | ||
| * Maps persisted graph state (including `__interrupt__`) into a JSON shape the | ||
| * frontend hook can merge without replaying `POST /run`. | ||
| */ | ||
| import { GRAPH_CONTEXT_CONFIG_KEY } from "../agents/core/types/graphContext.js"; | ||
| import type { GraphContext } from "../agents/core/types/graphContext.js"; | ||
| import { resolveCheckpointerForRun } from "../agents/core/checkpoint/index.js"; | ||
| import { getRegisteredGraph } from "../agents/registry/graphRegistry.js"; | ||
| import type { WikiComposeSessionStatus } from "../schema/wikiComposeSessions.js"; | ||
|
|
||
| /** | ||
| * `GET /compose-sessions/:id` が返す UI projection。 | ||
| * Wire projection returned by `GET /compose-sessions/:id`. | ||
| */ | ||
| export interface ComposeSessionUiProjection { | ||
| phase?: string; | ||
| briefQuestions?: unknown[]; | ||
| pageSnapshot?: unknown; | ||
| pendingSources?: unknown[]; | ||
| latestBatch?: unknown; | ||
| approvedSources?: unknown[]; | ||
| outlineProposal?: unknown[]; | ||
| draftedSections?: unknown[]; | ||
| completedMarkdown?: string | null; | ||
| } | ||
|
|
||
| function phaseFromSessionRow(phase: string, status: WikiComposeSessionStatus): string { | ||
| if (status === "completed") return "completed"; | ||
| if (phase.startsWith("brief")) return "brief"; | ||
| if (phase.startsWith("research")) return "research"; | ||
| if (phase.startsWith("structure")) return "structure"; | ||
| if (phase.startsWith("draft")) return "draft"; | ||
| return "brief"; | ||
| } | ||
|
|
||
| /** | ||
| * Build UI projection from a LangGraph state snapshot (values + interrupts). | ||
| */ | ||
| export function projectComposeStateValues( | ||
| state: Record<string, unknown>, | ||
| ): ComposeSessionUiProjection { | ||
| const projection: ComposeSessionUiProjection = {}; | ||
|
|
||
| if (Array.isArray(state.briefQuestions)) { | ||
| projection.briefQuestions = state.briefQuestions; | ||
| } | ||
| if (state.pageSnapshot && typeof state.pageSnapshot === "object") { | ||
| projection.pageSnapshot = state.pageSnapshot; | ||
| } | ||
| if (Array.isArray(state.pendingSources)) { | ||
| projection.pendingSources = state.pendingSources; | ||
| } | ||
| if (Array.isArray(state.batches) && state.batches.length > 0) { | ||
| projection.latestBatch = state.batches[state.batches.length - 1]; | ||
| } | ||
| if (Array.isArray(state.approvedResearch)) { | ||
| projection.approvedSources = state.approvedResearch; | ||
| } | ||
| if (Array.isArray(state.outlineProposal) && state.outlineProposal.length > 0) { | ||
| projection.outlineProposal = state.outlineProposal; | ||
| } else { | ||
| const approved = state.approvedOutline as { sections?: unknown[] } | undefined; | ||
| if (approved?.sections?.length) { | ||
| projection.outlineProposal = approved.sections; | ||
| } | ||
| } | ||
|
|
||
| if (Array.isArray(state.draftedSections) && state.draftedSections.length > 0) { | ||
| projection.draftedSections = state.draftedSections; | ||
| } | ||
|
|
||
| const completion = state.completion; | ||
| if (completion && typeof completion === "object") { | ||
| const c = completion as { markdown?: string; sections?: unknown[] }; | ||
| if (typeof c.markdown === "string") { | ||
| projection.completedMarkdown = c.markdown; | ||
| } | ||
| if (Array.isArray(c.sections)) { | ||
| projection.draftedSections = c.sections; | ||
| } | ||
| } | ||
|
|
||
| const interrupts = state.__interrupt__; | ||
| if (Array.isArray(interrupts) && interrupts.length > 0) { | ||
| const entry = interrupts[0]; | ||
| const value = | ||
| entry && typeof entry === "object" ? (entry as { value?: unknown }).value : undefined; | ||
| if (value && typeof value === "object" && "kind" in value) { | ||
| const payload = value as { | ||
| kind: string; | ||
| questions?: unknown[]; | ||
| pageSnapshot?: unknown; | ||
| batch?: unknown; | ||
| pendingSources?: unknown[]; | ||
| outline?: unknown[]; | ||
| approvedSources?: unknown[]; | ||
| }; | ||
| switch (payload.kind) { | ||
| case "human_review_brief": | ||
| if (payload.questions) projection.briefQuestions = payload.questions; | ||
| if (payload.pageSnapshot) projection.pageSnapshot = payload.pageSnapshot; | ||
| projection.phase = "brief"; | ||
| break; | ||
| case "human_review_research": | ||
| if (payload.batch) projection.latestBatch = payload.batch; | ||
| if (payload.pendingSources) projection.pendingSources = payload.pendingSources; | ||
| projection.phase = "research"; | ||
| break; | ||
| case "human_review_outline": | ||
| if (payload.outline) projection.outlineProposal = payload.outline; | ||
| if (payload.approvedSources) projection.approvedSources = payload.approvedSources; | ||
| projection.phase = "structure"; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Interrupt-derived phase wins; row `phase` is only a fallback. | ||
| // interrupt 由来の phase を優先し、行の phase はフォールバックのみ。 | ||
| if (typeof state.phase === "string" && projection.phase === undefined) { | ||
| projection.phase = phaseFromSessionRow(state.phase, "interrupted"); | ||
| } | ||
|
|
||
| return projection; | ||
| } | ||
|
|
||
| /** | ||
| * チェックポイントから UI projection を読み込む。利用不可時は `null`。 | ||
| * Load checkpoint projection for a compose session row, or `null` when unavailable. | ||
| */ | ||
| export async function loadComposeSessionProjection(input: { | ||
| sessionId: string; | ||
| pageId: string; | ||
| graphId: string; | ||
| status: WikiComposeSessionStatus; | ||
| phase: string; | ||
| context: GraphContext; | ||
| }): Promise<ComposeSessionUiProjection | null> { | ||
| if (input.status !== "interrupted" && input.status !== "completed" && input.status !== "failed") { | ||
| return null; | ||
| } | ||
|
|
||
| const checkpointer = await resolveCheckpointerForRun(); | ||
| if (checkpointer === false) return null; | ||
|
|
||
| const registered = getRegisteredGraph(input.graphId); | ||
| if (!registered) return null; | ||
|
|
||
| const graph = registered.factory({ checkpointer }) as { | ||
| getState?: (config: unknown) => Promise<{ values?: Record<string, unknown> } | undefined>; | ||
| }; | ||
| if (typeof graph.getState !== "function") return null; | ||
|
|
||
| const config = { | ||
| configurable: { | ||
| thread_id: input.sessionId, | ||
| [GRAPH_CONTEXT_CONFIG_KEY]: input.context, | ||
| }, | ||
| }; | ||
|
|
||
| try { | ||
| const snap = await graph.getState(config); | ||
| const values = snap?.values; | ||
| if (!values || typeof values !== "object") return null; | ||
| const projection = projectComposeStateValues(values); | ||
| if (!projection.phase) { | ||
| projection.phase = phaseFromSessionRow(input.phase, input.status); | ||
| } | ||
| return projection; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.