@softr/sdk 0.0.90-alpha.2 → 0.0.90-alpha.4

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,145 +0,0 @@
1
- ---
2
- name: agent-chat
3
- description: >-
4
- Let visitors chat with one of the app's Softr agents: listAgents() from
5
- @softr/sdk gives the agents the visitor may use, and useAgentChat(agentId, {
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
10
- components. The agent acts on the app's data through the app's MCP, so
11
- softrMcpMiddleware must list the actions it may use.
12
- metadata:
13
- type: sub-skill
14
- library: softr-sdk
15
- library_version: 0.0.90-alpha.2
16
- ---
17
-
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.
19
-
20
- # Agent chat
21
-
22
- A Softr agent is set up in Studio's Agents tab and attached to the app there. The app gives it a chat. There is no ready-made chat widget: the hook gives the state, and the app draws it with its own components.
23
-
24
- ## Setup
25
-
26
- ```ts
27
- import { listAgents, useAgentChat } from '@softr/sdk'
28
- ```
29
-
30
- `listAgents()` resolves to the agents this visitor may chat with, `{ id, title, avatar, thumbnail }[]`. Call it in the route's `loader`. An agent hidden from this visitor isn't in the list, so show no chat when it's empty. When there are several, let the visitor pick one, or pick the one the builder named by its `title`. Take the id from the list, never write one by hand.
31
-
32
- ## Core pattern
33
-
34
- ```tsx
35
- // agentId comes from listAgents() in the route's loader.
36
- function AgentPanel({ agentId, ticket }: { agentId: string; ticket: Ticket | null }) {
37
- const chat = useAgentChat(agentId, {
38
- record: ticket && { id: ticket.id, fields: { Title: ticket.title, Status: ticket.status } },
39
- })
40
- const [draft, setDraft] = useState('')
41
-
42
- return (
43
- <>
44
- {chat.messages.map((message) => (
45
- <Message key={message.id} message={message} onApprove={chat.approve} onReject={chat.reject} />
46
- ))}
47
- {chat.error && <p role="alert">{chat.error.message}</p>}
48
- <form onSubmit={(e) => { e.preventDefault(); chat.send(draft); setDraft('') }}>
49
- <Textarea value={draft} onChange={(e) => setDraft(e.target.value)} />
50
- {chat.status === 'idle'
51
- ? <Button type="submit">Send</Button>
52
- : <Button type="button" onClick={chat.stop}>Stop</Button>}
53
- </form>
54
- </>
55
- )
56
- }
57
- ```
58
-
59
- ## What the hook returns
60
-
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.
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`.
63
- - `loading`: `true` while a past chat loads. Show a spinner, not the empty-chat greeting.
64
- - `error`: why the last request failed, or `null`.
65
- - `send(text)`, `stop()`, `newChat()`.
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.
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 this agent from now on.
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.
69
-
70
- ## Rendering a message
71
-
72
- Each message has `role` (`user` or `assistant`) and `blocks`, in the order the agent produced them. Render every block type:
73
-
74
- | Block | Show it as |
75
- | --- | --- |
76
- | `text` | Markdown text |
77
- | `tool` | One line: the tool `name` and its `status`. Collapse finished ones. Expanded, show `thought`, `input`, `error` and `durationMs`. |
78
- | `todo` | A checklist of the agent's plan. It updates in place. |
79
- | `question` | The questions with their options. One question with a single choice sends its label with `send` on click. Several questions, or `multiSelect`, collect the picks and show a Send button, enabled once each question has one. It sends one line per question, picked labels joined by ", ". |
80
- | `error` | An error line inside the reply |
81
-
82
- A `tool` with status `awaiting-approval` blocks the agent until the visitor answers. Give it a split Approve button and a Reject button, as classic apps do. The button says "Approve once" (`approve(id)`), and its menu offers "Approve for this session" (`approve(id, 'SESSION')`) and "Approve for this agent" (`approve(id, 'USER')`). With only one Approve, the visitor has to approve the same action again on every call. The agent continues on its own after an approval.
83
-
84
- A `tool` with status `awaiting-connection` needs the visitor's own `integration` account, such as their Gmail. Say "You need to connect to {integration} for this request. You'll only need to connect once.", with the name in readable case, and show a Connect button that calls `connect`. While it's `connecting`, show "Connecting…" and disable the button. The agent continues on its own once they're signed in. Any integration works the same way, so don't build one per integration.
85
-
86
- ## The chat panel
87
-
88
- Visitors expect the panel classic Softr apps ship:
89
-
90
- - A round `primary` button in a corner opens it, with the agent's avatar or a chat icon. It hides while the panel is open.
91
- - A right-side panel, about 420px, full screen on mobile.
92
- - Header: avatar, name, agent picker when there are several, new chat, past chats, close.
93
- - The list follows the streaming reply until the visitor scrolls up, then offers a jump back down.
94
- - Text renders as Markdown, tables included.
95
- - The input says "Ask {agent title}…". Enter sends, Shift+Enter adds a line. Under it: "AI can make mistakes. Please double-check responses."
96
- - An answered question stays locked.
97
- - A tool's `input` shows as `Label: value` lines, never raw JSON. Empty `thought`, `input`, `error` or duration are left out.
98
-
99
- ## How it looks
100
-
101
- Defaults for the first build. The builder's prompt overrides them.
102
-
103
- - Reuse the app's own components and styles: its Markdown renderer, cards, buttons, avatars. Add no copies and no new colours.
104
- - One loud thing at a time. Tool lines stay quiet; only a block waiting for the visitor stands out.
105
- - Visitor messages are right-aligned `primary` bubbles. Agent replies have no bubble and run full width, so tables read like a page.
106
- - A tool is one muted `text-xs` line with a status icon, expandable.
107
- - Approve, connect and questions sit in a `card`: a title, a short line, the main action `primary`.
108
- - While `thinking`, a muted pulsing "Thinking…" where the reply will appear.
109
- - An empty chat shows a large avatar and "Hi, I'm {agent title}". Add suggestion chips only when the builder said what the agent does.
110
- - The composer is one rounded box holding the textarea and send button. It grows to about six lines.
111
- - Short slide and fade motion that respects reduced motion.
112
-
113
- ## Context
114
-
115
- `record` is the one record the visitor is looking at, so they can say "close this one". Pass it where a page shows one record, and `null` elsewhere. Use readable field names as keys. The agent reads them as they are.
116
-
117
- The page title and URL go along on their own. Nothing else about the page does, so don't try to send whole lists.
118
-
119
- ## Letting the agent act on the app
120
-
121
- The agent reads and changes data through the app's own MCP, as the visitor who is chatting. Their groups and each action's `permissions` apply. The actions it may use are the ones listed in `softrMcpMiddleware`. With no MCP wired, the agent can only talk. Load [mcp/SKILL.md](../mcp/SKILL.md) to wire it.
122
-
123
- **The agent calls a listed action directly, with any input.** Pages and server functions are skipped, so the action's `permissions` are its only guard: a read returns every row those groups may see, an update or delete takes any id.
124
-
125
- - List the app's existing actions. Don't define copies for the agent.
126
- - Narrow `permissions` to who may really run it. When roles differ, split the action: each writes only that role's fields, with `customGroups` for that role.
127
- - Don't list an action that is only safe for a visitor's own records. Nothing scopes it to them.
128
- - Tell the builder which actions the agent can use and who may run each one.
129
-
130
- ## Failure modes
131
-
132
- **Building a chat without the hook.** Calling the agents service from the browser fails: only the app's server can sign the request in as the visitor. Use `useAgentChat`.
133
-
134
- **Rendering only `text` blocks.** A tool waiting for approval never shows, and the agent waits forever. Render every block type.
135
-
136
- **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.
137
-
138
- **One action for every role.** An update any signed-in visitor may run, writing the status, lets an employee have the agent approve their own vacation request. Give approve its own action, limited to managers.
139
-
140
- **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`.
141
-
142
- ## References
143
-
144
- - [mcp/SKILL.md](../mcp/SKILL.md): the actions the agent may call
145
- - [current-user/SKILL.md](../current-user/SKILL.md): showing the chat to signed-in visitors only