1
1
import assert from 'node:assert' ;
2
2
import type { AIUsageInfo , PlanningAIResponse , UIContext } from '@/types' ;
3
+ import { PromptTemplate } from '@langchain/core/prompts' ;
4
+ import { AIActionType , type AIArgs , callAiFn } from './common' ;
3
5
import {
4
- AIActionType ,
5
- type AIArgs ,
6
- callAiFn ,
7
- transformUserMessages ,
8
- } from '../common' ;
9
- import { systemPromptToTaskPlanning } from '../prompt/planning' ;
10
- import { describeUserPage } from '../prompt/util' ;
6
+ automationUserPrompt ,
7
+ systemPromptToTaskPlanning ,
8
+ taskBackgroundContext ,
9
+ } from './prompt/planning' ;
10
+ import { describeUserPage } from './prompt/util' ;
11
11
12
12
export async function plan (
13
13
userPrompt : string ,
@@ -23,25 +23,21 @@ export async function plan(
23
23
const { description : pageDescription , elementByPosition } =
24
24
await describeUserPage ( context ) ;
25
25
26
- const systemPrompt = systemPromptToTaskPlanning ( ) ;
26
+ const systemPrompt = await systemPromptToTaskPlanning ( ) ;
27
+ const userInstructionPrompt = await automationUserPrompt . format ( {
28
+ pageDescription,
29
+ userPrompt,
30
+ taskBackgroundContext : taskBackgroundContext (
31
+ opts . originalPrompt ,
32
+ opts . whatHaveDone ,
33
+ ) ,
34
+ } ) ;
27
35
28
- let taskBackgroundContext = '' ;
29
- if ( opts . originalPrompt && opts . whatHaveDone ) {
30
- taskBackgroundContext = `For your information, this is a task that some important person handed to you. Here is the original task description and what have been done after the previous actions:
31
- =====================================
32
- Original task description:
33
- ${ opts . originalPrompt }
34
- =====================================
35
- What have been done:
36
- ${ opts . whatHaveDone }
37
- =====================================
38
- ` ;
39
- }
40
36
const msgs : AIArgs = [
41
37
{ role : 'system' , content : systemPrompt } ,
42
38
{
43
39
role : 'user' ,
44
- content : transformUserMessages ( [
40
+ content : [
45
41
{
46
42
type : 'image_url' ,
47
43
image_url : {
@@ -51,28 +47,14 @@ ${opts.whatHaveDone}
51
47
} ,
52
48
{
53
49
type : 'text' ,
54
- text : `
55
- pageDescription:\n
56
- ${ pageDescription }
57
- \n
58
- Here is the instruction:
59
- =====================================
60
- ${ userPrompt }
61
- =====================================
62
-
63
- ${ taskBackgroundContext }
64
- ` . trim ( ) ,
50
+ text : userInstructionPrompt ,
65
51
} ,
66
- ] ) ,
52
+ ] ,
67
53
} ,
68
54
] ;
69
55
70
56
const call = callAI || callAiFn ;
71
- const { content, usage } = await call ( {
72
- msgs,
73
- AIActionType : AIActionType . PLAN ,
74
- } ) ;
75
-
57
+ const { content, usage } = await call ( msgs , AIActionType . PLAN ) ;
76
58
const planFromAI = content ;
77
59
78
60
const actions = planFromAI ?. actions || [ ] ;
0 commit comments