@tansy/ai 0.0.0-stage → 0.1.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.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/admin.d.ts +2 -0
  4. package/admin.js +17 -0
  5. package/ai.css +280 -0
  6. package/api.d.ts +91 -0
  7. package/api.js +79 -0
  8. package/assistant.d.ts +3 -0
  9. package/assistant.js +290 -0
  10. package/attach.d.ts +48 -0
  11. package/attach.js +141 -0
  12. package/attachments.d.ts +34 -0
  13. package/attachments.js +66 -0
  14. package/chats.d.ts +35 -0
  15. package/chats.js +78 -0
  16. package/client.d.ts +3 -0
  17. package/client.js +88 -0
  18. package/composer.d.ts +38 -0
  19. package/composer.js +155 -0
  20. package/errors.d.ts +5 -0
  21. package/errors.js +25 -0
  22. package/history.d.ts +15 -0
  23. package/history.js +47 -0
  24. package/icons.generated.d.ts +5 -0
  25. package/icons.generated.js +66 -0
  26. package/images.d.ts +21 -0
  27. package/images.js +23 -0
  28. package/index.d.ts +26 -0
  29. package/index.js +701 -0
  30. package/logic.d.ts +77 -0
  31. package/logic.js +166 -0
  32. package/markdown.d.ts +38 -0
  33. package/markdown.js +142 -0
  34. package/media.d.ts +24 -0
  35. package/media.js +64 -0
  36. package/messages.d.ts +891 -0
  37. package/messages.js +689 -0
  38. package/models.d.ts +7 -0
  39. package/models.js +47 -0
  40. package/package.json +46 -4
  41. package/parts.d.ts +59 -0
  42. package/parts.js +110 -0
  43. package/provider.d.ts +134 -0
  44. package/provider.js +117 -0
  45. package/role.d.ts +24 -0
  46. package/role.js +25 -0
  47. package/workflow/check.d.ts +46 -0
  48. package/workflow/check.js +63 -0
  49. package/workflow/client.d.ts +90 -0
  50. package/workflow/client.js +51 -0
  51. package/workflow/editor.d.ts +13 -0
  52. package/workflow/editor.js +133 -0
  53. package/workflow/engine.d.ts +232 -0
  54. package/workflow/engine.js +779 -0
  55. package/workflow/icons.d.ts +26 -0
  56. package/workflow/icons.js +67 -0
  57. package/workflow/model-picker.d.ts +22 -0
  58. package/workflow/model-picker.js +69 -0
  59. package/workflow/page.d.ts +3 -0
  60. package/workflow/page.js +118 -0
  61. package/workflow/routes.d.ts +12 -0
  62. package/workflow/routes.js +418 -0
  63. package/workflow/run.d.ts +8 -0
  64. package/workflow/run.js +162 -0
  65. package/workflow/schema.d.ts +183 -0
  66. package/workflow/schema.js +143 -0
  67. package/workflow/templates.d.ts +9 -0
  68. package/workflow/templates.js +81 -0
  69. package/workflow/workflow.css +241 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohamed Salah
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
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ <!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # @tansy/ai
4
+
5
+ An AI assistant for tansy on the Vercel AI SDK: chat that can create and edit content, filling forms, and writing fields, with any model provider. Add a provider package, such as @tansy/ai-openai.
6
+
7
+ ```bash
8
+ npm install @tansy/ai
9
+ ```
10
+
11
+ Add it to `tansy.config.ts`. See [the ai plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/ai.md) for what it adds and the options it takes.
12
+
13
+ A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
package/admin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare const _default: import("tansy/admin").AdminPluginParts;
2
+ export default _default;
package/admin.js ADDED
@@ -0,0 +1,17 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { defineAdminPlugin } from "tansy/admin";
3
+ import { Assistant } from "./assistant";
4
+ import { Workflows } from "./workflow/page";
5
+ /** A four-pointed spark, in the navigation's 16 by 16 box. */
6
+ const SPARK = {
7
+ paths: ["M8 1.25a.75.75 0 0 1 .72.54l.9 3.06a2.25 2.25 0 0 0 1.53 1.53l3.06.9a.75.75 0 0 1 0 1.44l-3.06.9a2.25 2.25 0 0 0-1.53 1.53l-.9 3.06a.75.75 0 0 1-1.44 0l-.9-3.06a2.25 2.25 0 0 0-1.53-1.53l-3.06-.9a.75.75 0 0 1 0-1.44l3.06-.9a2.25 2.25 0 0 0 1.53-1.53l.9-3.06A.75.75 0 0 1 8 1.25Z"],
8
+ };
9
+ export default defineAdminPlugin({
10
+ pages: [
11
+ { path: "ai", title: "Assistant", permission: "ext.ai:use", component: () => _jsx(Assistant, {}) },
12
+ { path: "ai-workflows", title: "Workflows", permission: "ext.ai:workflows", component: () => _jsx(Workflows, {}) },
13
+ ],
14
+ section: { title: "AI", icon: SPARK, items: [{ page: "ai" }, { page: "ai-workflows" }, { settings: true }] },
15
+ // The assistant from any page, at the end of the top bar.
16
+ shortcut: { page: "ai", title: "Ask AI", icon: SPARK },
17
+ });
package/ai.css ADDED
@@ -0,0 +1,280 @@
1
+ /* The AI plugin's dashboard parts. Colours and spacing come from the dashboard's own tokens, so they follow its themes. */
2
+
3
+ /*
4
+ * The assistant fills the window under the top bar: saved chats on one side, the chat on the
5
+ * other. The chat scrolls inside itself, never the page, so following a streaming answer can't
6
+ * fight the page's own scroll.
7
+ */
8
+ .ai-app { --ai-column: 48rem; --ai-side: 16.5rem; position: relative; display: flex; height: calc(100dvh - var(--cms-topbar-height)); margin-block: calc(-1 * var(--cms-space-6)) calc(-1 * var(--cms-space-12)); overflow: hidden; }
9
+ @media (min-width: 40rem) { .ai-app { margin-inline: calc(-1 * var(--cms-space-6)); } }
10
+ @media (min-width: 64rem) { .ai-app { margin-inline: calc(-1 * var(--cms-space-8)); } }
11
+ .ai-icon { width: 1rem; height: 1rem; flex: none; }
12
+ .ai-main { position: relative; display: flex; flex: 1 1 auto; flex-direction: column; min-width: 0; }
13
+ .ai-column { width: 100%; max-width: var(--ai-column); margin-inline: auto; padding-inline: var(--cms-space-4); }
14
+ @media (min-width: 40rem) { .ai-column { padding-inline: var(--cms-space-6); } }
15
+ .ai-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-anchor: none; overscroll-behavior: contain; scrollbar-gutter: stable; }
16
+ .ai-column-empty { display: flex; min-height: 100%; flex-direction: column; justify-content: center; }
17
+ @media (max-width: 39.99rem) { .ai-hide-sm { display: none; } }
18
+
19
+ /* The chat's top line: the chats panel, the chat's name, a new chat. */
20
+ .ai-bar { display: flex; flex: none; align-items: center; gap: var(--cms-space-2); height: 2.75rem; padding-inline: var(--cms-space-3); }
21
+ .ai-bar-title { flex: 1 1 auto; min-width: 0; overflow: hidden; padding-inline: var(--cms-space-1); font-size: var(--cms-text-sm); font-weight: 500; color: var(--cms-color-text-strong); text-overflow: ellipsis; white-space: nowrap; }
22
+
23
+ /* Small borderless buttons: copy, retry, new chat. */
24
+ .ai-ghost { display: inline-flex; flex: none; align-items: center; gap: var(--cms-space-1); height: 1.75rem; padding-inline: var(--cms-space-2); border: 0; border-radius: var(--cms-radius-md); background: none; font-size: var(--cms-text-sm); color: var(--cms-color-text-muted); cursor: pointer; transition: background var(--cms-transition), color var(--cms-transition); }
25
+ .ai-ghost:hover { background: var(--cms-color-surface-sunken); color: var(--cms-color-text-strong); }
26
+ .ai-ghost:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 1px; }
27
+ .ai-ghost-icon { justify-content: center; width: 1.75rem; padding: 0; }
28
+
29
+ /* Saved chats: a panel beside the chat on wide screens, over it on narrow ones. */
30
+ .ai-side { display: flex; flex: none; flex-direction: column; width: var(--ai-side); min-height: 0; border-inline-end: 1px solid var(--cms-color-border); background: var(--cms-color-surface-muted); animation: ai-slide 180ms ease-out; }
31
+ .ai-scrim { display: none; }
32
+ @media (max-width: 63.99rem) {
33
+ .ai-side { position: absolute; inset-block: 0; inset-inline-start: 0; z-index: 30; width: min(20rem, 85%); box-shadow: var(--cms-shadow-xl); }
34
+ .ai-scrim { position: absolute; inset: 0; z-index: 29; display: block; border: 0; background: rgb(0 0 0 / 0.25); cursor: default; animation: ai-fade 180ms ease-out; }
35
+ }
36
+ .ai-side-head { display: flex; flex: none; align-items: center; gap: var(--cms-space-1); padding: var(--cms-space-2) var(--cms-space-2) var(--cms-space-1); }
37
+ .ai-side-new { display: flex; flex: 1 1 auto; align-items: center; gap: var(--cms-space-2); height: 2.25rem; padding-inline: var(--cms-space-2); border: 0; border-radius: var(--cms-radius-md); background: none; font-size: var(--cms-text-sm); font-weight: 500; color: var(--cms-color-text-strong); cursor: pointer; transition: background var(--cms-transition); }
38
+ .ai-side-new:hover { background: var(--cms-color-surface-sunken); }
39
+ .ai-side-new:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: -2px; }
40
+ .ai-side-new-mark { display: grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: 999px; background: var(--cms-color-accent); color: var(--cms-color-on-accent); }
41
+ .ai-side-new-mark .ai-icon { width: 0.875rem; height: 0.875rem; }
42
+ .ai-side-search { display: flex; flex: none; align-items: center; gap: var(--cms-space-2); margin: var(--cms-space-1) var(--cms-space-2) var(--cms-space-2); padding-inline: var(--cms-space-2); height: 2rem; border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-md); background: var(--cms-color-surface); color: var(--cms-color-text-faint); }
43
+ .ai-side-search:focus-within { border-color: var(--cms-color-border-strong); }
44
+ .ai-side-search input { flex: 1 1 auto; min-width: 0; border: 0; background: none; font: inherit; font-size: var(--cms-text-sm); color: var(--cms-color-text-strong); outline: none; }
45
+ .ai-side-search input::-webkit-search-cancel-button { display: none; }
46
+ .ai-side-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 0 var(--cms-space-2) var(--cms-space-4); }
47
+ .ai-side-group + .ai-side-group { margin-top: var(--cms-space-4); }
48
+ .ai-side-heading { position: sticky; top: 0; z-index: 1; padding: var(--cms-space-2) var(--cms-space-2) var(--cms-space-1); background: var(--cms-color-surface-muted); font-size: var(--cms-text-xs); font-weight: 500; color: var(--cms-color-text-muted); }
49
+ .ai-side-group ul { display: flex; flex-direction: column; gap: 1px; margin: 0; padding: 0; list-style: none; }
50
+ .ai-side-empty { padding: var(--cms-space-4) var(--cms-space-2); font-size: var(--cms-text-sm); color: var(--cms-color-text-muted); }
51
+ .ai-chat { position: relative; display: flex; align-items: center; border-radius: var(--cms-radius-md); transition: background var(--cms-transition); }
52
+ .ai-chat:hover { background: var(--cms-color-surface-sunken); }
53
+ .ai-chat-current, .ai-chat-current:hover { background: var(--cms-color-surface); box-shadow: inset 0 0 0 1px var(--cms-color-border); }
54
+ .ai-chat-open { flex: 1 1 auto; min-width: 0; padding: 0.4375rem var(--cms-space-2); border: 0; border-radius: var(--cms-radius-md); background: none; text-align: start; font-size: var(--cms-text-sm); color: var(--cms-color-text); cursor: pointer; }
55
+ .ai-chat-current .ai-chat-open { font-weight: 500; color: var(--cms-color-text-strong); }
56
+ .ai-chat-open:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: -2px; }
57
+ .ai-chat-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
58
+ .ai-chat-action { margin-inline-end: 2px; opacity: 0; }
59
+ .ai-chat:hover .ai-chat-action, .ai-chat-action:focus-visible, .ai-chat-current .ai-chat-action { opacity: 1; }
60
+ @media (hover: none) { .ai-chat-action { opacity: 1; } }
61
+ .ai-chat-confirm { display: flex; flex: none; align-items: center; gap: 2px; margin-inline-end: 2px; }
62
+ .ai-chat-delete { height: 1.5rem; padding-inline: var(--cms-space-2); border: 0; border-radius: var(--cms-radius-sm); background: var(--cms-color-danger-tint); font-size: var(--cms-text-xs); font-weight: 500; color: var(--cms-color-danger-on-tint); cursor: pointer; }
63
+ .ai-chat-delete:hover { filter: brightness(0.96); }
64
+
65
+ /* A new chat's start: a greeting, the composer and a few quick asks. */
66
+ .ai-start { display: flex; flex-direction: column; gap: var(--cms-space-4); padding-block: var(--cms-space-6) 14vh; }
67
+ .ai-hero { display: flex; align-items: center; justify-content: center; gap: var(--cms-space-3); margin-bottom: var(--cms-space-2); text-align: center; }
68
+ .ai-hero-mark { width: 1.75rem; height: 1.75rem; color: var(--cms-color-caution); animation: ai-spin-in 600ms cubic-bezier(0.2, 0.8, 0.2, 1); }
69
+ .ai-hero-title { font-size: clamp(1.5rem, 1.1rem + 1.6vw, 2rem); line-height: 1.2; font-weight: 500; letter-spacing: -0.02em; color: var(--cms-color-text-strong); text-wrap: balance; }
70
+ .ai-welcome { max-width: 32rem; margin-inline: auto; text-align: center; font-size: var(--cms-text-xs); line-height: var(--cms-leading-sm); color: var(--cms-color-text-muted); text-wrap: pretty; }
71
+ .ai-notice { padding: var(--cms-space-3) var(--cms-space-4); border: 1px solid var(--cms-color-warning-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-warning-surface); color: var(--cms-color-warning-on-tint); font-size: var(--cms-text-sm); }
72
+ .ai-notice a { color: inherit; font-weight: 500; text-decoration: underline; text-underline-offset: 2px; }
73
+
74
+ /* Quick asks: small pills. */
75
+ .ai-chips { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--cms-space-2); }
76
+ .ai-chip { height: 1.875rem; padding-inline: var(--cms-space-3); border: 1px solid var(--cms-color-border); border-radius: 999px; background: var(--cms-color-surface); font-size: var(--cms-text-xs); font-weight: 450; color: var(--cms-color-text-soft); white-space: nowrap; cursor: pointer; transition: border-color var(--cms-transition), background var(--cms-transition), color var(--cms-transition); }
77
+ .ai-chip:hover { border-color: var(--cms-color-border-strong); background: var(--cms-color-surface-muted); color: var(--cms-color-text-strong); }
78
+ .ai-chip:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 2px; }
79
+
80
+ /* The conversation. */
81
+ .ai-thread { display: flex; flex-direction: column; gap: var(--cms-space-6); padding-block: var(--cms-space-4) var(--cms-space-8); }
82
+ .ai-row { display: flex; flex-direction: column; min-width: 0; animation: ai-rise 220ms ease-out; }
83
+ .ai-row-user { align-items: flex-end; gap: 2px; }
84
+ .ai-bubble { max-width: min(85%, 36rem); padding: var(--cms-space-2) var(--cms-space-4); border-radius: 1.125rem; background: var(--cms-color-surface-sunken); color: var(--cms-color-text-strong); font-size: var(--cms-text-base); line-height: var(--cms-leading-base); white-space: pre-wrap; overflow-wrap: anywhere; }
85
+ .ai-row-assistant { gap: var(--cms-space-3); }
86
+ .ai-row-actions { display: flex; align-items: center; gap: 2px; min-height: 1.75rem; opacity: 0; transition: opacity var(--cms-transition); }
87
+ .ai-row-assistant .ai-row-actions { margin-top: calc(-1 * var(--cms-space-2)); margin-inline-start: calc(-1 * var(--cms-space-2)); }
88
+ .ai-row:hover .ai-row-actions, .ai-row-actions:focus-within, .ai-row-assistant:last-child .ai-row-actions { opacity: 1; }
89
+ @media (hover: none) { .ai-row-actions { opacity: 1; } }
90
+ .ai-model-tag { display: inline-flex; align-items: center; gap: 0.3125rem; margin-inline-start: var(--cms-space-1); max-width: 14rem; overflow: hidden; font-size: var(--cms-text-xs); color: var(--cms-color-text-faint); text-overflow: ellipsis; white-space: nowrap; }
91
+ .ai-cursor { display: inline-flex; color: var(--cms-color-caution); }
92
+ .ai-cursor .ai-icon { width: 0.875rem; height: 0.875rem; animation: ai-pulse 1.4s ease-in-out infinite; }
93
+ .ai-waiting { display: inline-flex; align-items: center; gap: var(--cms-space-2); font-size: var(--cms-text-sm); }
94
+ .ai-waiting-mark { color: var(--cms-color-caution); animation: ai-rotate 2.4s linear infinite; }
95
+ .ai-shimmer { background: linear-gradient(90deg, var(--cms-color-text-muted) 0%, var(--cms-color-text-faint) 40%, var(--cms-color-text-muted) 80%); background-size: 200% 100%; background-clip: text; -webkit-background-clip: text; color: transparent; animation: ai-shimmer 1.6s linear infinite; }
96
+
97
+ /* Answers, written in Markdown. */
98
+ .ai-prose { color: var(--cms-color-text-strong); font-size: var(--cms-text-base); line-height: 1.7; overflow-wrap: anywhere; }
99
+ @media (min-width: 40rem) { .ai-prose { font-size: 0.9375rem; } }
100
+ .ai-prose > * + * { margin-top: 0.8em; }
101
+ .ai-prose h2, .ai-prose h3, .ai-prose h4, .ai-prose h5, .ai-prose h6 { margin-top: 1.4em; font-weight: 600; line-height: 1.3; color: var(--cms-color-text-strong); }
102
+ .ai-prose h2 { font-size: 1.2em; }
103
+ .ai-prose h3 { font-size: 1.08em; }
104
+ .ai-prose h4, .ai-prose h5, .ai-prose h6 { font-size: 1em; }
105
+ .ai-prose > :first-child { margin-top: 0; }
106
+ .ai-prose ul, .ai-prose ol { padding-inline-start: 1.4em; }
107
+ .ai-prose ul { list-style: disc; }
108
+ .ai-prose ol { list-style: decimal; }
109
+ .ai-prose li + li { margin-top: 0.3em; }
110
+ .ai-prose li::marker { color: var(--cms-color-text-muted); }
111
+ .ai-prose strong { font-weight: 600; }
112
+ .ai-prose em { font-style: italic; }
113
+ .ai-prose a { color: inherit; text-decoration: underline; text-decoration-color: var(--cms-color-text-faint); text-underline-offset: 3px; }
114
+ .ai-prose a:hover { text-decoration-color: currentColor; }
115
+ .ai-prose code { padding: 0.1em 0.35em; border-radius: var(--cms-radius-sm); background: var(--cms-color-surface-sunken); font-family: var(--cms-font-mono); font-size: 0.85em; }
116
+ .ai-prose blockquote { padding-inline-start: 1em; border-inline-start: 3px solid var(--cms-color-border-strong); color: var(--cms-color-text-soft); }
117
+ .ai-prose hr { border: 0; border-top: 1px solid var(--cms-color-border); }
118
+ .ai-prose table { display: block; overflow-x: auto; border-collapse: collapse; font-size: 0.92em; }
119
+ .ai-prose th, .ai-prose td { padding: 0.4em 0.75em; border: 1px solid var(--cms-color-border); text-align: start; }
120
+ .ai-prose th { background: var(--cms-color-surface-muted); font-weight: 600; }
121
+ .ai-code { overflow: hidden; border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface-muted); }
122
+ .ai-code-head { display: flex; align-items: center; justify-content: space-between; padding: var(--cms-space-1) var(--cms-space-1) var(--cms-space-1) var(--cms-space-3); border-bottom: 1px solid var(--cms-color-border); font-family: var(--cms-font-mono); font-size: var(--cms-text-xs); color: var(--cms-color-text-muted); }
123
+ .ai-code pre { overflow-x: auto; margin: 0; padding: var(--cms-space-3); font-size: var(--cms-text-sm); line-height: 1.6; }
124
+ .ai-code pre code { padding: 0; background: none; font-size: inherit; }
125
+
126
+ /* What the model thought: a quiet line that opens. */
127
+ .ai-thinking { display: flex; flex-direction: column; align-items: flex-start; gap: var(--cms-space-2); }
128
+ .ai-thinking-head { display: inline-flex; align-items: center; gap: var(--cms-space-2); height: 1.75rem; margin-inline-start: calc(-1 * var(--cms-space-2)); padding-inline: var(--cms-space-2); border: 0; border-radius: var(--cms-radius-md); background: none; font-size: var(--cms-text-sm); color: var(--cms-color-text-muted); cursor: pointer; }
129
+ .ai-thinking-head:hover { background: var(--cms-color-surface-sunken); color: var(--cms-color-text-strong); }
130
+ .ai-thinking-head:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 1px; }
131
+ .ai-thinking-active .ai-thinking-head span { background: linear-gradient(90deg, var(--cms-color-text-muted) 0%, var(--cms-color-text-faint) 40%, var(--cms-color-text-muted) 80%); background-size: 200% 100%; background-clip: text; -webkit-background-clip: text; color: transparent; animation: ai-shimmer 1.6s linear infinite; }
132
+ .ai-thinking-body { width: 100%; max-height: 14rem; overflow-y: auto; padding-inline-start: var(--cms-space-4); border-inline-start: 2px solid var(--cms-color-border); font-size: var(--cms-text-sm); line-height: 1.6; color: var(--cms-color-text-muted); white-space: pre-wrap; overflow-wrap: anywhere; }
133
+
134
+ /* Tool calls: one quiet line each, opening to show what was sent and what came back. */
135
+ .ai-tool { display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface); font-size: var(--cms-text-sm); }
136
+ .ai-tool-head { display: flex; align-items: center; gap: var(--cms-space-2); width: 100%; min-height: 2.25rem; padding: var(--cms-space-1) var(--cms-space-3); border: 0; background: none; text-align: start; color: var(--cms-color-text); cursor: pointer; }
137
+ .ai-tool-head:disabled { cursor: default; }
138
+ .ai-tool-head:hover:not(:disabled) { background: var(--cms-color-surface-muted); }
139
+ .ai-tool-head:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: -2px; }
140
+ .ai-tool-mark { display: grid; flex: none; place-items: center; width: 1.125rem; height: 1.125rem; border-radius: 999px; background: var(--cms-color-surface-sunken); color: var(--cms-color-text-muted); }
141
+ .ai-tool-mark .ai-icon { width: 0.6875rem; height: 0.6875rem; }
142
+ .ai-tool-done .ai-tool-mark { background: var(--cms-color-success-tint); color: var(--cms-color-success-on-tint); }
143
+ .ai-tool-failed .ai-tool-mark { background: var(--cms-color-danger-tint); color: var(--cms-color-danger-on-tint); }
144
+ .ai-tool-ask .ai-tool-mark { background: var(--cms-color-warning-tint); color: var(--cms-color-warning-on-tint); }
145
+ .ai-tool-title { flex: 1 1 auto; min-width: 0; overflow: hidden; font-weight: 500; color: var(--cms-color-text-strong); text-overflow: ellipsis; white-space: nowrap; }
146
+ .ai-tool-summary { font-weight: 400; color: var(--cms-color-text-soft); }
147
+ .ai-tool-status { flex: none; font-size: var(--cms-text-xs); color: var(--cms-color-text-muted); }
148
+ .ai-chevron { width: 0.75rem; height: 0.75rem; color: var(--cms-color-text-faint); transition: transform var(--cms-transition); }
149
+ .ai-chevron-open { transform: rotate(90deg); }
150
+ [dir="rtl"] .ai-chevron:not(.ai-chevron-open) { transform: scaleX(-1); }
151
+ .ai-tool-ask { border-color: var(--cms-color-warning-border); box-shadow: var(--cms-shadow-md); }
152
+ .ai-tool-ask .ai-tool-status { color: var(--cms-color-warning-text); font-weight: 500; }
153
+ .ai-tool-error { padding: 0 var(--cms-space-3) var(--cms-space-2); color: var(--cms-color-danger-text); overflow-wrap: anywhere; }
154
+ .ai-tool-detail { max-height: 16rem; overflow: auto; margin: 0; padding: var(--cms-space-3); border-top: 1px solid var(--cms-color-border); background: var(--cms-color-surface-muted); font-family: var(--cms-font-mono); font-size: var(--cms-text-xs); line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }
155
+ .ai-tool-actions { display: flex; align-items: center; gap: var(--cms-space-2); padding: var(--cms-space-1) var(--cms-space-3) var(--cms-space-3); }
156
+ .ai-btn { display: inline-flex; align-items: center; gap: var(--cms-space-1); height: 1.875rem; padding-inline: var(--cms-space-3); border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-md); background: var(--cms-color-surface); font-size: var(--cms-text-sm); font-weight: 500; color: var(--cms-color-text-strong); cursor: pointer; transition: background var(--cms-transition), border-color var(--cms-transition); }
157
+ .ai-btn:hover { background: var(--cms-color-surface-muted); border-color: var(--cms-color-border-strong); }
158
+ .ai-btn:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 2px; }
159
+ .ai-btn-primary { border-color: transparent; background: var(--cms-color-accent); color: var(--cms-color-on-accent); }
160
+ .ai-btn-primary:hover { border-color: transparent; background: var(--cms-color-accent-hover); }
161
+ .ai-btn .ai-icon { width: 0.875rem; height: 0.875rem; }
162
+
163
+ /* Failing. */
164
+ .ai-spinner { width: 0.6875rem; height: 0.6875rem; border: 1.5px solid currentColor; border-top-color: transparent; border-radius: 999px; animation: ai-rotate 700ms linear infinite; }
165
+ .ai-failure { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--cms-space-2); padding: var(--cms-space-2) var(--cms-space-2) var(--cms-space-2) var(--cms-space-4); border: 1px solid var(--cms-color-danger-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-danger-surface); color: var(--cms-color-danger-on-tint); font-size: var(--cms-text-sm); }
166
+ .ai-failure .ai-ghost { color: inherit; }
167
+ .ai-failure .ai-ghost:hover { background: var(--cms-color-danger-tint); }
168
+
169
+ /* The composer, docked under a running chat. */
170
+ .ai-dock { position: relative; flex: none; padding-bottom: var(--cms-space-2); }
171
+ .ai-dock::before { content: ""; position: absolute; inset-inline: 0; bottom: 100%; height: var(--cms-space-6); background: linear-gradient(to bottom, transparent, var(--cms-color-page)); pointer-events: none; }
172
+ .ai-dock .ai-column { position: relative; }
173
+ .ai-jump { position: absolute; bottom: calc(100% + var(--cms-space-3)); left: 50%; z-index: 2; display: grid; place-items: center; width: 2rem; height: 2rem; border: 1px solid var(--cms-color-border); border-radius: 999px; background: var(--cms-color-surface); color: var(--cms-color-text); box-shadow: var(--cms-shadow-lg); cursor: pointer; transform: translateX(-50%); animation: ai-fade 150ms ease-out; }
174
+ .ai-jump:hover { background: var(--cms-color-surface-muted); color: var(--cms-color-text-strong); }
175
+ .ai-jump:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 2px; }
176
+ .ai-foot { padding-top: var(--cms-space-2); text-align: center; font-size: var(--cms-text-xs); color: var(--cms-color-text-faint); }
177
+ .ai-compose { display: flex; flex-direction: column; gap: var(--cms-space-2); padding: var(--cms-space-3) var(--cms-space-2) var(--cms-space-2) var(--cms-space-4); border: 1px solid var(--cms-color-border); border-radius: 1.25rem; background: var(--cms-color-surface); box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px -4px rgb(0 0 0 / 0.08); cursor: text; transition: border-color var(--cms-transition), box-shadow var(--cms-transition); }
178
+ .ai-compose:focus-within { border-color: var(--cms-color-border-strong); box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 8px 24px -6px rgb(0 0 0 / 0.14); }
179
+ .ai-input { width: 100%; min-height: 1.5rem; max-height: 18rem; padding: 0 var(--cms-space-2) 0 0; border: 0; background: none; resize: none; font: inherit; font-size: var(--cms-text-base); line-height: 1.55; color: var(--cms-color-text-strong); outline: none; }
180
+ .ai-input::placeholder { color: var(--cms-color-text-faint); }
181
+ .ai-input:disabled { cursor: not-allowed; }
182
+ .ai-compose-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--cms-space-2); margin-inline-start: calc(-1 * var(--cms-space-2)); }
183
+ .ai-compose-tools { display: flex; flex: 1 1 auto; align-items: center; gap: var(--cms-space-1); min-width: 0; }
184
+ .ai-send { display: grid; flex: none; place-items: center; width: 2rem; height: 2rem; border: 0; border-radius: 999px; background: var(--cms-color-accent); color: var(--cms-color-on-accent); cursor: pointer; transition: background var(--cms-transition), opacity var(--cms-transition), transform var(--cms-transition); }
185
+ .ai-send:hover:not(:disabled) { background: var(--cms-color-accent-hover); }
186
+ .ai-send:active:not(:disabled) { transform: scale(0.94); }
187
+ .ai-send:disabled { opacity: 0.25; cursor: not-allowed; }
188
+ .ai-send:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 2px; }
189
+
190
+ /* The composer's model and thinking pickers, and their menus, which open upwards. */
191
+ .ai-menu-root { position: relative; min-width: 0; }
192
+ .ai-pill { display: inline-flex; align-items: center; gap: 0.3125rem; max-width: 100%; height: 1.875rem; padding-inline: var(--cms-space-2); border: 0; border-radius: 999px; background: none; font-size: var(--cms-text-xs); font-weight: 500; color: var(--cms-color-text-muted); cursor: pointer; transition: background var(--cms-transition), color var(--cms-transition); }
193
+ .ai-pill:hover, .ai-pill[aria-expanded="true"] { background: var(--cms-color-surface-sunken); color: var(--cms-color-text-strong); }
194
+ .ai-pill:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 1px; }
195
+ .ai-pill .ai-icon { width: 0.875rem; height: 0.875rem; }
196
+ /* Brand icons from @lobehub/icons, shared with the workflows pages. */
197
+ .wf-brand { display: inline-block; flex: none; vertical-align: middle; border-radius: 4px; }
198
+ .wf-brand-mono { background: currentColor; mask-size: contain; mask-repeat: no-repeat; mask-position: center; -webkit-mask-size: contain; -webkit-mask-repeat: no-repeat; -webkit-mask-position: center; }
199
+ .wf-brand-letter { display: inline-grid; place-items: center; background: var(--cms-color-surface-sunken); color: var(--cms-color-text-muted); font-weight: 600; line-height: 1; }
200
+ .ai-pill .wf-brand, .ai-model-tag .wf-brand { color: var(--cms-color-text-strong); }
201
+ .ai-menu-roles { width: min(20rem, calc(100vw - 2rem)); max-height: min(24rem, 60vh); }
202
+ .ai-menu-scroll { overflow-y: auto; min-height: 0; }
203
+ .ai-menu-roles .ai-menu-detail { white-space: normal; }
204
+ .ai-pill-text { min-width: 0; max-width: 11rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
205
+ @media (max-width: 39.99rem) { .ai-pill-text { max-width: 6.5rem; } }
206
+ .ai-pill-on { color: var(--cms-color-info-text, var(--cms-color-text-strong)); background: var(--cms-color-info-tint, var(--cms-color-surface-sunken)); }
207
+ .ai-pill-static { cursor: default; }
208
+ .ai-pill-static:hover { background: none; color: var(--cms-color-text-muted); }
209
+ .ai-caret { width: 0.75rem !important; height: 0.75rem !important; opacity: 0.7; }
210
+ .ai-menu { position: absolute; bottom: calc(100% + var(--cms-space-2)); inset-inline-start: 0; z-index: 40; display: flex; flex-direction: column; padding: var(--cms-space-1); border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface); box-shadow: var(--cms-shadow-xl); animation: ai-pop 140ms ease-out; cursor: default; }
211
+ .ai-column-empty .ai-menu { bottom: auto; top: calc(100% + var(--cms-space-2)); }
212
+ .ai-menu-models { width: min(22rem, calc(100vw - 2rem)); max-height: min(24rem, 60vh); }
213
+ .ai-menu-thinking { width: 15rem; }
214
+ /* The provider picker sits in the top bar, so its menu opens downwards from the end. */
215
+ .ai-bar .ai-menu { top: calc(100% + var(--cms-space-1)); bottom: auto; inset-inline-start: auto; inset-inline-end: 0; }
216
+ .ai-menu-providers { width: min(16rem, calc(100vw - 2rem)); }
217
+ .ai-menu-search { display: flex; flex: none; align-items: center; gap: var(--cms-space-2); margin-bottom: var(--cms-space-1); padding: 0 var(--cms-space-2); height: 2.125rem; border-bottom: 1px solid var(--cms-color-border); color: var(--cms-color-text-faint); }
218
+ .ai-menu-search input { flex: 1 1 auto; min-width: 0; border: 0; background: none; font: inherit; font-size: var(--cms-text-sm); color: var(--cms-color-text-strong); outline: none; }
219
+ .ai-menu-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; margin: 0; padding: 0; list-style: none; }
220
+ .ai-menu-heading { padding: var(--cms-space-1) var(--cms-space-2); font-size: var(--cms-text-xs); font-weight: 500; color: var(--cms-color-text-muted); }
221
+ .ai-menu-item { display: flex; align-items: center; gap: var(--cms-space-2); width: 100%; padding: 0.375rem var(--cms-space-2); border: 0; border-radius: var(--cms-radius-md); background: none; text-align: start; font-size: var(--cms-text-sm); color: var(--cms-color-text); cursor: pointer; }
222
+ button.ai-menu-item:hover, .ai-menu-active { background: var(--cms-color-surface-sunken); }
223
+ button.ai-menu-item:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: -2px; }
224
+ .ai-menu-item > .ai-icon { width: 0.875rem; height: 0.875rem; color: var(--cms-color-text-strong); }
225
+ .ai-menu-text { display: flex; flex: 1 1 auto; flex-direction: column; min-width: 0; }
226
+ .ai-menu-label { overflow: hidden; color: var(--cms-color-text-strong); text-overflow: ellipsis; white-space: nowrap; }
227
+ .ai-menu-detail { overflow: hidden; font-size: var(--cms-text-xs); color: var(--cms-color-text-muted); text-overflow: ellipsis; white-space: nowrap; }
228
+ .ai-menu-note { padding: var(--cms-space-2); font-size: var(--cms-text-xs); color: var(--cms-color-text-muted); }
229
+
230
+ /* Files in the chat: attaching them, the ones sent, and pictures the assistant draws. */
231
+ .ai-compose { position: relative; }
232
+ .ai-compose-drop { border-color: var(--cms-color-accent-border); background: var(--cms-color-accent-tint); }
233
+ .ai-drop-note { position: absolute; inset: 0; z-index: 1; display: grid; place-items: center; border-radius: inherit; font-size: var(--cms-text-sm); font-weight: 500; color: var(--cms-color-accent-strong); pointer-events: none; }
234
+ .ai-pill-icon { width: 1.875rem; justify-content: center; padding: 0; }
235
+ .ai-pill:disabled { opacity: 0.5; cursor: default; }
236
+ .ai-files { display: flex; flex-wrap: wrap; gap: var(--cms-space-2); margin: 0; padding: 0; list-style: none; }
237
+ .ai-files-pending { margin-inline-end: var(--cms-space-2); }
238
+ .ai-files-sent { justify-content: flex-end; max-width: min(85%, 36rem); }
239
+ .ai-file { display: inline-flex; align-items: center; gap: var(--cms-space-2); max-width: 15rem; min-height: 2.75rem; padding: var(--cms-space-1) var(--cms-space-2) var(--cms-space-1) var(--cms-space-1); border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface); font-size: var(--cms-text-xs); color: var(--cms-color-text-strong); text-decoration: none; }
240
+ a.ai-file:hover { border-color: var(--cms-color-border-strong); }
241
+ .ai-file-failed { border-color: var(--cms-color-danger-border); background: var(--cms-color-danger-surface); }
242
+ .ai-file-failed .ai-file-note { color: var(--cms-color-danger-text); }
243
+ .ai-file-thumb { flex: none; width: 2.25rem; height: 2.25rem; border-radius: var(--cms-radius-md); object-fit: cover; background: var(--cms-color-surface-sunken); }
244
+ .ai-file-thumb-icon { display: grid; place-items: center; color: var(--cms-color-text-muted); }
245
+ .ai-file-text { display: flex; flex-direction: column; min-width: 0; }
246
+ .ai-file-name, .ai-file-note { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
247
+ .ai-file-note { color: var(--cms-color-text-muted); }
248
+ .ai-file-note:empty { display: none; }
249
+ .ai-file-remove { display: grid; flex: none; place-items: center; width: 1.375rem; height: 1.375rem; margin-inline-start: auto; padding: 0; border: 0; border-radius: 999px; background: none; color: var(--cms-color-text-muted); cursor: pointer; }
250
+ .ai-file-remove:hover { background: var(--cms-color-surface-sunken); color: var(--cms-color-text-strong); }
251
+ .ai-file-remove:focus-visible, a.ai-file:focus-visible { outline: 2px solid var(--cms-focus-ring); outline-offset: 1px; }
252
+ .ai-file-image { max-width: none; padding: 0; overflow: hidden; }
253
+ .ai-file-picture { display: block; max-width: 14rem; max-height: 14rem; object-fit: cover; }
254
+ .ai-picture { display: flex; flex-direction: column; gap: var(--cms-space-1); margin: 0; max-width: min(100%, 32rem); }
255
+ .ai-picture img { display: block; width: 100%; height: auto; border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface-sunken); }
256
+ .ai-picture-working { flex-direction: row; align-items: center; gap: var(--cms-space-2); width: min(100%, 20rem); aspect-ratio: 4 / 3; justify-content: center; border: 1px dashed var(--cms-color-border-strong); border-radius: var(--cms-radius-lg); background: var(--cms-color-surface-sunken); font-size: var(--cms-text-sm); color: var(--cms-color-text-muted); }
257
+ .ai-picture-bar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--cms-space-1); }
258
+ .ai-picture-bar .ai-ghost { text-decoration: none; }
259
+
260
+ @keyframes ai-rise { from { opacity: 0; transform: translateY(4px); } }
261
+ @keyframes ai-pulse { 50% { opacity: 0.4; transform: scale(0.85); } }
262
+ @keyframes ai-rotate { to { transform: rotate(360deg); } }
263
+ @keyframes ai-spin-in { from { opacity: 0; transform: rotate(-90deg) scale(0.6); } }
264
+ @keyframes ai-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
265
+ @keyframes ai-fade { from { opacity: 0; } }
266
+ @keyframes ai-pop { from { opacity: 0; transform: translateY(4px) scale(0.98); } }
267
+ @keyframes ai-slide { from { opacity: 0; transform: translateX(-8px); } }
268
+ @media (prefers-reduced-motion: reduce) {
269
+ .ai-row, .ai-hero-mark, .ai-cursor .ai-icon, .ai-waiting-mark, .ai-spinner, .ai-shimmer, .ai-thinking-active .ai-thinking-head span, .ai-menu, .ai-side, .ai-scrim, .ai-jump { animation: none; }
270
+ }
271
+
272
+ /* The record editor's AI parts. */
273
+ .ai-link { align-self: flex-start; background: none; border: 0; padding: 0; color: var(--cms-color-text-soft); text-decoration: underline; cursor: pointer; font-size: var(--cms-text-sm); }
274
+ .ai-panel { display: flex; flex-direction: column; align-items: stretch; }
275
+ .ai-panel .cms-btn { align-self: flex-start; }
276
+ .ai-write { position: relative; }
277
+ .ai-write-menu { position: absolute; right: 0; top: calc(100% + 0.25rem); z-index: 20; display: flex; flex-direction: column; gap: 2px; width: 15rem; padding: var(--cms-space-2); border: 1px solid var(--cms-color-border); border-radius: var(--cms-radius-md); background: var(--cms-color-surface); box-shadow: var(--cms-shadow-lg); }
278
+ .ai-write-menu .cms-input { margin-bottom: var(--cms-space-1); }
279
+ .ai-write-item { padding: 0.375rem var(--cms-space-2); border: 0; border-radius: var(--cms-radius-sm); background: none; text-align: left; font-size: var(--cms-text-sm); color: var(--cms-color-text-strong); cursor: pointer; }
280
+ .ai-write-item:hover:not(:disabled), .ai-write-item:focus-visible { background: var(--cms-color-surface-sunken); }
package/api.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ export type Reasoning = "provider-default" | "none" | "low" | "medium" | "high";
2
+ export interface AiStatus {
3
+ ready: boolean;
4
+ provider?: string;
5
+ providerLabel?: string;
6
+ /** The model the settings choose. */
7
+ model?: string;
8
+ /** The thinking the settings choose. */
9
+ reasoning?: Reasoning;
10
+ /** Whether the person may choose the model and thinking in the chat. */
11
+ chooseModel?: boolean;
12
+ /** Whether the provider can list its models. */
13
+ listsModels?: boolean;
14
+ /** The provider's icon from @lobehub/icons, when its adapter names one. */
15
+ providerIcon?: string;
16
+ /** The role the AI acts with unless the person picks another. */
17
+ role?: string;
18
+ /** Whether the person may pick the role in the chat. */
19
+ chooseRole?: boolean;
20
+ /** The providers set up, while the person may choose the model. The chat offers them when there's more than one. */
21
+ providers?: ProviderChoice[];
22
+ }
23
+ export interface ProviderChoice {
24
+ id: string;
25
+ label: string;
26
+ icon?: string;
27
+ /** The model it uses unless the person picks another. */
28
+ model?: string;
29
+ listsModels: boolean;
30
+ }
31
+ export interface RoleChoice {
32
+ value: string;
33
+ label: string;
34
+ description?: string;
35
+ }
36
+ /** The roles the person may give the assistant, or the reason they can't be listed. */
37
+ export declare function chatRoles(): Promise<{
38
+ options: RoleChoice[];
39
+ } | {
40
+ error: string;
41
+ }>;
42
+ export interface ModelChoice {
43
+ value: string;
44
+ label?: string;
45
+ }
46
+ /** The models of a provider, the site's when left out, the person may choose from in the chat, or the reason they can't be listed. */
47
+ export declare function chatModels(provider?: string | null): Promise<{
48
+ options: ModelChoice[];
49
+ } | {
50
+ error: string;
51
+ }>;
52
+ /** Whether this person may use AI and a provider is set up. Asked once per page load: every field's menu needs it. Null means they may not. */
53
+ export declare function aiStatus(): Promise<AiStatus | null>;
54
+ /** Posts to one of the plugin's routes and returns its JSON, or throws with the message the route gave. */
55
+ export declare function post<T>(path: string, body: unknown): Promise<T>;
56
+ export interface ChatSummary {
57
+ id: string;
58
+ title: string;
59
+ updatedAt: string;
60
+ }
61
+ /** The signed-in person's saved chats, most recent first. Empty when they can't be read. */
62
+ export declare function listChats(): Promise<ChatSummary[]>;
63
+ /** One saved chat with its messages, or null when it's gone. */
64
+ export declare function loadChat<M>(id: string): Promise<(ChatSummary & {
65
+ messages: M[];
66
+ }) | null>;
67
+ export declare function deleteChat(id: string): Promise<boolean>;
68
+ export interface ChatCapabilities {
69
+ model: string;
70
+ /** The file types the chat's model may be sent. */
71
+ accept: string[];
72
+ /** Whether the provider says so, rather than the model's name. */
73
+ reported: boolean;
74
+ maxBytes: number;
75
+ /** Whether the assistant can draw pictures. */
76
+ draws: boolean;
77
+ }
78
+ /** What the chat's model reads, or null when it can't be told. */
79
+ export declare function chatCapabilities(model: string | null, provider?: string | null): Promise<ChatCapabilities | null>;
80
+ export interface Attachment {
81
+ id: string;
82
+ url: string;
83
+ mediaType: string;
84
+ name: string;
85
+ bytes: number;
86
+ }
87
+ /** Keeps a file for the chat, for the signed-in person alone. */
88
+ export declare function uploadAttachment(file: Blob, name: string, mediaType: string): Promise<Attachment>;
89
+ export declare function deleteAttachment(id: string): Promise<void>;
90
+ /** Copies a picture the assistant drew into the site's files, as the person, and gives its file id. */
91
+ export declare function addToFiles(url: string, name: string): Promise<string>;
package/api.js ADDED
@@ -0,0 +1,79 @@
1
+ "use client";
2
+ import { apiPath } from "tansy/admin";
3
+ /** The roles the person may give the assistant, or the reason they can't be listed. */
4
+ export async function chatRoles() {
5
+ const response = await fetch(apiPath("/ext/ai/roles"), { cache: "no-store" }).catch(() => null);
6
+ const json = (await response?.json().catch(() => null));
7
+ if (response?.ok && Array.isArray(json?.options))
8
+ return { options: json.options };
9
+ return { error: json?.error?.message ?? "The roles couldn't be listed." };
10
+ }
11
+ /** The models of a provider, the site's when left out, the person may choose from in the chat, or the reason they can't be listed. */
12
+ export async function chatModels(provider) {
13
+ const response = await fetch(apiPath(`/ext/ai/chat/models${provider ? `?provider=${encodeURIComponent(provider)}` : ""}`), { cache: "no-store" }).catch(() => null);
14
+ const json = (await response?.json().catch(() => null));
15
+ if (response?.ok && Array.isArray(json?.options))
16
+ return { options: json.options };
17
+ return { error: json?.error?.message ?? "The models couldn't be listed." };
18
+ }
19
+ let status = null;
20
+ /** Whether this person may use AI and a provider is set up. Asked once per page load: every field's menu needs it. Null means they may not. */
21
+ export function aiStatus() {
22
+ status ??= fetch(apiPath("/ext/ai/status"), { cache: "no-store" })
23
+ .then(async (response) => (response.ok ? (await response.json()) : null))
24
+ .catch(() => null);
25
+ return status;
26
+ }
27
+ /** Posts to one of the plugin's routes and returns its JSON, or throws with the message the route gave. */
28
+ export async function post(path, body) {
29
+ const response = await fetch(apiPath(`/ext/ai${path}`), { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
30
+ const json = (await response.json().catch(() => null));
31
+ if (!response.ok)
32
+ throw new Error(json?.error?.message ?? "AI couldn't do that just now. Try again in a moment.");
33
+ return json;
34
+ }
35
+ /** The signed-in person's saved chats, most recent first. Empty when they can't be read. */
36
+ export async function listChats() {
37
+ const response = await fetch(apiPath("/ext/ai/chats"), { cache: "no-store" }).catch(() => null);
38
+ if (!response?.ok)
39
+ return [];
40
+ return (await response.json()).chats;
41
+ }
42
+ /** One saved chat with its messages, or null when it's gone. */
43
+ export async function loadChat(id) {
44
+ const response = await fetch(apiPath(`/ext/ai/chats/${encodeURIComponent(id)}`), { cache: "no-store" }).catch(() => null);
45
+ return response?.ok ? (await response.json()) : null;
46
+ }
47
+ export async function deleteChat(id) {
48
+ const response = await fetch(apiPath(`/ext/ai/chats/${encodeURIComponent(id)}`), { method: "DELETE" }).catch(() => null);
49
+ return Boolean(response?.ok);
50
+ }
51
+ /** What the chat's model reads, or null when it can't be told. */
52
+ export async function chatCapabilities(model, provider = null) {
53
+ const query = new URLSearchParams({ ...(model ? { model } : {}), ...(provider ? { provider } : {}) }).toString();
54
+ const response = await fetch(apiPath(`/ext/ai/chat/capabilities${query ? `?${query}` : ""}`), { cache: "no-store" }).catch(() => null);
55
+ return response?.ok ? (await response.json()) : null;
56
+ }
57
+ /** Keeps a file for the chat, for the signed-in person alone. */
58
+ export async function uploadAttachment(file, name, mediaType) {
59
+ const response = await fetch(apiPath(`/ext/ai/attachments?name=${encodeURIComponent(name)}`), { method: "POST", headers: { "content-type": mediaType }, body: file }).catch(() => null);
60
+ const json = (await response?.json().catch(() => null));
61
+ if (!response?.ok || !json?.id)
62
+ throw new Error(json?.error?.message ?? "The file couldn't be attached.");
63
+ return json;
64
+ }
65
+ export async function deleteAttachment(id) {
66
+ await fetch(apiPath(`/ext/ai/attachments/${encodeURIComponent(id)}`), { method: "DELETE" }).catch(() => null);
67
+ }
68
+ /** Copies a picture the assistant drew into the site's files, as the person, and gives its file id. */
69
+ export async function addToFiles(url, name) {
70
+ const picture = await fetch(url, { cache: "no-store" }).catch(() => null);
71
+ if (!picture?.ok)
72
+ throw new Error("The picture couldn't be read.");
73
+ const type = picture.headers.get("content-type") ?? "image/png";
74
+ const response = await fetch(apiPath(`/files/upload?name=${encodeURIComponent(name)}`), { method: "POST", headers: { "content-type": type }, body: await picture.blob() }).catch(() => null);
75
+ const json = (await response?.json().catch(() => null));
76
+ if (!response?.ok || !json?.id)
77
+ throw new Error(json?.error?.message ?? "The picture couldn't be added to the files.");
78
+ return json.id;
79
+ }
package/assistant.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import "./ai.css";
2
+ /** The assistant page: a chat that works on the CMS as the signed-in person, asking before every change. */
3
+ export declare function Assistant(): import("react").JSX.Element;