Files
simstudioai--sim/apps/sim/tools/twilio_voice/list_calls.ts
T
wehub-resource-sync d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

184 lines
6.0 KiB
TypeScript

import { createLogger } from '@sim/logger'
import type { TwilioListCallsOutput, TwilioListCallsParams } from '@/tools/twilio_voice/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('TwilioVoiceListCallsTool')
export const listCallsTool: ToolConfig<TwilioListCallsParams, TwilioListCallsOutput> = {
id: 'twilio_voice_list_calls',
name: 'Twilio Voice List Calls',
description: 'Retrieve a list of calls made to and from an account.',
version: '1.0.0',
params: {
accountSid: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Twilio Account SID',
},
authToken: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Twilio Auth Token',
},
to: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Filter by calls to this phone number in E.164 format (e.g., +14155551234)',
},
from: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Filter by calls from this phone number in E.164 format (e.g., +14155559876)',
},
status: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description:
'Filter by call status (e.g., queued, ringing, in-progress, completed, busy, failed, no-answer, canceled)',
},
startTimeAfter: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Filter calls that started on or after this date (YYYY-MM-DD)',
},
startTimeBefore: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Filter calls that started on or before this date (YYYY-MM-DD)',
},
pageSize: {
type: 'number',
required: false,
visibility: 'user-only',
description: 'Number of records to return (max 1000, default 50)',
},
},
request: {
url: (params) => {
if (!params.accountSid) {
throw new Error('Twilio Account SID is required')
}
if (!params.accountSid.startsWith('AC')) {
throw new Error(
`Invalid Account SID format. Account SID must start with "AC" (you provided: ${params.accountSid.substring(0, 2)}...)`
)
}
const baseUrl = `https://api.twilio.com/2010-04-01/Accounts/${params.accountSid}/Calls.json`
const queryParams = new URLSearchParams()
if (params.to) queryParams.append('To', params.to)
if (params.from) queryParams.append('From', params.from)
if (params.status) queryParams.append('Status', params.status)
if (params.startTimeAfter) queryParams.append('StartTime>', params.startTimeAfter)
if (params.startTimeBefore) queryParams.append('StartTime<', params.startTimeBefore)
if (params.pageSize) queryParams.append('PageSize', Number(params.pageSize).toString())
const queryString = queryParams.toString()
return queryString ? `${baseUrl}?${queryString}` : baseUrl
},
method: 'GET',
headers: (params) => {
if (!params.accountSid || !params.authToken) {
throw new Error('Twilio credentials are required')
}
const authToken = Buffer.from(`${params.accountSid}:${params.authToken}`).toString('base64')
return {
Authorization: `Basic ${authToken}`,
}
},
},
transformResponse: async (response, params) => {
const data = await response.json()
logger.info('Twilio List Calls Response:', { total: data.calls?.length || 0 })
if (data.error_code) {
return {
success: false,
output: {
success: false,
calls: [],
error: data.message || data.error_message || 'Failed to retrieve calls',
},
error: data.message || data.error_message || 'Failed to retrieve calls',
}
}
const authToken = Buffer.from(`${params?.accountSid}:${params?.authToken}`).toString('base64')
const calls = await Promise.all(
(data.calls || []).map(async (call: any) => {
let recordingSids: string[] = []
if (call.subresource_uris?.recordings) {
try {
const recordingsUrl = `https://api.twilio.com${call.subresource_uris.recordings}`
const recordingsResponse = await fetch(recordingsUrl, {
method: 'GET',
headers: { Authorization: `Basic ${authToken}` },
})
if (recordingsResponse.ok) {
const recordingsData = await recordingsResponse.json()
recordingSids = (recordingsData.recordings || []).map((rec: any) => rec.sid)
}
} catch (error) {
logger.warn(`Failed to fetch recordings for call ${call.sid}:`, error)
}
}
return {
callSid: call.sid,
from: call.from,
to: call.to,
status: call.status,
direction: call.direction,
duration: call.duration ? Number.parseInt(call.duration, 10) : null,
price: call.price,
priceUnit: call.price_unit,
startTime: call.start_time,
endTime: call.end_time,
dateCreated: call.date_created,
recordingSids,
}
})
)
logger.info('Transformed calls with recordings:', {
totalCalls: calls.length,
callsWithRecordings: calls.filter((c) => c.recordingSids.length > 0).length,
})
return {
success: true,
output: {
success: true,
calls,
total: calls.length,
page: data.page,
pageSize: data.page_size,
},
error: undefined,
}
},
outputs: {
success: { type: 'boolean', description: 'Whether the calls were successfully retrieved' },
calls: { type: 'array', description: 'Array of call objects' },
total: { type: 'number', description: 'Total number of calls returned' },
page: { type: 'number', description: 'Current page number' },
pageSize: { type: 'number', description: 'Number of calls per page' },
error: { type: 'string', description: 'Error message if retrieval failed' },
},
}