Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refactor(get_page): move the response projection into its own module (module-size ratchet)

Howard Park6ad26eb1657b
diff --git a/src/core/ops/get-page-projection.ts b/src/core/ops/get-page-projection.ts
new file mode 100644
index 0000000000..076a7c05d2
--- /dev/null
+++ b/src/core/ops/get-page-projection.ts
@@ -0,0 +1,56 @@
+import type { Page } from '../types.ts';
+import { serializePageToMarkdown } from '../markdown.ts';
+
+export interface GetPageProjectionOpts {
+ revision: string;
+ tags: string[];
+ /** include_content: add the canonical serialized `content` field. */
+ includeContent: boolean;
+ /** content_only: only meaningful with includeContent; ignored without it. */
+ contentOnly: boolean;
+ resolved_slug?: string;
+ content_flag?: { reason: string; detail: string } | null;
+ /** include_timeline_entries (#5709): the page's timeline rows, read by the caller; present in both shapes. */
+ timeline_entries?: unknown;
+ /** A held source file (sync could not import it); present in both shapes so an editor sees it. */
+ file_held?: unknown;
+}
+
+/**
+ * Shape the get_page response from the reader-visible page body.
+ *
+ * #2225: `content` is the canonical serialized markdown (frontmatter +
+ * compiled_truth + `<!-- timeline -->` sentinel + timeline), built from the
+ * visible body so the privacy-fence strip applies to untrusted readers too.
+ * content_only returns just what a get→edit→put_page round trip needs, without
+ * the duplicate compiled_truth / timeline / frontmatter the full shape carries
+ * next to `content` (a 30 KB page otherwise comes back as ~62 KB).
+ */
+export function projectGetPage(visibleBody: Page, o: GetPageProjectionOpts) {
+ const { revision, tags, resolved_slug, content_flag } = o;
+ const extras = {
+ ...(o.timeline_entries !== undefined ? { timeline_entries: o.timeline_entries } : {}),
+ ...(o.file_held !== undefined ? { file_held: o.file_held } : {}),
+ ...(resolved_slug ? { resolved_slug } : {}), ...(content_flag ? { content_flag } : {}),
+ };
+ if (o.includeContent && o.contentOnly) {
+ const deletedAt = visibleBody.deleted_at;
+ return {
+ slug: visibleBody.slug,
+ type: visibleBody.type,
+ title: visibleBody.title,
+ revision,
+ tags,
+ content: serializePageToMarkdown(visibleBody, tags),
+ ...(deletedAt ? { deleted_at: deletedAt } : {}),
+ ...extras,
+ };
+ }
+ return {
+ ...visibleBody,
+ revision,
+ tags,
+ ...(o.includeContent ? { content: serializePageToMarkdown(visibleBody, tags) } : {}),
+ ...extras,
+ };
+}
diff --git a/src/core/ops/pages.ts b/src/core/ops/pages.ts
index 9c15c63c96..d36d05106f 100644
--- a/src/core/ops/pages.ts
+++ b/src/core/ops/pages.ts
@@ -15,6 +15,7 @@ import type { Page } from '../types.ts';
import { decodeDeepResearchId, deepResearchPageUrl } from '../deep-research-id.ts';
import { PageSnapshotAmbiguousError, type PageSnapshot } from '../page-state/types.ts';
import { serializePageToMarkdown } from '../markdown.ts';
+import { projectGetPage } from './get-page-projection.ts';
import { isAutoLinkEnabled } from '../link-extraction.ts';
import { sanitizeRemoteBody } from '../remote-body.ts';
import { getContentFlag } from '../quarantine.ts';
@@ -94,9 +95,6 @@ const get_page: Operation = {
const includeDeleted = (p.include_deleted as boolean) === true;
const includeContent = (p.include_content as boolean) === true;
const includeTimelineEntries = (p.include_timeline_entries as boolean) === true;
- // Only meaningful with include_content: a round-trip reader needs `content`
- // + `revision`, not the same text again as compiled_truth + timeline.
- const contentOnly = includeContent && (p.content_only as boolean) === true;
// #4329: honor a per-call source_id (pre-fix it was silently dropped).
// resolveRequestedScope (inside federatedSearchScope) enforces the remote
// caller's grant on the explicit value.
@@ -202,36 +200,13 @@ const get_page: Operation = {
// Opt-in (include_content: true): get_page is the most-called read op, and
// `content` roughly duplicates compiled_truth + timeline — always emitting
// it would double every reader's payload for the round-trip minority.
- if (contentOnly) {
- // Everything a get→edit→put_page round trip needs, without the duplicate
- // compiled_truth / timeline / frontmatter the full shape carries next to
- // `content` (a 30 KB page otherwise comes back as ~62 KB).
- const deletedAt = (visibleBody as { deleted_at?: unknown }).deleted_at;
- return {
- slug: visibleBody.slug,
- type: visibleBody.type,
- title: visibleBody.title,
- revision: snapshot!.revision,
- tags,
- content: serializePageToMarkdown(visibleBody as Page, tags),
- ...(includeTimelineEntries
- ? { timeline_entries: await ctx.engine.getTimeline(page.slug, await readPolicyOpts(ctx, { sourceId: page.source_id })) } : {}),
- ...(deletedAt ? { deleted_at: deletedAt } : {}),
- ...(resolved_slug ? { resolved_slug } : {}),
- ...(content_flag ? { content_flag } : {}),
- };
- }
- return {
- ...visibleBody,
- revision: snapshot!.revision,
- tags,
- ...(includeContent ? { content: serializePageToMarkdown(visibleBody as Page, tags) } : {}),
- ...(includeTimelineEntries
- ? { timeline_entries: await ctx.engine.getTimeline(page.slug, await readPolicyOpts(ctx, { sourceId: page.source_id })) } : {}),
- ...(resolved_slug ? { resolved_slug } : {}),
- ...(content_flag ? { content_flag } : {}),
+ const timelineEntries = includeTimelineEntries
+ ? await ctx.engine.getTimeline(page.slug, await readPolicyOpts(ctx, { sourceId: page.source_id })) : undefined;
+ return projectGetPage(visibleBody, {
+ revision: snapshot!.revision, tags, includeContent, contentOnly: (p.content_only as boolean) === true, resolved_slug, content_flag,
+ ...(timelineEntries ? { timeline_entries: timelineEntries } : {}),
...(held ? { file_held: fileHeldField(held, isUntrustedReader) } : {}),
- };
+ });
},
scope: 'read', mutating: false,
cliHints: { name: 'get', positional: ['slug'] },