@app-assistant/host 0.1.0 → 0.2.1
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/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/react/index.d.ts +3 -1
- package/dist/react/index.js +7 -3
- package/dist/server/index.d.ts +16 -0
- package/dist/server/index.js +12 -2
- package/package.json +4 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mike Du
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -60,6 +60,23 @@ export const assistant = createAssistantHost({
|
|
|
60
60
|
});
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
**Scheduling (optional).** If your app can schedule posts, add the user's next free times to `context`, labelled
|
|
64
|
+
in their own time zone; the bot then picks one for each draft and shows a Schedule button:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
context: async (ws) => ({
|
|
68
|
+
language: "en",
|
|
69
|
+
post_times: (await freeSlots(ws, 5)).map((t) => ({ at: t.toISOString(), label: label(t, tzOf(ws)), why: "one of your best hours" })),
|
|
70
|
+
}),
|
|
71
|
+
saveDraft: async (ws, { text, thread, media, schedule_at }, key) => {
|
|
72
|
+
const draft = await db.drafts.findOrCreate({ ws, key, text, thread, media });
|
|
73
|
+
const answer = { draft_id: draft.id, edit_url: `https://shipnote.app/drafts/${draft.id}` };
|
|
74
|
+
if (!schedule_at) return answer; // Save draft
|
|
75
|
+
const ok = await schedule(draft.id, schedule_at); // the user tapped Schedule: that tap is their OK
|
|
76
|
+
return ok ? { ...answer, scheduled_at: schedule_at } : { ...answer, scheduled_at: null, schedule_note: "That time is taken now." };
|
|
77
|
+
},
|
|
78
|
+
```
|
|
79
|
+
|
|
63
80
|
**`once.claim` must be atomic and must expire on its own** (at least 60 s; 2 minutes is a good choice). `release`
|
|
64
81
|
runs when a request ends, but not when the request is killed mid-work (a function timeout, a redeploy): a claim with
|
|
65
82
|
no expiry would then stay forever, and every repeat of that key would get 409 (the bot doesn't retry a 409, so that
|
|
@@ -112,6 +129,9 @@ import { AssistantChat } from "@app-assistant/host/react";
|
|
|
112
129
|
<AssistantChat app="shipnote" botUrl="https://bot.example.com" sessionUrl="/api/assistant/session" />;
|
|
113
130
|
```
|
|
114
131
|
|
|
132
|
+
Optional props: `label` (the button, default "Write with AI"), `position` (`"left"`), and `scheduleLabel` (the word
|
|
133
|
+
before a draft's time picker when your `context` sends `post_times`: "Post at", "Send at"; default "Publish at").
|
|
134
|
+
|
|
115
135
|
It loads the bot's `widget.js` once per page; the widget draws its own floating open button and panel. Optional:
|
|
116
136
|
`label` (the button's words), `position` (`"left"` or `"right"`), and `children` for an open button of your own
|
|
117
137
|
in the page (`className` styles it). `openAssistant()` opens the chat from anywhere.
|
package/dist/react/index.d.ts
CHANGED
|
@@ -8,9 +8,11 @@ interface AssistantChatProps {
|
|
|
8
8
|
botUrl: string;
|
|
9
9
|
/** Your route that answers `{token}` for the signed-in user. Default `/api/assistant/session`. */
|
|
10
10
|
sessionUrl?: string;
|
|
11
|
-
/** The open button's words. The widget's default is "Write with
|
|
11
|
+
/** The open button's words. The widget's default is "Write with AI". */
|
|
12
12
|
label?: string;
|
|
13
13
|
position?: "left" | "right";
|
|
14
|
+
/** The draft's time picker, in your app's words ("Post at", "Send at"). Default "Publish at". */
|
|
15
|
+
scheduleLabel?: string;
|
|
14
16
|
/** An open button of your own, in your page's flow, as well as the widget's floating one. */
|
|
15
17
|
children?: ReactNode;
|
|
16
18
|
/** The class of your open button. */
|
package/dist/react/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var MARK = "data-app-assistant-host";
|
|
|
7
7
|
function openAssistant() {
|
|
8
8
|
window.dispatchEvent(new Event("assistant:open"));
|
|
9
9
|
}
|
|
10
|
-
function loadWidget({ app, botUrl, sessionUrl, label, position }) {
|
|
10
|
+
function loadWidget({ app, botUrl, sessionUrl, label, position, scheduleLabel }) {
|
|
11
11
|
if (document.querySelector(`script[${MARK}]`) || document.getElementById("assistant-widget")) return;
|
|
12
12
|
const script = document.createElement("script");
|
|
13
13
|
script.src = `${botUrl.replace(/\/+$/, "")}/widget.js`;
|
|
@@ -17,11 +17,15 @@ function loadWidget({ app, botUrl, sessionUrl, label, position }) {
|
|
|
17
17
|
script.dataset.sessionUrl = sessionUrl ?? "/api/assistant/session";
|
|
18
18
|
if (label) script.dataset.label = label;
|
|
19
19
|
if (position) script.dataset.position = position;
|
|
20
|
+
if (scheduleLabel) script.dataset.scheduleLabel = scheduleLabel;
|
|
20
21
|
document.body.appendChild(script);
|
|
21
22
|
}
|
|
22
23
|
function AssistantChat(props) {
|
|
23
|
-
const { app, botUrl, sessionUrl, label, position, children, className } = props;
|
|
24
|
-
useEffect(
|
|
24
|
+
const { app, botUrl, sessionUrl, label, position, scheduleLabel, children, className } = props;
|
|
25
|
+
useEffect(
|
|
26
|
+
() => loadWidget({ app, botUrl, sessionUrl, label, position, scheduleLabel }),
|
|
27
|
+
[app, botUrl, sessionUrl, label, position, scheduleLabel]
|
|
28
|
+
);
|
|
25
29
|
if (children === void 0 || children === null) return null;
|
|
26
30
|
return /* @__PURE__ */ jsx("button", { type: "button", className, onClick: openAssistant, children });
|
|
27
31
|
}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -32,6 +32,16 @@ interface AppContext {
|
|
|
32
32
|
/** In your own unit; `null` or left out when you don't count credits. At 0 or below the turn stops. */
|
|
33
33
|
credits_left?: number | null;
|
|
34
34
|
out_of_credits_message?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The user's next free times to post (at most 10, soonest first), when you can schedule a draft: `at` is an ISO
|
|
37
|
+
* time, `label` how the user reads it in their own time zone ("Tue 9:30 AM"), `why` a few words ("one of your best
|
|
38
|
+
* hours"). The bot picks one and the user can schedule the draft from the chat (`saveDraft`'s `schedule_at`).
|
|
39
|
+
*/
|
|
40
|
+
post_times?: {
|
|
41
|
+
at: string;
|
|
42
|
+
label: string;
|
|
43
|
+
why?: string;
|
|
44
|
+
}[];
|
|
35
45
|
}
|
|
36
46
|
interface DraftInput {
|
|
37
47
|
/** The first post. */
|
|
@@ -44,10 +54,16 @@ interface DraftInput {
|
|
|
44
54
|
alt: string;
|
|
45
55
|
mime: string;
|
|
46
56
|
}[];
|
|
57
|
+
/** Set when the user tapped Schedule: one of your `post_times`. Save the draft, then schedule it then. */
|
|
58
|
+
schedule_at?: string;
|
|
47
59
|
}
|
|
48
60
|
interface SavedDraft {
|
|
49
61
|
draft_id: string;
|
|
50
62
|
edit_url: string | null;
|
|
63
|
+
/** With `schedule_at`: the time you scheduled it, or `null` when you only saved it (say why in `schedule_note`). */
|
|
64
|
+
scheduled_at?: string | null;
|
|
65
|
+
/** Why it wasn't scheduled, in the user's words ("That time is taken now."). */
|
|
66
|
+
schedule_note?: string;
|
|
51
67
|
}
|
|
52
68
|
type ChargeResult = {
|
|
53
69
|
ok: true;
|
package/dist/server/index.js
CHANGED
|
@@ -278,8 +278,10 @@ async function endpoint(opts, name, req) {
|
|
|
278
278
|
}
|
|
279
279
|
function prepare(opts, name, ws, body, key) {
|
|
280
280
|
if (name === "drafts") {
|
|
281
|
-
const { text, thread, media } = body;
|
|
281
|
+
const { text, thread, media, schedule_at } = body;
|
|
282
282
|
if (!isText(text)) throw new Refused(400, "invalid", "The draft is empty.");
|
|
283
|
+
if (schedule_at !== void 0 && schedule_at !== null && !isText(schedule_at))
|
|
284
|
+
throw new Refused(400, "invalid", "schedule_at must be an ISO time.");
|
|
283
285
|
if (!isStrings(thread)) throw new Refused(400, "invalid", "thread must be a list of posts.");
|
|
284
286
|
const fields = ["url", "alt", "mime"];
|
|
285
287
|
if (!Array.isArray(media) || !media.every((m) => isObject(m) && fields.every((f) => typeof m[f] === "string")))
|
|
@@ -289,6 +291,7 @@ function prepare(opts, name, ws, body, key) {
|
|
|
289
291
|
thread,
|
|
290
292
|
media: media.map((m) => ({ url: m.url, alt: m.alt, mime: m.mime }))
|
|
291
293
|
};
|
|
294
|
+
if (isText(schedule_at)) draft.schedule_at = schedule_at;
|
|
292
295
|
return async () => {
|
|
293
296
|
const saved = await opts.saveDraft(ws, draft, key);
|
|
294
297
|
if (!isObject(saved) || !isText(saved.draft_id))
|
|
@@ -296,7 +299,14 @@ function prepare(opts, name, ws, body, key) {
|
|
|
296
299
|
const editUrl = saved.edit_url ?? null;
|
|
297
300
|
if (editUrl !== null && typeof editUrl !== "string")
|
|
298
301
|
throw new WrongAnswer("saveDraft()'s edit_url is a string or null");
|
|
299
|
-
|
|
302
|
+
const answer = { draft_id: saved.draft_id, edit_url: editUrl };
|
|
303
|
+
if (draft.schedule_at !== void 0) {
|
|
304
|
+
const at = saved.scheduled_at ?? null;
|
|
305
|
+
if (at !== null && !isText(at)) throw new WrongAnswer("saveDraft()'s scheduled_at is a string or null");
|
|
306
|
+
answer.scheduled_at = at;
|
|
307
|
+
if (at === null && isText(saved.schedule_note)) answer.schedule_note = saved.schedule_note;
|
|
308
|
+
}
|
|
309
|
+
return answer;
|
|
300
310
|
};
|
|
301
311
|
}
|
|
302
312
|
if (name === "material") {
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-assistant/host",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Host the app-assistant writing bot in a JavaScript app: the contract's four endpoints, its pack and tokens on Web Request/Response, and the chat window for React.",
|
|
5
|
-
"license": "
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/mikedutuandu/app-assistant/tree/main/sdk/js#readme",
|
|
7
|
+
"bugs": "https://github.com/mikedutuandu/app-assistant/issues",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
10
|
"url": "git+https://github.com/mikedutuandu/app-assistant.git",
|