|
| 1 | +import type { AshbyCustomField } from '@/tools/ashby/types' |
| 2 | +import { |
| 3 | + ashbyAuthHeaders, |
| 4 | + ashbyErrorMessage, |
| 5 | + CUSTOM_FIELD_ON_OBJECT_OUTPUT, |
| 6 | + mapCustomFieldOnObject, |
| 7 | +} from '@/tools/ashby/utils' |
| 8 | +import type { ToolConfig, ToolResponse } from '@/tools/types' |
| 9 | + |
| 10 | +interface AshbySetCustomFieldValueParams { |
| 11 | + apiKey: string |
| 12 | + objectId: string |
| 13 | + objectType: string |
| 14 | + fieldId: string |
| 15 | + fieldValue: unknown |
| 16 | +} |
| 17 | + |
| 18 | +interface AshbySetCustomFieldValueResponse extends ToolResponse { |
| 19 | + output: { |
| 20 | + customField: AshbyCustomField |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export const setCustomFieldValueTool: ToolConfig< |
| 25 | + AshbySetCustomFieldValueParams, |
| 26 | + AshbySetCustomFieldValueResponse |
| 27 | +> = { |
| 28 | + id: 'ashby_set_custom_field_value', |
| 29 | + name: 'Ashby Set Custom Field Value', |
| 30 | + description: |
| 31 | + 'Sets the value of a single custom field on an Ashby Application, Candidate, Job, or Opening. Custom fields are the only way to annotate a job or req, since Ashby has no job notes and no job tags. Requires the candidatesWrite permission.', |
| 32 | + version: '1.0.0', |
| 33 | + |
| 34 | + params: { |
| 35 | + apiKey: { |
| 36 | + type: 'string', |
| 37 | + required: true, |
| 38 | + visibility: 'user-only', |
| 39 | + description: 'Ashby API Key', |
| 40 | + }, |
| 41 | + objectId: { |
| 42 | + type: 'string', |
| 43 | + required: true, |
| 44 | + visibility: 'user-or-llm', |
| 45 | + description: |
| 46 | + 'UUID of the object to set the field on (application, candidate, job, or opening)', |
| 47 | + }, |
| 48 | + objectType: { |
| 49 | + type: 'string', |
| 50 | + required: true, |
| 51 | + visibility: 'user-or-llm', |
| 52 | + description: 'Type of the object: Application, Candidate, Job, or Opening', |
| 53 | + }, |
| 54 | + fieldId: { |
| 55 | + type: 'string', |
| 56 | + required: true, |
| 57 | + visibility: 'user-or-llm', |
| 58 | + description: |
| 59 | + 'UUID of the custom field definition to set, as returned by List Custom Fields. This is the field definition ID, not the ID of a value already on the object.', |
| 60 | + }, |
| 61 | + /** |
| 62 | + * Not marked required even though Ashby always expects the key: the shared |
| 63 | + * post-merge validator rejects a required `user-or-llm` param whose value is |
| 64 | + * `null`, and `null` is exactly how a custom field is cleared. The block |
| 65 | + * keeps its own required marker on the subblock, so a blank field is still |
| 66 | + * caught in the editor. See `validateRequiredParametersAfterMerge`. |
| 67 | + */ |
| 68 | + fieldValue: { |
| 69 | + type: 'json', |
| 70 | + required: false, |
| 71 | + visibility: 'user-or-llm', |
| 72 | + description: |
| 73 | + 'Value to write, matching the field type: boolean, number, string (String, LongText, Date, Url, or a ValueSelect option), string array (MultiValueSelect), or an object for Currency ({value, currencyCode}), NumberRange ({type, minValue, maxValue}), CompensationRange, and Location ({country, region, city}). Pass null to clear the value, which makes the annotation reversible.', |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + request: { |
| 78 | + url: 'https://api.ashbyhq.com/customField.setValue', |
| 79 | + method: 'POST', |
| 80 | + headers: (params) => ashbyAuthHeaders(params.apiKey), |
| 81 | + body: (params) => ({ |
| 82 | + objectId: params.objectId, |
| 83 | + objectType: params.objectType, |
| 84 | + fieldId: params.fieldId, |
| 85 | + fieldValue: params.fieldValue ?? null, |
| 86 | + }), |
| 87 | + }, |
| 88 | + |
| 89 | + transformResponse: async (response: Response) => { |
| 90 | + const data = await response.json() |
| 91 | + |
| 92 | + if (!data.success) { |
| 93 | + throw new Error(ashbyErrorMessage(data, 'Failed to set custom field value')) |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + success: true, |
| 98 | + output: { |
| 99 | + customField: mapCustomFieldOnObject(data.results), |
| 100 | + }, |
| 101 | + } |
| 102 | + }, |
| 103 | + |
| 104 | + outputs: { |
| 105 | + customField: { |
| 106 | + type: 'object', |
| 107 | + description: 'The custom field as stored on the object after the write', |
| 108 | + properties: CUSTOM_FIELD_ON_OBJECT_OUTPUT, |
| 109 | + }, |
| 110 | + }, |
| 111 | +} |
0 commit comments