akanjs 3.3.0-rc.80 → 3.3.0-rc.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.80",
3
+ "version": "3.3.0-rc.81",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -0,0 +1,44 @@
1
+ import { csrLocation, router } from "akanjs/client";
2
+ import { PendingNavigation } from "../../client/pendingNavigation";
3
+
4
+ const routeAt = (pathname: () => string): string | null => {
5
+ try {
6
+ const route = router.routeOf(pathname());
7
+ return route.length > 1 ? route.replace(/\/+$/, "") : route;
8
+ } catch {
9
+ return null;
10
+ }
11
+ };
12
+
13
+ /** What one `navigate` saw on its way: the page it started from, and each move a redirect began. */
14
+ export class NavigateMove {
15
+ readonly #from = routeAt(() => csrLocation.pathname);
16
+ readonly #redirectsBefore = router.redirectCount();
17
+ readonly #begun = new Set<string>();
18
+ readonly #stop: () => void;
19
+
20
+ constructor() {
21
+ this.#stop = PendingNavigation.subscribe(() => {
22
+ const target = PendingNavigation.target();
23
+ const route = target ? routeAt(() => new URL(target).pathname) : null;
24
+ if (route) this.#begun.add(route);
25
+ });
26
+ }
27
+
28
+ stop() {
29
+ this.#stop();
30
+ }
31
+
32
+ arrival(path: string): string {
33
+ const landed = routeAt(() => csrLocation.pathname);
34
+ const wanted = routeAt(() => new URL(path, "http://route.invalid").pathname);
35
+ if (!landed || !wanted || landed === wanted) return `Now on ${path}.`;
36
+ if (landed !== this.#from) return `Asked for ${path}, but the app sent the page to ${landed} instead.`;
37
+
38
+ const redirected =
39
+ router.redirectCount() !== this.#redirectsBefore || [...this.#begun].some((route) => route !== wanted);
40
+ return redirected
41
+ ? `Asked for ${path}, but the app sent the page back to ${landed}, the page it was on.`
42
+ : `Asked for ${path}, but the page stayed on ${landed}: the move did not land.`;
43
+ }
44
+ }
@@ -31,6 +31,12 @@ export class ScreenSettle {
31
31
  });
32
32
  }
33
33
 
34
+ /** Whether a load on a page in view is still pending, as `loaded()` reads it. */
35
+ static isLoading(): boolean {
36
+ const body = typeof document === "undefined" ? null : (document.body ?? document.documentElement);
37
+ return !!body && ScreenSettle.#loading(body);
38
+ }
39
+
34
40
  /** Resolves once no load on a page in view is pending, each `Load.*` fallback marking itself; `false` when none was. */
