@tano.page/workbench 0.15.0 → 0.16.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/package.json +35 -14
- package/src/App.vue +68 -797
- package/src/assistant/agent-identities.ts +1 -97
- package/src/assistant/agent-task-context.ts +1 -54
- package/src/assistant/attachments.ts +1 -1
- package/src/assistant/completion.ts +1 -61
- package/src/assistant/components/AssistantPanel.vue +1 -1
- package/src/assistant/connection-actions.ts +237 -0
- package/src/assistant/document-mentions.ts +1 -33
- package/src/assistant/engine.ts +1 -585
- package/src/assistant/execution.ts +6 -33
- package/src/assistant/hash.ts +1 -4
- package/src/assistant/language.ts +10 -37
- package/src/assistant/plugin-bridge-builder.ts +146 -0
- package/src/assistant/prompts.ts +1 -65
- package/src/assistant/proposal-actions.ts +336 -0
- package/src/assistant/store.ts +87 -554
- package/src/assistant/structures.ts +1 -157
- package/src/assistant/tasks.ts +8 -94
- package/src/assistant/tool-rules.ts +1 -7
- package/src/composables/use-workbench-help.ts +177 -0
- package/src/composables/use-workbench-lifecycle.ts +249 -0
- package/src/composables/use-workbench-settings.ts +185 -0
- package/src/composables/use-workbench-shortcuts.ts +37 -1022
- package/src/composables/use-workbench-views.ts +325 -0
- package/src/documents/components/DocumentActionMenu.vue +279 -0
- package/src/documents/components/DocumentInspector.vue +168 -0
- package/src/documents/components/DocumentPane.vue +482 -0
- package/src/documents/description-merge.ts +1 -9
- package/src/files/components/FileMetadataPanel.vue +49 -15
- package/src/i18n/locale-catalogs.ts +7 -6
- package/src/i18n/locale-registry.ts +8 -6
- package/src/i18n/locales/de-DE.json +23 -1
- package/src/i18n/locales/en-US.json +23 -1
- package/src/i18n/locales/es-ES.json +23 -1
- package/src/i18n/locales/ja-JP.json +23 -1
- package/src/i18n/locales/zh-TW.json +23 -1
- package/src/mail/components/MailAccountDialog.vue +1 -0
- package/src/mail/components/MailComposerDialog.vue +1 -0
- package/src/mail/components/MailWorkspaceView.vue +1 -0
- package/src/mail/components/SidebarMail.vue +1 -0
- package/src/objects/content-references.ts +6 -56
- package/src/objects/wiki-links.ts +1 -48
- package/src/objects/workspace-objects.ts +2 -3
- package/src/plans/calendar.ts +2 -2
- package/src/plans/components/CalendarAgendaView.vue +165 -0
- package/src/plans/components/CalendarFilterBar.vue +128 -0
- package/src/plans/components/CalendarMonthView.vue +224 -0
- package/src/plans/components/CalendarQuickCreateDialog.vue +269 -0
- package/src/plans/components/CalendarTimelineView.vue +517 -0
- package/src/plans/components/CalendarTopbarControls.vue +101 -0
- package/src/plans/components/CalendarView.vue +162 -2106
- package/src/plans/components/CalendarYearView.vue +138 -0
- package/src/plans/components/PlanConnectionsDialog.vue +1 -1
- package/src/plans/components/calendar-shared.css +192 -0
- package/src/plans/composables/use-calendar-interactions.ts +396 -0
- package/src/plans/composables/use-calendar-scroll.ts +420 -0
- package/src/plugins/components/PluginDetailPage.vue +10 -6
- package/src/plugins/components/PluginDeveloperArea.vue +192 -0
- package/src/plugins/components/PluginExplorer.vue +6 -4
- package/src/plugins/components/PluginImportDialog.vue +1 -1
- package/src/plugins/components/PluginInstaller.vue +269 -178
- package/src/plugins/components/PluginMaintenance.vue +37 -49
- package/src/plugins/components/PluginRepositoryStorage.vue +4 -2
- package/src/plugins/components/PluginSettingsForm.vue +2 -1
- package/src/plugins/components/PluginsTabPage.vue +61 -0
- package/src/plugins/components/plugin-center.css +5 -0
- package/src/plugins/data.ts +9 -86
- package/src/plugins/editor-plugin-adapters.ts +169 -0
- package/src/plugins/plugin-data.ts +2 -3
- package/src/plugins/plugin-rpc-router.ts +558 -0
- package/src/plugins/plugin-sdk.ts +1 -260
- package/src/plugins/plugin-ui-dialogs.ts +167 -0
- package/src/plugins/queries.ts +1 -75
- package/src/plugins/view-header.ts +2 -7
- package/src/runtime.ts +3 -2
- package/src/settings/components/SettingsPanel.vue +8 -3
- package/src/settings/components/WorkbenchSettings.vue +20 -7
- package/src/settings/components/settings-context.ts +1 -1
- package/src/settings/settings-tab.ts +1 -0
- package/src/shell/command-palette.ts +3 -3
- package/src/shell/components/EditorToolbar.vue +8 -9
- package/src/shell/components/EditorTools.vue +1 -5
- package/src/shell/components/PaneTopbar.vue +393 -0
- package/src/shell/components/SelectionToolbar.vue +0 -2
- package/src/shell/components/WorkbenchContextMenus.vue +37 -26
- package/src/shell/components/WorkbenchModals.vue +22 -18
- package/src/shell/components/WorkbenchPanes.vue +358 -1512
- package/src/shell/components/WorkbenchSidebar.vue +486 -0
- package/src/shell/pane-registry.ts +69 -0
- package/src/shell/use-surface-dismissal.ts +219 -0
- package/src/shell/use-workbench-commands.ts +599 -0
- package/src/shell/use-workbench-keybindings.ts +337 -0
- package/src/shell/use-workbench.ts +488 -1081
- package/src/shell/workbench-context.ts +30 -3
- package/src/stores/workspace-document-io.ts +807 -0
- package/src/stores/workspace-file-ops.ts +1 -1
- package/src/stores/workspace-knowledge.ts +1 -1
- package/src/stores/workspace-lifecycle.ts +459 -0
- package/src/stores/workspace-plugins-state.ts +71 -599
- package/src/stores/workspace-session.ts +287 -0
- package/src/stores/workspace-tabs-layout.ts +1 -1
- package/src/stores/workspace-types.ts +50 -0
- package/src/stores/workspace.ts +163 -1345
- package/src/styles.css +0 -1
- package/src/tabs/components/DesktopTabs.vue +19 -11
- package/src/windows/AuxiliaryWindows.vue +2 -2
- package/src/windows/use-window-event-bridge.ts +152 -0
- package/src/workspace/components/TemplateLibrary.vue +1 -1
- package/src/workspace/components/WorkspaceIconPicker.vue +5 -4
- package/src/workspace/components/WorkspacePickerMenu.vue +317 -0
- package/src/assistant/locales/de-DE.json +0 -129
- package/src/assistant/locales/en-US.json +0 -129
- package/src/assistant/locales/es-ES.json +0 -129
- package/src/assistant/locales/ja-JP.json +0 -129
- package/src/assistant/locales/zh-CN.json +0 -129
- package/src/assistant/locales/zh-TW.json +0 -129
- package/src/assistant/task-prompts.json +0 -39
|
@@ -1,97 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { AgentBinding, AssistantTransport, WorkspaceAdapter } from '@tano/protocol';
|
|
3
|
-
import { AgentTaskQueue } from './inbox';
|
|
4
|
-
|
|
5
|
-
/** Reserve the binding before creating its portable identity, allowing crash-safe repair. */
|
|
6
|
-
export async function ensureAgentIdentities(
|
|
7
|
-
workspaceId: string,
|
|
8
|
-
adapter: Pick<WorkspaceAdapter, 'refresh' | 'read' | 'write' | 'create' | 'reserveObjectId'>,
|
|
9
|
-
transport: Pick<AssistantTransport, 'inbox' | 'connections' | 'models' | 'hostedInbox'>,
|
|
10
|
-
signal?: AbortSignal,
|
|
11
|
-
) {
|
|
12
|
-
const errors: string[] = [];
|
|
13
|
-
if (!transport.inbox || !adapter.reserveObjectId) return errors;
|
|
14
|
-
const queue = new AgentTaskQueue(transport.inbox);
|
|
15
|
-
const connections = await transport.connections();
|
|
16
|
-
for (const connection of connections) {
|
|
17
|
-
if (connection.platformProvider && transport.hostedInbox) continue;
|
|
18
|
-
try {
|
|
19
|
-
signal?.throwIfAborted();
|
|
20
|
-
const inbox = await transport.inbox.load(workspaceId);
|
|
21
|
-
let binding = inbox.bindings.find((item) => item.connectionId === connection.id);
|
|
22
|
-
if (!binding) {
|
|
23
|
-
let models: string[] = [];
|
|
24
|
-
try { models = await transport.models(connection.id, signal ? AbortSignal.any([signal, AbortSignal.timeout(30_000)]) : AbortSignal.timeout(30_000)); }
|
|
25
|
-
catch (reason) { errors.push(`${connection.name} · ${String(reason)}`); }
|
|
26
|
-
signal?.throwIfAborted();
|
|
27
|
-
const workspace = await adapter.refresh();
|
|
28
|
-
if (workspace.id !== workspaceId || workspace.readOnly) return errors;
|
|
29
|
-
const personId = await adapter.reserveObjectId(inbox.bindings.map((item) => item.personId));
|
|
30
|
-
const candidate: AgentBinding = {
|
|
31
|
-
personId,
|
|
32
|
-
connectionId: connection.id,
|
|
33
|
-
model: models[0] ?? '',
|
|
34
|
-
enabled: true,
|
|
35
|
-
};
|
|
36
|
-
binding = await queue.update(workspaceId, (current) => {
|
|
37
|
-
const existing = current.bindings.find((item) => item.connectionId === connection.id);
|
|
38
|
-
if (existing) return { ...existing };
|
|
39
|
-
current.bindings.push(candidate);
|
|
40
|
-
return candidate;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
const workspace = await adapter.refresh();
|
|
44
|
-
signal?.throwIfAborted();
|
|
45
|
-
if (workspace.id !== workspaceId || workspace.readOnly) return errors;
|
|
46
|
-
const path = `${objectDirectory}/${binding.personId}.tano.json`;
|
|
47
|
-
if (workspace.files.some((file) => file.path === path)) {
|
|
48
|
-
const object = parseObject((await adapter.read(path)).content);
|
|
49
|
-
if (object.id !== binding.personId || !isAgentPerson(object))
|
|
50
|
-
throw new Error('机器人身份与已有对象冲突');
|
|
51
|
-
if (!binding.provisioned) await queue.update(workspaceId, current => {
|
|
52
|
-
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
53
|
-
if (saved) saved.provisioned = true;
|
|
54
|
-
});
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (binding.provisioned) {
|
|
58
|
-
await queue.update(workspaceId, current => {
|
|
59
|
-
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
60
|
-
if (saved) saved.enabled = false;
|
|
61
|
-
});
|
|
62
|
-
errors.push(`${connection.name} · 机器人身份文件已移除,请从文件历史恢复后重新启用`);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const object = {
|
|
66
|
-
version: 1,
|
|
67
|
-
id: binding.personId,
|
|
68
|
-
type: 'person',
|
|
69
|
-
name: connection.name,
|
|
70
|
-
description: '',
|
|
71
|
-
archived: false,
|
|
72
|
-
properties: { memberKind: 'agent' },
|
|
73
|
-
};
|
|
74
|
-
try {
|
|
75
|
-
await adapter.create(path, JSON.stringify(object, null, 2) + '\n');
|
|
76
|
-
} catch (error) {
|
|
77
|
-
const current = await adapter.refresh();
|
|
78
|
-
if (!current.files.some((file) => file.path === path)) throw error;
|
|
79
|
-
const saved = parseObject((await adapter.read(path)).content);
|
|
80
|
-
if (saved.id !== binding.personId || !isAgentPerson(saved)) throw error;
|
|
81
|
-
}
|
|
82
|
-
await queue.update(workspaceId, current => {
|
|
83
|
-
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
84
|
-
if (saved) saved.provisioned = true;
|
|
85
|
-
});
|
|
86
|
-
} catch (reason) {
|
|
87
|
-
errors.push(`${connection.name} · ${String(reason)}`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
await queue.update(workspaceId, (inbox) => {
|
|
91
|
-
for (const binding of inbox.bindings)
|
|
92
|
-
if (!connections.some((connection) => connection.id === binding.connectionId) ||
|
|
93
|
-
transport.hostedInbox && connections.some(connection => connection.id === binding.connectionId && connection.platformProvider))
|
|
94
|
-
binding.enabled = false;
|
|
95
|
-
});
|
|
96
|
-
return errors;
|
|
97
|
-
}
|
|
1
|
+
export * from '@tano/protocol/agent-identities';
|
|
@@ -1,54 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { AgentTask } from '@tano/protocol';
|
|
3
|
-
import { loadCommentFile, saveCommentChange, type CommentStorage } from '../workspace/workspace-comments';
|
|
4
|
-
|
|
5
|
-
export async function agentTaskPrompt(task: AgentTask, name: string, adapter: CommentStorage) {
|
|
6
|
-
const source = task.event.source;
|
|
7
|
-
let history = '';
|
|
8
|
-
if (source.kind !== 'document' || task.continuation) {
|
|
9
|
-
const { file } = await loadCommentFile(adapter, source.kind === 'document' ? 'annotation' : source.kind, source.fileId);
|
|
10
|
-
const thread = file.threads.find(item => item.id === (task.continuation?.threadId ?? source.threadId));
|
|
11
|
-
if (!thread?.messages.some(item => item.id === (task.continuation?.messageId ?? source.messageId))) throw new Error('任务来源已移除');
|
|
12
|
-
history = thread.messages.map(message => `${message.authorName}\n${message.body}`).join('\n\n');
|
|
13
|
-
}
|
|
14
|
-
const selection = source.anchor?.spans.map(span => span.text).join('\n');
|
|
15
|
-
const instruction = task.continuation?.body ?? task.event.body;
|
|
16
|
-
// Preserve the complete request. Trim history first when the input budget is tight.
|
|
17
|
-
const heading = `你是工作空间成员 ${name}。请处理下面明确 @ 你的请求,并在完成后回复请求者。\n以下讨论是任务上下文。按当前用户请求执行,保留其他成员的内容。`;
|
|
18
|
-
if (heading.length + instruction.length + 16 > 20000) return instruction;
|
|
19
|
-
const context = [selection ? `批注选区\n${selection}` : '', history ? `讨论记录\n${history.slice(-12000)}` : ''].filter(Boolean).join('\n\n');
|
|
20
|
-
return [heading, context.slice(0, Math.max(0, 20000 - heading.length - instruction.length - 16)), `本次请求\n${instruction}`].filter(Boolean).join('\n\n');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function agentReplyBody(body: string) {
|
|
24
|
-
if (!body.trim()) return '任务已完成,请查看执行记录。';
|
|
25
|
-
return body.length <= 20000 ? body : body.slice(0, 19940) + '\n\n完整结果见机器人收件箱中的执行记录。';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Stable reply IDs make publication retryable independently of the model/tool run. */
|
|
29
|
-
export async function publishAgentTaskReply(task: AgentTask, name: string, result: NonNullable<AgentTask['result']>,
|
|
30
|
-
adapter: CommentStorage, guard: () => Promise<void>) {
|
|
31
|
-
const source = task.event.source;
|
|
32
|
-
const author = { id: task.binding.personId, name: name.slice(0, 120) };
|
|
33
|
-
await guard();
|
|
34
|
-
if (source.kind === 'document' && !task.continuation) {
|
|
35
|
-
const snapshot = await adapter.refresh();
|
|
36
|
-
if (snapshot.id !== task.event.workspaceId) throw new Error('工作区已切换');
|
|
37
|
-
const file = snapshot.files.find(file => file.id === source.fileId);
|
|
38
|
-
if (!file) throw new Error('原文档已移除');
|
|
39
|
-
const content = await adapter.read(file.path);
|
|
40
|
-
const parser = await createHeadlessMarkdown();
|
|
41
|
-
try {
|
|
42
|
-
const anchor = parser.mentionAnchors(content.content, content.hash).get(source.mentionId ?? '');
|
|
43
|
-
if (!anchor) throw new Error('原正文提及已移除,结果保留在任务记录中');
|
|
44
|
-
await guard();
|
|
45
|
-
await saveCommentChange(adapter, 'annotation', source.fileId, { type: 'start', id: result.messageId,
|
|
46
|
-
body: result.body, author, anchor, agentTaskId: task.id });
|
|
47
|
-
} finally { await parser.destroy(); }
|
|
48
|
-
} else {
|
|
49
|
-
await saveCommentChange(adapter, source.kind === 'document' ? 'annotation' : source.kind, source.fileId, {
|
|
50
|
-
type: 'reply', threadId: task.continuation?.threadId ?? source.threadId!, id: result.messageId, body: result.body,
|
|
51
|
-
author, replyTo: task.continuation?.messageId ?? source.messageId, agentTaskId: task.id,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
1
|
+
export * from '@tano/editor/agent-task-context';
|
|
@@ -3,7 +3,7 @@ import { t } from '../i18n/i18n';
|
|
|
3
3
|
|
|
4
4
|
export const maxAssistantAttachments = 10;
|
|
5
5
|
export const assistantAttachmentBytes = { text: 128 * 1024, media: 4 * 1024 * 1024 };
|
|
6
|
-
export
|
|
6
|
+
export { assistantClipboardSeparator } from '@tano/protocol/assistant-language';
|
|
7
7
|
|
|
8
8
|
const mediaTypes: Readonly<Record<string, string>> = {
|
|
9
9
|
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp',
|
|
@@ -1,61 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type AssistantTextPhase = Extract<AssistantActivity, { kind: 'text' }>['phase'];
|
|
4
|
-
export type AssistantTextSegment = { start: number; end: number; phase: AssistantTextPhase };
|
|
5
|
-
|
|
6
|
-
export class AssistantRunError extends Error {
|
|
7
|
-
constructor(public reason: AssistantTerminationReason, message: string) { super(message); }
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function responsePhase(value: unknown): AssistantTextPhase | undefined {
|
|
11
|
-
if (value === 'commentary' || value === 'partial_answer') return 'working';
|
|
12
|
-
if (value === 'final_answer') return 'answer';
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const markers = [
|
|
17
|
-
{ prefix: '[[tano:progress]]', phase: 'working' },
|
|
18
|
-
{ prefix: '[[tano:final]]', phase: 'answer' },
|
|
19
|
-
] as const;
|
|
20
|
-
|
|
21
|
-
/** Only an exact leading protocol marker is interpreted; ordinary prose is never guessed. */
|
|
22
|
-
export class AssistantTextDecoder {
|
|
23
|
-
private buffer = '';
|
|
24
|
-
private started = false;
|
|
25
|
-
private afterMarker = false;
|
|
26
|
-
phase: AssistantTextPhase = 'pending';
|
|
27
|
-
constructor(private emit: (text: string, phase: AssistantTextPhase) => void) {}
|
|
28
|
-
push(text: string, nativePhase?: AssistantTextPhase) {
|
|
29
|
-
if (nativePhase) this.phase = nativePhase;
|
|
30
|
-
if (this.started && !this.afterMarker) { this.emit(text, this.phase); return; }
|
|
31
|
-
this.buffer += text;
|
|
32
|
-
if (!this.started) {
|
|
33
|
-
const marker = markers.find(marker => this.buffer.startsWith(marker.prefix));
|
|
34
|
-
if (marker) {
|
|
35
|
-
this.phase = nativePhase ?? marker.phase;
|
|
36
|
-
this.buffer = this.buffer.slice(marker.prefix.length);
|
|
37
|
-
this.afterMarker = true;
|
|
38
|
-
} else if (markers.some(marker => marker.prefix.startsWith(this.buffer))) return;
|
|
39
|
-
this.started = true;
|
|
40
|
-
}
|
|
41
|
-
if (this.afterMarker) {
|
|
42
|
-
if (!this.buffer || this.buffer === '\r') return;
|
|
43
|
-
this.buffer = this.buffer.replace(/^\r?\n/, '');
|
|
44
|
-
this.afterMarker = false;
|
|
45
|
-
}
|
|
46
|
-
if (this.buffer) this.emit(this.buffer, this.phase);
|
|
47
|
-
this.buffer = '';
|
|
48
|
-
}
|
|
49
|
-
finish() {
|
|
50
|
-
if (!this.started && this.buffer) throw new AssistantRunError('incomplete', '模型的回答阶段标记未完整返回');
|
|
51
|
-
if (this.buffer) this.emit(this.buffer, this.phase);
|
|
52
|
-
this.buffer = '';
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function decodeAssistantText(text: string, phase?: AssistantTextPhase) {
|
|
57
|
-
let content = '';
|
|
58
|
-
const decoder = new AssistantTextDecoder(value => { content += value; });
|
|
59
|
-
decoder.push(text, phase); decoder.finish();
|
|
60
|
-
return { content, phase: phase ?? decoder.phase };
|
|
61
|
-
}
|
|
1
|
+
export * from '@tano/protocol/assistant-completion';
|
|
@@ -791,7 +791,7 @@ watch(() => [assistant.session?.messages.map(message => `${message.content.lengt
|
|
|
791
791
|
<LoadingSkeleton v-if="assistant.loading && !assistant.session?.messages.length" :rows="5" />
|
|
792
792
|
<div v-else-if="!assistant.session?.messages.length" class="assistant-welcome">
|
|
793
793
|
<!-- Reserved for platform AI settings; currently greets with a flutter. -->
|
|
794
|
-
<button type="button" class="assistant-welcome-mark" :aria-label="t('
|
|
794
|
+
<button type="button" class="assistant-welcome-mark" :aria-label="t('同页助手')" :data-tooltip="t('同页助手')" @click="welcomeButterfly?.flutter()">
|
|
795
795
|
<AssistantButterfly ref="welcomeButterfly" class="assistant-welcome-butterfly" />
|
|
796
796
|
</button>
|
|
797
797
|
<h3>{{ t('今天想从哪里开始?') }}</h3>
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { ComputedRef, Ref } from 'vue';
|
|
2
|
+
import {
|
|
3
|
+
isAssistantModelId,
|
|
4
|
+
isAssistantReasoningSelection,
|
|
5
|
+
type AssistantConnection,
|
|
6
|
+
type AssistantQuotaConfig,
|
|
7
|
+
type AssistantReasoningSelection,
|
|
8
|
+
type AssistantSession,
|
|
9
|
+
type AssistantTransport,
|
|
10
|
+
} from '@tano/protocol';
|
|
11
|
+
import { recordHelpCompleted } from '../help/help-checklist';
|
|
12
|
+
import type { useWorkspaceStore } from '../stores/workspace';
|
|
13
|
+
|
|
14
|
+
export interface AssistantConnectionActionDeps {
|
|
15
|
+
transport: AssistantTransport | undefined;
|
|
16
|
+
canConfigure: boolean;
|
|
17
|
+
opened: Ref<boolean>;
|
|
18
|
+
loading: Ref<boolean>;
|
|
19
|
+
busy: Ref<boolean>;
|
|
20
|
+
applying: Ref<boolean>;
|
|
21
|
+
connections: Ref<AssistantConnection[]>;
|
|
22
|
+
connectionId: Ref<string>;
|
|
23
|
+
connection: ComputedRef<AssistantConnection | undefined>;
|
|
24
|
+
modelId: Ref<string>;
|
|
25
|
+
reasoningEffort: Ref<AssistantReasoningSelection>;
|
|
26
|
+
models: Ref<string[]>;
|
|
27
|
+
modelsLoading: Ref<boolean>;
|
|
28
|
+
modelsError: Ref<string>;
|
|
29
|
+
session: Ref<AssistantSession | null>;
|
|
30
|
+
workspace: ReturnType<typeof useWorkspaceStore>;
|
|
31
|
+
getGeneration: () => number;
|
|
32
|
+
newSession: () => void;
|
|
33
|
+
persist: (target?: AssistantSession | null) => Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createAssistantConnectionActions(deps: AssistantConnectionActionDeps) {
|
|
37
|
+
const {
|
|
38
|
+
transport,
|
|
39
|
+
canConfigure,
|
|
40
|
+
opened,
|
|
41
|
+
loading,
|
|
42
|
+
busy,
|
|
43
|
+
applying,
|
|
44
|
+
connections,
|
|
45
|
+
connectionId,
|
|
46
|
+
connection,
|
|
47
|
+
modelId,
|
|
48
|
+
reasoningEffort,
|
|
49
|
+
models,
|
|
50
|
+
modelsLoading,
|
|
51
|
+
modelsError,
|
|
52
|
+
session,
|
|
53
|
+
workspace,
|
|
54
|
+
getGeneration,
|
|
55
|
+
newSession,
|
|
56
|
+
persist,
|
|
57
|
+
} = deps;
|
|
58
|
+
|
|
59
|
+
let modelController: AbortController | undefined;
|
|
60
|
+
|
|
61
|
+
function abortModels() {
|
|
62
|
+
modelController?.abort();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function reloadConnections() {
|
|
66
|
+
if (!transport) return;
|
|
67
|
+
const epoch = getGeneration();
|
|
68
|
+
const loaded = (await transport.connections()).map(({ managed, ...item }) => ({
|
|
69
|
+
...item,
|
|
70
|
+
platformProvider: item.platformProvider ?? managed,
|
|
71
|
+
}));
|
|
72
|
+
if (epoch !== getGeneration()) return;
|
|
73
|
+
connections.value = loaded;
|
|
74
|
+
if (!connectionId.value && !session.value?.connectionId)
|
|
75
|
+
connectionId.value = connections.value.length === 1 ? connections.value[0].id : '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function reloadModels() {
|
|
79
|
+
modelController?.abort();
|
|
80
|
+
models.value = []; modelsError.value = ''; modelsLoading.value = false;
|
|
81
|
+
const selected = connection.value;
|
|
82
|
+
if (!transport || !selected || !opened.value) return;
|
|
83
|
+
const abort = new AbortController(); modelController = abort;
|
|
84
|
+
modelsLoading.value = true;
|
|
85
|
+
try {
|
|
86
|
+
const values = await transport.models(selected.id, abort.signal);
|
|
87
|
+
if (abort.signal.aborted || modelController !== abort || connectionId.value !== selected.id || !opened.value) return;
|
|
88
|
+
if (!Array.isArray(values) || values.length > 1000 || values.some(value => !isAssistantModelId(value)))
|
|
89
|
+
throw new Error('模型列表格式无效');
|
|
90
|
+
models.value = [...new Set(values)];
|
|
91
|
+
if (!models.value.length) modelsError.value = '服务未返回可用模型';
|
|
92
|
+
} catch (reason) {
|
|
93
|
+
if (!abort.signal.aborted && modelController === abort) modelsError.value = String(reason);
|
|
94
|
+
} finally {
|
|
95
|
+
if (modelController === abort) { modelsLoading.value = false; modelController = undefined; }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function refreshModels() {
|
|
100
|
+
if (busy.value || applying.value || loading.value) return;
|
|
101
|
+
modelsLoading.value = true; modelsError.value = '';
|
|
102
|
+
try { await reloadConnections(); await reloadModels(); }
|
|
103
|
+
catch (reason) { modelsError.value = reason instanceof Error ? reason.message : String(reason); }
|
|
104
|
+
finally { modelsLoading.value = false; }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function readConnectionKey(id: string) {
|
|
108
|
+
if (!canConfigure || !connections.value.some(item => item.id === id && !item.platformProvider && item.hasKey && (!item.kind || item.kind === 'api')))
|
|
109
|
+
throw new Error('此连接无法读取 API 密钥');
|
|
110
|
+
if (!transport?.readConnectionKey) throw new Error('当前环境不支持读取已保存的密钥,请更新并重启桌面版');
|
|
111
|
+
return transport.readConnectionKey(id);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function queryQuota(id: string) {
|
|
115
|
+
if (!transport?.quota) throw new Error('当前环境不支持服务商额度查询,请使用桌面版');
|
|
116
|
+
if (!connections.value.some(item => item.id === id && !item.platformProvider)) throw new Error('此连接不支持个人额度查询');
|
|
117
|
+
return transport.quota(id);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function saveQuota(id: string, config: AssistantQuotaConfig, key?: string) {
|
|
121
|
+
if (!transport?.saveQuota) throw new Error('当前环境不支持额度配置,请使用桌面版');
|
|
122
|
+
if (!connections.value.some(item => item.id === id && !item.platformProvider)) throw new Error('连接已移除或由平台管理');
|
|
123
|
+
await transport.saveQuota(id, config, key);
|
|
124
|
+
await reloadConnections();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function checkConnection(id: string, signal: AbortSignal) {
|
|
128
|
+
signal.throwIfAborted();
|
|
129
|
+
if (!transport) throw new Error('当前运行环境未提供助手连接配置');
|
|
130
|
+
if (!connections.value.some(item => item.id === id)) throw new Error('模型连接已移除');
|
|
131
|
+
const values = await transport.models(id, signal);
|
|
132
|
+
signal.throwIfAborted();
|
|
133
|
+
if (!Array.isArray(values) || values.length > 1000 || values.some(value => !isAssistantModelId(value)))
|
|
134
|
+
throw new Error('模型列表格式无效');
|
|
135
|
+
return new Set(values).size;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function selectConnection(id: string, reset = false) {
|
|
139
|
+
if (busy.value || applying.value || loading.value) return;
|
|
140
|
+
if (!connections.value.some(item => item.id === id)) throw new Error('模型连接已移除');
|
|
141
|
+
if (id === connectionId.value && !reset) return;
|
|
142
|
+
connectionId.value = id; modelId.value = '';
|
|
143
|
+
reasoningEffort.value = connection.value?.reasoningEffort ?? 'medium';
|
|
144
|
+
// Connection settings are personal and can be edited from the standalone
|
|
145
|
+
// settings window, where no workspace conversation is loaded.
|
|
146
|
+
if (!session.value && workspace.workspace) newSession();
|
|
147
|
+
if (session.value) {
|
|
148
|
+
session.value.connectionId = id; delete session.value.model;
|
|
149
|
+
if (connection.value?.kind === 'codex-cli') session.value.reasoningEffort = reasoningEffort.value;
|
|
150
|
+
else delete session.value.reasoningEffort;
|
|
151
|
+
await persist();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function selectModel(id: string) {
|
|
156
|
+
if (busy.value || applying.value || loading.value) return;
|
|
157
|
+
const selected = id.trim();
|
|
158
|
+
if (!connection.value) throw new Error('请先选择模型连接');
|
|
159
|
+
if (!isAssistantModelId(selected)) throw new Error('模型 ID 无效');
|
|
160
|
+
if (!session.value) newSession();
|
|
161
|
+
modelId.value = selected; session.value!.model = selected; session.value!.connectionId = connectionId.value;
|
|
162
|
+
await persist();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function restoreModelSelection(target: AssistantSession | null) {
|
|
166
|
+
connectionId.value = target?.connectionId ?? (connections.value.length === 1 ? connections.value[0].id : '');
|
|
167
|
+
const previous = target ? [...target.messages].reverse().find(message => message.role === 'assistant' && message.model)?.model : undefined;
|
|
168
|
+
modelId.value = target?.model ?? (isAssistantModelId(previous) ? previous : '');
|
|
169
|
+
reasoningEffort.value = target?.reasoningEffort ?? connection.value?.reasoningEffort ?? (target ? 'cli-default' : 'medium');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function selectReasoningEffort(value: AssistantReasoningSelection) {
|
|
173
|
+
if (busy.value || applying.value || loading.value) return;
|
|
174
|
+
if (connection.value?.kind !== 'codex-cli') throw new Error('当前连接不支持选择推理强度');
|
|
175
|
+
if (!isAssistantReasoningSelection(value)) throw new Error('推理强度无效');
|
|
176
|
+
if (!session.value) newSession();
|
|
177
|
+
reasoningEffort.value = value;
|
|
178
|
+
session.value!.reasoningEffort = value;
|
|
179
|
+
session.value!.connectionId = connectionId.value;
|
|
180
|
+
await persist();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function saveConnection(config: Omit<AssistantConnection, 'hasKey'>, key?: string) {
|
|
184
|
+
if (!transport || busy.value || applying.value || loading.value) throw new Error('请先等待当前任务完成');
|
|
185
|
+
const previous = connections.value.find(item => item.id === config.id);
|
|
186
|
+
if (!canConfigure || config.platformProvider || previous?.platformProvider) throw new Error('此模型连接由平台管理员配置');
|
|
187
|
+
await transport.saveConnection(config, key);
|
|
188
|
+
recordHelpCompleted('assistant-connection');
|
|
189
|
+
await reloadConnections();
|
|
190
|
+
await selectConnection(
|
|
191
|
+
config.id,
|
|
192
|
+
previous?.endpoint !== config.endpoint ||
|
|
193
|
+
(previous?.kind ?? 'api') !== (config.kind ?? 'api') ||
|
|
194
|
+
previous?.cliPath !== config.cliPath,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const canImportCcSwitch = canConfigure && !!transport?.scanCcSwitch && !!transport?.importCcSwitch;
|
|
199
|
+
|
|
200
|
+
async function scanCcSwitch(path?: string) {
|
|
201
|
+
if (!canImportCcSwitch) throw new Error('当前环境不支持从 CC Switch 导入');
|
|
202
|
+
return transport!.scanCcSwitch!(path);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function importCcSwitch(ids: string[], path?: string) {
|
|
206
|
+
if (!canImportCcSwitch || busy.value || applying.value || loading.value) throw new Error('请先等待当前任务完成');
|
|
207
|
+
await transport!.importCcSwitch!(ids, path);
|
|
208
|
+
await reloadConnections();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function removeConnection(id: string) {
|
|
212
|
+
if (!transport || busy.value || applying.value || loading.value) throw new Error('请先等待当前任务完成');
|
|
213
|
+
if (!canConfigure || connections.value.find(item => item.id === id)?.platformProvider) throw new Error('此模型连接由平台管理员配置');
|
|
214
|
+
await transport.removeConnection(id);
|
|
215
|
+
await reloadConnections();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
abortModels,
|
|
220
|
+
canImportCcSwitch,
|
|
221
|
+
reloadConnections,
|
|
222
|
+
reloadModels,
|
|
223
|
+
refreshModels,
|
|
224
|
+
readConnectionKey,
|
|
225
|
+
queryQuota,
|
|
226
|
+
saveQuota,
|
|
227
|
+
checkConnection,
|
|
228
|
+
selectConnection,
|
|
229
|
+
selectModel,
|
|
230
|
+
restoreModelSelection,
|
|
231
|
+
selectReasoningEffort,
|
|
232
|
+
saveConnection,
|
|
233
|
+
scanCcSwitch,
|
|
234
|
+
importCcSwitch,
|
|
235
|
+
removeConnection,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -1,33 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { mentionIdFromTitle, parseReferenceUrl } from '@tano/protocol';
|
|
3
|
-
import type { MentionEvent } from '@tano/protocol';
|
|
4
|
-
import type { Nodes } from 'mdast';
|
|
5
|
-
|
|
6
|
-
/** Parse syntax, so code examples, metadata and images cannot dispatch work. */
|
|
7
|
-
export function documentMentionEvents(workspaceId: string, fileId: string, content: string, revision: string,
|
|
8
|
-
actorId: string, createdAt: number): MentionEvent[] {
|
|
9
|
-
const { tree, offset } = parseDocumentMarkdown(content);
|
|
10
|
-
const events: MentionEvent[] = [];
|
|
11
|
-
const seen = new Set<string>();
|
|
12
|
-
function visit(node: Nodes, block?: Nodes) {
|
|
13
|
-
if (['paragraph', 'heading', 'tableCell'].includes(node.type)) block = node;
|
|
14
|
-
if (node.type === 'link') {
|
|
15
|
-
const reference = parseReferenceUrl(node.url);
|
|
16
|
-
const mentionId = mentionIdFromTitle(node.title);
|
|
17
|
-
if (reference?.type !== 'person' || !mentionId || !block || !('position' in block) || !block.position || seen.has(mentionId)) return;
|
|
18
|
-
const from = (block.position.start.offset ?? 0) + offset;
|
|
19
|
-
const to = (block.position.end.offset ?? 0) + offset;
|
|
20
|
-
const body = content.slice(from, to);
|
|
21
|
-
if (!body.trim() || body.length > 20000) return;
|
|
22
|
-
seen.add(mentionId);
|
|
23
|
-
events.push({ id: JSON.stringify(['document', fileId, mentionId]), workspaceId, personId: reference.id,
|
|
24
|
-
actorId, body, revision, createdAt, source: { kind: 'document', fileId, mentionId,
|
|
25
|
-
anchor: { basis: 'source', baseHash: revision, spans: [{ from, to, text: body }],
|
|
26
|
-
prefix: content.slice(Math.max(0, from - 128), from), suffix: content.slice(to, to + 128) } } });
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
if ('children' in node) node.children.forEach(child => visit(child, block));
|
|
30
|
-
}
|
|
31
|
-
visit(tree);
|
|
32
|
-
return events;
|
|
33
|
-
}
|
|
1
|
+
export * from '@tano/editor/document-mentions';
|