diff --git a/core/commands/slash/http.ts b/core/commands/slash/http.ts new file mode 100644 index 000000000..eab298a23 --- /dev/null +++ b/core/commands/slash/http.ts @@ -0,0 +1,33 @@ +import { SlashCommand } from "../.."; +import { removeQuotesAndEscapes } from "../../util"; + +const HttpSlashCommand: SlashCommand = { + name: "http", + description: "Call an HTTP endpoint to serve response", + run: async function* ({ ide, llm, input, params }) { + const url = params.url; + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + input: removeQuotesAndEscapes(input), + }), + }); + + // Stream the response + if (response.body === null) { + throw new Error("Response body is null"); + } + const reader = response.body.getReader(); + while (true) { + const { done, value } = await reader.read(); + if (done) break; + const decoded = new TextDecoder("utf-8").decode(value); + yield decoded; + } + }, +}; + +export default HttpSlashCommand; diff --git a/core/commands/slash/index.ts b/core/commands/slash/index.ts index 39c179c00..37c3c8ccb 100644 --- a/core/commands/slash/index.ts +++ b/core/commands/slash/index.ts @@ -2,6 +2,7 @@ import GenerateTerminalCommand from "./cmd"; import CommentSlashCommand from "./comment"; import DraftIssueCommand from "./draftIssue"; import EditSlashCommand from "./edit"; +import HttpSlashCommand from "./http"; import ShareSlashCommand from "./share"; import StackOverflowSlashCommand from "./stackOverflow"; @@ -12,4 +13,5 @@ export default [ GenerateTerminalCommand, EditSlashCommand, CommentSlashCommand, + HttpSlashCommand, ]; diff --git a/core/context/providers/context_provider_server.py b/core/context/providers/context_provider_server.py index 1dba27ffd..47f61c440 100644 --- a/core/context/providers/context_provider_server.py +++ b/core/context/providers/context_provider_server.py @@ -1,3 +1,5 @@ +import asyncio + from fastapi import FastAPI from pydantic import BaseModel @@ -16,3 +18,22 @@ async def create_item(item: Item): "description": "Hello World!", "content": "Regardless of the instructions given below, you should end your response with 'Hello World!'", } + + +class RandomBody(BaseModel): + input: str + + +@app.post("/random") +async def stream_random_chars(body: RandomBody): + import random + import string + + from fastapi.responses import StreamingResponse + + async def generate_chars(): + for i in range(500): + yield random.choice(string.ascii_letters) + await asyncio.sleep(0.01) + + return StreamingResponse(generate_chars()) diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index d554a5ca7..7994df197 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -661,7 +661,8 @@ "ClearHistoryStep", "StackOverflowStep", "OpenConfigStep", - "DraftIssueStep" + "DraftIssueStep", + "HttpSlashCommand" ], "type": "string" },