@softr/sdk 0.0.90-alpha.5 → 0.0.90-alpha.6
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/agent/agent-fns.d.ts +30 -0
- package/dist/agent/agent-fns.js +215 -0
- package/dist/agent/use-agent-chat.d.ts +106 -0
- package/dist/agent/use-agent-chat.js +486 -0
- package/dist/auth/isomorphic.d.ts +1 -1
- package/dist/auth/isomorphic.js +0 -2
- package/dist/formatting.d.ts +3 -3
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +7 -3
- package/dist/package.js +1 -1
- package/dist/pwa.d.ts +2 -2
- package/package.json +3 -3
- package/skills/softr-sdk/SKILL.md +5 -1
- package/skills/softr-sdk/agent-chat/SKILL.md +109 -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 +1 -1
- package/skills/softr-sdk/mcp/SKILL.md +2 -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/app-identity.js +0 -77
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: agent-chat
|
|
3
|
+
description: >-
|
|
4
|
+
Chat with one of the app's Softr agents: listAgents() and useAgentChat(agentId,
|
|
5
|
+
{ record }) from @softr/sdk give the chat state with no UI — messages, the
|
|
6
|
+
streaming reply, past chats, approve, reject, connect. Load it for an agent
|
|
7
|
+
chat, approving its actions, which records the agent may change for each
|
|
8
|
+
visitor, or connecting the visitor's account a tool needs.
|
|
9
|
+
metadata:
|
|
10
|
+
type: sub-skill
|
|
11
|
+
library: softr-sdk
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
**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.
|
|
16
|
+
|
|
17
|
+
# Agent chat
|
|
18
|
+
|
|
19
|
+
A Softr agent is set up in Studio's Agents tab and attached to the app there. There is no chat widget: the hook gives the state, and the app draws it with its own components.
|
|
20
|
+
|
|
21
|
+
## Setup
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { listAgents, useAgentChat } from '@softr/sdk'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`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. A hand-written id fails on the first message, and shows an agent the visitor may not see.
|
|
28
|
+
|
|
29
|
+
## Core pattern
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
// agentId comes from listAgents() in the route's loader.
|
|
33
|
+
function AgentPanel({ agentId, ticket }: { agentId: string; ticket: Ticket | null }) {
|
|
34
|
+
const chat = useAgentChat(agentId, {
|
|
35
|
+
record: ticket && { id: ticket.id, fields: { Title: ticket.title, Status: ticket.status } },
|
|
36
|
+
})
|
|
37
|
+
const [draft, setDraft] = useState('')
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
{chat.messages.map((message) => (
|
|
42
|
+
<Message key={message.id} message={message} onApprove={chat.approve} onReject={chat.reject} />
|
|
43
|
+
))}
|
|
44
|
+
{chat.error && <p role="alert">{chat.error.message}</p>}
|
|
45
|
+
<form onSubmit={(e) => { e.preventDefault(); chat.send(draft); setDraft('') }}>
|
|
46
|
+
<Textarea value={draft} onChange={(e) => setDraft(e.target.value)} />
|
|
47
|
+
{chat.status === 'idle'
|
|
48
|
+
? <Button type="submit">Send</Button>
|
|
49
|
+
: <Button type="button" onClick={chat.stop}>Stop</Button>}
|
|
50
|
+
</form>
|
|
51
|
+
</>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## What the hook returns
|
|
57
|
+
|
|
58
|
+
- `messages`: the whole chat, oldest first. While the agent answers, its reply is the last message and grows as it streams.
|
|
59
|
+
- `status`: `thinking` until the first part of the reply arrives, `replying` until it ends, then `idle`. Show a typing hint while it's `thinking`.
|
|
60
|
+
- `loading`: `true` while a past chat loads. Show a spinner, not the empty-chat greeting.
|
|
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 }[]`, 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.
|
|
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 this agent from now on.
|
|
65
|
+
- `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.
|
|
66
|
+
|
|
67
|
+
## Rendering a message
|
|
68
|
+
|
|
69
|
+
Each message has `role` (`user` or `assistant`) and `blocks`. Render every block type, in the order given. Show only `text` and a tool waiting for approval never appears, so the agent waits forever. A tool waiting for Approve or Connect comes last in its message, below the text asking for it.
|
|
70
|
+
|
|
71
|
+
| Block | Show it as |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| `text` | Markdown, tables included |
|
|
74
|
+
| `tool` | One line: the tool `name` and its `status`. Collapse finished ones. Expanded, show `thought`, `input` as `Label: value` lines (never raw JSON), `error` and `durationMs`, leaving out empty ones. |
|
|
75
|
+
| `todo` | A checklist of the agent's plan. It updates in place. |
|
|
76
|
+
| `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 ", ". An answered question stays locked. |
|
|
77
|
+
| `error` | An error line inside the reply |
|
|
78
|
+
|
|
79
|
+
A `tool` with status `awaiting-approval` blocks the agent until the visitor answers. Give it a split Approve button and a Reject button. 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 approves the same action again on every call. The agent continues on its own after an approval.
|
|
80
|
+
|
|
81
|
+
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. Every integration works the same way, so build one button, not one per integration.
|
|
82
|
+
|
|
83
|
+
## Context
|
|
84
|
+
|
|
85
|
+
`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.
|
|
86
|
+
|
|
87
|
+
The page title and URL go along on their own. Nothing else about the page does, so don't try to send whole lists.
|
|
88
|
+
|
|
89
|
+
## Letting the agent act on the app
|
|
90
|
+
|
|
91
|
+
The agent reads and changes data through the app's own MCP, as the visitor who is chatting, using the actions `softrMcpMiddleware` lists. With no MCP wired, the agent can only talk. Load [mcp/SKILL.md](../mcp/SKILL.md) to wire it and choose the actions.
|
|
92
|
+
|
|
93
|
+
**The agent calls a listed action directly, with any input.** It skips pages and server functions. Two things guard it: the action's `permissions` say who may run it, and the app's data restrictions say which rows each user group reaches. A read returns every row the visitor may see, and an update or delete takes any id that passes both.
|
|
94
|
+
|
|
95
|
+
- **By default, each visitor changes only their own records.** On a table whose rows belong to a person, such as requests or bookings, open its create, update and delete to every signed-in visitor, and let the table's data restriction narrow edit and delete to the visitor's own rows. The groups that manage everyone's records get no restriction.
|
|
96
|
+
- Split an action by role only for fields one role sets. A request's status goes on its own update with `customGroups` for the managers, and the visitor's update doesn't list it. One update open to every signed-in visitor that writes the status lets an employee have the agent approve their own vacation request.
|
|
97
|
+
- Don't list an update or delete on a table whose rows belong to people until its restriction is in place. Without it, the agent edits anyone's row.
|
|
98
|
+
- Where the app's pages show people only their own rows, the restriction needs a view rule too. The pages' filter lives in server functions, and without the rule the agent reads everyone's rows.
|
|
99
|
+
|
|
100
|
+
## Failure modes
|
|
101
|
+
|
|
102
|
+
**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`.
|
|
103
|
+
|
|
104
|
+
**The hook inside a component that unmounts when the panel closes.** Closing it stops a reply that is still coming. Call `useAgentChat` in the component that owns the panel's open state and pass `chat` down. Keep the selected agent there too, or closing the panel resets it.
|
|
105
|
+
|
|
106
|
+
## References
|
|
107
|
+
|
|
108
|
+
- [mcp/SKILL.md](../mcp/SKILL.md): the actions the agent may call
|
|
109
|
+
- [current-user/SKILL.md](../current-user/SKILL.md): showing the chat to signed-in visitors only
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 0.0.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 0.0.90-alpha.
|
|
10
|
+
library_version: 0.0.90-alpha.6
|
|
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.
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 0.0.90-alpha.
|
|
10
|
+
library_version: 0.0.90-alpha.6
|
|
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.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.
|
|
@@ -53,6 +53,7 @@ The list is the whole tool surface. Write it out:
|
|
|
53
53
|
|
|
54
54
|
- An action you leave out is invisible to the assistant, even where the app's pages use it freely.
|
|
55
55
|
- An action you list still obeys its own `permissions`. Listing it does not widen who may run it — the assistant runs as whoever signed in, with that user's groups.
|
|
56
|
+
- The assistant calls the action directly and skips server functions, so a row filter written in one does not apply. A listed read returns every row the user's data restrictions allow, and a listed update or delete takes any id that passes them. On a table whose rows belong to people, list an action only once a data restriction scopes the table to the user's own rows.
|
|
56
57
|
- Two actions with the same `id` throw at startup. So does anything that is not a `defineQuery` or `defineMutation` value.
|
|
57
58
|
|
|
58
59
|
Reads first. Add a write only when the user asked for one, and name it back to them.
|
|
@@ -8,7 +8,7 @@ description: >-
|
|
|
8
8
|
metadata:
|
|
9
9
|
type: sub-skill
|
|
10
10
|
library: softr-sdk
|
|
11
|
-
library_version: 0.0.90-alpha.
|
|
11
|
+
library_version: 0.0.90-alpha.6
|
|
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.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.90-alpha.
|
|
12
|
+
library_version: 0.0.90-alpha.6
|
|
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.
|
package/dist/app-identity.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { version } from "./package.js";
|
|
2
|
-
import { assert } from "./assertions.js";
|
|
3
|
-
import { createMiddleware } from "@tanstack/react-start";
|
|
4
|
-
import * as startServer from "@tanstack/react-start/server";
|
|
5
|
-
|
|
6
|
-
//#region src/app-identity.ts
|
|
7
|
-
const HTML_TAG = new TextEncoder().encode("<html");
|
|
8
|
-
const HTML_TAG_SEARCH_LIMIT = 1024;
|
|
9
|
-
/**
|
|
10
|
-
* Marks every HTML page with the Softr app it belongs to and the SDK version it
|
|
11
|
-
* runs on, so support can read both from the page source.
|
|
12
|
-
*
|
|
13
|
-
* They go on `<html>` as `data-appid` and `data-sdk-version`, and
|
|
14
|
-
* in a comment just before it. React keeps the attributes when it hydrates, but
|
|
15
|
-
* drops them if it ever renders the document again on the client. The comment
|
|
16
|
-
* sits outside what React owns, so it always stays.
|
|
17
|
-
*/
|
|
18
|
-
function appIdentityMiddleware() {
|
|
19
|
-
return createMiddleware({ type: "request" }).server(async ({ next }) => {
|
|
20
|
-
const result = await next();
|
|
21
|
-
const { response } = result;
|
|
22
|
-
if (!response.body || !response.headers.get("content-type")?.startsWith("text/html")) return result;
|
|
23
|
-
const applicationId = process.env.SOFTR_APPLICATION_ID;
|
|
24
|
-
if (!applicationId) return result;
|
|
25
|
-
assert(/^[\w-]+$/.test(applicationId), `SOFTR_APPLICATION_ID is not an id: "${applicationId}"`);
|
|
26
|
-
const encoder = new TextEncoder();
|
|
27
|
-
const comment = encoder.encode(`<!-- Softr app ${applicationId}, @softr/sdk ${version} -->`);
|
|
28
|
-
const attributes = encoder.encode(` data-appid="${applicationId}" data-sdk-version="${version}"`);
|
|
29
|
-
let held = new Uint8Array(0);
|
|
30
|
-
let done = false;
|
|
31
|
-
const body = response.body.pipeThrough(new TransformStream({
|
|
32
|
-
transform(chunk, controller) {
|
|
33
|
-
if (done) {
|
|
34
|
-
controller.enqueue(chunk);
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const bytes = new Uint8Array(held.length + chunk.length);
|
|
38
|
-
bytes.set(held);
|
|
39
|
-
bytes.set(chunk, held.length);
|
|
40
|
-
let at = -1;
|
|
41
|
-
search: for (let i = 0; i + HTML_TAG.length < bytes.length; i++) {
|
|
42
|
-
for (let j = 0; j < HTML_TAG.length; j++) if (bytes[i + j] !== HTML_TAG[j]) continue search;
|
|
43
|
-
const after = bytes[i + HTML_TAG.length];
|
|
44
|
-
if (after === 32 || after === 62) {
|
|
45
|
-
at = i;
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (at === -1) {
|
|
50
|
-
if (bytes.length > HTML_TAG_SEARCH_LIMIT) {
|
|
51
|
-
done = true;
|
|
52
|
-
controller.enqueue(bytes);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
held = bytes;
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
done = true;
|
|
59
|
-
const end = at + HTML_TAG.length;
|
|
60
|
-
controller.enqueue(bytes.subarray(0, at));
|
|
61
|
-
controller.enqueue(comment);
|
|
62
|
-
controller.enqueue(bytes.subarray(at, end));
|
|
63
|
-
controller.enqueue(attributes);
|
|
64
|
-
controller.enqueue(bytes.subarray(end));
|
|
65
|
-
},
|
|
66
|
-
flush(controller) {
|
|
67
|
-
if (!done) controller.enqueue(held);
|
|
68
|
-
}
|
|
69
|
-
}));
|
|
70
|
-
const marked = new Response(body, response);
|
|
71
|
-
marked.headers.delete("content-length");
|
|
72
|
-
return "transferResponseBodyOwnership" in startServer ? startServer.transferResponseBodyOwnership(response, marked) : marked;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
//#endregion
|
|
77
|
-
export { appIdentityMiddleware };
|