11import * as lark from "@larksuiteoapi/node-sdk" ;
22import { OverflowConfig , CardTableConfig } from "../config.js" ;
33import { sanitizeContent , formatWarnings , countTables , optimizeForCard } from "../format/index.js" ;
4- import { buildCard , buildSimpleCard , buildMarkdownCard , buildThinkingPanel , collapsiblePanel , markdown , hr , buildHeader , buildFooterElement , buildStatsTags } from "../card/index.js" ;
4+ import { buildCard , buildMarkdownCard , buildThinkingPanel , collapsiblePanel , markdown , hr , buildHeader , buildFooterElement , buildStatsTags } from "../card/index.js" ;
55import type { CardBuildOptions } from "../card/index.js" ;
6- import { getTenantAccessToken , checkTokenExpiry } from "./lark.js" ;
7- import { createOverflowDocument , registerDocument , cleanupOldDocuments } from "./document.js" ;
6+ import { getTenantAccessToken , checkTokenExpiry , replyMessage , patchMessage , sendMessage } from "./lark.js" ;
7+ import { createAndRegisterOverflowDoc , cleanupOldDocuments } from "./document.js" ;
88import type { DocumentMeta } from "../format/index.js" ;
99
1010// ── 共享类型 ──────────────────────────────────────────────────
@@ -67,11 +67,11 @@ export async function replyText(
6767 text : string
6868) : Promise < string > {
6969 const card = buildMarkdownCard ( text ) ;
70- const res = await ( client . im . message as any ) . reply ( {
71- path : { message_id : rootMsgId } ,
72- data : { content : JSON . stringify ( card ) , msg_type : "interactive" , reply_in_thread : false } ,
70+ const { messageId } = await replyMessage ( client , rootMsgId , {
71+ content : JSON . stringify ( card ) ,
72+ msgType : "interactive" ,
7373 } ) ;
74- return res . data ?. message_id ?? "" ;
74+ return messageId ;
7575}
7676
7777export async function updateText (
@@ -250,14 +250,14 @@ async function sendMessageChunk(
250250 try {
251251 const optimizedContent = optimizeForCard ( finalContent ) ;
252252 const card = buildMarkdownCard ( optimizedContent , [ ] , { thinking, cardTitle } ) ;
253- await ( client . im . message as any ) . reply ( {
254- path : { message_id : rootMsgId } ,
255- data : { content : JSON . stringify ( card ) , msg_type : "interactive" , reply_in_thread : false } ,
253+ await replyMessage ( client , rootMsgId , {
254+ content : JSON . stringify ( card ) ,
255+ msgType : "interactive" ,
256256 } ) ;
257257 } catch {
258- await ( client . im . message as any ) . reply ( {
259- path : { message_id : rootMsgId } ,
260- data : { content : JSON . stringify ( { text : finalContent } ) , msg_type : "text" , reply_in_thread : false } ,
258+ await replyMessage ( client , rootMsgId , {
259+ content : JSON . stringify ( { text : finalContent } ) ,
260+ msgType : "text" ,
261261 } ) ;
262262 }
263263}
@@ -314,10 +314,7 @@ async function patchOrCreateCard(
314314 if ( waitingMsgId ) {
315315 try {
316316 const card = buildMarkdownCard ( content ) ;
317- await ( client . im . message as any ) . patch ( {
318- path : { message_id : waitingMsgId } ,
319- data : { content : JSON . stringify ( card ) } ,
320- } ) ;
317+ await patchMessage ( client , waitingMsgId , JSON . stringify ( card ) ) ;
321318 return true ;
322319 } catch { }
323320 }
@@ -339,11 +336,11 @@ async function replyWithDocument(
339336 let waitingMsgId : string | undefined ;
340337 try {
341338 const waitingCard = buildMarkdownCard ( "📝 内容较长,正在写入云文档..." ) ;
342- const waitRes = await ( client . im . message as any ) . reply ( {
343- path : { message_id : rootMsgId } ,
344- data : { content : JSON . stringify ( waitingCard ) , msg_type : "interactive" , reply_in_thread : false } ,
339+ const { messageId : waitId } = await replyMessage ( client , rootMsgId , {
340+ content : JSON . stringify ( waitingCard ) ,
341+ msgType : "interactive" ,
345342 } ) ;
346- waitingMsgId = waitRes ?. data ?. message_id ;
343+ waitingMsgId = waitId || undefined ;
347344 } catch { }
348345
349346 try {
@@ -352,9 +349,7 @@ async function replyWithDocument(
352349 const cleanupConfig = context . overflow . document . cleanup ;
353350 await cleanupOldDocuments ( token , cleanupConfig . max_docs , context . profile ) ;
354351
355- const { docUrl, docId, warnings : docWarnings } = await createOverflowDocument ( token , title , markdown , originalMessage , meta ) ;
356-
357- registerDocument ( docId , context . profile ) ;
352+ const { docUrl, docId, warnings : docWarnings } = await createAndRegisterOverflowDoc ( token , title , markdown , originalMessage , meta , context . profile ) ;
358353
359354 let replyMsg = `📝 内容较长,已写入云文档:${ docUrl } ` ;
360355 if ( docWarnings . length > 0 ) {
@@ -386,10 +381,7 @@ async function replyWithDocument(
386381
387382 if ( waitingMsgId ) {
388383 try {
389- await ( client . im . message as any ) . patch ( {
390- path : { message_id : waitingMsgId } ,
391- data : { content : JSON . stringify ( docCard ) } ,
392- } ) ;
384+ await patchMessage ( client , waitingMsgId , JSON . stringify ( docCard ) ) ;
393385 return ;
394386 } catch { }
395387 }
@@ -444,11 +436,11 @@ export async function sendToolCard(
444436
445437 const content = `${ label } \n\`${ detail } \`\n${ statusIcon } ` ;
446438 const card = buildMarkdownCard ( content ) ;
447- const res = await ( client . im . message as any ) . reply ( {
448- path : { message_id : rootMsgId } ,
449- data : { content : JSON . stringify ( card ) , msg_type : "interactive" , reply_in_thread : false } ,
439+ const { messageId } = await replyMessage ( client , rootMsgId , {
440+ content : JSON . stringify ( card ) ,
441+ msgType : "interactive" ,
450442 } ) ;
451- return res . data ?. message_id ?? "" ;
443+ return messageId ;
452444}
453445
454446export async function updateToolCard (
@@ -483,38 +475,4 @@ export async function updateToolCard(
483475}
484476
485477// ── 卡片构建 ─────────────────────────────────────────────────
486- // buildMarkdownCard 已迁移至 ../card/card.ts,通过 barrel re-export 使用
487-
488- // ── 发送辅助 ─────────────────────────────────────────────────
489-
490- export async function sendMarkdownCardMessage (
491- client : lark . Client ,
492- chatId : string ,
493- content : string ,
494- options ?: {
495- rootMsgId ?: string ;
496- reply ?: boolean ;
497- } ,
498- ) : Promise < void > {
499- const { content : sanitizedContent , warnings } = sanitizeContent ( content ) ;
500- const card = buildSimpleCard ( sanitizedContent , warnings ) ;
501-
502- if ( options ?. reply && options . rootMsgId ) {
503- await client . im . message . reply ( {
504- path : { message_id : options . rootMsgId } ,
505- data : {
506- msg_type : "interactive" ,
507- content : JSON . stringify ( card ) ,
508- } ,
509- } ) ;
510- } else {
511- await client . im . message . create ( {
512- params : { receive_id_type : "chat_id" } ,
513- data : {
514- receive_id : chatId ,
515- msg_type : "interactive" ,
516- content : JSON . stringify ( card ) ,
517- } ,
518- } ) ;
519- }
520- }
478+ // buildMarkdownCard 已迁移至 ../card/compose.ts,通过 barrel re-export 使用
0 commit comments