shithub: oai

ref: 2385eae8d4de265c433778aa11f3fef391aaf62b
dir: /tools/writefile.tool/

View raw version
static char*
writefile(OToolcall tc, void*)
{
	JSON *j, *jf, *jc;
	char *file, *content;
	char *s;
	
	if (!allowed(tc, 1))
		return abortcall(tc);
	
	j = jsonparse(tc.arguments);
	jf = jsonbyname(j, "file");
	jc = jsonbyname(j, "content");
	
	if (!jf)
		goto Errnofile;
	if (!jc)
		goto Errnocont;
	
	file = jsonstr(jf);
	content = jsonstr(jc);
	if (!file)
		goto Errnofile;
	if (!content)
		goto Errnocont;
	
	s = writetofile(file, content);
	jsonfree(j);
	return s;

Errnofile:
	jsonfree(j);
	return strdup("error: no file");
	
Errnocont:
	jsonfree(j);
	return strdup("error: no content");
}

static char *writefiledesc = "overwrite a file. If the file doesn't exist yet, create it.";
static char *writefileargs = ""
%%json
{
	"type": "object",
	"properties": {
		"file": {
			"type": "string",
			"description": "path to the file"
		},
		"content": {
			"type": "string",
			"description": "new content of the file. Existing content will be overwritten."
		}
	},
	"required": [ "file", "content" ]
}
%/json
;