@softr/sdk 0.0.87-alpha.4 → 0.0.87

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,105 +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, and
7
- send, stop, approve and reject, with no UI. Build the chat from the app's own
8
- components. The agent acts on the app's data through the app's MCP, so
9
- softrMcpMiddleware must list the actions it may use.
10
- metadata:
11
- type: sub-skill
12
- library: softr-sdk
13
- library_version: 0.0.87-alpha.4
14
- ---
15
-
16
- **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.
17
-
18
- # Agent chat
19
-
20
- 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.
21
-
22
- ## Setup
23
-
24
- ```ts
25
- import { listAgents, useAgentChat } from '@softr/sdk'
26
- ```
27
-
28
- `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.
29
-
30
- ## Core pattern
31
-
32
- ```tsx
33
- // agentId comes from listAgents() in the route's loader.
34
- function AgentPanel({ agentId, ticket }: { agentId: string; ticket: Ticket | null }) {
35
- const chat = useAgentChat(agentId, {
36
- record: ticket && { id: ticket.id, fields: { Title: ticket.title, Status: ticket.status } },
37
- })
38
- const [draft, setDraft] = useState('')
39
-
40
- return (
41
- <>
42
- {chat.messages.map((message) => (
43
- <Message key={message.id} message={message} onApprove={chat.approve} onReject={chat.reject} />
44
- ))}
45
- {chat.error && <p role="alert">{chat.error.message}</p>}
46
- <form onSubmit={(e) => { e.preventDefault(); chat.send(draft); setDraft('') }}>
47
- <Textarea value={draft} onChange={(e) => setDraft(e.target.value)} />
48
- {chat.status === 'idle'
49
- ? <Button type="submit">Send</Button>
50
- : <Button type="button" onClick={chat.stop}>Stop</Button>}
51
- </form>
52
- </>
53
- )
54
- }
55
- ```
56
-
57
- ## What the hook returns
58
-
59
- - `messages`: the whole chat, oldest first. While the agent answers, its reply is the last message and grows as it streams.
60
- - `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
- - `error`: why the last request failed, or `null`.
62
- - `send(text)`, `stop()`, `newChat()`.
63
- - `listChats()` resolves to the visitor's past chats, `{ id, title }[]`. `openChat(id)` loads one. `chatId` is the open one, `null` for a new chat.
64
- - `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.
65
-
66
- ## Rendering a message
67
-
68
- Each message has `role` (`user` or `assistant`) and `blocks`, in the order the agent produced them. Render every block type:
69
-
70
- | Block | Show it as |
71
- | --- | --- |
72
- | `text` | Markdown text |
73
- | `tool` | One line: the tool `name` and its `status`. Collapse finished ones. |
74
- | `todo` | A checklist of the agent's plan. It updates in place. |
75
- | `question` | The questions with their options. A click on an option sends its label with `send`. |
76
- | `error` | An error line inside the reply |
77
-
78
- A `tool` with status `awaiting-approval` blocks the agent until the visitor answers. Show Approve and Reject buttons on it. The agent continues on its own after an approval.
79
-
80
- A `tool` with status `awaiting-connection` needs the visitor to connect the `integration` first. Code apps can't do that yet. Say which integration the agent needs, and that the builder has to connect it for this agent in Studio.
81
-
82
- ## Context
83
-
84
- `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.
85
-
86
- The page title goes along on its own. Nothing else about the page does, so don't try to send whole lists.
87
-
88
- ## Letting the agent act on the app
89
-
90
- 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.
91
-
92
- ## Failure modes
93
-
94
- **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`.
95
-
96
- **Rendering only `text` blocks.** A tool waiting for approval never shows, and the agent waits forever. Render every block type.
97
-
98
- **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`.
99
-
100
- **Expecting the agent to change data with no MCP wired.** It answers but can't act. List the actions it needs in `softrMcpMiddleware`.
101
-
102
- ## References
103
-
104
- - [mcp/SKILL.md](../mcp/SKILL.md): the actions the agent may call
105
- - [current-user/SKILL.md](../current-user/SKILL.md): showing the chat to signed-in visitors only