|
|
@@ -1,7 +1,7 @@
|
|
|
import type { PaasProjectBackup } from "@/paas"
|
|
|
import type { ArtifactEntry } from "./artifact-library"
|
|
|
import { For, Show, createEffect, createMemo, createSignal, on, onCleanup, onMount, untrack } from "solid-js"
|
|
|
-import { createStore } from "solid-js/store"
|
|
|
+import { createStore, reconcile } from "solid-js/store"
|
|
|
import { Portal } from "solid-js/web"
|
|
|
import { ResearchReport } from "./report"
|
|
|
import { exportPluginAnswer } from "@/plugins/runtime"
|
|
|
@@ -197,6 +197,139 @@ export function PaasChat(props: {
|
|
|
() => setUI("agentOverride", ""),
|
|
|
),
|
|
|
)
|
|
|
+
|
|
|
+ function showAgentPicker(trigger: HTMLElement, editing = false) {
|
|
|
+ dialog.show(() => (
|
|
|
+ <Dialog
|
|
|
+ class="paas-agent-picker-dialog"
|
|
|
+ title={editing ? "选择要编辑的智能体" : "选择执行角色"}
|
|
|
+ onCloseAutoFocus={(event) => {
|
|
|
+ event.preventDefault()
|
|
|
+ trigger.focus()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <div class="paas-agent-picker">
|
|
|
+ <p class="paas-muted">
|
|
|
+ {editing
|
|
|
+ ? "自动匹配会随任务目标选择角色,请明确选择要修改配置的智能体。"
|
|
|
+ : blank()
|
|
|
+ ? "自动匹配负责选择角色;工作区助手处理项目文件和代码。也可在此指定角色。"
|
|
|
+ : "当前会话沿用原智能体。选择其他角色会新建会话,原记录与草稿保留。"}
|
|
|
+ </p>
|
|
|
+ <div
|
|
|
+ role="listbox"
|
|
|
+ aria-label="执行角色"
|
|
|
+ onKeyDown={(event) => {
|
|
|
+ if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return
|
|
|
+ event.preventDefault()
|
|
|
+ const options = [...event.currentTarget.querySelectorAll<HTMLButtonElement>("[role=option]")]
|
|
|
+ const index = options.indexOf(document.activeElement as HTMLButtonElement)
|
|
|
+ options[
|
|
|
+ event.key === "Home"
|
|
|
+ ? 0
|
|
|
+ : event.key === "End"
|
|
|
+ ? options.length - 1
|
|
|
+ : (index + (event.key === "ArrowDown" ? 1 : -1) + options.length) % options.length
|
|
|
+ ]?.focus()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <Show when={blank() && !editing}>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ role="option"
|
|
|
+ autofocus={!ui.agentOverride}
|
|
|
+ aria-selected={!ui.agentOverride}
|
|
|
+ onClick={() => {
|
|
|
+ setUI("agentOverride", "")
|
|
|
+ dialog.close()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <strong>自动匹配</strong>
|
|
|
+ <small>{routed().reason}</small>
|
|
|
+ </button>
|
|
|
+ </Show>
|
|
|
+ <For each={directAgents(props.agents)}>
|
|
|
+ {(agent) => (
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ role="option"
|
|
|
+ autofocus={
|
|
|
+ editing
|
|
|
+ ? agent === directAgents(props.agents)[0]
|
|
|
+ : blank()
|
|
|
+ ? ui.agentOverride === agent.id
|
|
|
+ : current()?.agentID === agent.id
|
|
|
+ }
|
|
|
+ aria-selected={blank() ? ui.agentOverride === agent.id : current()?.agentID === agent.id}
|
|
|
+ onClick={() => {
|
|
|
+ if (editing) {
|
|
|
+ dialog.close()
|
|
|
+ props.onEditAgent?.(paasText(agent.id))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (!blank()) model.choose(agent, true)
|
|
|
+ model.route(paasText(agent.id))
|
|
|
+ setUI("agentOverride", paasText(agent.id))
|
|
|
+ dialog.close()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <strong>{paasText(agent.name)}</strong>
|
|
|
+ <small>{resolveAgentProfile(agent).summary}</small>
|
|
|
+ </button>
|
|
|
+ )}
|
|
|
+ </For>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </Dialog>
|
|
|
+ ))
|
|
|
+ }
|
|
|
+ function editAgent(event?: MouseEvent & { currentTarget: HTMLButtonElement }) {
|
|
|
+ if (blank() && !overrideAgent()) {
|
|
|
+ showAgentPicker(event?.currentTarget || (document.activeElement as HTMLElement), true)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const agent = activeAgent()
|
|
|
+ if (agent) props.onEditAgent?.(paasText(agent.id))
|
|
|
+ }
|
|
|
+ function archiveChat(event: MouseEvent & { currentTarget: HTMLButtonElement }) {
|
|
|
+ const chat = current()
|
|
|
+ if (!chat) return
|
|
|
+ if (chat.archived) return model.archive(chat.id)
|
|
|
+ const trigger = event.currentTarget
|
|
|
+ dialog.show(() => (
|
|
|
+ <Dialog
|
|
|
+ title="归档此会话?"
|
|
|
+ fit
|
|
|
+ onCloseAutoFocus={(event) => {
|
|
|
+ event.preventDefault()
|
|
|
+ if (trigger.isConnected) trigger.focus()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <div class="paas-archive-confirm">
|
|
|
+ <p>
|
|
|
+ <strong>{chat.title || "当前会话"}</strong> 将移到本机归档,仍可查看和恢复。
|
|
|
+ </p>
|
|
|
+ <p class="paas-muted">归档不会删除记录,也不会停止正在执行的任务。</p>
|
|
|
+ <div class="paas-archive-confirm-actions">
|
|
|
+ <button type="button" onClick={() => dialog.close()}>
|
|
|
+ 取消
|
|
|
+ </button>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ class="paas-primary"
|
|
|
+ onClick={() => {
|
|
|
+ model.archive(chat.id)
|
|
|
+ dialog.close()
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ 确认归档
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </Dialog>
|
|
|
+ ))
|
|
|
+ }
|
|
|
+
|
|
|
const currentTitle = createMemo(() => current()?.title || paasText(currentAgent()?.name) || "λ-Agent")
|
|
|
createEffect(
|
|
|
on(
|
|
|
@@ -379,6 +512,43 @@ export function PaasChat(props: {
|
|
|
if (!props.api || !props.connected) return
|
|
|
props.agents.forEach((agent) => void history(paasText(agent.id)))
|
|
|
}
|
|
|
+ const isDelegated = (thread: Record<string, unknown>) =>
|
|
|
+ thread.delegated === true || ["code", "paas"].includes(paasText(paasRecord(thread.origin)?.kind))
|
|
|
+ // A child may appear after the first history load, or acquire provenance when
|
|
|
+ // submission finishes. Refresh across agents, not just the active role.
|
|
|
+ createEffect(
|
|
|
+ on(
|
|
|
+ () =>
|
|
|
+ [
|
|
|
+ props.connected,
|
|
|
+ props.visible,
|
|
|
+ current()
|
|
|
+ ?.messages.map((message) => `${message.runID}:${message.status}`)
|
|
|
+ .join("|"),
|
|
|
+ props
|
|
|
+ .inboxTasks()
|
|
|
+ .map((run) => `${run.run_id}:${run.status}`)
|
|
|
+ .join("|"),
|
|
|
+ ].join(";"),
|
|
|
+ () => {
|
|
|
+ if (!props.connected || !props.visible) return
|
|
|
+ const timer = setTimeout(refreshHistory, 200)
|
|
|
+ onCleanup(() => clearTimeout(timer))
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ onMount(() => {
|
|
|
+ const timer = setInterval(() => {
|
|
|
+ if (
|
|
|
+ props.connected &&
|
|
|
+ props.visible &&
|
|
|
+ !document.hidden &&
|
|
|
+ state.chats.some((chat) => chat.messages.some(working))
|
|
|
+ )
|
|
|
+ refreshHistory()
|
|
|
+ }, 10000)
|
|
|
+ onCleanup(() => clearInterval(timer))
|
|
|
+ })
|
|
|
async function history(agentID: string) {
|
|
|
if (!props.api || !props.connected || ui.loading[agentID]) return
|
|
|
const epoch = life.epoch
|
|
|
@@ -386,14 +556,14 @@ export function PaasChat(props: {
|
|
|
try {
|
|
|
const rows = paasRows(await props.api.request({ action: "threads", agentID }), "threads")
|
|
|
if (life.active && epoch === life.epoch) {
|
|
|
- setUI("threads", agentID, rows)
|
|
|
+ setUI("threads", agentID, reconcile(rows, { key: "id" }))
|
|
|
for (const row of rows) {
|
|
|
- if (typeof row.delegated === "boolean")
|
|
|
+ if (typeof row.delegated === "boolean" || isDelegated(row))
|
|
|
setState(
|
|
|
"chats",
|
|
|
(chat) => chat.agentID === agentID && chat.threadID === row.id,
|
|
|
"delegated",
|
|
|
- row.delegated,
|
|
|
+ isDelegated(row),
|
|
|
)
|
|
|
}
|
|
|
model.save()
|
|
|
@@ -544,6 +714,23 @@ export function PaasChat(props: {
|
|
|
if (life.active && epoch === life.epoch) setUI("exporting", "")
|
|
|
}
|
|
|
}
|
|
|
+ const childThreads = createMemo(
|
|
|
+ (previous: { agentID: string; thread: Record<string, unknown> }[]) =>
|
|
|
+ Object.entries(ui.threads).flatMap(([agentID, threads]) =>
|
|
|
+ threads
|
|
|
+ .filter(
|
|
|
+ (thread) =>
|
|
|
+ current()?.threadID &&
|
|
|
+ paasRecord(thread.origin)?.kind === "paas" &&
|
|
|
+ paasRecord(thread.origin)?.session_id === current()?.threadID,
|
|
|
+ )
|
|
|
+ .map(
|
|
|
+ (thread) =>
|
|
|
+ previous.find((item) => item.agentID === agentID && item.thread === thread) || { agentID, thread },
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ [],
|
|
|
+ )
|
|
|
const matches = (title: string) => title.toLocaleLowerCase().includes(ui.search.trim().toLocaleLowerCase())
|
|
|
const sessionGroups = createMemo(() =>
|
|
|
props.agents.flatMap((agent) => {
|
|
|
@@ -552,7 +739,7 @@ export function PaasChat(props: {
|
|
|
(chat) =>
|
|
|
chat.agentID === agent.id &&
|
|
|
!chat.delegated &&
|
|
|
- !(ui.threads[id] || []).some((thread) => thread.id === chat.threadID && thread.delegated === true) &&
|
|
|
+ !(ui.threads[id] || []).some((thread) => thread.id === chat.threadID && isDelegated(thread)) &&
|
|
|
chat.projectID === props.projectID &&
|
|
|
!!chat.archived === ui.archived &&
|
|
|
hasConversationHistory(chat) &&
|
|
|
@@ -561,7 +748,7 @@ export function PaasChat(props: {
|
|
|
const threads = (props.projectID === "project-default" ? ui.threads[id] || [] : []).filter(
|
|
|
(thread) =>
|
|
|
!ui.archived &&
|
|
|
- thread.delegated !== true &&
|
|
|
+ !isDelegated(thread) &&
|
|
|
matches(paasText(thread.title) || "历史会话") &&
|
|
|
!state.chats.some((chat) => chat.agentID === agent.id && chat.threadID === thread.id),
|
|
|
)
|
|
|
@@ -774,7 +961,7 @@ export function PaasChat(props: {
|
|
|
aria-label="研究设置"
|
|
|
title="研究设置"
|
|
|
disabled={!props.connected}
|
|
|
- onClick={() => props.onEditAgent?.(current()!.agentID)}
|
|
|
+ onClick={editAgent}
|
|
|
>
|
|
|
<Icon name="sliders" size="small" />
|
|
|
</button>
|
|
|
@@ -817,11 +1004,7 @@ export function PaasChat(props: {
|
|
|
}}
|
|
|
>
|
|
|
<Show when={currentAgent() && props.onEditAgent}>
|
|
|
- <button
|
|
|
- class="paas-menu-settings"
|
|
|
- disabled={!props.connected}
|
|
|
- onClick={() => props.onEditAgent?.(current()!.agentID)}
|
|
|
- >
|
|
|
+ <button class="paas-menu-settings" disabled={!props.connected} onClick={editAgent}>
|
|
|
研究设置
|
|
|
</button>
|
|
|
</Show>
|
|
|
@@ -837,7 +1020,7 @@ export function PaasChat(props: {
|
|
|
</Show>
|
|
|
<Show when={current()}>
|
|
|
<button onClick={() => setUI({ renaming: true, title: current()!.title })}>重命名会话</button>
|
|
|
- <button title="仅整理本机列表,不会停止后台任务" onClick={() => model.archive()}>
|
|
|
+ <button title="仅整理本机列表,不会停止后台任务" onClick={archiveChat}>
|
|
|
{current()?.archived ? "取消归档" : "在本机归档"}
|
|
|
</button>
|
|
|
</Show>
|
|
|
@@ -1245,34 +1428,16 @@ export function PaasChat(props: {
|
|
|
{(error) => (
|
|
|
<ResearchIssue
|
|
|
message={error}
|
|
|
- onSettings={current() ? () => props.onEditAgent?.(current()!.agentID) : undefined}
|
|
|
+ onSettings={props.onEditAgent ? () => editAgent() : undefined}
|
|
|
onConnection={props.onConnection}
|
|
|
onTasks={props.onOpenTasks}
|
|
|
/>
|
|
|
)}
|
|
|
</For>
|
|
|
- <Show
|
|
|
- when={Object.values(ui.threads)
|
|
|
- .flat()
|
|
|
- .some(
|
|
|
- (thread) =>
|
|
|
- paasRecord(thread.origin)?.kind === "paas" &&
|
|
|
- paasRecord(thread.origin)?.session_id === current()?.threadID,
|
|
|
- )}
|
|
|
- >
|
|
|
+ <Show when={childThreads().length}>
|
|
|
<section class="paas-child-conversations" aria-label="本会话的子任务">
|
|
|
<strong>子任务</strong>
|
|
|
- <For
|
|
|
- each={Object.entries(ui.threads).flatMap(([agentID, threads]) =>
|
|
|
- threads
|
|
|
- .filter(
|
|
|
- (thread) =>
|
|
|
- paasRecord(thread.origin)?.kind === "paas" &&
|
|
|
- paasRecord(thread.origin)?.session_id === current()?.threadID,
|
|
|
- )
|
|
|
- .map((thread) => ({ agentID, thread })),
|
|
|
- )}
|
|
|
- >
|
|
|
+ <For each={childThreads()}>
|
|
|
{(child) => (
|
|
|
<button
|
|
|
type="button"
|
|
|
@@ -1301,76 +1466,7 @@ export function PaasChat(props: {
|
|
|
type="button"
|
|
|
class="paas-agent-picker-trigger"
|
|
|
aria-label="选择执行角色"
|
|
|
- onClick={(event) => {
|
|
|
- const trigger = event.currentTarget
|
|
|
- dialog.show(() => (
|
|
|
- <Dialog
|
|
|
- title="选择执行角色"
|
|
|
- onCloseAutoFocus={(event) => {
|
|
|
- event.preventDefault()
|
|
|
- trigger.focus()
|
|
|
- }}
|
|
|
- >
|
|
|
- <div class="paas-agent-picker">
|
|
|
- <p class="paas-muted">
|
|
|
- {blank()
|
|
|
- ? "自动按目标匹配,也可指定本次任务的智能体。"
|
|
|
- : "当前会话沿用原智能体。选择其他角色会新建会话,原记录与草稿保留。"}
|
|
|
- </p>
|
|
|
- <div
|
|
|
- role="listbox"
|
|
|
- aria-label="执行角色"
|
|
|
- onKeyDown={(event) => {
|
|
|
- if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return
|
|
|
- event.preventDefault()
|
|
|
- const options = [...event.currentTarget.querySelectorAll<HTMLButtonElement>("[role=option]")]
|
|
|
- const index = options.indexOf(document.activeElement as HTMLButtonElement)
|
|
|
- options[
|
|
|
- event.key === "Home"
|
|
|
- ? 0
|
|
|
- : event.key === "End"
|
|
|
- ? options.length - 1
|
|
|
- : (index + (event.key === "ArrowDown" ? 1 : -1) + options.length) % options.length
|
|
|
- ]?.focus()
|
|
|
- }}
|
|
|
- >
|
|
|
- <Show when={blank()}>
|
|
|
- <button
|
|
|
- type="button"
|
|
|
- role="option"
|
|
|
- aria-selected={!ui.agentOverride}
|
|
|
- onClick={() => {
|
|
|
- setUI("agentOverride", "")
|
|
|
- dialog.close()
|
|
|
- }}
|
|
|
- >
|
|
|
- <strong>自动匹配</strong>
|
|
|
- <small>{routed().reason}</small>
|
|
|
- </button>
|
|
|
- </Show>
|
|
|
- <For each={directAgents(props.agents)}>
|
|
|
- {(agent) => (
|
|
|
- <button
|
|
|
- type="button"
|
|
|
- role="option"
|
|
|
- aria-selected={blank() ? ui.agentOverride === agent.id : current()?.agentID === agent.id}
|
|
|
- onClick={() => {
|
|
|
- if (!blank()) model.choose(agent, true)
|
|
|
- model.route(paasText(agent.id))
|
|
|
- setUI("agentOverride", paasText(agent.id))
|
|
|
- dialog.close()
|
|
|
- }}
|
|
|
- >
|
|
|
- <strong>{paasText(agent.name)}</strong>
|
|
|
- <small>{resolveAgentProfile(agent).summary}</small>
|
|
|
- </button>
|
|
|
- )}
|
|
|
- </For>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </Dialog>
|
|
|
- ))
|
|
|
- }}
|
|
|
+ onClick={(event) => showAgentPicker(event.currentTarget)}
|
|
|
>
|
|
|
<ResearchIcon kind="agent" />
|
|
|
<span>
|