@softr/sdk 0.0.89-alpha.2 → 0.0.89-alpha.3

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.
@@ -34,7 +34,10 @@ type AgentQuestion = {
34
34
  }>;
35
35
  multiSelect: boolean;
36
36
  };
37
- /** One piece of a message, in the order the agent produced it. */
37
+ /**
38
+ * One piece of a message, in the order the agent produced it. A tool waiting for
39
+ * the visitor comes last, below the text that asks for it, until they answer.
40
+ */
38
41
  type AgentBlock = {
39
42
  type: "text";
40
43
  text: string;
@@ -262,11 +262,11 @@ function useAgentChat(agentId, options = {}) {
262
262
  reset
263
263
  ]);
264
264
  return {
265
- messages: live === null ? messages : [...messages, {
265
+ messages: (live === null ? messages : [...messages, {
266
266
  id: "live",
267
267
  role: "assistant",
268
268
  blocks: live
269
- }],
269
+ }]).map(waitingLast),
270
270
  status,
271
271
  loading,
272
272
  error,
@@ -281,6 +281,15 @@ function useAgentChat(agentId, options = {}) {
281
281
  connect
282
282
  };
283
283
  }
284
+ const waitsForVisitor = (b) => b.type === "tool" && (b.status === "awaiting-approval" || b.status === "awaiting-connection" || b.status === "connecting");
285
+ function waitingLast(message) {
286
+ if (!message.blocks.some(waitsForVisitor)) return message;
287
+ const blocks = [...message.blocks.filter((b) => !waitsForVisitor(b)), ...message.blocks.filter(waitsForVisitor)];
288
+ return {
289
+ ...message,
290
+ blocks
291
+ };
292
+ }
284
293
  const CONNECT_TIMEOUT_MS = 5 * 6e4;
285
294
  const CONNECT_RETRY_MS = 3e3;
286
295
  function popupFeatures(width, height) {
@@ -449,18 +458,22 @@ function fromHistory(history) {
449
458
  });
450
459
  }
451
460
  const CHAT_GONE_MESSAGE = "This chat is no longer available. Start a new one.";
452
- const chatKey = (agentId, userId) => `softr-agent-chat:${agentId}:${userId ?? "visitor"}`;
461
+ const VISITOR = "visitor";
462
+ const chatKey = (agentId, userId) => `softr-code-app:agent-chat:${agentId}:${userId ?? VISITOR}`;
463
+ function chatStore(key) {
464
+ return key.endsWith(`:${VISITOR}`) ? sessionStorage : localStorage;
465
+ }
453
466
  function savedChatId(key) {
454
467
  try {
455
- return localStorage.getItem(key);
468
+ return chatStore(key).getItem(key);
456
469
  } catch {
457
470
  return null;
458
471
  }
459
472
  }
460
473
  function saveChatId(key, chatId) {
461
474
  try {
462
- if (chatId) localStorage.setItem(key, chatId);
463
- else localStorage.removeItem(key);
475
+ if (chatId) chatStore(key).setItem(key, chatId);
476
+ else chatStore(key).removeItem(key);
464
477
  } catch {}
465
478
  }
466
479
  async function failureMessage(res) {
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import * as react2 from "react";
2
+ import * as react0 from "react";
3
3
  import { ReactNode } from "react";
4
4
 
5
5
  //#region src/formatting.d.ts
@@ -94,7 +94,7 @@ declare function FormattingScope({
94
94
  /** A BCP 47 locale, such as `de-DE`. It drops the app's fixed date format inside the scope. */
95
95
  locale?: string;
96
96
  children?: ReactNode;
97
- }): react2.FunctionComponentElement<react2.ProviderProps<Scope>>;
97
+ }): react0.FunctionComponentElement<react0.ProviderProps<Scope>>;
98
98
  /**
99
99
  * A date in the app's format, safe to render on the server. A timestamp whose
100
100
  * zone is not known yet renders `fallback` until the browser supplies it, so
@@ -115,6 +115,6 @@ declare function FormattedDate({
115
115
  time?: boolean;
116
116
  /** What renders while the visitor's zone is unknown. */
