Files
simstudioai--sim/apps/sim/tools/google_contacts/update.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

189 lines
5.3 KiB
TypeScript

import { createLogger } from '@sim/logger'
import {
DEFAULT_PERSON_FIELDS,
type GoogleContactsUpdateParams,
type GoogleContactsUpdateResponse,
PEOPLE_API_BASE,
transformPerson,
} from '@/tools/google_contacts/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('GoogleContactsUpdate')
export const updateTool: ToolConfig<GoogleContactsUpdateParams, GoogleContactsUpdateResponse> = {
id: 'google_contacts_update',
name: 'Google Contacts Update',
description: 'Update an existing contact in Google Contacts',
version: '1.0.0',
oauth: {
required: true,
provider: 'google-contacts',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Access token for Google People API',
},
resourceName: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Resource name of the contact (e.g., people/c1234567890)',
},
etag: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'ETag from a previous get request (required for concurrency control)',
},
givenName: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated first name',
},
familyName: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated last name',
},
email: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated email address',
},
emailType: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Email type: home, work, or other',
},
phone: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated phone number',
},
phoneType: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Phone type: mobile, home, work, or other',
},
organization: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated organization/company name',
},
jobTitle: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated job title',
},
notes: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Updated notes or biography',
},
},
request: {
url: (params: GoogleContactsUpdateParams) => {
const updateFields: string[] = []
if (params.givenName || params.familyName) updateFields.push('names')
if (params.email) updateFields.push('emailAddresses')
if (params.phone) updateFields.push('phoneNumbers')
if (params.organization || params.jobTitle) updateFields.push('organizations')
if (params.notes) updateFields.push('biographies')
if (updateFields.length === 0) {
throw new Error('At least one field to update must be provided')
}
const updatePersonFields = updateFields.join(',')
return `${PEOPLE_API_BASE}/${params.resourceName.trim()}:updateContact?updatePersonFields=${updatePersonFields}&personFields=${DEFAULT_PERSON_FIELDS}`
},
method: 'PATCH',
headers: (params: GoogleContactsUpdateParams) => ({
Authorization: `Bearer ${params.accessToken}`,
'Content-Type': 'application/json',
}),
body: (params: GoogleContactsUpdateParams) => {
const person: Record<string, any> = {
etag: params.etag,
metadata: { sources: [{ type: 'CONTACT', etag: params.etag }] },
}
if (params.givenName || params.familyName) {
person.names = [
{
...(params.givenName ? { givenName: params.givenName } : {}),
...(params.familyName ? { familyName: params.familyName } : {}),
},
]
}
if (params.email) {
person.emailAddresses = [{ value: params.email, type: params.emailType || 'other' }]
}
if (params.phone) {
person.phoneNumbers = [{ value: params.phone, type: params.phoneType || 'mobile' }]
}
if (params.organization || params.jobTitle) {
person.organizations = [
{
...(params.organization ? { name: params.organization } : {}),
...(params.jobTitle ? { title: params.jobTitle } : {}),
},
]
}
if (params.notes) {
person.biographies = [{ value: params.notes, contentType: 'TEXT_PLAIN' }]
}
return person
},
},
transformResponse: async (response: Response) => {
const data = await response.json()
if (!response.ok) {
const errorMessage = data.error?.message || 'Failed to update contact'
logger.error('Failed to update contact', { status: response.status, error: errorMessage })
throw new Error(errorMessage)
}
const contact = transformPerson(data)
return {
success: true,
output: {
content: `Contact "${contact.displayName || contact.resourceName}" updated successfully`,
metadata: contact,
},
}
},
outputs: {
content: { type: 'string', description: 'Contact update confirmation message' },
metadata: {
type: 'json',
description: 'Updated contact metadata',
},
},
}