Files
simstudioai--sim/apps/sim/tools/microsoft_teams/read_chat.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

190 lines
5.8 KiB
TypeScript

import type {
MicrosoftTeamsReadResponse,
MicrosoftTeamsToolParams,
} from '@/tools/microsoft_teams/types'
import {
downloadAllReferenceAttachments,
extractMessageAttachments,
fetchHostedContentsForChatMessage,
} from '@/tools/microsoft_teams/utils'
import type { ToolConfig } from '@/tools/types'
export const readChatTool: ToolConfig<MicrosoftTeamsToolParams, MicrosoftTeamsReadResponse> = {
id: 'microsoft_teams_read_chat',
name: 'Read Microsoft Teams Chat',
description: 'Read content from a Microsoft Teams chat',
version: '1.0',
errorExtractor: 'nested-error-object',
oauth: {
required: true,
provider: 'microsoft-teams',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'The access token for the Microsoft Teams API',
},
chatId: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description:
'The ID of the chat to read from (e.g., "19:abc123def456@thread.v2" - from chat listings)',
},
includeAttachments: {
type: 'boolean',
required: false,
visibility: 'user-only',
description: 'Download and include message attachments (hosted contents) into storage',
},
},
request: {
url: (params) => {
const chatId = params.chatId?.trim()
if (!chatId) {
throw new Error('Chat ID is required')
}
return `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chatId)}/messages?$top=50&$orderby=createdDateTime desc`
},
method: 'GET',
headers: (params) => {
if (!params.accessToken) {
throw new Error('Access token is required')
}
return {
Authorization: `Bearer ${params.accessToken}`,
}
},
},
transformResponse: async (response: Response, params?: MicrosoftTeamsToolParams) => {
const data = await response.json()
const messages = data.value || []
if (messages.length === 0) {
return {
success: true,
output: {
content: 'No messages found in this chat.',
metadata: {
chatId: '',
messageCount: 0,
messages: [],
totalAttachments: 0,
attachmentTypes: [],
},
},
}
}
const processedMessages = await Promise.all(
messages.map(async (message: any) => {
const content = message.body?.content || 'No content'
const messageId = message.id
const attachments = extractMessageAttachments(message)
let uploaded: any[] = []
if (params?.includeAttachments && params.accessToken && params.chatId && messageId) {
try {
const hostedContents = await fetchHostedContentsForChatMessage({
accessToken: params.accessToken,
chatId: params.chatId,
messageId,
})
uploaded.push(...hostedContents)
const referenceFiles = await downloadAllReferenceAttachments({
accessToken: params.accessToken,
attachments,
})
uploaded.push(...referenceFiles)
} catch (_e) {
uploaded = []
}
}
return {
id: messageId,
content: content, // Keep original content without modification
sender: message.from?.user?.displayName || 'Unknown',
timestamp: message.createdDateTime,
messageType: message.messageType || 'message',
attachments, // Raw attachment metadata
uploadedFiles: uploaded, // Uploaded file infos (paths/keys)
}
})
)
// Format the messages into a readable text (no attachment info in content)
const formattedMessages = processedMessages
.map((message: any) => {
const sender = message.sender
const timestamp = message.timestamp
? new Date(message.timestamp).toLocaleString()
: 'Unknown time'
return `[${timestamp}] ${sender}: ${message.content}`
})
.join('\n\n')
// Calculate attachment statistics
const allAttachments = processedMessages.flatMap((msg: any) => msg.attachments || [])
const attachmentTypes: string[] = []
const seenTypes = new Set<string>()
allAttachments.forEach((att: any) => {
if (
att.contentType &&
typeof att.contentType === 'string' &&
!seenTypes.has(att.contentType)
) {
attachmentTypes.push(att.contentType)
seenTypes.add(att.contentType)
}
})
// Create document metadata
const metadata = {
chatId: messages[0]?.chatId || params?.chatId || '',
messageCount: messages.length,
totalAttachments: allAttachments.length,
attachmentTypes,
messages: processedMessages,
}
// Flatten uploaded files across all messages for convenience
const flattenedUploads = processedMessages.flatMap((m: any) => m.uploadedFiles || [])
return {
success: true,
output: {
content: formattedMessages,
metadata,
attachments: flattenedUploads,
},
}
},
outputs: {
success: { type: 'boolean', description: 'Teams chat read operation success status' },
messageCount: { type: 'number', description: 'Number of messages retrieved from chat' },
chatId: { type: 'string', description: 'ID of the chat that was read from' },
messages: { type: 'array', description: 'Array of chat message objects' },
attachmentCount: { type: 'number', description: 'Total number of attachments found' },
attachmentTypes: { type: 'array', description: 'Types of attachments found' },
content: { type: 'string', description: 'Formatted content of chat messages' },
attachments: {
type: 'file[]',
description: 'Uploaded attachments for convenience (flattened)',
},
},
}