@agent-native/core 0.12.15 → 0.12.16

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.
Files changed (38) hide show
  1. package/dist/agent/run-manager.d.ts.map +1 -1
  2. package/dist/agent/run-manager.js +56 -42
  3. package/dist/agent/run-manager.js.map +1 -1
  4. package/dist/cli/workspace-dev.js +26 -5
  5. package/dist/cli/workspace-dev.js.map +1 -1
  6. package/dist/client/AssistantChat.d.ts.map +1 -1
  7. package/dist/client/AssistantChat.js +33 -6
  8. package/dist/client/AssistantChat.js.map +1 -1
  9. package/dist/client/MultiTabAssistantChat.d.ts.map +1 -1
  10. package/dist/client/MultiTabAssistantChat.js +7 -2
  11. package/dist/client/MultiTabAssistantChat.js.map +1 -1
  12. package/dist/client/NewWorkspaceAppFlow.d.ts.map +1 -1
  13. package/dist/client/NewWorkspaceAppFlow.js +2 -0
  14. package/dist/client/NewWorkspaceAppFlow.js.map +1 -1
  15. package/dist/client/sharing/ShareButton.js +6 -1
  16. package/dist/client/sharing/ShareButton.js.map +1 -1
  17. package/dist/client/sharing/ShareButton.spec.d.ts +2 -0
  18. package/dist/client/sharing/ShareButton.spec.d.ts.map +1 -0
  19. package/dist/client/sharing/ShareButton.spec.js +90 -0
  20. package/dist/client/sharing/ShareButton.spec.js.map +1 -0
  21. package/dist/client/sse-event-processor.d.ts.map +1 -1
  22. package/dist/client/sse-event-processor.js +10 -2
  23. package/dist/client/sse-event-processor.js.map +1 -1
  24. package/dist/client/use-chat-threads.d.ts.map +1 -1
  25. package/dist/client/use-chat-threads.js +19 -2
  26. package/dist/client/use-chat-threads.js.map +1 -1
  27. package/dist/server/agent-chat-plugin.d.ts.map +1 -1
  28. package/dist/server/agent-chat-plugin.js +11 -7
  29. package/dist/server/agent-chat-plugin.js.map +1 -1
  30. package/dist/templates/default/AGENTS.md +7 -1
  31. package/dist/templates/default/DEVELOPING.md +12 -0
  32. package/dist/templates/default/app/hooks/use-navigation-state.ts +81 -0
  33. package/dist/templates/default/app/root.tsx +11 -5
  34. package/package.json +1 -1
  35. package/src/templates/default/AGENTS.md +7 -1
  36. package/src/templates/default/DEVELOPING.md +12 -0
  37. package/src/templates/default/app/hooks/use-navigation-state.ts +81 -0
  38. package/src/templates/default/app/root.tsx +11 -5
@@ -50,6 +50,12 @@ Ephemeral UI state is stored in the SQL `application_state` table, accessed via
50
50
 
51
51
  The `navigation` key is written by the UI whenever the route changes. The `navigate` key is a one-shot command: the agent writes it, the UI reads and executes the navigation, then deletes it.
52
52
 
53
+ ## Mounted Workspace Routing
54
+
55
+ This app may be mounted under `/<app-id>` in a workspace. Inside app source, React Router paths are app-local: use `<Link to="/review">` and `navigate("/review")`, not `/<app-id>/review`. The workspace gateway and `APP_BASE_PATH` add the mounted prefix in the browser; hardcoding it inside React Router links causes doubled URLs such as `/<app-id>/<app-id>/review`.
56
+
57
+ For raw paths outside React Router, use the core helpers: `appPath()` for static assets or normal hrefs, `appApiPath()` for `/api/*`, and `agentNativePath()` for `/_agent-native/*`.
58
+
53
59
  ## Agent Operations
54
60
 
55
61
  **Always know what the user is currently viewing before you edit anything.** The user's view can change mid-conversation. Stale IDs lead to editing the wrong record.
