Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
149 lines (138 loc) · 4.09 KB
/
Copy pathutils.ts
File metadata and controls
149 lines (138 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { FileIcon, FolderIcon, AgentIcon, ImageIcon, TerminalIcon, ThinkingIcon } from '../../components/Icons'
import type { Attachment } from './types'
/**
* 从 API file part 创建 Attachment
* 注意:source.text.value 只是用户输入的 @mention 文本,不是文件内容
* 真正的文件内容会通过 synthetic text part 传递
*/
export function fromFilePart(
part: {
id?: string
type: 'file'
mime: string
url: string
filename?: string
source?: {
text?: { value: string; start: number; end: number }
path?: string
}
},
content?: string,
): Attachment {
const isFolder = part.mime === 'application/x-directory'
return {
id: part.id || crypto.randomUUID(),
type: isFolder ? 'folder' : 'file',
displayName: part.filename || part.source?.path || 'file',
url: part.url,
mime: part.mime,
relativePath: part.source?.path,
// content 优先使用传入的 synthetic content
// source.text.value 只是 @mention 文本,不作为 content
content: content,
textRange: part.source?.text
? {
value: part.source.text.value,
start: part.source.text.start,
end: part.source.text.end,
}
: undefined,
category: 'user',
originalSource: part.source,
}
}
/**
* 从 API agent part 创建 Attachment
*/
export function fromAgentPart(part: {
id?: string
type: 'agent'
name: string
source?: { value: string; start: number; end: number }
}): Attachment {
return {
id: part.id || crypto.randomUUID(),
type: 'agent',
displayName: part.name,
agentName: part.name,
textRange: part.source
? {
value: part.source.value,
start: part.source.start,
end: part.source.end,
}
: undefined,
category: 'user',
originalSource: part.source,
}
}
/**
* 从 API synthetic text part 创建 Attachment
*/
export function fromTextPart(part: { id?: string; text: string }): Attachment {
const text = part.text.trim()
// 简化命名逻辑,直接使用内容摘要
const displayName =
text.length > 30 ? text.slice(0, 30).replace(/\n/g, ' ') + '...' : text.replace(/\n/g, ' ') || 'Context'
return {
id: part.id || crypto.randomUUID(),
type: 'text',
displayName,
content: part.text,
category: 'system',
}
}
/**
* 生成 @ mention 的显示文本
*/
export function getMentionText(attachment: Attachment): string {
if (attachment.type === 'agent') {
return `@${attachment.agentName}`
}
// file/folder 用相对路径
const path = attachment.relativePath || attachment.displayName
return `@${path}`
}
/**
* 判断附件是否有可展开的内容
*/
export function hasExpandableContent(attachment: Attachment): boolean {
const isImage = attachment.mime?.startsWith('image/')
switch (attachment.type) {
case 'file':
// 图片需要 url,其他文件需要 content 或 relativePath
return isImage ? !!attachment.url : !!attachment.content || !!attachment.relativePath
case 'folder':
return !!attachment.relativePath
case 'agent':
return !!attachment.agentName
case 'skill':
return !!attachment.content
case 'text':
return !!attachment.content
default:
return false
}
}
export function getAttachmentIcon(attachment: Attachment): { Icon: React.FC; colorClass: string } {
const isImage = attachment.mime?.startsWith('image/')
if (isImage) {
return { Icon: ImageIcon, colorClass: 'text-accent-secondary-100' }
}
switch (attachment.type) {
case 'file':
return { Icon: FileIcon, colorClass: 'text-info-100' }
case 'folder':
return { Icon: FolderIcon, colorClass: 'text-warning-100' }
case 'agent':
return { Icon: AgentIcon, colorClass: 'text-accent-main-100' }
case 'text':
return { Icon: TerminalIcon, colorClass: 'text-text-400' }
case 'command':
return { Icon: TerminalIcon, colorClass: 'text-accent-secondary-100' }
case 'skill':
return { Icon: ThinkingIcon, colorClass: 'text-info-100' }
default:
return { Icon: FileIcon, colorClass: 'text-text-400' }
}
}