Skip to content

Commit 8b676a5

Browse files
committed
improvement(google-contacts): add advanced mode, error handling, and input trimming
- Set mode: 'advanced' on optional fields (emailType, phoneType, notes, pageSize, pageToken, sortOrder) - Add createLogger and response.ok error handling to all 6 tools - Add .trim() on resourceName in get, update, delete URL builders
1 parent 405e2c5 commit 8b676a5

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

apps/sim/blocks/blocks/google_contacts.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const GoogleContactsBlock: BlockConfig<GoogleContactsResponse> = {
8484
{ label: 'Other', id: 'other' },
8585
],
8686
value: () => 'work',
87+
mode: 'advanced',
8788
},
8889
{
8990
id: 'phone',
@@ -104,6 +105,7 @@ export const GoogleContactsBlock: BlockConfig<GoogleContactsResponse> = {
104105
{ label: 'Other', id: 'other' },
105106
],
106107
value: () => 'mobile',
108+
mode: 'advanced',
107109
},
108110
{
109111
id: 'organization',
@@ -125,6 +127,7 @@ export const GoogleContactsBlock: BlockConfig<GoogleContactsResponse> = {
125127
type: 'long-input',
126128
placeholder: 'Additional notes about the contact',
127129
condition: { field: 'operation', value: ['create', 'update'] },
130+
mode: 'advanced',
128131
},
129132

130133
// Get / Update / Delete Fields
@@ -164,13 +167,15 @@ export const GoogleContactsBlock: BlockConfig<GoogleContactsResponse> = {
164167
type: 'short-input',
165168
placeholder: '100',
166169
condition: { field: 'operation', value: ['list', 'search'] },
170+
mode: 'advanced',
167171
},
168172
{
169173
id: 'pageToken',
170174
title: 'Page Token',
171175
type: 'short-input',
172176
placeholder: 'Token from previous list request',
173177
condition: { field: 'operation', value: 'list' },
178+
mode: 'advanced',
174179
},
175180
{
176181
id: 'sortOrder',
@@ -184,6 +189,7 @@ export const GoogleContactsBlock: BlockConfig<GoogleContactsResponse> = {
184189
{ label: 'Last Name (Ascending)', id: 'LAST_NAME_ASCENDING' },
185190
],
186191
value: () => 'LAST_MODIFIED_DESCENDING',
192+
mode: 'advanced',
187193
},
188194
],
189195
tools: {

apps/sim/tools/google_contacts/create.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
DEFAULT_PERSON_FIELDS,
34
type GoogleContactsCreateParams,
@@ -7,6 +8,8 @@ import {
78
} from '@/tools/google_contacts/types'
89
import type { ToolConfig } from '@/tools/types'
910

11+
const logger = createLogger('GoogleContactsCreate')
12+
1013
export const createTool: ToolConfig<GoogleContactsCreateParams, GoogleContactsCreateResponse> = {
1114
id: 'google_contacts_create',
1215
name: 'Google Contacts Create',
@@ -125,6 +128,13 @@ export const createTool: ToolConfig<GoogleContactsCreateParams, GoogleContactsCr
125128

126129
transformResponse: async (response: Response) => {
127130
const data = await response.json()
131+
132+
if (!response.ok) {
133+
const errorMessage = data.error?.message || 'Failed to create contact'
134+
logger.error('Failed to create contact', { status: response.status, error: errorMessage })
135+
throw new Error(errorMessage)
136+
}
137+
128138
const contact = transformPerson(data)
129139

130140
return {

apps/sim/tools/google_contacts/delete.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
type GoogleContactsDeleteParams,
34
type GoogleContactsDeleteResponse,
45
PEOPLE_API_BASE,
56
} from '@/tools/google_contacts/types'
67
import type { ToolConfig } from '@/tools/types'
78

9+
const logger = createLogger('GoogleContactsDelete')
10+
811
export const deleteTool: ToolConfig<GoogleContactsDeleteParams, GoogleContactsDeleteResponse> = {
912
id: 'google_contacts_delete',
1013
name: 'Google Contacts Delete',
@@ -33,7 +36,7 @@ export const deleteTool: ToolConfig<GoogleContactsDeleteParams, GoogleContactsDe
3336

3437
request: {
3538
url: (params: GoogleContactsDeleteParams) =>
36-
`${PEOPLE_API_BASE}/${params.resourceName}:deleteContact`,
39+
`${PEOPLE_API_BASE}/${params.resourceName.trim()}:deleteContact`,
3740
method: 'DELETE',
3841
headers: (params: GoogleContactsDeleteParams) => ({
3942
Authorization: `Bearer ${params.accessToken}`,
@@ -56,7 +59,9 @@ export const deleteTool: ToolConfig<GoogleContactsDeleteParams, GoogleContactsDe
5659
}
5760

5861
const errorData = await response.json()
59-
throw new Error(errorData.error?.message || 'Failed to delete contact')
62+
const errorMessage = errorData.error?.message || 'Failed to delete contact'
63+
logger.error('Failed to delete contact', { status: response.status, error: errorMessage })
64+
throw new Error(errorMessage)
6065
},
6166

6267
outputs: {

apps/sim/tools/google_contacts/get.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
DEFAULT_PERSON_FIELDS,
34
type GoogleContactsGetParams,
@@ -7,6 +8,8 @@ import {
78
} from '@/tools/google_contacts/types'
89
import type { ToolConfig } from '@/tools/types'
910

11+
const logger = createLogger('GoogleContactsGet')
12+
1013
export const getTool: ToolConfig<GoogleContactsGetParams, GoogleContactsGetResponse> = {
1114
id: 'google_contacts_get',
1215
name: 'Google Contacts Get',
@@ -35,7 +38,7 @@ export const getTool: ToolConfig<GoogleContactsGetParams, GoogleContactsGetRespo
3538

3639
request: {
3740
url: (params: GoogleContactsGetParams) =>
38-
`${PEOPLE_API_BASE}/${params.resourceName}?personFields=${DEFAULT_PERSON_FIELDS}`,
41+
`${PEOPLE_API_BASE}/${params.resourceName.trim()}?personFields=${DEFAULT_PERSON_FIELDS}`,
3942
method: 'GET',
4043
headers: (params: GoogleContactsGetParams) => ({
4144
Authorization: `Bearer ${params.accessToken}`,
@@ -45,6 +48,13 @@ export const getTool: ToolConfig<GoogleContactsGetParams, GoogleContactsGetRespo
4548

4649
transformResponse: async (response: Response) => {
4750
const data = await response.json()
51+
52+
if (!response.ok) {
53+
const errorMessage = data.error?.message || 'Failed to get contact'
54+
logger.error('Failed to get contact', { status: response.status, error: errorMessage })
55+
throw new Error(errorMessage)
56+
}
57+
4858
const contact = transformPerson(data)
4959

5060
return {

apps/sim/tools/google_contacts/list.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
DEFAULT_PERSON_FIELDS,
34
type GoogleContactsListParams,
@@ -7,6 +8,8 @@ import {
78
} from '@/tools/google_contacts/types'
89
import type { ToolConfig } from '@/tools/types'
910

11+
const logger = createLogger('GoogleContactsList')
12+
1013
export const listTool: ToolConfig<GoogleContactsListParams, GoogleContactsListResponse> = {
1114
id: 'google_contacts_list',
1215
name: 'Google Contacts List',
@@ -65,6 +68,13 @@ export const listTool: ToolConfig<GoogleContactsListParams, GoogleContactsListRe
6568

6669
transformResponse: async (response: Response) => {
6770
const data = await response.json()
71+
72+
if (!response.ok) {
73+
const errorMessage = data.error?.message || 'Failed to list contacts'
74+
logger.error('Failed to list contacts', { status: response.status, error: errorMessage })
75+
throw new Error(errorMessage)
76+
}
77+
6878
const connections = data.connections || []
6979
const contacts = connections.map((person: Record<string, any>) => transformPerson(person))
7080

apps/sim/tools/google_contacts/search.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
DEFAULT_PERSON_FIELDS,
34
type GoogleContactsSearchParams,
@@ -7,6 +8,8 @@ import {
78
} from '@/tools/google_contacts/types'
89
import type { ToolConfig } from '@/tools/types'
910

11+
const logger = createLogger('GoogleContactsSearch')
12+
1013
export const searchTool: ToolConfig<GoogleContactsSearchParams, GoogleContactsSearchResponse> = {
1114
id: 'google_contacts_search',
1215
name: 'Google Contacts Search',
@@ -58,6 +61,13 @@ export const searchTool: ToolConfig<GoogleContactsSearchParams, GoogleContactsSe
5861

5962
transformResponse: async (response: Response) => {
6063
const data = await response.json()
64+
65+
if (!response.ok) {
66+
const errorMessage = data.error?.message || 'Failed to search contacts'
67+
logger.error('Failed to search contacts', { status: response.status, error: errorMessage })
68+
throw new Error(errorMessage)
69+
}
70+
6171
const results = data.results || []
6272
const contacts = results.map((result: Record<string, any>) =>
6373
transformPerson(result.person || result)

apps/sim/tools/google_contacts/update.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLogger } from '@sim/logger'
12
import {
23
DEFAULT_PERSON_FIELDS,
34
type GoogleContactsUpdateParams,
@@ -7,6 +8,8 @@ import {
78
} from '@/tools/google_contacts/types'
89
import type { ToolConfig } from '@/tools/types'
910

11+
const logger = createLogger('GoogleContactsUpdate')
12+
1013
export const updateTool: ToolConfig<GoogleContactsUpdateParams, GoogleContactsUpdateResponse> = {
1114
id: 'google_contacts_update',
1215
name: 'Google Contacts Update',
@@ -108,7 +111,7 @@ export const updateTool: ToolConfig<GoogleContactsUpdateParams, GoogleContactsUp
108111

109112
const updatePersonFields = updateFields.join(',')
110113

111-
return `${PEOPLE_API_BASE}/${params.resourceName}:updateContact?updatePersonFields=${updatePersonFields}&personFields=${DEFAULT_PERSON_FIELDS}`
114+
return `${PEOPLE_API_BASE}/${params.resourceName.trim()}:updateContact?updatePersonFields=${updatePersonFields}&personFields=${DEFAULT_PERSON_FIELDS}`
112115
},
113116
method: 'PATCH',
114117
headers: (params: GoogleContactsUpdateParams) => ({
@@ -156,6 +159,13 @@ export const updateTool: ToolConfig<GoogleContactsUpdateParams, GoogleContactsUp
156159

157160
transformResponse: async (response: Response) => {
158161
const data = await response.json()
162+
163+
if (!response.ok) {
164+
const errorMessage = data.error?.message || 'Failed to update contact'
165+
logger.error('Failed to update contact', { status: response.status, error: errorMessage })
166+
throw new Error(errorMessage)
167+
}
168+
159169
const contact = transformPerson(data)
160170

161171
return {

0 commit comments

Comments
 (0)