@softr/sdk 0.0.89-alpha.4 → 0.0.90-ai.0
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.
- package/dist/action.d.ts +34 -1
- package/dist/action.js +12 -1
- package/dist/auth/platform.js +1 -1
- package/dist/core/ai.d.ts +79 -0
- package/dist/core/ai.js +64 -0
- package/dist/core/file-upload.js +3 -2
- package/dist/core/workflow.js +5 -4
- package/dist/datasource/record.js +1 -1
- package/dist/fetch.js +63 -1
- package/dist/formatting.d.ts +3 -3
- package/dist/index.d.ts +3 -4
- package/dist/index.js +2 -4
- package/dist/locale-settings.js +1 -1
- package/dist/mcp.js +4 -8
- package/dist/package.js +1 -1
- package/dist/pwa.d.ts +2 -2
- package/dist/pwa.js +2 -2
- package/dist/server.d.ts +2 -1
- package/dist/server.js +2 -1
- package/package.json +1 -1
- package/skills/softr-sdk/SKILL.md +13 -8
- package/skills/softr-sdk/ai/SKILL.md +168 -0
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +1 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
- package/skills/softr-sdk/formatting/SKILL.md +1 -1
- package/skills/softr-sdk/integrations/SKILL.md +8 -4
- package/skills/softr-sdk/mcp/SKILL.md +1 -1
- package/skills/softr-sdk/pwa/SKILL.md +1 -1
- package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
- package/skills/softr-sdk/record-queries/SKILL.md +1 -1
- package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
- package/dist/agent/agent-fns.d.ts +0 -30
- package/dist/agent/agent-fns.js +0 -215
- package/dist/agent/use-agent-chat.d.ts +0 -106
- package/dist/agent/use-agent-chat.js +0 -486
- package/skills/softr-sdk/agent-chat/SKILL.md +0 -124
|
@@ -1,124 +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.89-alpha.4
|
|
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
|
-
Classic Softr apps ship a chat panel. Visitors expect the same from yours:
|
|
89
|
-
|
|
90
|
-
- A round button in a corner opens it. A right-side panel on desktop, full screen on mobile.
|
|
91
|
-
- The header shows the agent's avatar and name, a picker when there are several agents, new chat, past chats and close.
|
|
92
|
-
- An empty chat greets the visitor with the agent's avatar.
|
|
93
|
-
- The list scrolls to the newest message as the reply streams, until the visitor scrolls up. Then a button takes them back down.
|
|
94
|
-
- Text renders as Markdown, tables included.
|
|
95
|
-
- The input says "Ask {agent title}…". Enter sends. Shift+Enter adds a line.
|
|
96
|
-
- A line under the input: "AI can make mistakes. Please double-check responses."
|
|
97
|
-
- An answered question stays locked.
|
|
98
|
-
- 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.
|
|
99
|
-
- A tool that needs the visitor's account shows a Connect button.
|
|
100
|
-
|
|
101
|
-
## Context
|
|
102
|
-
|
|
103
|
-
`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.
|
|
104
|
-
|
|
105
|
-
The page title and URL go along on their own. Nothing else about the page does, so don't try to send whole lists.
|
|
106
|
-
|
|
107
|
-
## Letting the agent act on the app
|
|
108
|
-
|
|
109
|
-
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.
|
|
110
|
-
|
|
111
|
-
## Failure modes
|
|
112
|
-
|
|
113
|
-
**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`.
|
|
114
|
-
|
|
115
|
-
**Rendering only `text` blocks.** A tool waiting for approval never shows, and the agent waits forever. Render every block type.
|
|
116
|
-
|
|
117
|
-
**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.
|
|
118
|
-
|
|
119
|
-
**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`.
|
|
120
|
-
|
|
121
|
-
## References
|
|
122
|
-
|
|
123
|
-
- [mcp/SKILL.md](../mcp/SKILL.md): the actions the agent may call
|
|
124
|
-
- [current-user/SKILL.md](../current-user/SKILL.md): showing the chat to signed-in visitors only
|