-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
881 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
|
||
import { API } from "@/core/network/api"; | ||
import { asURL } from "@/utils/url"; | ||
import type { LanguageAdapterType } from "../language/types"; | ||
import { getCodes } from "../copilot/getCodes"; | ||
|
||
/** | ||
* Request to edit code with AI | ||
*/ | ||
export async function requestEditCompletion(opts: { | ||
prompt: string; | ||
selection: string; | ||
code: string; | ||
codeBefore: string; | ||
language: LanguageAdapterType; | ||
}): Promise<string> { | ||
const currentCode = opts.code; | ||
|
||
const otherCodes = getCodes(currentCode); | ||
// Other code to include is the codeBefore and the other codes | ||
const includeOtherCode = `${opts.codeBefore}\n${otherCodes}`; | ||
|
||
const response = await fetch(asURL("api/ai/completion").toString(), { | ||
method: "POST", | ||
headers: API.headers(), | ||
body: JSON.stringify({ | ||
prompt: opts.prompt, | ||
code: opts.selection, | ||
includeOtherCode: includeOtherCode, | ||
language: opts.language, | ||
}), | ||
}); | ||
|
||
const reader = response.body?.getReader(); | ||
if (!reader) { | ||
throw new Error("Failed to get response reader"); | ||
} | ||
|
||
let result = ""; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
result += new TextDecoder().decode(value); | ||
} | ||
|
||
return result; | ||
} |
Oops, something went wrong.