117
117
  fallback?: ReactNode;
118
- }): react2.FunctionComponentElement<react2.FragmentProps>;
118
+ }): react0.FunctionComponentElement<react0.FragmentProps>;
119
119
  //#endregion
120
120
  export { DateOptions, DateParts, DateSeparator, DateValue, FixedDateFormat, FormattedDate, Formatting, FormattingScope, WeekDay, formatDate, formatDateTime, formatNumber, useFormatting };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.0.89-alpha.2";
2
+ var version = "0.0.89-alpha.3";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/dist/pwa.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as react0 from "react";
1
+ import * as react4 from "react";
2
2
  import "@tanstack/react-start";
3
3
 
4
4
  //#region src/pwa.d.ts
@@ -23,7 +23,7 @@ import "@tanstack/react-start";
23
23
  * )
24
24
  * }
25
25
  */
26
- declare function SoftrPwaHead(): react0.FunctionComponentElement<react0.FragmentProps>;
26
+ declare function SoftrPwaHead(): react4.FunctionComponentElement<react4.FragmentProps>;
27
27
  type PwaPlatform = "ios" | "android" | "desktop";
28
28
  type PwaInstall = {
29
29
  available: false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "0.0.89-alpha.2",
3
+ "version": "0.0.89-alpha.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: core
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  # Softr SDK core
@@ -86,7 +86,7 @@ Calling a connected third-party API (Slack, Stripe, Gmail, Google Calendar, …)
86
86
  Opening the app to an AI assistant over MCP?
87
87
  load softr-sdk/mcp
88
88
 
89
- Adding a chat with one of the app's Softr agents?
89
+ Adding a chat with one of the app's Softr agents, or a Connect button for the visitor's account it needs?
90
90
  load softr-sdk/agent-chat
91
91
 
92
92
  Adding an install button, or making the app installable?
@@ -3,14 +3,16 @@ name: agent-chat
3
3
  description: >-
4
4
  Let visitors chat with one of the app's Softr agents: listAgents() from
5
5
  @softr/sdk gives the agents the visitor may use, and useAgentChat(agentId, {
6
- record }) returns the messages, the reply as it streams, and
7
- send, stop, approve and reject, with no UI. Build the chat from the app's own
6
+ record }) returns the messages, the reply as it streams, past chats, and
7
+ send, stop, approve, reject and connect, with no UI. Load it for an agent
8
+ chat panel, approving the agent's actions, or connecting the visitor's own
9
+ account (Gmail, Slack…) that a tool needs. Build the chat from the app's own
8
10
  components. The agent acts on the app's data through the app's MCP, so
9
11
  softrMcpMiddleware must list the actions it may use.
10
12
  metadata:
11
13
  type: sub-skill
12
14
  library: softr-sdk
13
- library_version: 0.0.89-alpha.2
15
+ library_version: 0.0.89-alpha.3
14
16
  ---
15
17
 
16
18
  **Boundary.** `listAgents` and `useAgentChat` live on `@softr/sdk`. `listAgents` runs in a route loader, `useAgentChat` in a component. Neither needs a route, server function or registration of its own. It chats as the signed-in visitor, or anonymously when no one is signed in.
