Skip to content

Commit f52961a

Browse files
Merge pull request #27 from offendingcommit/feat/dream-viewer
feat(web): add dream output viewer
2 parents a17b577 + e549200 commit f52961a

16 files changed

Lines changed: 1288 additions & 0 deletions
352 KB
Loading
217 KB
Loading
338 KB
Loading
205 KB
Loading

packages/web/e2e/dreams.spec.ts

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
const STORE_KEY = "openconcho:instances";
4+
const STORE_VALUE = JSON.stringify({
5+
instances: [{ id: "i1", name: "Local", baseUrl: "http://localhost:9999", token: "" }],
6+
activeId: "i1",
7+
});
8+
9+
test.describe("Dreams route", () => {
10+
test.beforeEach(async ({ context }) => {
11+
await context.addInitScript(
12+
([key, value]) => {
13+
window.localStorage.setItem(key, value);
14+
},
15+
[STORE_KEY, STORE_VALUE],
16+
);
17+
// Stub the conclusions/list endpoint so the route can render real dreams.
18+
// :9999 is unreachable; this intercept replaces the network call entirely.
19+
// Use a function matcher so the trailing query string (?page=&page_size=) doesn't
20+
// break a glob.
21+
await context.route(
22+
(url) => url.pathname.endsWith("/conclusions/list"),
23+
async (route) => {
24+
const now = Date.now();
25+
const iso = (offsetMs: number) => new Date(now - offsetMs).toISOString();
26+
const items = [
27+
// Dream A — burst
28+
{
29+
id: "ind-1",
30+
content: "Alice prefers asynchronous communication",
31+
observer_id: "alice",
32+
observed_id: "bob",
33+
session_id: "sess-1",
34+
created_at: iso(1000),
35+
conclusion_type: "inductive",
36+
reasoning_tree: {
37+
conclusion_id: "ind-1",
38+
premises: [{ conclusion_id: "ded-1" }],
39+
},
40+
},
41+
{
42+
id: "ded-1",
43+
content: "Alice mentioned email twice and declined two meetings",
44+
observer_id: "alice",
45+
observed_id: "bob",
46+
session_id: "sess-1",
47+
created_at: iso(2000),
48+
conclusion_type: "deductive",
49+
reasoning_tree: {
50+
conclusion_id: "ded-1",
51+
premises: [{ conclusion_id: "exp-1" }, { conclusion_id: "exp-2" }],
52+
},
53+
},
54+
{
55+
id: "exp-1",
56+
content: "Alice said 'just email me'",
57+
observer_id: "alice",
58+
observed_id: "bob",
59+
session_id: "sess-1",
60+
created_at: iso(3000),
61+
conclusion_type: "explicit",
62+
},
63+
{
64+
id: "exp-2",
65+
content: "Alice declined the Tuesday standup",
66+
observer_id: "alice",
67+
observed_id: "bob",
68+
session_id: "sess-1",
69+
created_at: iso(4000),
70+
conclusion_type: "explicit",
71+
},
72+
// Dream B — 30 minutes ago, different pair → clusters separately
73+
{
74+
id: "ded-2",
75+
content: "Carol responds in the evenings",
76+
observer_id: "carol",
77+
observed_id: "dan",
78+
session_id: "sess-2",
79+
created_at: iso(30 * 60_000),
80+
conclusion_type: "deductive",
81+
},
82+
];
83+
await route.fulfill({
84+
status: 200,
85+
contentType: "application/json",
86+
body: JSON.stringify({
87+
items,
88+
total: items.length,
89+
pages: 1,
90+
page: 1,
91+
size: items.length,
92+
}),
93+
});
94+
},
95+
);
96+
});
97+
98+
test("shows a Dreams entry in the workspace sub-nav", async ({ page }) => {
99+
await page.goto("/workspaces/ws-test/dreams");
100+
// Sidebar link with the Dreams label
101+
const dreamsLink = page.getByRole("link", { name: /^Dreams$/ });
102+
await expect(dreamsLink.first()).toBeVisible();
103+
});
104+
105+
test("renders heading and breadcrumb on the dreams route", async ({ page }) => {
106+
await page.goto("/workspaces/ws-test/dreams");
107+
await expect(page.getByRole("heading", { name: /^Dreams$/ })).toBeVisible();
108+
// Breadcrumb specifically — the sidebar has a "Workspaces" link too, so scope.
109+
await expect(
110+
page.getByLabel("Breadcrumb").getByRole("link", { name: "Workspaces" }),
111+
).toBeVisible();
112+
});
113+
114+
test("clusters mocked conclusions into dreams and opens detail on click", async ({ page }) => {
115+
await page.goto("/workspaces/ws-test/dreams");
116+
117+
// Two dreams: alice→bob burst, and the older carol→dan
118+
const rows = page.locator('button[aria-pressed]');
119+
await expect(rows).toHaveCount(2);
120+
121+
// Alice→bob row should show count chips
122+
await expect(rows.first()).toContainText("alice");
123+
await expect(rows.first()).toContainText("bob");
124+
await expect(rows.first()).toContainText("2 explicit");
125+
await expect(rows.first()).toContainText("1 deductive");
126+
await expect(rows.first()).toContainText("1 inductive");
127+
128+
// Click → detail panel renders three columns
129+
await rows.first().click();
130+
await expect(page.getByText("Dream detail")).toBeVisible();
131+
await expect(page.getByText("Explicit", { exact: true })).toBeVisible();
132+
await expect(page.getByText("Deductive", { exact: true })).toBeVisible();
133+
await expect(page.getByText("Inductive", { exact: true })).toBeVisible();
134+
});
135+
136+
test("expands premise tree for an inductive conclusion", async ({ page }) => {
137+
await page.goto("/workspaces/ws-test/dreams");
138+
await page.locator('button[aria-pressed]').first().click();
139+
140+
const showPremises = page.getByRole("button", { name: /^Show premises$/i });
141+
await expect(showPremises).toBeVisible();
142+
await showPremises.click();
143+
144+
// The reasoning chain renders with the deductive premise (ded-1)
145+
await expect(page.getByText("Reasoning chain")).toBeVisible();
146+
await expect(page.getByLabel("Premise tree")).toBeVisible();
147+
});
148+
});

