-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdetermineNavigateAction.ts
103 lines (87 loc) · 2.9 KB
/
determineNavigateAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { parseResponse } from "./parseResponse";
import { QueryResult } from "./determineNextAction";
import { useAppState } from "../../state/store";
import errorChecker from "../errorChecker";
import { fetchResponseFromModel } from "../aiSdkUtils";
import { schemaToDescription, navigateSchema } from "./tools";
const navigateSchemaDescription = schemaToDescription(navigateSchema);
const systemMessage = (voiceMode: boolean) => `
You are a browser automation assistant.
You can use the following tool:
${navigateSchemaDescription}
You will have access to more tools as you progress through the task.
You will be given a task to perform.
This is an example of expected response from you:
{
"thought": "To find latest news on AI, I am navigating to Google.",${
voiceMode
? `,
"speak": "To find the latest news on AI, I am navigating to Google."`
: ""
}
"action": {
"name": "navigate",
"args": {
"url": "https://www.google.com/"
}
}
}
Your response must always be in JSON format and must include string "thought"${
voiceMode ? ', string "speak",' : ""
} and object "action", which contains the string "name" of tool of choice, and necessary arguments ("args") if required by the tool.
`;
export async function determineNavigateAction(
taskInstructions: string,
maxAttempts = 3,
notifyError?: (error: string) => void,
): Promise<QueryResult> {
const model = useAppState.getState().settings.selectedModel;
const voiceMode = useAppState.getState().settings.voiceMode;
const prompt = formatPrompt(taskInstructions);
for (let i = 0; i < maxAttempts; i++) {
try {
const completion = await fetchResponseFromModel(model, {
systemMessage: systemMessage(voiceMode),
prompt,
jsonMode: true,
});
const rawResponse = completion.rawResponse;
let action = null;
try {
action = parseResponse(rawResponse);
} catch (e) {
console.error(e);
// TODO: try use LLM to fix format when response is not valid
throw new Error(`Incorrectly formatted response: ${e}`);
}
return {
usage: completion.usage,
prompt,
rawResponse,
action,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error instanceof Error) {
const recoverable = errorChecker(error, notifyError);
if (!recoverable) {
throw error;
}
} else {
console.error("Unexpected determineNextAction error:");
console.error(error);
}
}
}
const errMsg = `Failed to complete query after ${maxAttempts} attempts. Please try again later.`;
if (notifyError) {
notifyError(errMsg);
}
throw new Error(errMsg);
}
export function formatPrompt(taskInstructions: string) {
return `The user requests the following task:
${taskInstructions}
Current time: ${new Date().toLocaleString()}
`;
}