Skip to content

Commit

Permalink
feat(agent): premature stop on sequence repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Sep 6, 2024
1 parent 9e47f1a commit d54eb2a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/agents/bee/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { halveString } from "@/internals/helpers/string.js";
import { parseBrokenJson } from "@/internals/helpers/schema.js";
import { Emitter } from "@/emitter/emitter.js";
import { NonUndefined } from "@/internals/types.js";
import { sumBy } from "remeda";

export interface BeeOutputParserOptions {
allowMultiLines: boolean;
Expand Down Expand Up @@ -58,6 +59,29 @@ interface Callbacks {
}) => Promise<void>;
}

class RepetitionChecker {
protected stash: string[] = [];

constructor(
protected readonly size: number,
protected threshold: number,
) {}

add(chunk: string) {
if (this.stash.length > this.size) {
this.stash.shift();
}
this.stash.push(chunk);

const occurrences = sumBy(this.stash, (token) => Number(token === chunk));
return occurrences >= this.threshold;
}

reset() {
this.stash.length = 0;
}
}

export class BeeOutputParser {
public isDone = false;
protected readonly lines: string[];
Expand All @@ -68,6 +92,7 @@ export class BeeOutputParser {
namespace: ["agent", "bee", "parser"],
});

public repetitionChecker = new RepetitionChecker(10, 5);
protected readonly options: BeeOutputParserOptions;

protected readonly result: BeeIterationSerializedResult = {
Expand All @@ -89,19 +114,34 @@ export class BeeOutputParser {
}

async add(chunk: string) {
if (this.isDone) {
return;
}

chunk = chunk ?? "";

const isRepeating = this.repetitionChecker.add(chunk);
if (isRepeating) {
chunk = NEW_LINE_CHARACTER;
}

this.stash += chunk;
if (!chunk.includes(NEW_LINE_CHARACTER)) {
return;
}

while (this.stash.includes(NEW_LINE_CHARACTER)) {
this.repetitionChecker.reset();
this.filterStash();
const [line, stash = ""] = halveString(this.stash, NEW_LINE_CHARACTER);
this.stash = stash;

await this._processChunk(line);
}

if (isRepeating) {
this.isDone = true;
}
}

protected filterStash() {
Expand Down

0 comments on commit d54eb2a

Please sign in to comment.