Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as jsonpatch from "npm:jsonpatch";
import { ai } from "https://esm.town/v/yawnxyz/ai";
import { covertest as test } from "https://esm.town/v/yawnxyz/covertest";
// import { covertest as test } from "../covertest/covertest.ts";
/**
* Generates a JSON patch from a given object and prompt.
*
* @param {Object} obj - The original JSON object to be modified.
* @param {string} prompt - Instructions for modifying the object.
* @returns {Promise<Array>} A promise that resolves to a JSON patch array.
* @throws {Error} If the AI response cannot be parsed as JSON.
*/
export async function getPatchFromPrompt(obj, prompt) {
const input = `Given this JSON document:
<json>${JSON.stringify(obj, null, 2)}</json>
<instructions>${prompt}
| Respond with a valid JSON patch array that would transform the original document as requested.
| Only add Remove operations to the end of the patch array
| Prefer to move (op: 'move' instead of 'add') objects/keys/values to the enrichedData object — this lets you avoid specifying exact values as the json patch will be reused for multiple objects.
| Only include the JSON patch array in your response, nothing else.
</instructions>`
const aiResponse = await ai(input,
{
provider: "anthropic",
model: "claude-3-sonnet-20240229",
}
);
try {
const jsonPatch = JSON.parse(aiResponse);
return jsonPatch;
} catch (error) {
console.error("Error parsing AI response as JSON:", error);
throw new Error("Failed to generate valid JSON patch");
}
}
/**
* Applies a JSON patch to a given object.
*
* @param {Object} obj - The original object to be patched.
* @param {Array} patch - The JSON patch array to apply.
* @returns {Object} The modified object after applying the patch.
*/
export function patchObject (obj, patch) {
return jsonpatch.apply_patch(obj, patch);
}
/**
* Applies a JSON patch to an array of objects.
*
* @param {Array} arr - The array of objects to be patched.
* @param {Array} patch - The JSON patch array to apply.
* @returns {Array} A new array with the patched objects.
*/
export function patchArray(arr, patch) {
return arr.map(obj => patchObject(obj, patch));
}
/**
* Modifies an object based on a prompt by generating a JSON patch.
*
* @param {Object} obj - The original object to be modified.
* @param {string} prompt - Instructions for modifying the object.
* @returns {Promise<Object>} A promise that resolves to the modified object.
* @throws {Error} If the patch generation fails.
*/
export async function patchObjectFromPrompt(obj, prompt) {
try {
const patch = await getPatchFromPrompt(obj, prompt);
const patchedData = patchObject(obj, patch);
return patchedData;
} catch (error) {
console.error("Error modifying JSON with instructions:", error);
throw new Error("Failed to modify JSON based on given instructions");
}
}
/**
* Modifies an array of objects based on a prompt by generating a JSON patch.
*
* @param {Array} arr - The array of objects to be modified.
* @param {string} prompt - Instructions for modifying the objects.
* @returns {Promise<Array>} A promise that resolves to the modified array.
* @throws {Error} If the patch generation fails.
*/
export async function patchArrayFromPrompt(arr, prompt) {
try {
// Get the patch from the prompt using the first object in the array as a sample
const patch = await getPatchFromPrompt(arr[0], prompt);
console.log('patchArrayFromPrompt/patch', patch);
// Use the patchArray function to apply the patch to all objects in the array
const patchedArray = patchArray(arr, patch);
September 2, 2024