Skip to content

Commit 6385133

Browse files
quanrulilac
andauthored
refactor(core): replace @langchain/core with native template literals (#1506)
Co-authored-by: lilac <[email protected]>
1 parent f897f40 commit 6385133

File tree

7 files changed

+1143
-2191
lines changed

7 files changed

+1143
-2191
lines changed

packages/core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"test:parse-action": "npm run test:ai -- tests/ai/parse-action.test.ts"
7373
},
7474
"dependencies": {
75-
"@langchain/core": "0.3.26",
7675
"@midscene/shared": "workspace:*",
7776
"@ui-tars/action-parser": "1.2.3",
7877
"dayjs": "^1.11.11",
@@ -82,7 +81,7 @@
8281
"jsonrepair": "3.12.0",
8382
"semver": "7.5.2",
8483
"js-yaml": "4.1.0",
85-
"zod": "3.25.0",
84+
"zod": "3.24.3",
8685
"socks-proxy-agent": "8.0.4"
8786
},
8887
"devDependencies": {

packages/core/src/ai-model/inspect.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ export async function AiLocateElement(options: {
140140
const targetElementDescriptionText = extraTextFromUserPrompt(
141141
targetElementDescription,
142142
);
143-
const userInstructionPrompt = await findElementPrompt.format({
144-
targetElementDescription: targetElementDescriptionText,
145-
});
143+
const userInstructionPrompt = findElementPrompt(
144+
targetElementDescriptionText,
145+
);
146146
const systemPrompt = systemPromptToLocateElement(vlMode);
147147

148148
let imagePayload = screenshotBase64;
@@ -282,9 +282,9 @@ export async function AiLocateSection(options: {
282282
const { screenshotBase64 } = context;
283283

284284
const systemPrompt = systemPromptToLocateSection(vlMode);
285-
const sectionLocatorInstructionText = await sectionLocatorInstruction.format({
286-
sectionDescription: extraTextFromUserPrompt(sectionDescription),
287-
});
285+
const sectionLocatorInstructionText = sectionLocatorInstruction(
286+
extraTextFromUserPrompt(sectionDescription),
287+
);
288288
const msgs: AIArgs = [
289289
{ role: 'system', content: systemPrompt },
290290
{
@@ -396,7 +396,7 @@ export async function AiExtractElementInfo<T>(options: {
396396
const systemPrompt = systemPromptToExtract();
397397
const { screenshotBase64 } = context;
398398

399-
const extractDataPromptText = await extractDataQueryPrompt(
399+
const extractDataPromptText = extractDataQueryPrompt(
400400
options.pageDescription || '',
401401
dataQuery,
402402
);

packages/core/src/ai-model/prompt/extraction.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { ResponseFormatJSONSchema } from 'openai/resources/index';
32

43
export function systemPromptToExtract() {
@@ -87,7 +86,7 @@ By viewing the screenshot and page contents, you can extract the following data:
8786
`;
8887
}
8988

90-
export const extractDataQueryPrompt = async (
89+
export const extractDataQueryPrompt = (
9190
pageDescription: string,
9291
dataQuery: string | Record<string, string>,
9392
) => {
@@ -97,23 +96,16 @@ export const extractDataQueryPrompt = async (
9796
} else {
9897
dataQueryText = JSON.stringify(dataQuery, null, 2);
9998
}
100-
const extractDataPrompt = new PromptTemplate({
101-
template: `
99+
100+
return `
102101
<PageDescription>
103-
{pageDescription}
102+
${pageDescription}
104103
</PageDescription>
105104
106105
<DATA_DEMAND>
107-
{dataQuery}
106+
${dataQueryText}
108107
</DATA_DEMAND>
109-
`,
110-
inputVariables: ['pageDescription', 'dataQuery'],
111-
});
112-
113-
return await extractDataPrompt.format({
114-
pageDescription,
115-
dataQuery: dataQueryText,
116-
});
108+
`;
117109
};
118110

119111
export const extractDataSchema: ResponseFormatJSONSchema = {

packages/core/src/ai-model/prompt/llm-locator.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { TVlModeTypes } from '@midscene/shared/env';
32
import { bboxDescription } from './common';
43
export function systemPromptToLocateElement(vlMode: TVlModeTypes | undefined) {
@@ -41,7 +40,5 @@ When no element is found:
4140
`;
4241
}
4342

44-
export const findElementPrompt = new PromptTemplate({
45-
template: 'Find: {targetElementDescription}',
46-
inputVariables: ['targetElementDescription'],
47-
});
43+
export const findElementPrompt = (targetElementDescription: string) =>
44+
`Find: ${targetElementDescription}`;

packages/core/src/ai-model/prompt/llm-section-locator.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { TVlModeTypes } from '@midscene/shared/env';
32
import { bboxDescription } from './common';
43

@@ -40,7 +39,5 @@ If the description is "delete button on the second row with title 'Peter'", retu
4039
`;
4140
}
4241

43-
export const sectionLocatorInstruction = new PromptTemplate({
44-
template: 'Find section containing: {sectionDescription}',
45-
inputVariables: ['sectionDescription'],
46-
});
42+
export const sectionLocatorInstruction = (sectionDescription: string) =>
43+
`Find section containing: ${sectionDescription}`;

packages/core/tests/unit-test/prompt/prompt.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ describe('extract element', () => {
220220
expect(prompt).toMatchSnapshot();
221221
});
222222

223-
it('extract element by extractDataPrompt', async () => {
224-
const prompt = await extractDataQueryPrompt(
223+
it('extract element by extractDataPrompt', () => {
224+
const prompt = extractDataQueryPrompt(
225225
'todo title, string',
226226
'todo title, string',
227227
);
228228
expect(prompt).toMatchSnapshot();
229229
});
230230

231-
it('extract element by extractDataPrompt - object', async () => {
232-
const prompt = await extractDataQueryPrompt('todo title, string', {
231+
it('extract element by extractDataPrompt - object', () => {
232+
const prompt = extractDataQueryPrompt('todo title, string', {
233233
foo: 'an array indicates the foo',
234234
});
235235
expect(prompt).toMatchSnapshot();

0 commit comments

Comments
 (0)