packages/web/src/api/keys.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ export const QK = {
3232
conclusionsQuery: (wsId: string, q: string, filters: Record<string, unknown>) =>
3333
["conclusions-query", wsId, q, filters] as const,
3434

35+
dreams: (wsId: string, filters: Record<string, unknown>, limit: number) =>
36+
["dreams", wsId, filters, limit] as const,
37+
3538
webhooks: (wsId: string) => ["webhooks", wsId] as const,
3639
};

packages/web/src/api/queries.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,50 @@ export function useDeleteConclusion(workspaceId: string) {
706706
});
707707
}
708708

709+
// ─── Dreams ───────────────────────────────────────────────────────────────────
710+
//
711+
// Dreams are synthetic groupings of conclusions: bursts produced by a single
712+
// dream run for one (observer, observed) pair. We fetch a generous batch of
713+
// conclusions and let the UI cluster them via `clusterConclusionsIntoDreams`.
714+
715+
const DREAM_FETCH_PAGE_SIZE = 100;
716+
const DREAM_MAX_PAGES = 4;
717+
718+
export function useDreams(
719+
workspaceId: string,
720+
filters: Record<string, unknown> = {},
721+
limit = DREAM_FETCH_PAGE_SIZE * DREAM_MAX_PAGES,
722+
) {
723+
return useQuery({
724+
queryKey: QK.dreams(workspaceId, filters, limit),
725+
queryFn: async () => {
726+
const collected: unknown[] = [];
727+
const pageSize = Math.min(DREAM_FETCH_PAGE_SIZE, limit);
728+
let page = 1;
729+
while (collected.length < limit) {
730+
const { data, error } = await client.current.POST(
731+
"/v3/workspaces/{workspace_id}/conclusions/list",
732+
{
733+
params: {
734+
path: { workspace_id: workspaceId },
735+
query: { page, page_size: pageSize, reverse: false },
736+
},
737+
body: filters,
738+
},
739+
);
740+
if (error) err(error);
741+
const items = (data as { items?: unknown[] } | undefined)?.items ?? [];
742+
const totalPages = (data as { pages?: number } | undefined)?.pages ?? 1;
743+
collected.push(...items);
744+
if (items.length === 0 || page >= totalPages || page >= DREAM_MAX_PAGES) break;
745+
page++;
746+
}
747+
return collected.slice(0, limit);
748+
},
749+
enabled: Boolean(workspaceId),
750+
});
751+
}
752+
709753
// ─── Webhooks ─────────────────────────────────────────────────────────────────
710754

711755
export function useWebhooks(workspaceId: string) {

0 commit comments

Comments
 (0)