@skaile/forge-layer-chat 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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +171 -2
- package/app/app.config.ts +8 -0
- package/app/components/AssistantToggle.vue +35 -0
- package/app/components/ChatAssistantPanel.vue +203 -0
- package/app/components/ChatDrawer.vue +262 -0
- package/app/components/ChatThreadRenameModal.vue +83 -0
- package/app/composables/useAssistant.ts +32 -0
- package/app/composables/useAssistantBackends.ts +35 -0
- package/app/middleware/chat-links.global.ts +12 -0
- package/app/plugins/chat.ts +73 -0
- package/app/utils/chatMarkdown.ts +49 -0
- package/nuxt.config.ts +14 -0
- package/package.json +50 -4
- package/server/api/chat.post.ts +8 -0
- package/server/plugins/chat.ts +11 -0
- package/server/utils/assistant-settings.ts +27 -0
- package/server/utils/chat-actions.ts +147 -0
- package/server/utils/chat-assistant.ts +189 -0
- package/server/utils/chat-threads.ts +149 -0
- package/server/utils/tool-schema.ts +56 -0
- package/shared/chat-context.ts +40 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @skaile/forge-layer-chat
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 064fbe0: New package: the chat layer of forge-template-app as a versioned Nuxt layer (`extends: ['@skaile/forge-layer-chat']`) instead of a copy in each app. The in-app assistant: a forge-chat drawer on every page answering with the app's `llm` actions as tools, persisted threads, the `assistant` model role and settings, and the assistant-backend registry other layers plug into. Compared with the template's copy, the route context is generic (`{ path, entity?, id? }` on any `/<entity>/<uuid>` page instead of `projectId`), the panel texts come from app config `assistant.placeholder` / `assistant.emptyHint`, and tool input schemas go to the model as JSON Schema draft 2020-12 (Anthropic rejects draft-07 tuples). Host contract in the README.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthias Ortegro
|
|
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,172 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @skaile/forge-layer-chat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Nuxt layer for apps built on [forge-template-app](https://github.com/skaile-ai/forge/tree/main/templates/app):
|
|
4
|
+
the in-app assistant. A forge-chat panel in a resizable right-hand drawer on every page, answering with the app's own
|
|
5
|
+
server actions as tools, with the signed-in person's rights. Threads are stored in the core's `chat_threads` /
|
|
6
|
+
`chat_messages`. The model is the `assistant` model role, chosen in Settings › Harness. It ships as source; the app
|
|
7
|
+
`extends` it.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add @skaile/forge-layer-chat @skaile/forge-chat marked
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// nuxt.config.ts
|
|
17
|
+
export default defineNuxtConfig({
|
|
18
|
+
extends: ['@skaile/forge-layer-chat'],
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```css
|
|
23
|
+
/* app/assets/css/main.css: Tailwind skips node_modules, so name the layer's sources */
|
|
24
|
+
@source "../../../node_modules/@skaile/forge-layer-chat/app";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Remove the `extends` entry (and the `@source` line) to drop it; the app does not import from the layer, the shell just gets no extension.
|
|
28
|
+
|
|
29
|
+
## Host contract
|
|
30
|
+
|
|
31
|
+
The layer runs on the core of forge-template-app and uses these parts of it by name. An app copied from the template
|
|
32
|
+
has all of them; an app that renamed or removed one overrides the layer file that uses it.
|
|
33
|
+
|
|
34
|
+
| Kind | Used | Where in the template |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| Server imports (`~~/`) | `server/db/schema` (`chatThreads`, `chatMessages`), `server/utils/actions/define` (`defineAppAction`, `AppActionContext`), `server/utils/actions/registry` (`registerActions`, `allActions`), `server/utils/ai/roles` (`registerModelRole`, `resolveRoleModel`, `RoleNotConfigured`), `server/utils/auth` (`requirePrincipal`), `server/utils/db` (`useDb`, `withTx`), `server/utils/principal` (`requireScope`, `Principal`), `server/utils/settings` (`defineSettingsScope`, `appSettings`, `getSettings`) | `server/` |
|
|
37
|
+
| Composables, utils | `useActions`, `actionErrorMessage`, `useConfirm`, `useDates`, `useFormDirty`, `applyServerError`, `missingFields` | `app/composables/`, `app/utils/` |
|
|
38
|
+
| Components | `FormModal` | `app/components/form/` |
|
|
39
|
+
| Shell | hook `app-shell:extend` (`shell.commands`, `shell.header`, `shell.drawers`, `shell.settingsSections`) | `app/types/app-shell.d.ts`, `app/layouts/default.vue` |
|
|
40
|
+
| Actions | `ai.status` (role `assistant`) and every `llm` action as a tool | `server/utils/actions/` |
|
|
41
|
+
| CSS | `@skaile/forge-chat/styles/chat.css` and `@source` lines for the forge-chat and forge-settings `dist` | `app/assets/css/main.css` |
|
|
42
|
+
|
|
43
|
+
App config `assistant.placeholder` and `assistant.emptyHint` set the panel's texts (defaults are generic; the template
|
|
44
|
+
names its projects and tasks).
|
|
45
|
+
|
|
46
|
+
Routes: the context of a turn names the first segment of a `/<segment>/<uuid>` page as its entity (`/projects/<id>` →
|
|
47
|
+
entity `projects`), so a record page should sit at the entity's name; on other routes only the path goes along.
|
|
48
|
+
|
|
49
|
+
Peers: `nuxt ^4`, `@nuxt/ui ^4.11`, `vue ^3.5`, `@skaile/forge-chat ^0.6`, `@skaile/forge-app-actions ^0.6`,
|
|
50
|
+
`@skaile/forge-settings ^2`, `ai ^7`, `zod ^4`, `drizzle-orm ^0.45`, `h3 ^1.15`, `marked ^15`, `@vueuse/core ^15`.
|
|
51
|
+
|
|
52
|
+
## What you get
|
|
53
|
+
|
|
54
|
+
| Where | What |
|
|
55
|
+
|---|---|
|
|
56
|
+
| Header | `AssistantToggle` (sparkles) is the rightmost button of every page header and opens the drawer; while the drawer is open the header hides it and the drawer's header shows it in the same top-right spot as the close button (`in-drawer`). ⌘J and the palette entry "Ask the assistant" (⌘K) do the same |
|
|
57
|
+
| Drawer | `ChatDrawer`: a right `UDashboardSidebar` (`id="assistant"`), resizable 20–48rem (default 28rem); width and desktop open state persist in localStorage under `skaile-app-sidebar-assistant` (`{ size, collapsed }`), applied after mount. Header: thread picker (switch, rename, delete), New conversation, `AssistantToggle` as close. Body: `ChatAssistantPanel`. Below `lg` it opens as a slideover that always starts closed and never changes the stored desktop state |
|
|
58
|
+
| Old links | `/chat` and `/chat/<id>` redirect to `/?assistant=<id>`, which opens the drawer on that thread (`app/middleware/chat-links.global.ts`) |
|
|
59
|
+
| Settings › Harness, Settings › Agents | Both pages are core; this layer adds the model role `assistant` (its model is picked in Harness) and, in Agents, the card "In-app assistant" (`assistant` settings scope: system prompt addendum, max steps) through `shell.settingsSections` |
|
|
60
|
+
| `POST /api/chat` | forge-chat AI SDK handler (`createAiSdkChatHandler`), SSE |
|
|
61
|
+
| Actions | `chat.threads.list/create/get/rename/delete` (audience `user`, so never chat/MCP tools). Whether the assistant can answer is `ai.status` (role `assistant`) |
|
|
62
|
+
|
|
63
|
+
## Overrides
|
|
64
|
+
|
|
65
|
+
A Nuxt layer is overridden piece by piece; the app's piece wins, the rest keeps coming from the package, so fixes in
|
|
66
|
+
the other files still arrive with updates:
|
|
67
|
+
|
|
68
|
+
- a component: a file with the same name in the app's `app/components/` (`AssistantToggle` and `ChatDrawer` too: the
|
|
69
|
+
plugin registers them by name)
|
|
70
|
+
- a page: a page at the same route in the app's `app/pages/`
|
|
71
|
+
- an auto-imported composable or util: an export with the same name in the app's `app/composables/` or `app/utils/`
|
|
72
|
+
(override the whole file: the app's file replaces every export of that name)
|
|
73
|
+
- `app/app.config.ts`: the app's own `app.config.ts` is merged over the layer's, so set only the keys you change
|
|
74
|
+
(e.g. `assistant.placeholder` and `assistant.emptyHint`, the panel's texts). Arrays such as `appShell.nav` are
|
|
75
|
+
joined, the app's entries first; an app entry with the same `to` replaces the layer's when the app's `useAppShell`
|
|
76
|
+
keeps the first entry per route, as forge-template-app's does (`firstPerRoute`)
|
|
77
|
+
|
|
78
|
+
Server code (`server/`) and `shared/` are imported by path inside the package and cannot be overridden this way; a
|
|
79
|
+
change there belongs in the package.
|
|
80
|
+
|
|
81
|
+
## How a turn runs
|
|
82
|
+
|
|
83
|
+
1. The browser posts `{ threadId, context, messages, input }`. `context` is
|
|
84
|
+
the current route (`shared/chat-context.ts`: path ≤ 200 chars; on a
|
|
85
|
+
`/<entity>/<uuid>` page also `entity` and `id`, e.g. `/projects/<id>`;
|
|
86
|
+
anything else falls back to `{ path: '/' }`).
|
|
87
|
+
2. `requirePrincipal(event)` (401 otherwise), the thread must be the caller's
|
|
88
|
+
(404 otherwise).
|
|
89
|
+
3. Model: `resolveRoleModel('assistant')` (`server/utils/ai/roles.ts`): the
|
|
90
|
+
registry selector assigned to the role in Settings › Harness
|
|
91
|
+
(`<provider>/<model>`, resolved by `@skaile/forge-settings/ai-sdk`), else the
|
|
92
|
+
env fallback `NUXT_AI_API_KEY` + `NUXT_AI_MODEL` (`NUXT_AI_BASE_URL`, default
|
|
93
|
+
OpenRouter; OpenAI-compatible). Neither: `503 { error: 'assistant_not_configured', reason }`,
|
|
94
|
+
which the panel shows as "Set up a model" with a link to Settings › Harness.
|
|
95
|
+
Resolved per request: a change needs no restart.
|
|
96
|
+
4. Tools: `serverActionToolSet(forAudience(allActions(), 'llm'), { principal, event })`,
|
|
97
|
+
built per request. Every call runs the action's own `authorize`, so the
|
|
98
|
+
model can never do more than the person. Data comes through the six
|
|
99
|
+
`app_data_*` tools (`{ entity, ... }`); destructive actions (`data.delete`)
|
|
100
|
+
are `['user']` and are not tools. Input schemas go out as JSON Schema
|
|
101
|
+
draft 2020-12 (`server/utils/tool-schema.ts`): the catalogue emits
|
|
102
|
+
draft-07 for MCP, and Anthropic rejects a request whose tool schema is
|
|
103
|
+
not 2020-12 (zod tuples differ).
|
|
104
|
+
5. System prompt: the app name (`app` settings), the route context, the
|
|
105
|
+
effect tools by name, the rule "state the change, then act", plus the
|
|
106
|
+
owner's addendum.
|
|
107
|
+
6. Persistence: the user turn is stored when the handler accepted the
|
|
108
|
+
request; the assistant's turn (deltas joined, activity dropped) when its
|
|
109
|
+
stream ended, registered with `event.waitUntil`. A thread replays through
|
|
110
|
+
the transport's `history()` (events from `chat.threads.get`).
|
|
111
|
+
|
|
112
|
+
## Assistant backends (contract for other layers)
|
|
113
|
+
|
|
114
|
+
`app/composables/useAssistantBackends.ts`:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
registerAssistantBackend({
|
|
118
|
+
id: 'workspace', // stored as chat_threads.transport
|
|
119
|
+
label: 'Workspace agent',
|
|
120
|
+
icon: 'i-lucide-bot',
|
|
121
|
+
status: async () => ({ available: true }),
|
|
122
|
+
createTransport: ({ threadId }) => myTransport(threadId) // forge-chat ChatTransport
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The built-in backend is `model` (AI SDK transport to `/api/chat`), registered
|
|
127
|
+
by `app/plugins/chat.ts`. The panel shows a switcher only when more than one
|
|
128
|
+
backend is registered and available; a new thread is created with
|
|
129
|
+
`chat.threads.create { transport: <backend id> }`.
|
|
130
|
+
|
|
131
|
+
Server utils for other layers (`server/utils/chat-threads.ts`):
|
|
132
|
+
`createThread(userId, { transport })`, `appendThreadEvents(threadId, events)`,
|
|
133
|
+
`loadThreadEvents(threadId)`, `listThreads(userId)`, `requireOwnThread(threadId, userId)`.
|
|
134
|
+
|
|
135
|
+
## Shell integration
|
|
136
|
+
|
|
137
|
+
The layer extends the shell through the core hook `app-shell:extend`
|
|
138
|
+
(`app/types/app-shell.d.ts`, `app/plugins/chat.ts`): the palette group,
|
|
139
|
+
`AssistantToggle` in `shell.header` and `ChatDrawer` in `shell.drawers`.
|
|
140
|
+
The layout renders the drawers inside its `UDashboardGroup`, after the page.
|
|
141
|
+
Nuxt UI ties every sidebar of a group to the group's collapse and mobile
|
|
142
|
+
toggle, so `ChatDrawer` provides its own dashboard context and is not
|
|
143
|
+
`collapsible`: the left sidebar's buttons never move it. The open state is
|
|
144
|
+
`useAssistant().open`; on desktop the drawer keeps it in the storage entry's `collapsed` (phones neither read nor write it).
|
|
145
|
+
The layer's server plugin registers the role (`registerModelRole`) and its
|
|
146
|
+
client plugin pushes its card (role and scope `assistant`) to `shell.settingsSections`
|
|
147
|
+
on `/settings/agents`; the pages and their nav entries are core.
|
|
148
|
+
|
|
149
|
+
## Model role and settings
|
|
150
|
+
|
|
151
|
+
`assistant` is a `chat` role (`ASSISTANT_ROLE` in
|
|
152
|
+
`server/utils/chat-assistant.ts`). The assignment lives in core's `ai` settings
|
|
153
|
+
scope; `assistant` settings only hold the prompt addendum and max steps. The
|
|
154
|
+
provider registry (`.data/ai/.skaile/providers.yml`) and the keys
|
|
155
|
+
(`.data/ai/.env`, mode 0600) are core's too, under `AI_PROVIDERS_DIR` (default
|
|
156
|
+
`.data/ai`); see `server/utils/ai/providers.ts` for the credential policy
|
|
157
|
+
(`env:` references named `AI_PROVIDER_<NAME>` or `NUXT_AI_API_KEY` only).
|
|
158
|
+
|
|
159
|
+
## Environment
|
|
160
|
+
|
|
161
|
+
| Variable | Purpose |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `NUXT_AI_API_KEY`, `NUXT_AI_MODEL` | Fallback model while the `assistant` role has no assignment (e.g. `openai/gpt-4.1-mini` on OpenRouter) |
|
|
164
|
+
| `NUXT_AI_BASE_URL` | OpenAI-compatible base URL of that fallback, default `https://openrouter.ai/api/v1` |
|
|
165
|
+
| `AI_PROVIDERS_DIR` | Registry + key file directory, default `.data/ai` |
|
|
166
|
+
|
|
167
|
+
## Tests
|
|
168
|
+
|
|
169
|
+
The layer needs the host's database and actions, so its tests live in the template (`templates/app/test/chat.test.ts`,
|
|
170
|
+
vitest, in-memory PGlite: thread order and ownership, empty-thread reuse, the tool set, 503 without a model, and a full
|
|
171
|
+
turn with a mock model) and run against the installed package. `bun scripts/check-templates.ts --local app` checks an
|
|
172
|
+
unpublished change of this package against the template.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default defineAppConfig({
|
|
2
|
+
// Texts of the assistant panel; an app sets its own in its `app.config.ts`
|
|
3
|
+
// (e.g. examples that name its entities).
|
|
4
|
+
assistant: {
|
|
5
|
+
placeholder: 'Ask a question, or tell me what to change…',
|
|
6
|
+
emptyHint: 'Ask a question about your data or give an instruction.',
|
|
7
|
+
},
|
|
8
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The assistant's one button, always in the top-right corner. Closed, it is the
|
|
4
|
+
* rightmost widget of every page header (`shell.header`) and opens the
|
|
5
|
+
* right-hand drawer (`ChatDrawer`), like ⌘J. Open, the header hides it and the
|
|
6
|
+
* drawer shows it (`in-drawer`) as its close button in the same corner, so the
|
|
7
|
+
* button stays where it was when the drawer opens.
|
|
8
|
+
*/
|
|
9
|
+
const props = defineProps<{
|
|
10
|
+
/** Rendered in the drawer's header as its close button. */
|
|
11
|
+
inDrawer?: boolean
|
|
12
|
+
}>()
|
|
13
|
+
|
|
14
|
+
const { open, toggle } = useAssistant()
|
|
15
|
+
const shown = computed(() => props.inDrawer || !open.value)
|
|
16
|
+
const label = computed(() => (open.value ? 'Close the assistant' : 'Ask the assistant'))
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<template>
|
|
20
|
+
<UTooltip
|
|
21
|
+
v-if="shown"
|
|
22
|
+
:text="label"
|
|
23
|
+
:kbds="['meta', 'J']"
|
|
24
|
+
>
|
|
25
|
+
<UButton
|
|
26
|
+
icon="i-lucide-sparkles"
|
|
27
|
+
color="neutral"
|
|
28
|
+
:variant="open ? 'soft' : 'ghost'"
|
|
29
|
+
:aria-label="label"
|
|
30
|
+
:aria-pressed="open"
|
|
31
|
+
:data-testid="inDrawer ? 'chat-close' : 'assistant-toggle'"
|
|
32
|
+
@click="toggle"
|
|
33
|
+
/>
|
|
34
|
+
</UTooltip>
|
|
35
|
+
</template>
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { ChatSession } from '@skaile/forge-chat'
|
|
3
|
+
import { createChatSession } from '@skaile/forge-chat'
|
|
4
|
+
import { ChatSessionPanel } from '@skaile/forge-chat/vue'
|
|
5
|
+
import type { PanelThread } from '../composables/useAssistant'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* One conversation with an assistant backend. `thread` null: the panel opens
|
|
9
|
+
* a (reused empty) thread of the preferred backend and reports it through
|
|
10
|
+
* `update:thread`. Shows a backend switcher only when more than one backend
|
|
11
|
+
* is registered and available, and a set-up state instead of a dead composer
|
|
12
|
+
* when the backend cannot answer.
|
|
13
|
+
*/
|
|
14
|
+
const props = defineProps<{ thread: PanelThread | null }>()
|
|
15
|
+
const emit = defineEmits<{ 'update:thread': [thread: PanelThread | null]; 'turn-end': [] }>()
|
|
16
|
+
|
|
17
|
+
const client = useChatClient()
|
|
18
|
+
const toast = useToast()
|
|
19
|
+
const { backends, find } = useAssistantBackends()
|
|
20
|
+
const preferred = useState('chat:backend', () => 'model')
|
|
21
|
+
const texts = useAppConfig().assistant
|
|
22
|
+
|
|
23
|
+
type Status = { available: boolean; reason?: string }
|
|
24
|
+
const statuses = ref<Record<string, Status>>({})
|
|
25
|
+
const checked = ref(false)
|
|
26
|
+
|
|
27
|
+
async function checkStatus(id: string): Promise<Status> {
|
|
28
|
+
const backend = find(id)
|
|
29
|
+
const status: Status = backend
|
|
30
|
+
? await backend.status().catch((error: unknown) => ({ available: false, reason: actionErrorMessage(error) }))
|
|
31
|
+
: { available: false, reason: `No assistant backend "${id}" in this app` }
|
|
32
|
+
statuses.value = { ...statuses.value, [id]: status }
|
|
33
|
+
return status
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const available = computed(() => backends.value.filter(backend => statuses.value[backend.id]?.available))
|
|
37
|
+
const backendId = computed(() => props.thread?.transport ?? preferred.value)
|
|
38
|
+
const backend = computed(() => find(backendId.value))
|
|
39
|
+
const status = computed(() => statuses.value[backendId.value])
|
|
40
|
+
const switcherItems = computed(() => available.value.map(b => ({ label: b.label, value: b.id, icon: b.icon })))
|
|
41
|
+
|
|
42
|
+
const session = shallowRef<ChatSession | null>(null)
|
|
43
|
+
const loading = ref(false)
|
|
44
|
+
let unsubscribe: (() => void) | undefined
|
|
45
|
+
|
|
46
|
+
function closeSession() {
|
|
47
|
+
unsubscribe?.()
|
|
48
|
+
unsubscribe = undefined
|
|
49
|
+
const current = session.value
|
|
50
|
+
session.value = null
|
|
51
|
+
if (current) void current.abort().finally(() => current.dispose())
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function openThread(thread: PanelThread) {
|
|
55
|
+
closeSession()
|
|
56
|
+
if (!statuses.value[thread.transport]) await checkStatus(thread.transport)
|
|
57
|
+
const chosen = find(thread.transport)
|
|
58
|
+
if (!chosen || !statuses.value[thread.transport]?.available) return
|
|
59
|
+
const next = createChatSession({
|
|
60
|
+
transport: chosen.createTransport({ threadId: thread.id }),
|
|
61
|
+
onEvent: event => {
|
|
62
|
+
if (event.type === 'turn-end') emit('turn-end')
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
loading.value = true
|
|
66
|
+
try {
|
|
67
|
+
await next.load()
|
|
68
|
+
} catch (error) {
|
|
69
|
+
toast.add({ title: 'Could not load the conversation', description: actionErrorMessage(error), color: 'error' })
|
|
70
|
+
} finally {
|
|
71
|
+
loading.value = false
|
|
72
|
+
}
|
|
73
|
+
// Another thread was selected while this one loaded.
|
|
74
|
+
if (props.thread?.id !== thread.id) {
|
|
75
|
+
next.dispose()
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
// A failed turn may mean the backend went away (model removed in Settings › Harness): re-check it.
|
|
79
|
+
unsubscribe = next.state.subscribe(state => {
|
|
80
|
+
if (state.error && state.phase === 'idle') void checkStatus(thread.transport)
|
|
81
|
+
})
|
|
82
|
+
session.value = next
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function ensureThread() {
|
|
86
|
+
if (props.thread) return openThread(props.thread)
|
|
87
|
+
if (!statuses.value[backendId.value]?.available) return
|
|
88
|
+
try {
|
|
89
|
+
const thread = await client.call('chat.threads.create', { transport: backendId.value as 'model' | 'workspace' })
|
|
90
|
+
emit('update:thread', { id: thread.id, transport: thread.transport })
|
|
91
|
+
} catch (error) {
|
|
92
|
+
toast.add({ title: 'Could not start a conversation', description: actionErrorMessage(error), color: 'error' })
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
onMounted(async () => {
|
|
97
|
+
await Promise.all(backends.value.map(b => checkStatus(b.id)))
|
|
98
|
+
checked.value = true
|
|
99
|
+
// The preferred backend went away (layer removed, agent not set up): fall back to the first one that answers.
|
|
100
|
+
if (!props.thread && !statuses.value[preferred.value]?.available && available.value[0])
|
|
101
|
+
preferred.value = available.value[0].id
|
|
102
|
+
await ensureThread()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
watch(
|
|
106
|
+
() => props.thread?.id,
|
|
107
|
+
() => {
|
|
108
|
+
if (checked.value) void ensureThread()
|
|
109
|
+
},
|
|
110
|
+
)
|
|
111
|
+
watch(
|
|
112
|
+
() => status.value?.available,
|
|
113
|
+
(now, before) => {
|
|
114
|
+
if (now && before === false) void ensureThread()
|
|
115
|
+
},
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
onBeforeUnmount(closeSession)
|
|
119
|
+
|
|
120
|
+
function switchBackend(id: string) {
|
|
121
|
+
if (id === backendId.value) return
|
|
122
|
+
preferred.value = id
|
|
123
|
+
emit('update:thread', null)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function retry() {
|
|
127
|
+
await checkStatus(backendId.value)
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<template>
|
|
132
|
+
<div class="flex min-h-0 flex-1 flex-col">
|
|
133
|
+
<div
|
|
134
|
+
v-if="switcherItems.length > 1"
|
|
135
|
+
class="flex items-center gap-2 border-b border-default px-4 py-2"
|
|
136
|
+
data-testid="chat-backend-switcher"
|
|
137
|
+
>
|
|
138
|
+
<span class="text-xs text-muted">Assistant</span>
|
|
139
|
+
<USelect
|
|
140
|
+
:model-value="backendId"
|
|
141
|
+
:items="switcherItems"
|
|
142
|
+
value-key="value"
|
|
143
|
+
size="sm"
|
|
144
|
+
class="w-56"
|
|
145
|
+
@update:model-value="(id: string) => switchBackend(id)"
|
|
146
|
+
/>
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
<div
|
|
150
|
+
v-if="!checked"
|
|
151
|
+
class="flex flex-1 items-center justify-center"
|
|
152
|
+
>
|
|
153
|
+
<UIcon
|
|
154
|
+
name="i-lucide-loader-circle"
|
|
155
|
+
class="size-5 animate-spin text-muted"
|
|
156
|
+
/>
|
|
157
|
+
</div>
|
|
158
|
+
|
|
159
|
+
<UEmpty
|
|
160
|
+
v-else-if="!status?.available"
|
|
161
|
+
data-testid="chat-setup"
|
|
162
|
+
:icon="backendId === 'model' ? 'i-lucide-plug-zap' : 'i-lucide-circle-off'"
|
|
163
|
+
:title="backendId === 'model' ? 'Set up a model' : `${backend?.label ?? backendId} is not available`"
|
|
164
|
+
:description="status?.reason ?? 'This assistant cannot answer right now.'"
|
|
165
|
+
:actions="[
|
|
166
|
+
...(backendId === 'model' ? [{ label: 'Open Settings › Harness', icon: 'i-lucide-settings', to: '/settings/harness' }] : []),
|
|
167
|
+
{ label: 'Check again', icon: 'i-lucide-refresh-cw', color: 'neutral' as const, variant: 'subtle' as const, onClick: retry }
|
|
168
|
+
]"
|
|
169
|
+
class="m-4 flex-1"
|
|
170
|
+
/>
|
|
171
|
+
|
|
172
|
+
<div
|
|
173
|
+
v-else-if="loading || !session"
|
|
174
|
+
class="flex flex-1 items-center justify-center"
|
|
175
|
+
>
|
|
176
|
+
<UIcon
|
|
177
|
+
name="i-lucide-loader-circle"
|
|
178
|
+
class="size-5 animate-spin text-muted"
|
|
179
|
+
/>
|
|
180
|
+
</div>
|
|
181
|
+
|
|
182
|
+
<ChatSessionPanel
|
|
183
|
+
v-else
|
|
184
|
+
:session="session"
|
|
185
|
+
:render-markdown="chatMarkdown"
|
|
186
|
+
:placeholder="texts.placeholder"
|
|
187
|
+
class="min-h-0 flex-1"
|
|
188
|
+
>
|
|
189
|
+
<template #empty>
|
|
190
|
+
<div class="mx-auto flex max-w-sm flex-col items-center gap-2 py-10 text-center text-sm text-muted">
|
|
191
|
+
<UIcon
|
|
192
|
+
:name="backend?.icon ?? 'i-lucide-sparkles'"
|
|
193
|
+
class="size-6 text-primary"
|
|
194
|
+
/>
|
|
195
|
+
<p>{{ texts.emptyHint }}</p>
|
|
196
|
+
<p class="text-xs">
|
|
197
|
+
The assistant acts with your rights; deleting stays with you.
|
|
198
|
+
</p>
|
|
199
|
+
</div>
|
|
200
|
+
</template>
|
|
201
|
+
</ChatSessionPanel>
|
|
202
|
+
</div>
|
|
203
|
+
</template>
|