Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

exports[`availableTargets > returns all available targets 1`] = `
[
{
"clients": [
{
"description": "A copy-and-pastable prompt, for any AI model, describing how to make the request.",
"extname": ".txt",
"key": "prompt",
"link": "https://github.com/readmeio/httpsnippet",
"title": "AI Prompt",
},
],
"default": "prompt",
"key": "ai",
"title": "AI",
},
{
"cli": "c",
"clients": [
Expand Down
19 changes: 19 additions & 0 deletions src/targets/ai/prompt/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Request } from '../../../index.js';

import short from '../../../fixtures/requests/short.cjs';
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures';

runCustomFixtures({
targetId: 'ai',
clientId: 'prompt',
tests: [
{
it: 'should support the markdownURL option',
input: short.log.entries[0].request as Request,
options: {
markdownURL: 'https://docs.example.com/reference/anything.md',
},
expected: 'markdown-url.txt',
},
],
});
73 changes: 73 additions & 0 deletions src/targets/ai/prompt/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @description
* HTTP code snippet generator to generate a plain-text, copy-and-pastable prompt for any AI model
* describing how to make the request.
*
* @author
* @erunion
*
* For any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
import type { Client } from '../../index.js';

import { CodeBuilder } from '../../../helpers/code-builder.js';

interface PromptOptions {
/**
* A URL, referenced at the end of the prompt, to a Markdown document where the AI (or the user)
* can find more information about the request.
*/
markdownURL?: string;
}

export const prompt: Client<PromptOptions> = {
info: {
key: 'prompt',
title: 'AI Prompt',
link: 'https://github.com/readmeio/httpsnippet',
description: 'A copy-and-pastable prompt, for any AI model, describing how to make the request.',
extname: '.txt',
},
convert: ({ method, fullUrl, allHeaders, postData }, options) => {
const opts = {
indent: ' ',
join: '\n',
...options,
};

const { blank, push, join } = new CodeBuilder(opts);

push(
"Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.",
);
blank();

push(`Method: ${method}`);
push(`URL: ${fullUrl}`);

const headerKeys = Object.keys(allHeaders);
if (headerKeys.length) {
blank();
push('Headers:');
headerKeys.forEach(key => {
push(`${key}: ${allHeaders[key]}`, 1);
});
}

if (postData.text) {
blank();
push(`Body (${postData.mimeType}):`);
push(postData.text);
}

blank();
push('The request the code makes must match the method, URL, headers, and body exactly as described above.');

if (opts.markdownURL) {
blank();
push(`Check ${opts.markdownURL} for more info.`);
}

return join();
},
};
12 changes: 12 additions & 0 deletions src/targets/ai/prompt/fixtures/application-form-encoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: application/x-www-form-urlencoded

Body (application/x-www-form-urlencoded):
foo=bar&hello=world

The request the code makes must match the method, URL, headers, and body exactly as described above.
12 changes: 12 additions & 0 deletions src/targets/ai/prompt/fixtures/application-json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: application/json

Body (application/json):
{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":[]}],"boolean":false}

The request the code makes must match the method, URL, headers, and body exactly as described above.
9 changes: 9 additions & 0 deletions src/targets/ai/prompt/fixtures/cookies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/cookies

Headers:
cookie: foo=bar; bar=baz

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/custom-method.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: PROPFIND
URL: https://httpbin.org/anything

The request the code makes must match the method, URL, headers, and body exactly as described above.
14 changes: 14 additions & 0 deletions src/targets/ai/prompt/fixtures/full.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value

Headers:
cookie: foo=bar; bar=baz
accept: application/json
content-type: application/x-www-form-urlencoded

Body (application/x-www-form-urlencoded):
foo=bar

The request the code makes must match the method, URL, headers, and body exactly as described above.
12 changes: 12 additions & 0 deletions src/targets/ai/prompt/fixtures/headers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/headers

Headers:
accept: application/json
x-foo: Bar
x-bar: Foo
quoted-value: "quoted" 'string'

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/http-insecure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: http://httpbin.org/anything

The request the code makes must match the method, URL, headers, and body exactly as described above.
14 changes: 14 additions & 0 deletions src/targets/ai/prompt/fixtures/jsonObj-multiline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: application/json

Body (application/json):
{
"foo": "bar"
}

The request the code makes must match the method, URL, headers, and body exactly as described above.
12 changes: 12 additions & 0 deletions src/targets/ai/prompt/fixtures/jsonObj-null-value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: application/json

Body (application/json):
{"foo":null}

The request the code makes must match the method, URL, headers, and body exactly as described above.
8 changes: 8 additions & 0 deletions src/targets/ai/prompt/fixtures/markdown-url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/anything

The request the code makes must match the method, URL, headers, and body exactly as described above.

Check https://docs.example.com/reference/anything.md for more info.
21 changes: 21 additions & 0 deletions src/targets/ai/prompt/fixtures/multipart-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: multipart/form-data; boundary=---011000010111000001101001

Body (multipart/form-data):
-----011000010111000001101001
Content-Disposition: form-data; name="foo"; filename="src/fixtures/files/hello.txt"
Content-Type: text/plain

Hello World
-----011000010111000001101001
Content-Disposition: form-data; name="bar"

Bonjour le monde
-----011000010111000001101001--

The request the code makes must match the method, URL, headers, and body exactly as described above.
17 changes: 17 additions & 0 deletions src/targets/ai/prompt/fixtures/multipart-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: multipart/form-data; boundary=---011000010111000001101001

Body (multipart/form-data):
-----011000010111000001101001
Content-Disposition: form-data; name="foo"; filename="src/fixtures/files/hello.txt"
Content-Type: text/plain


-----011000010111000001101001--

The request the code makes must match the method, URL, headers, and body exactly as described above.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
Content-Type: multipart/form-data

The request the code makes must match the method, URL, headers, and body exactly as described above.
16 changes: 16 additions & 0 deletions src/targets/ai/prompt/fixtures/multipart-form-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
Content-Type: multipart/form-data; boundary=---011000010111000001101001

Body (multipart/form-data):
-----011000010111000001101001
Content-Disposition: form-data; name="foo"

bar
-----011000010111000001101001--

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/nested.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value

The request the code makes must match the method, URL, headers, and body exactly as described above.
9 changes: 9 additions & 0 deletions src/targets/ai/prompt/fixtures/postdata-malformed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: application/json

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/query-encoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/query.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value

The request the code makes must match the method, URL, headers, and body exactly as described above.
6 changes: 6 additions & 0 deletions src/targets/ai/prompt/fixtures/short.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: GET
URL: https://httpbin.org/anything

The request the code makes must match the method, URL, headers, and body exactly as described above.
12 changes: 12 additions & 0 deletions src/targets/ai/prompt/fixtures/text-plain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write code that makes the HTTP request described below. Use my preferred programming language and HTTP client library — if I haven't told you what those are, ask me before writing any code.

Method: POST
URL: https://httpbin.org/anything

Headers:
content-type: text/plain

Body (text/plain):
Hello World

The request the code makes must match the method, URL, headers, and body exactly as described above.
14 changes: 14 additions & 0 deletions src/targets/ai/target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Target } from '../index.js';

import { prompt } from './prompt/client.js';

export const ai: Target = {
info: {
key: 'ai',
title: 'AI',
default: 'prompt',
},
clientsById: {
prompt,
},
};
Loading