@@ -101,7 +107,7 @@ Skills in `.agents/skills/` provide detailed guidance for each architectural rul
101
107
 
102
108
  As you build out this app, follow this checklist for each new feature:
103
109
 
104
- 1. **Add navigation state entries** -- create or extend `app/hooks/use-navigation-state.ts` to track new routes
110
+ 1. **Add navigation state entries** -- extend `app/hooks/use-navigation-state.ts` to track new routes
105
111
  2. **Enhance view-screen** -- make the view-screen script return relevant context for the new view
106
112
  3. **Create domain actions** -- add scripts for CRUD operations on new data models
107
113
  4. **Create domain skills** -- add `.agents/skills/<feature>/SKILL.md` documenting the data model, storage patterns, and agent operations
@@ -20,6 +20,18 @@ app/routes/inbox.$threadId.tsx → /inbox/:threadId
20
20
  app/routes/$id.tsx → /:id (dynamic param)
21
21
  ```
22
22
 
23
+ ## Mounted Workspace Routing
24
+
25
+ In a workspace, this app can be mounted under `/<app-id>`. React Router already receives `APP_BASE_PATH`/`VITE_APP_BASE_PATH` through `appBasePath()`, so route code stays app-local:
26
+
27
+ | Route file | App-internal route | Mounted browser URL |
28
+ | ----------------------- | ------------------ | ------------------- |
29
+ | `app/routes/_index.tsx` | `/` | `/<app-id>` |
30
+ | `app/routes/review.tsx` | `/review` | `/<app-id>/review` |
31
+ | `app/routes/$id.tsx` | `/:id` | `/<app-id>/:id` |
32
+
33
+ Use `<Link to="/review">` and `navigate("/review")` inside this app. Do not prefix React Router paths with `/<app-id>` or the URL can double-prefix, e.g. `/<app-id>/<app-id>/review`. Use `appPath()` for raw `href`s/static assets, `appApiPath()` for `/api/*`, and `agentNativePath()` for `/_agent-native/*`.
34
+
23
35
  Each route file exports a default component and optional `meta()`:
24
36
 
25
37
  ```tsx
@@ -0,0 +1,81 @@
1
+ import { useEffect } from "react";
2
+ import { useLocation, useNavigate } from "react-router";
3
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
4
+ import {
5
+ agentNativePath,
6
+ appBasePath,
7
+ appPath,
8
+ } from "@agent-native/core/client";
9
+
10
+ export interface NavigationState {
11
+ view: string;
12
+ path?: string;
13
+ }
14
+
15
+ export function useNavigationState() {
16
+ const location = useLocation();
17
+ const navigate = useNavigate();
18
+ const qc = useQueryClient();
19
+
20
+ useEffect(() => {
21
+ const state: NavigationState = {
22
+ view: viewFromPath(location.pathname),
23
+ path: appPath(location.pathname),
24
+ };
25
+
26
+ fetch(agentNativePath("/_agent-native/application-state/navigation"), {
27
+ method: "PUT",
28
+ keepalive: true,
29
+ headers: { "Content-Type": "application/json" },
30
+ body: JSON.stringify(state),
31
+ }).catch(() => {});
32
+ }, [location.pathname]);
33
+
34
+ const { data: navCommand } = useQuery({
35
+ queryKey: ["navigate-command"],
36
+ queryFn: async () => {
37
+ const res = await fetch(
38
+ agentNativePath("/_agent-native/application-state/navigate"),
39
+ );
40
+ if (!res.ok) return null;
41
+ const data = await res.json();
42
+ return data ? { ...data, _ts: Date.now() } : null;
43
+ },
44
+ refetchInterval: 2_000,
45
+ refetchIntervalInBackground: true,
46
+ structuralSharing: false,
47
+ });
48
+
49
+ useEffect(() => {
50
+ if (!navCommand) return;
51
+ fetch(agentNativePath("/_agent-native/application-state/navigate"), {
52
+ method: "DELETE",
53
+ headers: { "X-Agent-Native-CSRF": "1" },
54
+ }).catch(() => {});
55
+ const cmd = navCommand as NavigationState;
56
+
57
+ const path = routerPath(cmd.path || pathFromView(cmd.view));
58
+ navigate(path);
59
+ qc.setQueryData(["navigate-command"], null);
60
+ }, [navCommand, navigate, qc]);
61
+ }
62
+
63
+ function viewFromPath(pathname: string): string {
64
+ if (!pathname || pathname === "/") return "home";
65
+ return pathname.replace(/^\/+/, "") || "home";
66
+ }
67
+
68
+ function pathFromView(view: string | undefined): string {
69
+ if (!view || view === "home") return "/";
70
+ return `/${view.replace(/^\/+/, "")}`;
71
+ }
72
+
73
+ function routerPath(path: string): string {
74
+ const basePath = appBasePath();
75
+ if (!basePath) return path;
76
+ if (path === basePath) return "/";
77
+ if (path.startsWith(`${basePath}/`)) {
78
+ return path.slice(basePath.length) || "/";
79
+ }
80
+ return path;
81
+ }
@@ -16,10 +16,15 @@ import {
16
16
  } from "@tanstack/react-query";
17
17
  import { ThemeProvider } from "next-themes";
18
18
  import { useDbSync } from "@agent-native/core";
19
- import { ClientOnly, DefaultSpinner } from "@agent-native/core/client";
20
- import { getThemeInitScript } from "@agent-native/core/client";
19
+ import {
20
+ ClientOnly,
21
+ DefaultSpinner,
22
+ appPath,
23
+ getThemeInitScript,
24
+ } from "@agent-native/core/client";
21
25
  import { Toaster } from "sonner";
22
26
  import { configureTracking } from "@agent-native/core/client";
27
+ import { useNavigationState } from "./hooks/use-navigation-state";
23
28
  configureTracking({
24
29
  getDefaultProps: (_name, properties) => ({
25
30
  ...properties,
@@ -56,8 +61,8 @@ export function Layout({ children }: { children: React.ReactNode }) {
56
61
  suppressHydrationWarning
57
62
  dangerouslySetInnerHTML={{ __html: THEME_INIT_SCRIPT }}
58
63
  />
59
- <link rel="manifest" href="/manifest.json" />
60
- <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
64
+ <link rel="manifest" href={appPath("/manifest.json")} />
65
+ <link rel="icon" type="image/svg+xml" href={appPath("/favicon.svg")} />
61
66
  <meta name="theme-color" content="#111111" />
62
67
  <meta name="mobile-web-app-capable" content="yes" />
63
68
  <meta
@@ -65,7 +70,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
65
70
  content="black-translucent"
66
71
  />
67
72
  <meta name="apple-mobile-web-app-title" content="App" />
68
- <link rel="apple-touch-icon" href="/icon-180.svg" />
73
+ <link rel="apple-touch-icon" href={appPath("/icon-180.svg")} />
69
74
  <Meta />
70
75
  <Links />
71
76
  </head>
@@ -80,6 +85,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
80
85
 
81
86
  function DbSyncSetup() {
82
87
  const qc = useQueryClient();
88
+ useNavigationState();
83
89
  useDbSync({ queryClient: qc, queryKeys: ["files", "data"] });
84
90
  return null;
85
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-native/core",
3
- "version": "0.12.15",
3
+ "version": "0.12.16",
4
4
  "type": "module",
5
5
  "description": "Framework for agent-native application development — where AI agents and UI share state via files",
6
6
  "license": "MIT",
@@ -50,6 +50,12 @@ Ephemeral UI state is stored in the SQL `application_state` table, accessed via
50
50
 
51
51
  The `navigation` key is written by the UI whenever the route changes. The `navigate` key is a one-shot command: the agent writes it, the UI reads and executes the navigation, then deletes it.
52
52
 
53
+ ## Mounted Workspace Routing
54
+
55
+ This app may be mounted under `/<app-id>` in a workspace. Inside app source, React Router paths are app-local: use `<Link to="/review">` and `navigate("/review")`, not `/<app-id>/review`. The workspace gateway and `APP_BASE_PATH` add the mounted prefix in the browser; hardcoding it inside React Router links causes doubled URLs such as `/<app-id>/<app-id>/review`.
56
+
57
+ For raw paths outside React Router, use the core helpers: `appPath()` for static assets or normal hrefs, `appApiPath()` for `/api/*`, and `agentNativePath()` for `/_agent-native/*`.
58
+
53
59
  ## Agent Operations
54
60
 
55
61
  **Always know what the user is currently viewing before you edit anything.** The user's view can change mid-conversation. Stale IDs lead to editing the wrong record.
@@ -101,7 +107,7 @@ Skills in `.agents/skills/` provide detailed guidance for each architectural rul
101
107
 
102
108
  As you build out this app, follow this checklist for each new feature:
103
109
 
104
- 1. **Add navigation state entries** -- create or extend `app/hooks/use-navigation-state.ts` to track new routes
110
+ 1. **Add navigation state entries** -- extend `app/hooks/use-navigation-state.ts` to track new routes
105
111
  2. **Enhance view-screen** -- make the view-screen script return relevant context for the new view
106
112
  3. **Create domain actions** -- add scripts for CRUD operations on new data models
107
113
  4. **Create domain skills** -- add `.agents/skills/<feature>/SKILL.md` documenting the data model, storage patterns, and agent operations
@@ -20,6 +20,18 @@ app/routes/inbox.$threadId.tsx → /inbox/:threadId
20
20
  app/routes/$id.tsx → /:id (dynamic param)
21
21
  ```
22
22
 
23
+ ## Mounted Workspace Routing
24
+
25
+ In a workspace, this app can be mounted under `/<app-id>`. React Router already receives `APP_BASE_PATH`/`VITE_APP_BASE_PATH` through `appBasePath()`, so route code stays app-local:
26
+
27
+ | Route file | App-internal route | Mounted browser URL |
28
+ | ----------------------- | ------------------ | ------------------- |
29
+ | `app/routes/_index.tsx` | `/` | `/<app-id>` |
30
+ | `app/routes/review.tsx` | `/review` | `/<app-id>/review` |
31
+ | `app/routes/$id.tsx` | `/:id` | `/<app-id>/:id` |
32
+
33
+ Use `<Link to="/review">` and `navigate("/review")` inside this app. Do not prefix React Router paths with `/<app-id>` or the URL can double-prefix, e.g. `/<app-id>/<app-id>/review`. Use `appPath()` for raw `href`s/static assets, `appApiPath()` for `/api/*`, and `agentNativePath()` for `/_agent-native/*`.
34
+
23
35
  Each route file exports a default component and optional `meta()`:
24
36
 
25
37
  ```tsx
@@ -0,0 +1,81 @@
1
+ import { useEffect } from "react";
2
+ import { useLocation, useNavigate } from "react-router";
3
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
4
+ import {
5
+ agentNativePath,
6
+ appBasePath,
7
+ appPath,
8
+ } from "@agent-native/core/client";
9
+
10
+ export interface NavigationState {
11
+ view: string;
12
+ path?: string;
13
+ }
14
+
15
+ export function useNavigationState() {
16
+ const location = useLocation();
17
+ const navigate = useNavigate();
18
+ const qc = useQueryClient();
19
+
20
+ useEffect(() => {
21
+ const state: NavigationState = {
22
+ view: viewFromPath(location.pathname),
23
+ path: appPath(location.pathname),
24
+ };
25
+
26
+ fetch(agentNativePath("/_agent-native/application-state/navigation"), {
27
+ method: "PUT",
28
+ keepalive: true,
29
+ headers: { "Content-Type": "application/json" },
30
+ body: JSON.stringify(state),
31
+ }).catch(() => {});
32
+ }, [location.pathname]);
33
+
34
+ const { data: navCommand } = useQuery({
35
+ queryKey: ["navigate-command"],
36
+ queryFn: async () => {
37
+ const res = await fetch(
38
+ agentNativePath("/_agent-native/application-state/navigate"),
39
+ );
40
+ if (!res.ok) return null;
41
+ const data = await res.json();
42
+ return data ? { ...data, _ts: Date.now() } : null;
43
+ },
44
+ refetchInterval: 2_000,
45
+ refetchIntervalInBackground: true,
46
+ structuralSharing: false,
47
+ });
48
+
49
+ useEffect(() => {
50
+ if (!navCommand) return;
51
+ fetch(agentNativePath("/_agent-native/application-state/navigate"), {
52
+ method: "DELETE",
53
+ headers: { "X-Agent-Native-CSRF": "1" },
54
+ }).catch(() => {});
55
+ const cmd = navCommand as NavigationState;
56
+
57
+ const path = routerPath(cmd.path || pathFromView(cmd.view));
58
+ navigate(path);
59
+ qc.setQueryData(["navigate-command"], null);
60
+ }, [navCommand, navigate, qc]);
61
+ }
62
+
63
+ function viewFromPath(pathname: string): string {
64
+ if (!pathname || pathname === "/") return "home";
65
+ return pathname.replace(/^\/+/, "") || "home";
66
+ }
67
+
68
+ function pathFromView(view: string | undefined): string {
69
+ if (!view || view === "home") return "/";
70
+ return `/${view.replace(/^\/+/, "")}`;
71
+ }
72
+
73
+ function routerPath(path: string): string {
74
+ const basePath = appBasePath();
75
+ if (!basePath) return path;
76
+ if (path === basePath) return "/";
77
+ if (path.startsWith(`${basePath}/`)) {
78
+ return path.slice(basePath.length) || "/";
79
+ }
80
+ return path;
81
+ }
@@ -16,10 +16,15 @@ import {
16
16
  } from "@tanstack/react-query";
17
17
  import { ThemeProvider } from "next-themes";
18
18
  import { useDbSync } from "@agent-native/core";
19
- import { ClientOnly, DefaultSpinner } from "@agent-native/core/client";
20
- import { getThemeInitScript } from "@agent-native/core/client";
19
+ import {
20
+ ClientOnly,
21
+ DefaultSpinner,
22
+ appPath,
23
+ getThemeInitScript,
24
+ } from "@agent-native/core/client";
21
25
  import { Toaster } from "sonner";
22
26
  import { configureTracking } from "@agent-native/core/client";
27
+ import { useNavigationState } from "./hooks/use-navigation-state";
23
28
  configureTracking({
24
29
  getDefaultProps: (_name, properties) => ({
25
30
  ...properties,
@@ -56,8 +61,8 @@ export function Layout({ children }: { children: React.ReactNode }) {
56
61
  suppressHydrationWarning
57
62
  dangerouslySetInnerHTML={{ __html: THEME_INIT_SCRIPT }}
58
63
  />
59
- <link rel="manifest" href="/manifest.json" />
60
- <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
64
+ <link rel="manifest" href={appPath("/manifest.json")} />
65
+ <link rel="icon" type="image/svg+xml" href={appPath("/favicon.svg")} />
61
66
  <meta name="theme-color" content="#111111" />
62
67
  <meta name="mobile-web-app-capable" content="yes" />
63
68
  <meta
@@ -65,7 +70,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
65
70
  content="black-translucent"
66
71
  />
67
72
  <meta name="apple-mobile-web-app-title" content="App" />
68
- <link rel="apple-touch-icon" href="/icon-180.svg" />
73
+ <link rel="apple-touch-icon" href={appPath("/icon-180.svg")} />
69
74
  <Meta />
70
75
  <Links />
71
76
  </head>
@@ -80,6 +85,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
80
85
 
81
86
  function DbSyncSetup() {
82
87
  const qc = useQueryClient();
88
+ useNavigationState();
83
89
  useDbSync({ queryClient: qc, queryKeys: ["files", "data"] });
84
90
  return null;
85
91
  }