mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(auto-reply): share directive arg parsing
This commit is contained in:
40
src/auto-reply/reply/directive-parsing.ts
Normal file
40
src/auto-reply/reply/directive-parsing.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
export function skipDirectiveArgPrefix(raw: string): number {
|
||||
let i = 0;
|
||||
const len = raw.length;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (raw[i] === ":") {
|
||||
i += 1;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
export function takeDirectiveToken(
|
||||
raw: string,
|
||||
startIndex: number,
|
||||
): { token: string | null; nextIndex: number } {
|
||||
let i = startIndex;
|
||||
const len = raw.length;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (i >= len) {
|
||||
return { token: null, nextIndex: i };
|
||||
}
|
||||
const start = i;
|
||||
while (i < len && !/\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (start === i) {
|
||||
return { token: null, nextIndex: i };
|
||||
}
|
||||
const token = raw.slice(start, i);
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
return { token, nextIndex: i };
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { ExecAsk, ExecHost, ExecSecurity } from "../../../infra/exec-approvals.js";
|
||||
import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js";
|
||||
|
||||
type ExecDirectiveParse = {
|
||||
cleaned: string;
|
||||
@@ -48,17 +49,8 @@ function parseExecDirectiveArgs(raw: string): Omit<
|
||||
> & {
|
||||
consumed: number;
|
||||
} {
|
||||
let i = 0;
|
||||
const len = raw.length;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (raw[i] === ":") {
|
||||
i += 1;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
let i = skipDirectiveArgPrefix(raw);
|
||||
let consumed = i;
|
||||
let execHost: ExecHost | undefined;
|
||||
let execSecurity: ExecSecurity | undefined;
|
||||
@@ -75,21 +67,9 @@ function parseExecDirectiveArgs(raw: string): Omit<
|
||||
let invalidNode = false;
|
||||
|
||||
const takeToken = (): string | null => {
|
||||
if (i >= len) {
|
||||
return null;
|
||||
}
|
||||
const start = i;
|
||||
while (i < len && !/\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (start === i) {
|
||||
return null;
|
||||
}
|
||||
const token = raw.slice(start, i);
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
return token;
|
||||
const res = takeDirectiveToken(raw, i);
|
||||
i = res.nextIndex;
|
||||
return res.token;
|
||||
};
|
||||
|
||||
const splitToken = (token: string): { key: string; value: string } | null => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { QueueDropPolicy, QueueMode } from "./types.js";
|
||||
import { parseDurationMs } from "../../../cli/parse-duration.js";
|
||||
import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js";
|
||||
import { normalizeQueueDropPolicy, normalizeQueueMode } from "./normalize.js";
|
||||
|
||||
function parseQueueDebounce(raw?: string): number | undefined {
|
||||
@@ -45,17 +46,8 @@ function parseQueueDirectiveArgs(raw: string): {
|
||||
rawDrop?: string;
|
||||
hasOptions: boolean;
|
||||
} {
|
||||
let i = 0;
|
||||
const len = raw.length;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (raw[i] === ":") {
|
||||
i += 1;
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
let i = skipDirectiveArgPrefix(raw);
|
||||
let consumed = i;
|
||||
let queueMode: QueueMode | undefined;
|
||||
let queueReset = false;
|
||||
@@ -68,21 +60,9 @@ function parseQueueDirectiveArgs(raw: string): {
|
||||
let rawDrop: string | undefined;
|
||||
let hasOptions = false;
|
||||
const takeToken = (): string | null => {
|
||||
if (i >= len) {
|
||||
return null;
|
||||
}
|
||||
const start = i;
|
||||
while (i < len && !/\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
if (start === i) {
|
||||
return null;
|
||||
}
|
||||
const token = raw.slice(start, i);
|
||||
while (i < len && /\s/.test(raw[i])) {
|
||||
i += 1;
|
||||
}
|
||||
return token;
|
||||
const res = takeDirectiveToken(raw, i);
|
||||
i = res.nextIndex;
|
||||
return res.token;
|
||||
};
|
||||
while (i < len) {
|
||||
const token = takeToken();
|
||||
|
||||
Reference in New Issue
Block a user