Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
// Copyright 2025 Google LLC//// This is adapted from the edit corrector in the Gemini CLI which can be found// at https://github.com/google-gemini/gemini-cli/blob/5008aea90d4ea7ac6bb5872f3702f3c7a7878ed0/packages/core/src/utils/editCorrector.ts// and is available under the following license://// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// eslint-disable-next-line header/headerimport{Raw}from'@vscode/prompt-tsx';import*asJSONCfrom'jsonc-parser';importtype{LanguageModelChat}from'vscode';import{ChatFetchResponseType
filePath
oldString
newString
params
occurrences
currentContent oldString eol
r currentContent oldString eol
r r r
model
currentContent
originalParams expected_replacements
eol
healEndpoint
token
finalNewString originalParams
unescapeStringForGeminiBug model model model s s
newStringPotentiallyEscaped
originalParams
originalParams
expectedReplacements originalParams
finalOldString originalParams
occurrences currentContent finalOldString
occurrences expectedReplacements
newStringPotentiallyEscaped
finalNewString
healEndpoint
finalOldString
originalParams
token
occurrences expectedReplacements
expectedReplacements originalParams
occurrences expectedReplacements
result
params originalParams
occurrences
result
expectedReplacements
result
params originalParams
occurrences
result
result
params originalParams
occurrences
result
unescapedOldStringAttempt
originalParams
occurrences currentContent unescapedOldStringAttempt eol
occurrences expectedReplacements
finalOldString unescapedOldStringAttempt
newStringPotentiallyEscaped
finalNewString
healEndpoint
originalParams
unescapedOldStringAttempt
originalParams
token
occurrences
llmCorrectedOldString
healEndpoint
currentContent
unescapedOldStringAttempt
token
llmOldOccurrences
currentContent
llmCorrectedOldString
eol
llmOldOccurrences expectedReplacements
finalOldString llmCorrectedOldString
occurrences llmOldOccurrences
newStringPotentiallyEscaped
baseNewStringForLLMCorrection
originalParams
finalNewString
healEndpoint
originalParams
llmCorrectedOldString
baseNewStringForLLMCorrection
token
result
params originalParams
occurrences
result
result
params originalParams
occurrences
result
targetString pair
finalOldString
finalNewString
currentContent
expectedReplacements
finalOldString targetString
finalNewString pair
result
params
filePath originalParams
oldString finalOldString
newString finalNewString
occurrences currentContent finalOldString
result
oldString_CORRECTION_SCHEMA
type
properties
corrected_target_snippet
type
description
required
healEndpoint
fileContent
problematicSnippet
token
prompt
result healEndpoint prompt oldString_CORRECTION_SCHEMA corrected_target_snippet token
result
result
result
result
problematicSnippet
error
problematicSnippet
newString_CORRECTION_SCHEMA
type
properties
corrected_newString
type
description
required
endpoint
originalOldString
correctedOldString
originalNewString
token
originalOldString correctedOldString
originalNewString
prompt
result endpoint prompt newString_CORRECTION_SCHEMA corrected_newString token
result
result
result
result
originalNewString
error
originalNewString
type
properties
corrected_newString_escaping
type
description
required
geminiClient
oldString
potentiallyProblematicNewString
token
prompt
result geminiClient prompt corrected_newString_escaping token
result
result
result
result
potentiallyProblematicNewString
error
potentiallyProblematicNewString
endpoint prompt schema example object token
prompt
contents
role content type text
role content type text prompt
result endpoint
debugName
messages contents
finishedCb
enableRetryOnFilter
token
result
block result
block
idx result
resultidx
target
trimIfTargetTrims
currentContent
expectedReplacements
trimmedTargetString target
target trimmedTargetString
trimmedTargetOccurrences
currentContent
trimmedTargetString
trimmedTargetOccurrences expectedReplacements
trimmedReactiveString trimIfTargetTrims
targetString trimmedTargetString
pair trimmedReactiveString
targetString target
pair trimIfTargetTrims
inputString
inputString
match capturedChar
capturedChar
match
/**
* Defines the structure of the parameters within CorrectedEditResult
*/
interface
CorrectedEditParams
{
:
string
;
:
string
;
:
string
;
}
/**
* Defines the result structure for ensureCorrectEdit.
*/
export
interface
CorrectedEditResult
{
:
CorrectedEditParams
;
:
number
;
}
function
matchAndCount
(
:
string
,
:
string
,
:
string
)
{
const
=
findAndReplaceOne
(
,
,
'<none>'
,
)
;
return
.
type
===
'multiple'
?
.
matchPositions
.
length
:
.
editPosition
.
length
;
}
/**
* Attempts to correct edit parameters if the original oldString is not found.
* It tries unescaping, and then LLM-based correction.
* Results are cached to avoid redundant processing.
*
* @param currentContent The current content of the file.
* @param originalParams The original EditToolParams
* @param healEndpoint The GeminiClient for LLM calls.
* @returns A promise resolving to an object containing the (potentially corrected)
* EditToolParams (as CorrectedEditParams) and the final occurrences count.
*/
export
async
function
healReplaceStringParams
(
:
LanguageModelChat
|
undefined
,
:
string
,
:
IReplaceStringToolParams
&
{
?
:
number
}
,
// This is the EditToolParams from edit.ts, without \'corrected\'
:
string
,
:
IChatEndpoint
,
:
CancellationToken
,
)
:
Promise
<
CorrectedEditResult
>
{
let
=
.
newString
!
;
const
=
(
&&
(
isGeminiFamily
(
)
||
isHiddenModelF
(
)
)
)
?
_unescapeStringForGeminiBug
:
(
:
string
)
=>
;
const
=
unescapeStringForGeminiBug
(
.
newString
!
)
!==
.
newString
;
const
=
.
expected_replacements
??
1
;
let
=
.
oldString
!
;
let
=
count
(
,
)
;
if
(
===
)
{
if
(
)
{
=
await
correctNewStringEscaping
(
,
,
.
newString
!
,
,
)
;
}
}
else
if
(
>
)
{
const
=
.
expected_replacements
??
1
;
// If user expects multiple replacements, return as-is
if
(
===
)
{
const
:
CorrectedEditResult
=
{
:
{
...
}
,
,
}
;
return
;
}
// If user expects 1 but found multiple, try to correct (existing behavior)
if
(
===
1
)
{
const
:
CorrectedEditResult
=
{
:
{
...
}
,
,
}
;
return
;
}
// If occurrences don't match expected, return as-is (will fail validation later)
const
:
CorrectedEditResult
=
{
:
{
...
}
,
,
}
;
return
;
}
else
{
// occurrences is 0 or some other unexpected state initially
const
=
unescapeStringForGeminiBug
(
.
oldString
,
)
;
=
matchAndCount
(
,
,
)
;
if
(
===
)
{
=
;
if
(
)
{
=
await
correctNewString
(
,
.
oldString
,
// original old
,
// corrected old
.
newString
,
// original new (which is potentially escaped)
,
)
;
}
}
else
if
(
===
0
)
{
const
=
await
correctOldStringMismatch
(
,
,
,
,
)
;
const
=
matchAndCount
(
,
,
,
)
;
if
(
===
)
{
=
;
=
;
if
(
)
{
const
=
unescapeStringForGeminiBug
(
.
newString
,
)
;
=
await
correctNewString
(
,
.
oldString
,
// original old
,
// corrected old
,
// base new for correction
,
)
;
}
}
else
{
// LLM correction also failed for oldString
const
:
CorrectedEditResult
=
{
:
{
...
}
,
:
0
,
// Explicitly 0 as LLM failed
}
;
return
;
}
}
else
{
// Unescaping oldString resulted in > 1 occurrences
const
:
CorrectedEditResult
=
{
:
{
...
}
,
,
// This will be > 1
}
;
return
;
}
}
const
{
,
}
=
trimPairIfPossible
(
,
,
,
,
)
;
=
;
=
;
// Final result construction
const
:
CorrectedEditResult
=
{
:
{
:
.
filePath
,
:
,
:
,
}
,
:
count
(
,
)
,
// Recalculate occurrences with the final oldString
}
;
return
;
}
// Define the expected JSON schema for the LLM response for oldString correction
const
:
ObjectJsonSchema
=
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'The corrected version of the target snippet that exactly and uniquely matches a segment within the provided file content.'
,
}
,
}
,
:
[
'corrected_target_snippet'
]
,
}
;
export
async
function
correctOldStringMismatch
(
:
IChatEndpoint
,
:
string
,
:
string
,
:
CancellationToken
,
)
:
Promise
<
string
>
{
const
=
`
Context: A process needs to find an exact literal, unique match for a specific text snippet within a file's content. The provided snippet failed to match exactly. This is most likely because it has been overly escaped.
Task: Analyze the provided file content and the problematic target snippet. Identify the segment in the file content that the snippet was *most likely* intended to match. Output the *exact*, literal text of that segment from the file content. Focus *only* on removing extra escape characters and correcting formatting, whitespace, or minor differences to achieve a PERFECT literal match. The output must be the exact literal text as it appears in the file.
Problematic target snippet:
\`\`\`
${problematicSnippet}
\`\`\`
File Content:
\`\`\`
${fileContent}
\`\`\`
For example, if the problematic target snippet was "\\\\\\nconst greeting = \`Hello \\\\\`\${name}\\\\\`\`;" and the file content had content that looked like "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;", then corrected_target_snippet should likely be "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;" to fix the incorrect escaping to match the original file content.
If the differences are only in whitespace or formatting, apply similar whitespace/formatting changes to the corrected_target_snippet.
Return ONLY the corrected target snippet in the specified JSON format with the key 'corrected_target_snippet'. If no clear, unique match can be found, return an empty string for 'corrected_target_snippet'.
`
.
trim
(
)
;
try
{
const
=
await
getJsonResponse
(
,
,
,
{
:
'<corrected target snippet here>'
}
,
)
;
if
(
&&
typeof
.
corrected_target_snippet
===
'string'
&&
.
corrected_target_snippet
.
length
>
0
)
{
return
.
corrected_target_snippet
;
}
else
{
return
;
}
}
catch
(
)
{
return
;
}
}
// Define the expected JSON schema for the newString correction LLM response
const
:
ObjectJsonSchema
=
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'The original_newString adjusted to be a suitable replacement for the corrected_oldString, while maintaining the original intent of the change.'
,
}
,
}
,
:
[
'corrected_newString'
]
,
}
;
/**
* Adjusts the newString to align with a corrected oldString, maintaining the original intent.
*/
export
async
function
correctNewString
(
:
IChatEndpoint
,
:
string
,
:
string
,
:
string
,
:
CancellationToken
,
)
:
Promise
<
string
>
{
if
(
===
)
{
return
;
}
const
=
`
Context: A text replacement operation was planned. The original text to be replaced (original_oldString) was slightly different from the actual text in the file (corrected_oldString). The original_oldString has now been corrected to match the file content.
We now need to adjust the replacement text (original_newString) so that it makes sense as a replacement for the corrected_oldString, while preserving the original intent of the change.
original_oldString (what was initially intended to be found):
\`\`\`
${originalOldString}
\`\`\`
corrected_oldString (what was actually found in the file and will be replaced):
\`\`\`
${correctedOldString}
\`\`\`
original_newString (what was intended to replace original_oldString):
\`\`\`
${originalNewString}
\`\`\`
Task: Based on the differences between original_oldString and corrected_oldString, and the content of original_newString, generate a corrected_newString. This corrected_newString should be what original_newString would have been if it was designed to replace corrected_oldString directly, while maintaining the spirit of the original transformation.
For example, if original_oldString was "\\\\\\nconst greeting = \`Hello \\\\\`\${name}\\\\\`\`;" and corrected_oldString is "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;", and original_newString was "\\\\\\nconst greeting = \`Hello \\\\\`\${name} \${lastName}\\\\\`\`;", then corrected_newString should likely be "\nconst greeting = \`Hello ${'\\`'}\${name} \${lastName}${'\\`'}\`;" to fix the incorrect escaping.
If the differences are only in whitespace or formatting, apply similar whitespace/formatting changes to the corrected_newString.
Return ONLY the corrected string in the specified JSON format with the key 'corrected_newString'. If no adjustment is deemed necessary or possible, return the original_newString.
`
.
trim
(
)
;
try
{
const
=
await
getJsonResponse
(
,
,
,
{
:
'<corrected newString here>'
}
,
)
;
if
(
&&
typeof
.
corrected_newString
===
'string'
&&
.
corrected_newString
.
length
>
0
)
{
return
.
corrected_newString
;
}
else
{
return
;
}
}
catch
(
)
{
return
;
}
}
const
CORRECT_newString_ESCAPING_SCHEMA
:
ObjectJsonSchema
=
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'The newString with corrected escaping, ensuring it is a proper replacement for the oldString, especially considering potential over-escaping issues from previous LLM generations.'
,
}
,
}
,
:
[
'corrected_newString_escaping'
]
,
}
;
export
async
function
correctNewStringEscaping
(
:
IChatEndpoint
,
:
string
,
:
string
,
:
CancellationToken
,
)
:
Promise
<
string
>
{
const
=
`
Context: A text replacement operation is planned. The text to be replaced (oldString) has been correctly identified in the file. However, the replacement text (newString) might have been improperly escaped by a previous LLM generation (e.g. too many backslashes for newlines like \\n instead of \n, or unnecessarily quotes like \\"Hello\\" instead of "Hello").
oldString (this is the exact text that will be replaced):
\`\`\`
${oldString}
\`\`\`
potentially_problematic_newString (this is the text that should replace oldString, but MIGHT have bad escaping, or might be entirely correct):
\`\`\`
${potentiallyProblematicNewString}
\`\`\`
Task: Analyze the potentially_problematic_newString. If it's syntactically invalid due to incorrect escaping (e.g., "\n", "\t", "\\", "\\'", "\\""), correct the invalid syntax. The goal is to ensure the newString, when inserted into the code, will be a valid and correctly interpreted.
For example, if oldString is "foo" and potentially_problematic_newString is "bar\\nbaz", the corrected_newString_escaping should be "bar\nbaz".
If potentially_problematic_newString is console.log(\\"Hello World\\"), it should be console.log("Hello World").
Return ONLY the corrected string in the specified JSON format with the key 'corrected_newString_escaping'. If no escaping correction is needed, return the original potentially_problematic_newString.
`
.
trim
(
)
;
try
{
const
=
await
getJsonResponse
(
,
,
CORRECT_newString_ESCAPING_SCHEMA
,
{
:
'<corrected newString here>'
}
,
)
;
if
(
&&
typeof
.
corrected_newString_escaping
===
'string'
&&
.
corrected_newString_escaping
.
length
>
0
)
{
return
.
corrected_newString_escaping
;
}
else
{
return
;
}
}
catch
(
)
{
return
;
}
}
async
function
getJsonResponse
(
:
IChatEndpoint
,
:
string
,
:
ObjectJsonSchema
,
:
,
:
CancellationToken
)
{
+=
`\n\nYour response must follow the JSON format:
\`\`\`
${JSON.stringify(schema,null,2)}
\`\`\`
For example: ${JSON.stringify(example)}`
.
trim
(
)
;
const
:
Raw
.
ChatMessage
[
]
=
[
// Some system message to avoid tripping CAPI
{
:
Raw
.
ChatRole
.
System
,
:
[
{
:
Raw
.
ChatCompletionContentPartKind
.
Text
,
:
'You are an expert at analyzing files and patterns.'
}
]
}
,
{
:
Raw
.
ChatRole
.
User
,
:
[
{
:
Raw
.
ChatCompletionContentPartKind
.
Text
,
:
}
]
}
,
]
;
const
=
await
.
makeChatRequest2
(
{
:
'healStringReplace'
,
:
,
:
undefined
,
location
:
ChatLocation
.
Other
,
:
true
,
}
,
)
;
if
(
.
type
!==
ChatFetchResponseType
.
Success
)
{
return
undefined
;
}
for
(
const
of
extractCodeBlocks
(
.
value
)
)
{
try
{
return
JSONC
.
parse
(
.
code
)
;
}
catch
{
// ignored
}
}
const
=
.
value
.
indexOf
(
'{'
)
;
return
JSONC
.
parse
(
.
value
.
slice
(
)
)
||
undefined
;
}
function
trimPairIfPossible
(
:
string
,
:
string
,
:
string
,
:
number
,
)
{
const
=
.
trim
(
)
;
if
(
.
length
!==
.
length
)
{
const
=
count
(
,
,
)
;
if
(
===
)
{
const
=
.
trim
(
)
;
return
{
:
,
:
,
}
;
}
}
return
{
:
,
:
,
}
;
}
/**
* Unescapes a string that might have been overly escaped by an LLM.
*/
export
function
_unescapeStringForGeminiBug
(
:
string
)
:
string
{
// Regex explanation:
// \\ : Matches exactly one literal backslash character.
// (n|t|r|'|"|`|\\|\n) : This is a capturing group. It matches one of the following:
// n, t, r, ', ", ` : These match the literal characters 'n', 't', 'r', single quote, double quote, or backtick.
// This handles cases like "\\n", "\\`", etc.
// \\ : This matches a literal backslash. This handles cases like "\\\\" (escaped backslash).
// \n : This matches an actual newline character. This handles cases where the input
// string might have something like "\\\n" (a literal backslash followed by a newline).
// g : Global flag, to replace all occurrences.
return
.
replace
(
/\\+(n|t|r|'|"|`|\\|\n)/g
,
(
,
)
=>
{
// 'match' is the entire erroneous sequence, e.g., if the input (in memory) was "\\\\`", match is "\\\\`".
// 'capturedChar' is the character that determines the true meaning, e.g., '`'.