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