15 * @throws {Error} If the AI response cannot be parsed as JSON.
16 */
17export async function getPatchFromPrompt(obj, prompt) {
18 const input = `Given this JSON document:
19 <json>${JSON.stringify(obj, null, 2)}</json>
48 * @returns {Object} The modified object after applying the patch.
49 */
50export function patchObject (obj, patch) {
51 return jsonpatch.apply_patch(obj, patch);
52}
59 * @returns {Array} A new array with the patched objects.
60 */
61export function patchArray(arr, patch) {
62 return arr.map(obj => patchObject(obj, patch));
63}
71 * @throws {Error} If the patch generation fails.
72 */
73export async function patchObjectFromPrompt(obj, prompt) {
74 try {
75 const patch = await getPatchFromPrompt(obj, prompt);
90 * @throws {Error} If the patch generation fails.
91 */
92export async function patchArrayFromPrompt(arr, prompt) {
93 try {
94 // Get the patch from the prompt using the first object in the array as a sample
97 console.log('patchArrayFromPrompt/patch', patch);
98
99 // Use the patchArray function to apply the patch to all objects in the array
100 const patchedArray = patchArray(arr, patch);
101
115 * @returns {Array} The modified array after merging.
116 */
117export function patchObjectsByKey(obj, newObj, joinKey = "name") {
118 const patch = [];
119
149 * @throws {Error} If the patch generation or application fails.
150 */
151export async function patchObjects(obj, newObj, options = {}) {
152 let {
153 provider = "anthropic",
226 * @throws {Error} If the patch generation or application fails.
227 */
228export async function patchArrays(arr, newArr, options = {}) {
229 let {
230 provider = "anthropic",
267export const tests = {
268 /**
269 * Tests the getPatchFromPrompt function by providing a sample document and prompt.
270 * Validates that the generated patch modifies the document as expected.
271 *
272 * @returns {Promise<void>} A promise that resolves when the test is complete.
273 */
274 testGetPatchFromPrompt: async function() {
275 const doc = { "name": "John", "age": 30 };
276 const prompt = "Increase the age by 5 years and add a 'city' field with the value 'New York'";
290
291 /**
292 * Tests the patchObject function by applying a patch to a sample document.
293 * Validates that the patch is applied correctly.
294 *
295 * @returns {void}
296 */
297 testPatchObject: function() {
298 const mydoc = { "baz": "qux", "foo": "bar" };
299 const thepatch = [{ "op": "replace", "path": "/baz", "value": "boo" }];
310
311 /**
312 * Tests the patchObjectFromPrompt function by providing a sample document and instructions.
313 * Validates that the modified document matches the expected output.
314 *
315 * @returns {Promise<void>} A promise that resolves when the test is complete.
316 */
317 testPatchObjectFromPrompt: async function() {
318 const doc = { "name": "Alice", "age": 25, "hobbies": ["reading"] };
319 const instructions = "Add a 'job' field with the value 'Engineer' and add 'coding' to the hobbies array";
330
331 /**
332 * Tests the patchObjectsByKey function by merging two arrays of objects based on a specified key.
333 * Validates that the merged data contains the expected properties.
334 *
335 * @returns {void}
336 */
337 testPatchObjectsByKey: function() {
338 const baseData = [
339 { name: "John", age: 30, city: "New York" },
359
360 /**
361 * Tests the patchObjects function by modifying an object based on additional data and a prompt.
362 * Validates that the enriched data contains the expected properties.
363 *
364 * @returns {Promise<void>} A promise that resolves when the test is complete.
365 */
366 testPatchObjects: async function() {
367 const baseData = {
368 project: { name: "Project A", status: "In Progress" },
390
391 /**
392 * Tests the patchObjectsWithNotionAndAirtable function by combining Notion and Airtable data.
393 * Validates that the enriched data contains the expected properties.
394 *
395 * @returns {Promise<void>} A promise that resolves when the test is complete.
396 */
397 testPatchObjectsWithNotionAndAirtable: async function() {
398 const notionData = {
399 id: "page1",
435
436 /**
437 * Tests the patchObjectsWithJsonPatch function by using a predefined JSON patch.
438 * Validates that the enriched data contains the expected properties.
439 *
440 * @returns {Promise<void>} A promise that resolves when the test is complete.
441 */
442 testPatchObjectsWithJsonPatch: async function() {
443 const baseData = { name: "John", age: 30 };
444 const additionalData = { city: "New York", job: "Developer" };
464
465 /**
466 * Tests the patchArrays function by modifying an array of objects based on additional data and a prompt.
467 * Validates that the enriched data array contains the expected properties.
468 *
469 * @returns {Promise<void>} A promise that resolves when the test is complete.
470 */
471 testPatchArrays: async function() {
472 const baseArray = [
473 {
538
539 /**
540 * Tests the patchArray function by applying a patch to an array of objects.
541 * Validates that the patched array contains the expected properties.
542 *
543 * @returns {void}
544 */
545 testPatchArray: function() {
546 const arr = [
547 { name: "John", age: 30 },
566
567 /**
568 * Tests the patchArrayFromPrompt function by providing an array of objects and a prompt.
569 * Validates that the modified array matches the expected output.
570 *
571 * @returns {Promise<void>} A promise that resolves when the test is complete.
572 */
573 testPatchArrayFromPrompt: async function() {
574 const arr = [
575 { name: "John", details: { age: 30, address: { city: "New York", street: "Broadway" } } },