新增: 多模态图片 URL 转 image part + 修复 / 技能联想浮层定位
前端发送前扫图片扩展 URL 转 ContentPart image(模型原生视觉,非工具下载); SkillMention 移入 .ai-input-wrap relative 内(治 / 浮层定位错位不可见)。
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
<template>
|
||||
<div class="ai-input-area">
|
||||
<SkillMention
|
||||
:skill-open="skillOpen"
|
||||
:filtered-skills="filteredSkills"
|
||||
:skill-index="skillIndex"
|
||||
:pending-skill="pendingSkill"
|
||||
:skill-query="skillQuery"
|
||||
@select="selectSkill"
|
||||
@clear="clearSkill"
|
||||
@update:skill-index="skillIndex = $event"
|
||||
/>
|
||||
<div class="ai-input-wrap">
|
||||
<!-- 技能联想浮层须在 .ai-input-wrap(position:relative)内,
|
||||
否则 .ai-skill-popover 的 absolute 定位错位跑到页面外不可见(/ 未触发根因)。 -->
|
||||
<SkillMention
|
||||
:skill-open="skillOpen"
|
||||
:filtered-skills="filteredSkills"
|
||||
:skill-index="skillIndex"
|
||||
:pending-skill="pendingSkill"
|
||||
:skill-query="skillQuery"
|
||||
@select="selectSkill"
|
||||
@clear="clearSkill"
|
||||
@update:skill-index="skillIndex = $event"
|
||||
/>
|
||||
<!-- F-260614-05 Phase 2b: 待发送图片预览 -->
|
||||
<ImageInput :pending-images="pendingImages" @remove="removePendingImage" />
|
||||
<textarea
|
||||
@@ -103,6 +105,7 @@ import { useI18n } from 'vue-i18n'
|
||||
import { useAiStore } from '../../stores/ai'
|
||||
import { useProjectStore } from '../../stores/project'
|
||||
import { getConvState, textIdle } from '../../composables/ai/useAiEvents'
|
||||
import { extractImageUrlParts } from '../../composables/ai/utils'
|
||||
import SkillMention from './SkillMention.vue'
|
||||
import ImageInput from './ImageInput.vue'
|
||||
import MentionPopover from './MentionPopover.vue'
|
||||
@@ -688,6 +691,14 @@ async function handleSend() {
|
||||
}),
|
||||
]
|
||||
: undefined
|
||||
// F-260614-05 Phase 2c: 扫输入文本里的图片 URL → image 片(url 模式),与粘贴 base64 图合并。
|
||||
// 治会话 01f05167:用户贴 URL 文本 → 没转 parts → 模型只收字符串 → AI fetch 失败 L1 熔断。
|
||||
// text 保留原样(供 audit/纯文本模型),parts 附加 image 让 vision 模型直接看图。
|
||||
// parts 已含 text 片(粘贴图场景)时直接展开追加 url 图;parts 为空且 text 非空时补 text 片对齐上面构造逻辑。
|
||||
const urlParts = extractImageUrlParts(text)
|
||||
const mergedParts: ContentPart[] | undefined = urlParts.length > 0
|
||||
? [...(parts ?? (text ? [{ type: 'text' as const, text } as ContentPart] : [])), ...urlParts]
|
||||
: parts
|
||||
// 生成中不再拦截:sendMessage 会把消息排入待发送队列,完成后自动续发
|
||||
// 先清输入(无论立即发还是入队);队列满/IPC 失败时 catch 回填
|
||||
inputText.value = ''
|
||||
@@ -704,7 +715,7 @@ async function handleSend() {
|
||||
try {
|
||||
// Input Augmentation: 透传 mentionSpans(L0 直接 doSend 注入 + 本地 user 消息挂 mentionSpans 渲染 chip;
|
||||
// L1 入队续发当前不挂 spans,见 useAiSend.sendMessage 设计取舍注释)
|
||||
await store.sendMessage(text, skill?.name, false, parts, snapshotSpans.length > 0 ? snapshotSpans : undefined)
|
||||
await store.sendMessage(text, skill?.name, false, mergedParts, snapshotSpans.length > 0 ? snapshotSpans : undefined)
|
||||
} catch (e) {
|
||||
console.error('[AI] 发送失败:', e)
|
||||
inputText.value = text
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* AI composables 通用纯函数工具(无 Vue 依赖,便于单测)。
|
||||
*
|
||||
* 当前仅含 F-260614-05 Phase 2c 扩展:输入框文本中的图片 URL → ContentPart[] image 片。
|
||||
* 治会话 01f05167:用户贴图片 URL(文本)→ 没转 parts → 模型只收 URL 字符串 →
|
||||
* AI 转 fetch_url/download 失败 L1 熔断卡死。这里在发送前扫 URL 转 image 片,
|
||||
* 让 vision 模型直接拿到图片(provider 端 url 模式转发 image_url{url} 给 OpenAI/商汤)。
|
||||
*/
|
||||
import type { ContentPart } from '../../api/types'
|
||||
|
||||
/**
|
||||
* 图片 URL 正则:
|
||||
* - https?:// 协议头
|
||||
* - \S+? 主机+路径(非贪婪,避免贪婪吞掉后续 URL)
|
||||
* - \.(png|jpg|jpeg|webp|gif) 图片扩展名(大小写不敏感)
|
||||
* - (\?\S*)? 可选查询参数(?foo=bar,直到空白为止)
|
||||
*
|
||||
* g 全局 + i 忽略大小写。不锚定行首/尾,允许 URL 嵌在任意文本里(markdown `` / 纯 URL / 句中)。
|
||||
*
|
||||
* 边界说明:可选组 (?:\?\S*)? 仅在扩展名后紧跟 ? 时才吞后续非空白字符,因此 URL 末尾的标点
|
||||
* (逗号/句号/中文标点/fragment #anchor)不会被吞进 url —— 这是正确行为,vision fetch 拿干净 URL。
|
||||
*/
|
||||
const IMAGE_URL_RE = /https?:\/\/\S+?\.(?:png|jpe?g|webp|gif)(?:\?\S*)?/gi
|
||||
|
||||
/**
|
||||
* 从文本中扫描图片 URL,转成 ContentPart[] image 片(url 模式)。
|
||||
*
|
||||
* - 仅 url 模式(base64 / media_type 留空):vision provider 端按 url 模式直接转发 http URL 给上游,
|
||||
* OpenAI/商汤无需 base64。
|
||||
* - 同 URL 去重(保持首次出现顺序)。
|
||||
* - 非 URL(本地路径 / data URI / 邮件附件)不提取:这些不是 http(s) URL,vision provider 也取不到。
|
||||
* - 非图片扩展(.html/.com/.pdf 等)不提取。
|
||||
*
|
||||
* @param text 输入框文本(原始,未 trim 也可)
|
||||
* @returns image 片数组(可能为空)
|
||||
*/
|
||||
export function extractImageUrlParts(text: string): ContentPart[] {
|
||||
if (!text) return []
|
||||
const seen = new Set<string>()
|
||||
const parts: ContentPart[] = []
|
||||
// 注意:lastIndex 在 g 标志正则上是状态化的,这里每次新建迭代器从 0 开始,无需手动 reset
|
||||
for (const m of text.matchAll(IMAGE_URL_RE)) {
|
||||
const url = m[0]
|
||||
if (seen.has(url)) continue
|
||||
seen.add(url)
|
||||
parts.push({
|
||||
type: 'image',
|
||||
url,
|
||||
base64: null,
|
||||
media_type: null,
|
||||
alt: null,
|
||||
})
|
||||
}
|
||||
return parts
|
||||
}
|
||||
Reference in New Issue
Block a user