Skip to content

Commit

Permalink
[Freestyler] Add multiline answer parsing
Browse files Browse the repository at this point in the history
Bug: 340805507
Change-Id: Ie3421d1acf26586aafe7e1ad7c9ba90256493dbb
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5633336
Commit-Queue: Alex Rudenko <[email protected]>
Reviewed-by: Ergün Erdoğmuş <[email protected]>
  • Loading branch information
OrKoN authored and Devtools-frontend LUCI CQ committed Jun 14, 2024
1 parent c17b29c commit f377833
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
35 changes: 35 additions & 0 deletions front_end/panels/freestyler/FreestylerAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,41 @@ describeWithEnvironment('FreestylerAgent', () => {
answer: payload,
});
});
it('parses a multiline answer', async () => {
const payload = `a
b
c`;
assert.deepStrictEqual(FreestylerAgent.parseResponse(`ANSWER: ${payload}`), {
action: undefined,
thought: undefined,
answer: payload,
});
assert.deepStrictEqual(FreestylerAgent.parseResponse(` ANSWER: ${payload}`), {
action: undefined,
thought: undefined,
answer: payload,
});
assert.deepStrictEqual(FreestylerAgent.parseResponse(`Something\n ANSWER: ${payload}`), {
action: undefined,
thought: undefined,
answer: payload,
});
assert.deepStrictEqual(FreestylerAgent.parseResponse(`ANSWER: ${payload}\nTHOUGHT: thought`), {
action: undefined,
thought: 'thought',
answer: payload,
});
assert.deepStrictEqual(FreestylerAgent.parseResponse(`ANSWER: ${payload}\nOBSERVATION: observation`), {
action: undefined,
thought: undefined,
answer: payload,
});
assert.deepStrictEqual(FreestylerAgent.parseResponse(`ANSWER: ${payload}\nACTION\naction\nSTOP`), {
action: 'action',
thought: undefined,
answer: payload,
});
});
it('parses an action', async () => {
const payload = `const data = {
someKey: "value",
Expand Down
22 changes: 19 additions & 3 deletions front_end/panels/freestyler/FreestylerAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ export class FreestylerAgent {
let thought: string|undefined;
let action: string|undefined;
let answer: string|undefined;
for (let i = 0; i < lines.length; i++) {
let i = 0;
while (i < lines.length) {
const trimmed = lines[i].trim();
if (trimmed.startsWith('THOUGHT:') && !thought) {
// TODO: multiline thoughts.
thought = trimmed.substring('THOUGHT:'.length).trim();
i++;
} else if (trimmed.startsWith('ACTION') && !action) {
const actionLines = [];
let j = i + 1;
Expand All @@ -139,8 +141,22 @@ export class FreestylerAgent {
action = actionLines.join('\n').trim();
i = j + 1;
} else if (trimmed.startsWith('ANSWER:') && !answer) {
// TODO: multiline answers.
answer = trimmed.substring('ANSWER:'.length).trim();
const answerLines = [
trimmed.substring('ANSWER:'.length).trim(),
];
let j = i + 1;
while (j < lines.length) {
const line = lines[j].trim();
if (line.startsWith('ACTION') || line.startsWith('OBSERVATION:') || line.startsWith('THOUGHT:')) {
break;
}
answerLines.push(lines[j]);
j++;
}
answer = answerLines.join('\n').trim();
i = j;
} else {
i++;
}
}
return {thought, action, answer};
Expand Down

0 comments on commit f377833

Please sign in to comment.