Skip to content

Commit 7664f4a

Browse files
authored
fix: enable apply patch for more models (#389)
* fix: enable apply patch for more models * fix: add `modelPrefersJsonNotebookRepresentation`
1 parent 1b175c1 commit 7664f4a

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/extension/intents/node/agentIntent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const getTools = (instaService: IInstantiationService, request: vscode.ChatReque
6262
const allowTools: Record<string, boolean> = {};
6363
allowTools[ToolName.EditFile] = true;
6464
allowTools[ToolName.ReplaceString] = modelSupportsReplaceString(model) || !!(model.family.includes('gemini') && configurationService.getExperimentBasedConfig(ConfigKey.Internal.GeminiReplaceString, experimentationService));
65-
allowTools[ToolName.ApplyPatch] = modelSupportsApplyPatch(model) && !!toolsService.getTool(ToolName.ApplyPatch);
65+
allowTools[ToolName.ApplyPatch] = await modelSupportsApplyPatch(model) && !!toolsService.getTool(ToolName.ApplyPatch);
6666

6767
if (modelCanUseReplaceStringExclusively(model)) {
6868
allowTools[ToolName.ReplaceString] = true;

src/platform/endpoint/common/chatModelCapabilities.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
import type { LanguageModelChat } from 'vscode';
77
import type { IChatEndpoint } from '../../networking/common/networking';
88

9+
10+
function getSha256Hash(text: string): Promise<string> {
11+
const encoder = new TextEncoder();
12+
const data = encoder.encode(text);
13+
return crypto.subtle.digest('SHA-256', data).then(hashBuffer => {
14+
const hashArray = Array.from(new Uint8Array(hashBuffer));
15+
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
16+
});
17+
}
18+
919
/**
1020
* Returns whether the instructions should be given in a user message instead
1121
* of a system message when talking to the model.
@@ -25,7 +35,17 @@ export function modelPrefersInstructionsAfterHistory(modelFamily: string) {
2535
/**
2636
* Model supports apply_patch as an edit tool.
2737
*/
28-
export function modelSupportsApplyPatch(model: LanguageModelChat | IChatEndpoint): boolean {
38+
export async function modelSupportsApplyPatch(model: LanguageModelChat | IChatEndpoint): Promise<boolean> {
39+
if (model.family === 'gpt-4.1' || model.family === 'o4-mini') {
40+
return true;
41+
}
42+
return await getSha256Hash(model.family) === 'a99dd17dfee04155d863268596b7f6dd36d0a6531cd326348dbe7416142a21a3';
43+
}
44+
45+
/**
46+
* Model prefers JSON notebook representation.
47+
*/
48+
export function modelPrefersJsonNotebookRepresentation(model: LanguageModelChat | IChatEndpoint): boolean {
2949
return model.family === 'gpt-4.1' || model.family === 'o4-mini';
3050
}
3151

src/platform/notebook/common/alternativeContent.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { findCell } from '../../../util/common/notebooks';
88
import { createServiceIdentifier } from '../../../util/common/services';
99
import { Range } from '../../../vscodeTypes';
1010
import { ConfigKey, IConfigurationService } from '../../configuration/common/configurationService';
11+
import { modelPrefersJsonNotebookRepresentation } from '../../endpoint/common/chatModelCapabilities';
12+
import { IChatEndpoint } from '../../networking/common/networking';
13+
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
1114
import { BaseAlternativeNotebookContentProvider } from './alternativeContentProvider';
1215
import { AlternativeJsonNotebookContentProvider, isJsonContent } from './alternativeContentProvider.json';
1316
import { AlternativeTextNotebookContentProvider } from './alternativeContentProvider.text';
1417
import { AlternativeXmlNotebookContentProvider, isXmlContent } from './alternativeContentProvider.xml';
15-
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
16-
import { IChatEndpoint } from '../../networking/common/networking';
17-
import { modelSupportsApplyPatch } from '../../endpoint/common/chatModelCapabilities';
1818

1919
export type AlternativeContentFormat = 'xml' | 'text' | 'json';
2020

@@ -63,7 +63,7 @@ export class AlternativeNotebookContentService implements IAlternativeNotebookCo
6363
}
6464
getFormat(options: LanguageModelChat | IChatEndpoint | undefined): AlternativeContentFormat {
6565
// GPT 4.1 supports apply_patch, such models work best with JSON format (doesn't have great support for XML yet, thats being worked on).
66-
if (options && modelSupportsApplyPatch(options)) {
66+
if (options && modelPrefersJsonNotebookRepresentation(options)) {
6767
return 'json';
6868
}
6969

0 commit comments

Comments
 (0)