root commited on
Commit
bfb5328
·
1 Parent(s): 7e06208

协作 AI:长资料自动分段发送

Browse files
Files changed (1) hide show
  1. fanqie/agent.js +38 -3
fanqie/agent.js CHANGED
@@ -436,6 +436,20 @@ module.exports = function buildAgent(express, projects) {
436
  return i >= 0 ? items[i] : patch
437
  })
438
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
  const recState = x => x.state || (x.ok ? 'done' : 'error')
440
  const jobs = new Map() // ask_id -> { st, promise }
441
  function startJob(pid, rec, cfg, message, attachOnly = false) {
@@ -447,7 +461,27 @@ module.exports = function buildAgent(express, projects) {
447
  if (cfg.mode === 'surf') {
448
  await surfEnsure(cfg)
449
  const instant = !!cfg.instant
450
- await surfConverse(cfg, st, { sid: rec.session_id, message: attachOnly ? '' : message, model: cfg.surf_model || 'pro', effort: cfg.surf_effort || '', instant })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  if (st.cancelled) throw bad('已停止')
452
  if (st.error && !st.text.trim()) throw Object.assign(bad(st.error.message), { code: st.error.code })
453
  reply = st.text
@@ -486,7 +520,8 @@ module.exports = function buildAgent(express, projects) {
486
  await surfEnsure(cfg)
487
  const on = await surfStreaming(cfg, x.session_id)
488
  if (on === true) { startJob(pid, x, cfg, '', true); return withLive(x) }
489
- const reply = await surfLastReply(cfg, x.session_id)
 
490
  return (await patchRec(pid, x.ask_id, reply.trim() ? { state: 'done', ok: true, reply: reply.slice(0, 80000), recovered: true } : { state: 'error', ok: false, error: '服务重启后没能取回回复,请重新提问' })) || x
491
  } catch (e) { return x }
492
  }
@@ -528,7 +563,7 @@ module.exports = function buildAgent(express, projects) {
528
  session_id: sid, url: sid ? `${SURF_WEB}/chat/${sid}` : null, continued_from: cont ? cont.ask_id : null,
529
  }
530
  await patchRec(pid, rec.ask_id, rec, true)
531
- const job = startJob(pid, { ...rec, _system: system, _prompt: prompt }, cfg, message)
532
  const wait = Math.min(270, Math.max(0, b.wait == null ? 50 : Number(b.wait) || 0))
533
  if (wait) await Promise.race([job.promise, sleep(wait * 1000)])
534
  const d = await readJson(logKey(pid), { items: [] })
 
436
  return i >= 0 ? items[i] : patch
437
  })
438
  }
439
+ // 按行切分长消息(Surf 单条约 3.2 万字以内)
440
+ const PART_MAX = 28000
441
+ function splitMsg(msg) {
442
+ if (msg.length <= PART_MAX) return [msg]
443
+ const out = []; let cur = ''
444
+ for (const line of msg.split('\n')) {
445
+ let l = line
446
+ while (l.length > PART_MAX) { if (cur) { out.push(cur); cur = '' } out.push(l.slice(0, PART_MAX)); l = l.slice(PART_MAX) }
447
+ if (cur.length + l.length + 1 > PART_MAX) { out.push(cur); cur = '' }
448
+ cur += (cur ? '\n' : '') + l
449
+ }
450
+ if (cur) out.push(cur)
451
+ return out
452
+ }
453
  const recState = x => x.state || (x.ok ? 'done' : 'error')
454
  const jobs = new Map() // ask_id -> { st, promise }
455
  function startJob(pid, rec, cfg, message, attachOnly = false) {
 
461
  if (cfg.mode === 'surf') {
462
  await surfEnsure(cfg)
463
  const instant = !!cfg.instant
464
+ const conv = { sid: rec.session_id, model: cfg.surf_model || 'pro', effort: cfg.surf_effort || '', instant }
465
+ let send = attachOnly ? '' : message, preCost = 0
466
+ const parts = attachOnly ? [] : splitMsg(message)
467
+ if (parts.length > 1) { // Surf 单条消息有长度上限:同一会话里分段发,最后一段再请它作答
468
+ const n = parts.length
469
+ for (let i = 0; i < n - 1; i++) {
470
+ if (st.cancelled) break
471
+ st.text = `(正在分段发送资料 ${i + 1}/${n}…)`
472
+ const s1 = { text: '', turn: null, lastId: null, done: false, error: null, result: null, sawChunk: false, events: 0, tools: 0 }
473
+ st.abort = () => { s1.cancelled = true; try { s1.abort && s1.abort() } catch {} }
474
+ await surfConverse(cfg, s1, { ...conv, message: `【分段发送 ${i + 1}/${n}】这是一条长消息的第 ${i + 1} 部分,还没发完。请只回复「已收到 ${i + 1}/${n}」,先不要分析。\n\n${parts[i]}` })
475
+ if (s1.error && !s1.text.trim()) throw Object.assign(bad(`分段发送第 ${i + 1}/${n} 段失败:${s1.error.message}`), { code: s1.error.code })
476
+ preCost += Number(s1.result?.cost_usd) || 0
477
+ await patchRec(pid, rec.ask_id, { parts_sent: i + 1, parts: n }).catch(() => {})
478
+ }
479
+ if (st.cancelled) throw bad('已停止')
480
+ st.text = ''
481
+ send = `【分段发送 ${n}/${n}】这是最后一部分。请把第 1~${n} 部分当成同一条完整消息,按其中【任务】的要求正式作答。\n\n${parts[n - 1]}${rec._q ? `\n\n(任务原文再附一次:)\n${rec._q}` : ''}`
482
+ }
483
+ await surfConverse(cfg, st, { ...conv, message: send })
484
+ if (preCost && st.result) st.result.cost_usd = (Number(st.result.cost_usd) || 0) + preCost
485
  if (st.cancelled) throw bad('已停止')
486
  if (st.error && !st.text.trim()) throw Object.assign(bad(st.error.message), { code: st.error.code })
487
  reply = st.text
 
520
  await surfEnsure(cfg)
521
  const on = await surfStreaming(cfg, x.session_id)
522
  if (on === true) { startJob(pid, x, cfg, '', true); return withLive(x) }
523
+ let reply = await surfLastReply(cfg, x.session_id)
524
+ if (/^\s*已收到\s*\d+\s*\/\s*\d+/.test(reply) && reply.length < 200) reply = ''
525
  return (await patchRec(pid, x.ask_id, reply.trim() ? { state: 'done', ok: true, reply: reply.slice(0, 80000), recovered: true } : { state: 'error', ok: false, error: '服务重启后没能取回回复,请重新提问' })) || x
526
  } catch (e) { return x }
527
  }
 
563
  session_id: sid, url: sid ? `${SURF_WEB}/chat/${sid}` : null, continued_from: cont ? cont.ask_id : null,
564
  }
565
  await patchRec(pid, rec.ask_id, rec, true)
566
+ const job = startJob(pid, { ...rec, _system: system, _prompt: prompt, _q: question.length <= 4000 ? question : '' }, cfg, message)
567
  const wait = Math.min(270, Math.max(0, b.wait == null ? 50 : Number(b.wait) || 0))
568
  if (wait) await Promise.race([job.promise, sleep(wait * 1000)])
569
  const d = await readJson(logKey(pid), { items: [] })