Files
sim/apps/sim/tools/intercom/create_company.ts

174 lines
4.5 KiB
TypeScript

import { createLogger } from '@/lib/logs/console/logger'
import type { ToolConfig } from '@/tools/types'
import { buildIntercomUrl, handleIntercomError } from './types'
const logger = createLogger('IntercomCreateCompany')
export interface IntercomCreateCompanyParams {
accessToken: string
company_id: string
name?: string
website?: string
plan?: string
size?: number
industry?: string
monthly_spend?: number
custom_attributes?: string
remote_created_at?: number
}
export interface IntercomCreateCompanyResponse {
success: boolean
output: {
company: any
metadata: {
operation: 'create_company'
companyId: string
}
success: boolean
}
}
export const intercomCreateCompanyTool: ToolConfig<
IntercomCreateCompanyParams,
IntercomCreateCompanyResponse
> = {
id: 'intercom_create_company',
name: 'Create Company in Intercom',
description: 'Create or update a company in Intercom',
version: '1.0.0',
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Intercom API access token',
},
company_id: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Your unique identifier for the company',
},
name: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The name of the company',
},
website: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The company website',
},
plan: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The company plan name',
},
size: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'The number of employees in the company',
},
industry: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The industry the company operates in',
},
monthly_spend: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description:
'How much revenue the company generates for your business. Note: This field truncates floats to whole integers (e.g., 155.98 becomes 155)',
},
custom_attributes: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Custom attributes as JSON object',
},
remote_created_at: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'The time the company was created by you as a Unix timestamp',
},
},
request: {
url: () => buildIntercomUrl('/companies'),
method: 'POST',
headers: (params) => ({
Authorization: `Bearer ${params.accessToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
'Intercom-Version': '2.14',
}),
body: (params) => {
const company: any = {
company_id: params.company_id,
}
if (params.name) company.name = params.name
if (params.website) company.website = params.website
if (params.plan) company.plan = params.plan
if (params.size) company.size = params.size
if (params.industry) company.industry = params.industry
if (params.monthly_spend) company.monthly_spend = params.monthly_spend
if (params.custom_attributes) {
try {
company.custom_attributes = JSON.parse(params.custom_attributes)
} catch (error) {
logger.warn('Failed to parse custom attributes', { error })
}
}
if (params.remote_created_at) company.remote_created_at = params.remote_created_at
return company
},
},
transformResponse: async (response: Response) => {
if (!response.ok) {
const data = await response.json()
handleIntercomError(data, response.status, 'create_company')
}
const data = await response.json()
return {
success: true,
output: {
company: data,
metadata: {
operation: 'create_company' as const,
companyId: data.id,
},
success: true,
},
}
},
outputs: {
success: { type: 'boolean', description: 'Operation success status' },
output: {
type: 'object',
description: 'Created or updated company data',
properties: {
company: { type: 'object', description: 'Company object' },
metadata: { type: 'object', description: 'Operation metadata' },
success: { type: 'boolean', description: 'Operation success' },
},
},
},
}