@@ -56,12 +58,12 @@ function AgentPanel({ agentId, ticket }: { agentId: string; ticket: Ticket | nul
56
58
 
57
59
  ## What the hook returns
58
60
 
59
- - `messages`: the whole chat, oldest first. While the agent answers, its reply is the last message and grows as it streams.
61
+ - `messages`: the whole chat, oldest first. While the agent answers, its reply is the last message and grows as it streams. A tool waiting for Approve or Connect comes last in its message, so render blocks in the order given.
60
62
  - `status`: `thinking` until the first part of the reply arrives, `replying` until it ends, then `idle`. Show a typing hint while it's `thinking`.
61
63
  - `loading`: `true` while a past chat loads. Show a spinner, not the empty-chat greeting.
62
64
  - `error`: why the last request failed, or `null`.
63
65
  - `send(text)`, `stop()`, `newChat()`.
64
- - `listChats()` resolves to the visitor's past chats, `{ id, title }[]`, with `title` `null` until the agent names the chat. `openChat(id)` loads one. `chatId` is the open one, `null` for a new chat. The open chat is kept in the browser for each visitor, so the hook reopens it after a remount or a reload.
66
+ - `listChats()` resolves to the visitor's past chats, `{ id, title }[]`, with `title` `null` until the agent names the chat. `openChat(id)` loads one. `chatId` is the open one, `null` for a new chat. The open chat is kept in the browser for each signed-in user, so the hook reopens it after a remount or a reload. A signed-out visitor keeps it only until the tab closes.
65
67
  - `approve(toolCallId, scope?)` and `reject(toolCallId)` answer a tool waiting for approval. `scope` is `ONCE` (default), `SESSION` for the rest of this chat, or `USER` for always.
66
68
  - `connect(toolCallId)` signs the visitor in to a tool's `integration` in a popup. Call it straight from the click, or the browser blocks the popup.
67
69
 
@@ -92,7 +94,7 @@ Classic Softr apps ship a chat panel. Visitors expect the same from yours:
92
94
  - Text renders as Markdown, tables included.
93
95
  - The input says "Ask {agent title}…". Enter sends. Shift+Enter adds a line.
94
96
  - A line under the input: "AI can make mistakes. Please double-check responses."
95
- - A question with one choice sends on click. One with `multiSelect` or several questions sends once all are answered. An answered question stays locked.
97
+ - An answered question stays locked.
96
98
  - Approve offers the three scopes: once, for this chat, always for this agent.
97
99
  - A tool's `input` is JSON. Show it readable: one `Label: value` line per field, with line breaks kept, never the raw JSON. Leave out `thought`, `input`, `error` or the duration when it's empty.
98
100
  - A tool that needs the visitor's account shows a Connect button.
@@ -113,12 +115,10 @@ The agent reads and changes data through the app's own MCP, as the visitor who i
113
115
 
114
116
  **Rendering only `text` blocks.** A tool waiting for approval never shows, and the agent waits forever. Render every block type.
115
117
 
116
- **The hook inside a panel that unmounts when it closes.** Closing it stops a reply that is still coming. Call `useAgentChat` in the component that owns the open state, such as the layout, and pass `chat` into the panel. Keep the selected agent there too, or closing the panel resets it.
118
+ **The hook inside a panel that unmounts when it closes.** Closing it stops a reply that is still coming. Call `useAgentChat` in the component that owns the open state, such as `AppLayout` in `src/routes/_app.tsx`, and pass `chat` into the panel. Keep the selected agent there too, or closing the panel resets it.
117
119
 
118
120
  **An agent id written by hand.** The chat fails on the first message, and an agent hidden from the visitor still shows. Take the id from `listAgents`.
119
121
 
120
- **Expecting the agent to change data with no MCP wired.** It answers but can't act. List the actions it needs in `softrMcpMiddleware`.
121
-
122
122
  ## References
123
123
 
124
124
  - [mcp/SKILL.md](../mcp/SKILL.md): the actions the agent may call
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  # The current user
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 0.0.89-alpha.2
10
+ library_version: 0.0.89-alpha.3
11
11
  ---
12
12
 
13
13
  **Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 0.0.89-alpha.2
11
+ library_version: 0.0.89-alpha.3
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 0.0.89-alpha.2
10
+ library_version: 0.0.89-alpha.3
11
11
  ---
12
12
 
13
13
  **Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 0.0.89-alpha.2
11
+ library_version: 0.0.89-alpha.3
12
12
  ---
13
13
 
14
14
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. The same holds for any helper that calls `ds`: declare it inside the handler. A function at module level that calls `ds` keeps the server-only import in the browser bundle, and every page stops loading until it moves. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.89-alpha.2
12
+ library_version: 0.0.89-alpha.3
13
13
  ---
14
14
 
15
15
  **Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.