1+ import { createLogger } from '@/lib/logs/console-logger'
12import type { ToolConfig } from '../types'
3+ import { extractMessageAttachments } from './attachment-utils'
24import type { MicrosoftTeamsReadResponse , MicrosoftTeamsToolParams } from './types'
35
6+ const logger = createLogger ( 'MicrosoftTeamsReadChannel' )
7+
48export const readChannelTool : ToolConfig < MicrosoftTeamsToolParams , MicrosoftTeamsReadResponse > = {
59 id : 'microsoft_teams_read_channel' ,
610 name : 'Read Microsoft Teams Channel' ,
711 description : 'Read content from a Microsoft Teams channel' ,
8- version : '1.0 ' ,
12+ version : '1.1 ' ,
913 oauth : {
1014 required : true ,
1115 provider : 'microsoft-teams' ,
@@ -60,10 +64,16 @@ export const readChannelTool: ToolConfig<MicrosoftTeamsToolParams, MicrosoftTeam
6064 }
6165 } ,
6266 } ,
63- transformResponse : async ( response : Response ) => {
67+ transformResponse : async ( response : Response , params ?: MicrosoftTeamsToolParams ) => {
6468 if ( ! response . ok ) {
6569 const errorText = await response . text ( )
66- throw new Error ( `Failed to read Microsoft Teams channel: ${ errorText } ` )
70+ logger . error (
71+ `Microsoft Teams channel API error: ${ response . status } ${ response . statusText } ` ,
72+ errorText
73+ )
74+ throw new Error (
75+ `Failed to read Microsoft Teams channel: ${ response . status } ${ response . statusText } - ${ errorText } `
76+ )
6777 }
6878
6979 const data = await response . json ( )
@@ -81,36 +91,88 @@ export const readChannelTool: ToolConfig<MicrosoftTeamsToolParams, MicrosoftTeam
8191 channelId : '' ,
8292 messageCount : 0 ,
8393 messages : [ ] ,
94+ totalAttachments : 0 ,
95+ attachmentTypes : [ ] ,
8496 } ,
8597 } ,
8698 }
8799 }
88100
89- // Format the messages into a readable text
90- const formattedMessages = messages
91- . map ( ( message : any ) => {
101+ if ( ! params ?. teamId || ! params ?. channelId ) {
102+ throw new Error ( 'Missing required parameters: teamId and channelId' )
103+ }
104+ // Process messages with attachments
105+ const processedMessages = messages . map ( ( message : any , index : number ) => {
106+ try {
92107 const content = message . body ?. content || 'No content'
93- const sender = message . from ?. user ?. displayName || 'Unknown sender'
94- const timestamp = message . createdDateTime
95- ? new Date ( message . createdDateTime ) . toLocaleString ( )
108+ const messageId = message . id
109+
110+ const attachments = extractMessageAttachments ( message )
111+
112+ let sender = 'Unknown'
113+ if ( message . from ?. user ?. displayName ) {
114+ sender = message . from . user . displayName
115+ } else if ( message . messageType === 'systemEventMessage' ) {
116+ sender = 'System'
117+ }
118+
119+ return {
120+ id : messageId ,
121+ content : content ,
122+ sender,
123+ timestamp : message . createdDateTime ,
124+ messageType : message . messageType || 'message' ,
125+ attachments,
126+ }
127+ } catch ( error ) {
128+ logger . error ( `Error processing message at index ${ index } :` , error )
129+ return {
130+ id : message . id || `unknown-${ index } ` ,
131+ content : 'Error processing message' ,
132+ sender : 'Unknown' ,
133+ timestamp : message . createdDateTime || new Date ( ) . toISOString ( ) ,
134+ messageType : 'error' ,
135+ attachments : [ ] ,
136+ }
137+ }
138+ } )
139+
140+ // Format the messages into a readable text (no attachment info in content)
141+ const formattedMessages = processedMessages
142+ . map ( ( message : any ) => {
143+ const sender = message . sender
144+ const timestamp = message . timestamp
145+ ? new Date ( message . timestamp ) . toLocaleString ( )
96146 : 'Unknown time'
97147
98- return `[${ timestamp } ] ${ sender } : ${ content } `
148+ return `[${ timestamp } ] ${ sender } : ${ message . content } `
99149 } )
100150 . join ( '\n\n' )
101151
152+ // Calculate attachment statistics
153+ const allAttachments = processedMessages . flatMap ( ( msg : any ) => msg . attachments || [ ] )
154+ const attachmentTypes : string [ ] = [ ]
155+ const seenTypes = new Set < string > ( )
156+
157+ allAttachments . forEach ( ( att : any ) => {
158+ if (
159+ att . contentType &&
160+ typeof att . contentType === 'string' &&
161+ ! seenTypes . has ( att . contentType )
162+ ) {
163+ attachmentTypes . push ( att . contentType )
164+ seenTypes . add ( att . contentType )
165+ }
166+ } )
167+
102168 // Create document metadata
103169 const metadata = {
104- teamId : messages [ 0 ] ?. channelIdentity ?. teamId || '' ,
105- channelId : messages [ 0 ] ?. channelIdentity ?. channelId || '' ,
170+ teamId : messages [ 0 ] ?. channelIdentity ?. teamId || params . teamId || '' ,
171+ channelId : messages [ 0 ] ?. channelIdentity ?. channelId || params . channelId || '' ,
106172 messageCount : messages . length ,
107- messages : messages . map ( ( msg : any ) => ( {
108- id : msg . id ,
109- content : msg . body ?. content || '' ,
110- sender : msg . from ?. user ?. displayName || 'Unknown' ,
111- timestamp : msg . createdDateTime ,
112- messageType : msg . messageType || 'message' ,
113- } ) ) ,
173+ totalAttachments : allAttachments . length ,
174+ attachmentTypes,
175+ messages : processedMessages ,
114176 }
115177
116178 return {
0 commit comments