35
41
  static loaded({ timeoutMs = 5000 }: { timeoutMs?: number } = {}): Promise<boolean> {
36
42
  const body = typeof document === "undefined" ? null : (document.body ?? document.documentElement);
@@ -1,8 +1,9 @@
1
- import { csrLocation, isNotFoundView, router } from "akanjs/client";
1
+ import { isNotFoundView, router } from "akanjs/client";
2
2
  import { AgenticSurface, type SurfaceSource, type ToolEntry, ToolReturn } from "../../vendor/use-agentic";
3
3
  import { InlineToken } from "../../common/inlineToken";
4
4
  import { AgentNavigate } from "../agentic/AgentNavigate";
5
5
  import { AgentBridge } from "./AgentBridge";
6
+ import { NavigateMove } from "./NavigateMove";
6
7
  import { ScreenFlash } from "./ScreenFlash";
7
8
  import { ScreenImage } from "./ScreenImage";
8
9
  import { ScreenReader } from "./ScreenReader";
@@ -66,25 +67,29 @@ export class StoreSurfaceSource implements SurfaceSource {
66
67
  const path = String(args.path);
67
68
 
68
69
  const linkedBefore = ScreenTarget.linkedRoutes();
70
+ const move = new NavigateMove();
69
71
  try {
70
- router.push(path);
71
- await router.navigation();
72
- } catch (error) {
73
- if (StoreSurfaceSource.#isKept(error)) throw new Error(StoreSurfaceSource.#kept);
74
- throw new Error(
75
- `There is no route at ${path}, so the page did not move. ${StoreSurfaceSource.#linked(linkedBefore)}`,
76
- );
77
- }
78
-
79
- await StoreSurfaceSource.#settled();
80
- if (isNotFoundView()) {
81
- const left = await StoreSurfaceSource.#leaveNotFound();
82
- throw new Error(
83
- `There is no route at ${path}, so ${left ? "the page went back" : "the page shows the app's not-found page"}. ${StoreSurfaceSource.#linked(left ? linkedBefore : [])}`,
84
- );
72
+ try {
73
+ router.push(path);
74
+ await router.navigation();
75
+ } catch (error) {
76
+ if (StoreSurfaceSource.#isKept(error)) throw new Error(StoreSurfaceSource.#kept);
77
+ throw new Error(
78
+ `There is no route at ${path}, so the page did not move. ${StoreSurfaceSource.#linked(linkedBefore)}`,
79
+ );
80
+ }
81
+
82
+ await StoreSurfaceSource.#settled();
83
+ if (isNotFoundView()) {
84
+ const left = await StoreSurfaceSource.#leaveNotFound();
85
+ throw new Error(
86
+ `There is no route at ${path}, so ${left ? "the page went back" : "the page shows the app's not-found page"}. ${StoreSurfaceSource.#linked(left ? linkedBefore : [])}`,
87
+ );
88
+ }
89
+ return `${move.arrival(path)} ${StoreSurfaceSource.#arrived()}`;
90
+ } finally {
91
+ move.stop();
85
92
  }
86
- const landed = StoreSurfaceSource.#redirected(path);
87
- return `${landed ? `Asked for ${path}, but the app sent the page to ${landed} instead.` : `Now on ${path}.`} ${StoreSurfaceSource.#arrived}`;
88
93
  },
89
94
  };
90
95
  }
@@ -109,8 +114,13 @@ export class StoreSurfaceSource implements SurfaceSource {
109
114
  return left;
110
115
  }
111
116
 
112
- static readonly #arrived =
113
- "This screen's own tools and state replace the previous screen's and are listed on your next turn — call them then, by the names listed. Call readScreen to see the page.";
117
+ static #arrived() {
118
+ const listed =
119
+ "This screen's own tools and state replace the previous screen's and are listed on your next turn — call them then, by the names listed. Call readScreen to see the page.";
120
+ return ScreenSettle.isLoading()
121
+ ? `Part of this page is still loading, so what it shows and the tools it mounts may arrive after your next turn's list. ${listed}`
122
+ : listed;
123
+ }
114
124
 
115
125
  static readonly #kept =
116
126
  "The app asked before leaving this page and kept it, so the page did not move. Tell the user why you could not move, and do not try again unless they ask.";
@@ -126,18 +136,6 @@ export class StoreSurfaceSource implements SurfaceSource {
126
136
  : "Call readScreen — it prints each link on this screen with its own path — and navigate to one of those.";
127
137
  }
128
138
 
129
- /** A redirect moves the page somewhere other than the path it was sent to. */
130
- static #redirected(path: string): string | null {
131
- const trimmed = (route: string) => (route.length > 1 ? route.replace(/\/+$/, "") : route);
132
- try {
133
- const landed = router.routeOf(csrLocation.pathname);
134
- const wanted = router.routeOf(new URL(path, "http://route.invalid").pathname);
135
- return trimmed(landed) === trimmed(wanted) ? null : landed;
136
- } catch {
137
- return null;
138
- }
139
- }
140
-
141
139
  static #goBack(): ToolEntry {
142
140
  return {
143
141
  name: "goBack",
@@ -152,7 +150,7 @@ export class StoreSurfaceSource implements SurfaceSource {
152
150
  if (StoreSurfaceSource.#isKept(error)) throw new Error(StoreSurfaceSource.#kept);
153
151
  });
154
152
  await StoreSurfaceSource.#settled();
155
- return `Back on ${router.getPath()}. ${StoreSurfaceSource.#arrived}`;
153
+ return `Back on ${router.getPath()}. ${StoreSurfaceSource.#arrived()}`;
156
154
  },
157
155
  };
158
156
  }
@@ -0,0 +1,7 @@
1
+ /** What one `navigate` saw on its way: the page it started from, and each move a redirect began. */
2
+ export declare class NavigateMove {
3
+ #private;
4
+ constructor();
5
+ stop(): void;
6
+ arrival(path: string): string;
7
+ }
@@ -9,6 +9,8 @@ export interface ScreenSettleOptions {
9
9
  export declare class ScreenSettle {
10
10
  #private;
11
11
  static wait({ quietMs, appearMs, timeoutMs }?: ScreenSettleOptions): Promise<void>;
12
+ /** Whether a load on a page in view is still pending, as `loaded()` reads it. */
13
+ static isLoading(): boolean;
12
14
  /** Resolves once no load on a page in view is pending, each `Load.*` fallback marking itself; `false` when none was. */
13
15
  static loaded({ timeoutMs }?: {
14
16
  timeoutMs?: number;