akanjs 3.0.0-alpha.57 → 3.0.0-alpha.58

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.
@@ -1,9 +1,10 @@
1
1
  "use client";
2
2
  import type { ReactDOMAttributes } from "@use-gesture/react/dist/declarations/src/types";
3
3
  import type { PromiseOrObject } from "akanjs/base";
4
- import { createContext, type ForwardRefExoticComponent, type ReactNode, type RefObject, useContext } from "react";
4
+ import { type ForwardRefExoticComponent, type ReactNode, type RefObject, useContext } from "react";
5
5
  import type { AnimatedComponent, AnimatedProps, Interpolation, SpringValue } from "react-spring";
6
6
  import type { RouterInstance } from "./router";
7
+ import { sharedContext } from "./sharedContext";
7
8
  import type { ReactFont } from "./types";
8
9
 
9
10
  export type TransitionType = "none" | "fade" | "bottomUp" | "stack" | "scaleOut";
@@ -349,7 +350,7 @@ export type UseCsrTransition = CsrTransitionStyles & {
349
350
  };
350
351
 
351
352
  export type CsrContextType = RouteState & UseCsrTransition;
352
- export const csrContext = createContext<CsrContextType>({} as unknown as CsrContextType);
353
+ export const csrContext = sharedContext<CsrContextType>("csr", {} as unknown as CsrContextType);
353
354
  export const useCsr = () => {
354
355
  const contextValues = useContext(csrContext);
355
356
  return contextValues;
@@ -363,7 +364,7 @@ export interface PathContextType {
363
364
  setGestureEnabled: (enabled: boolean) => void;
364
365
  registerFrameSlot: (slot: FrameSlotRegistration) => () => void;
365
366
  }
366
- export const pathContext = createContext<PathContextType>({} as unknown as PathContextType);
367
+ export const pathContext = sharedContext<PathContextType>("path", {} as unknown as PathContextType);
367
368
  export const usePathCtx = () => {
368
369
  const contextValues = useContext(pathContext);
369
370
  return contextValues;
package/client/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./locale";
11
11
  export * from "./makePageProto";
12
12
  export * from "./router";
13
13
  export * from "./rscNavigation";
14
+ export * from "./sharedContext";
14
15
  export * from "./storage";
15
16
  export * from "./translator";
16
17
  export * from "./types";
package/client/locale.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  "use client";
2
- import { createContext } from "react";
2
+ import { sharedContext } from "./sharedContext";
3
3
 
4
- export const dictionaryContext = createContext<{ [key: string]: { [key: string]: string } }>({});
4
+ export const dictionaryContext = sharedContext<{ [key: string]: { [key: string]: string } }>("dictionary", {});
@@ -0,0 +1,30 @@
1
+ import { type Context, createContext } from "react";
2
+
3
+ /**
4
+ * A React context interned on `globalThis`, which is how every context this framework owns must be made.
5
+ *
6
+ * An akan build does not give an app one copy of `akanjs`: it inlines each reachable module into every client
7
+ * chunk that reaches it — four copies of the agent runtime in this repo's own docs app. A context is identified
8
+ * by object identity, so a Provider mounted from one copy is invisible to a consumer holding another, and the
9
+ * consumer silently reads the context's default instead.
10
+ *
11
+ * Silently is the whole problem. `UiOverrideContext` was a plain `createContext`, so a route's `_overrides.tsx`
12
+ * bound its slots in the chunk holding the generated provider and every overridable component in another chunk
13
+ * kept rendering the framework default — no throw, no warning, and only *some* slots affected, which reads like
14
+ * a bug in the app's own component. The same shape cost a zone every tool it declared.
15
+ *
16
+ * The rule is therefore mechanical rather than case-by-case: a context whose Provider and consumers can be
17
+ * bundled apart is every context worth having, so all of them go through here. A test fails if one does not.
18
+ *
19
+ * `use-agentic` carries its own twin under a `useAgentic.` prefix, deliberately — it ships as a standalone
20
+ * package and cannot import this one. The prefixes keep the two namespaces from ever meeting.
21
+ */
22
+ export const sharedContext = <T>(name: string, initial: T): Context<T> => {
23
+ const key = Symbol.for(`akanjs.context.${name}`);
24
+ const holder = globalThis as typeof globalThis & { [slot: symbol]: Context<T> | undefined };
25
+ const existing = holder[key];
26
+ if (existing) return existing;
27
+ const created = createContext(initial);
28
+ holder[key] = created;
29
+ return created;
30
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.0.0-alpha.57",
3
+ "version": "3.0.0-alpha.58",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -11,6 +11,7 @@ export * from "./locale.d.ts";
11
11
  export * from "./makePageProto.d.ts";
12
12
  export * from "./router.d.ts";
13
13
  export * from "./rscNavigation.d.ts";
14
+ export * from "./sharedContext.d.ts";
14
15
  export * from "./storage.d.ts";
15
16
  export * from "./translator.d.ts";
16
17
  export * from "./types.d.ts";
@@ -0,0 +1,21 @@
1
+ import { type Context } from "react";
2
+ /**
3
+ * A React context interned on `globalThis`, which is how every context this framework owns must be made.
4
+ *
5
+ * An akan build does not give an app one copy of `akanjs`: it inlines each reachable module into every client
6
+ * chunk that reaches it — four copies of the agent runtime in this repo's own docs app. A context is identified
7
+ * by object identity, so a Provider mounted from one copy is invisible to a consumer holding another, and the
8
+ * consumer silently reads the context's default instead.
9
+ *
10
+ * Silently is the whole problem. `UiOverrideContext` was a plain `createContext`, so a route's `_overrides.tsx`
11
+ * bound its slots in the chunk holding the generated provider and every overridable component in another chunk
12
+ * kept rendering the framework default — no throw, no warning, and only *some* slots affected, which reads like
13
+ * a bug in the app's own component. The same shape cost a zone every tool it declared.
14
+ *
15
+ * The rule is therefore mechanical rather than case-by-case: a context whose Provider and consumers can be
16
+ * bundled apart is every context worth having, so all of them go through here. A test fails if one does not.
17
+ *
18
+ * `use-agentic` carries its own twin under a `useAgentic.` prefix, deliberately — it ships as a standalone
19
+ * package and cannot import this one. The prefixes keep the two namespaces from ever meeting.
20
+ */
21
+ export declare const sharedContext: <T>(name: string, initial: T) => Context<T>;
@@ -16,5 +16,11 @@ export interface HistoryProps {
16
16
  *
17
17
  * Restoring follows the session's one rule: it lands only while nothing has happened to the conversation yet, so
18
18
  * mounting with the zone restores and mounting later saves from there on.
19
+ *
20
+ * The store is attached for exactly as long as this is mounted. A zone's own session dies with it, so that is the
21
+ * whole story there — but a session the app handed in (`Agent.Zone`'s `session` prop) outlives this, and its
22
+ * saving stops on unmount: nothing is claiming to persist it any more, and a component that is gone should not
23
+ * still be writing. A host that wants the store to outlive the view calls `session.setHistory` itself, which also
24
+ * takes the slot, so a later unmount here leaves it alone.
19
25
  */
20
26
  export declare const History: ({ load, save, clear, onCompact }: HistoryProps) => null;
@@ -1,4 +1,4 @@
1
- import { type ReactNode } from "react";
1
+ import type { ReactNode } from "react";
2
2
  export interface DialogContextType {
3
3
  open: boolean;
4
4
  setOpen: (open: boolean) => void;
@@ -1,4 +1,4 @@
1
- import { type RefObject } from "react";
1
+ import type { RefObject } from "react";
2
2
  interface TabContextType {
3
3
  defaultMenu: string | null;
4
4
  menu: string | null;
@@ -1,4 +1,4 @@
1
- import { type ComponentType } from "react";
1
+ import type { ComponentType } from "react";
2
2
  import type { ClassNameValue as ClassValue } from "tailwind-merge";
3
3
  import type { ApprovalProps as AgentApprovalProps } from "../Agent/Approval.d.ts";
4
4
  import type { BubbleProps as AgentBubbleProps } from "../Agent/Bubble.d.ts";
@@ -134,10 +134,12 @@ export declare class AgentSession {
134
134
  * Restoring follows the rule an async `load` already follows: it lands only while nothing has happened to this
135
135
  * session yet. Attach before the first turn and it restores; attach after and it saves from there on, with the
136
136
  * store never asked for a transcript that would be discarded. One rule rather than a mount-order surprise.
137
+ *
138
+ * Returns the detach, which clears the slot only while this call's store is still the one in it.
137
139
  */
138
- setHistory: (history: SessionHistory | null) => void;
140
+ setHistory: (history: SessionHistory | null) => () => void;
139
141
  /** The compaction hook as a setter, for the same reason `setHistory` is one: a host attaches it after the fact. */
140
- setOnCompact: (onCompact: AgentSessionOptions["onCompact"] | null) => void;
142
+ setOnCompact: (onCompact: AgentSessionOptions["onCompact"] | null) => () => void;
141
143
  /**
142
144
  * Re-runs the last user message, dropping what the previous attempt produced. Turns fail for reasons that have
143
145
  * nothing to do with what was asked — a refused relay, a model that is unavailable — and retyping is otherwise the
@@ -20,6 +20,12 @@ export interface HistoryProps {
20
20
  *
21
21
  * Restoring follows the session's one rule: it lands only while nothing has happened to the conversation yet, so
22
22
  * mounting with the zone restores and mounting later saves from there on.
23
+ *
24
+ * The store is attached for exactly as long as this is mounted. A zone's own session dies with it, so that is the
25
+ * whole story there — but a session the app handed in (`Agent.Zone`'s `session` prop) outlives this, and its
26
+ * saving stops on unmount: nothing is claiming to persist it any more, and a component that is gone should not
27
+ * still be writing. A host that wants the store to outlive the view calls `session.setHistory` itself, which also
28
+ * takes the slot, so a later unmount here leaves it alone.
23
29
  */
24
30
  export const History = ({ load, save, clear, onCompact }: HistoryProps) => {
25
31
  const session = useContext(SessionContext);
@@ -28,16 +34,15 @@ export const History = ({ load, save, clear, onCompact }: HistoryProps) => {
28
34
  const latest = useRef({ load, save, clear, onCompact });
29
35
  latest.current = { load, save, clear, onCompact };
30
36
  useEffect(() => {
31
- session.setHistory({
37
+ const detachHistory = session.setHistory({
32
38
  load: () => latest.current.load(),
33
39
  save: (messages) => latest.current.save(messages),
34
40
  clear: () => latest.current.clear(),
35
41
  });
36
- session.setOnCompact((replaced, summary) => latest.current.onCompact?.(replaced, summary));
42
+ const detachCompact = session.setOnCompact((replaced, summary) => latest.current.onCompact?.(replaced, summary));
37
43
  return () => {
38
-
39
- session.setHistory(null);
40
- session.setOnCompact(null);
44
+ detachHistory();
45
+ detachCompact();
41
46
  };
42
47
  }, [session]);
43
48
  return null;
@@ -1,5 +1,6 @@
1
1
  "use client";
2
- import { createContext, type ReactNode } from "react";
2
+ import type { ReactNode } from "react";
3
+ import { sharedContext } from "../../client/sharedContext";
3
4
 
4
5
  export interface DialogContextType {
5
6
  open: boolean;
@@ -18,7 +19,7 @@ export interface DialogContextType {
18
19
  setAction: (action: ReactNode) => void;
19
20
  }
20
21
 
21
- export const DialogContext = createContext<DialogContextType>({
22
+ export const DialogContext = sharedContext<DialogContextType>("dialog", {
22
23
  open: false,
23
24
  setOpen: (open: boolean) => null,
24
25
  openDialog: () => null,
package/ui/DragAction.tsx CHANGED
@@ -3,8 +3,9 @@ import { useGesture } from "@use-gesture/react";
3
3
  import type { ReactDOMAttributes } from "@use-gesture/react/dist/declarations/src/types";
4
4
  import { cn } from "akanjs/client";
5
5
  import { animated } from "akanjs/ui";
6
- import { createContext, type ReactNode, useContext, useRef } from "react";
6
+ import { type ReactNode, useContext, useRef } from "react";
7
7
  import { SpringValue, useSpring } from "react-spring";
8
+ import { sharedContext } from "../client/sharedContext";
8
9
 
9
10
  interface DragActionContextType {
10
11
  bind: () => ReactDOMAttributes;
@@ -17,7 +18,7 @@ interface DragActionContextType {
17
18
  onClick?: () => void;
18
19
  }
19
20
 
20
- const DragActionContext = createContext<DragActionContextType>({
21
+ const DragActionContext = sharedContext<DragActionContextType>("dragAction", {
21
22
  bind: () => ({}),
22
23
  x: new SpringValue(0),
23
24
  y: new SpringValue(0),
@@ -4,9 +4,10 @@ import { useGesture } from "@use-gesture/react";
4
4
  import { cn } from "akanjs/client";
5
5
  import { useFieldTool } from "akanjs/store";
6
6
  import { animated } from "akanjs/ui";
7
- import { createContext, type ReactElement, type ReactNode, useContext, useRef } from "react";
7
+ import { type ReactElement, type ReactNode, useContext, useRef } from "react";
8
8
  import { BiTrash } from "react-icons/bi";
9
9
  import { MdDragIndicator } from "react-icons/md";
10
+ import { sharedContext } from "../client/sharedContext";
10
11
  import { agentAttrs } from "./agentAttrs";
11
12
  import { buttonRecipe } from "./Button";
12
13
  import { useUiRecipe } from "./UiOverride";
@@ -24,7 +25,7 @@ interface DragListContextType<V> {
24
25
  bind: (...args: any[]) => any;
25
26
  onRemove: (value: V) => void;
26
27
  }
27
- const dragListContext = createContext<DragListContextType<any>>({} as unknown as DragListContextType<any>);
28
+ const dragListContext = sharedContext<DragListContextType<any>>("dragList", {} as unknown as DragListContextType<any>);
28
29
  const useDragList = () => useContext(dragListContext);
29
30
 
30
31
  interface DragListProps<V> {
@@ -4,8 +4,9 @@ import { cn } from "akanjs/client";
4
4
  import { capitalize } from "akanjs/common";
5
5
  import { st } from "akanjs/store";
6
6
  import { animated } from "akanjs/ui";
7
- import { createContext, type ReactNode, useContext, useEffect, useRef, useState } from "react";
7
+ import { type ReactNode, useContext, useEffect, useRef, useState } from "react";
8
8
  import { SpringValue, useSpringValue } from "react-spring";
9
+ import { sharedContext } from "../client/sharedContext";
9
10
 
10
11
  interface ScreenNavigatorContextType {
11
12
  bind: (...args: any[]) => any;
@@ -16,7 +17,7 @@ interface ScreenNavigatorContextType {
16
17
  onClickMenu: (menu: string) => void;
17
18
  }
18
19
 
19
- const ScreenNavigatorContext = createContext<ScreenNavigatorContextType>({
20
+ const ScreenNavigatorContext = sharedContext<ScreenNavigatorContextType>("screenNavigator", {
20
21
  bind: () => ({}),
21
22
  xValue: new SpringValue(0),
22
23
  setMenu: null as unknown as (menu: string) => void,
package/ui/Tab/context.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  "use client";
2
- import { createContext, type RefObject } from "react";
2
+ import type { RefObject } from "react";
3
+ import { sharedContext } from "../../client/sharedContext";
3
4
 
4
5
  interface TabContextType {
5
6
  defaultMenu: string | null;
@@ -10,7 +11,7 @@ interface TabContextType {
10
11
  switchTab: (menu: string) => void;
11
12
  }
12
13
 
13
- export const TabContext = createContext<TabContextType>({
14
+ export const TabContext = sharedContext<TabContextType>("tab", {
14
15
  defaultMenu: null,
15
16
  menu: null,
16
17
  setMenu: (value: string | null) => null,
@@ -1,6 +1,7 @@
1
1
  "use client";
2
- import { type ComponentType, createContext } from "react";
2
+ import type { ComponentType } from "react";
3
3
  import type { ClassNameValue as ClassValue } from "tailwind-merge";
4
+ import { sharedContext } from "../../client/sharedContext";
4
5
  import type { ApprovalProps as AgentApprovalProps } from "../Agent/Approval";
5
6
  import type { BubbleProps as AgentBubbleProps } from "../Agent/Bubble";
6
7
  import type { ChatProps as AgentChatProps } from "../Agent/Chat";
@@ -130,4 +131,4 @@ export type AkanUiOverrideManifest = Partial<AkanUiOverrides> & { recipes?: Part
130
131
  * merged (child wins) by each nested `UiOverrideProvider`, mirroring how nested
131
132
  * `_layout.tsx` / `_overrides.tsx` stack down the route tree.
132
133
  */
133
- export const UiOverrideContext = createContext<AkanUiOverrideManifest>({});
134
+ export const UiOverrideContext = sharedContext<AkanUiOverrideManifest>("uiOverride", {});
@@ -1,5 +1,6 @@
1
1
  "use client";
2
- import { createContext, useContext } from "react";
2
+ import { useContext } from "react";
3
+ import { sharedContext } from "../client/sharedContext";
3
4
 
4
5
  /**
5
6
  * Overlay surfaces render through `createPortal(document.body)`, so they leave the DOM subtree of the
@@ -22,7 +23,7 @@ export const OVERLAY_LAYER_ATTR = "data-akan-overlay";
22
23
  export const overlayZ = { select: 90, dropdown: 100, popconfirmScrim: 105, popconfirm: 110 } as const;
23
24
 
24
25
  /** Scope that rendered the surrounding content — empty at page level. */
25
- const OverlayOwnerContext = createContext("");
26
+ const OverlayOwnerContext = sharedContext("overlayOwner", "");
26
27
 
27
28
  /** Wrap the content a dismissable container owns, with the scope from {@link useOverlayScope}. */
28
29
  export const OverlayOwnerProvider = OverlayOwnerContext.Provider;
@@ -313,22 +313,30 @@ export class AgentSession {
313
313
  * Restoring follows the rule an async `load` already follows: it lands only while nothing has happened to this
314
314
  * session yet. Attach before the first turn and it restores; attach after and it saves from there on, with the
315
315
  * store never asked for a transcript that would be discarded. One rule rather than a mount-order surprise.
316
+ *
317
+ * Returns the detach, which clears the slot only while this call's store is still the one in it.
316
318
  */
317
319
  setHistory = (history: SessionHistory | null) => {
318
320
  this.#history = history ?? undefined;
319
- if (!history || this.#version !== 0) return;
320
- const restored = AgentSession.#restored(history);
321
- if (!(restored instanceof Promise)) {
322
- this.#restore(restored);
323
- return;
321
+ if (history && this.#version === 0) {
322
+ const restored = AgentSession.#restored(history);
323
+ if (restored instanceof Promise) {
324
+ this.#restoring = true;
325
+ void this.#hydrate(restored);
326
+ } else this.#restore(restored);
324
327
  }
325
- this.#restoring = true;
326
- void this.#hydrate(restored);
328
+
329
+ return () => {
330
+ if (history && this.#history === history) this.#history = undefined;
331
+ };
327
332
  };
328
333
 
329
334
  /** The compaction hook as a setter, for the same reason `setHistory` is one: a host attaches it after the fact. */
330
335
  setOnCompact = (onCompact: AgentSessionOptions["onCompact"] | null) => {
331
336
  this.#onCompact = onCompact ?? undefined;
337
+ return () => {
338
+ if (onCompact && this.#onCompact === onCompact) this.#onCompact = undefined;
339
+ };
332
340
  };
333
341
 
334
342
  /**