mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
refactor: improve code readability by formatting and restructuring functions in OpenAIClient and ApplyPatchTool
This commit is contained in:
@@ -62,14 +62,20 @@ export class OpenAIClient implements LLMClient {
|
||||
return await this.executeWithRetries(payload);
|
||||
}
|
||||
|
||||
private mapMessagesToInput(messages: Array<ChatMessage>): Array<ResponsesMessage> {
|
||||
return messages.map((message: ChatMessage) => ({
|
||||
role: this.mapRoleToResponsesRole(message.role),
|
||||
content: this.createContentBlocksForMessage(message),
|
||||
}));
|
||||
private mapMessagesToInput(
|
||||
messages: Array<ChatMessage>,
|
||||
): Array<ResponsesMessage> {
|
||||
return messages.map((message: ChatMessage) => {
|
||||
return {
|
||||
role: this.mapRoleToResponsesRole(message.role),
|
||||
content: this.createContentBlocksForMessage(message),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private async executeWithRetries(payload: ResponsesRequestPayload): Promise<ChatMessage> {
|
||||
private async executeWithRetries(
|
||||
payload: ResponsesRequestPayload,
|
||||
): Promise<ChatMessage> {
|
||||
let lastError: unknown;
|
||||
|
||||
for (let attempt: number = 1; attempt <= this.maxAttempts; attempt += 1) {
|
||||
@@ -149,7 +155,9 @@ export class OpenAIClient implements LLMClient {
|
||||
}
|
||||
}
|
||||
|
||||
private mapResponsesToChatMessage(body: OpenAIResponsesAPIResponse): ChatMessage {
|
||||
private mapResponsesToChatMessage(
|
||||
body: OpenAIResponsesAPIResponse,
|
||||
): ChatMessage {
|
||||
const outputItems: Array<ResponsesOutputItem> = Array.isArray(body.output)
|
||||
? (body.output as Array<ResponsesOutputItem>)
|
||||
: [];
|
||||
@@ -162,7 +170,8 @@ export class OpenAIClient implements LLMClient {
|
||||
}
|
||||
|
||||
if (item.type === "message" || this.isMessageOutput(item)) {
|
||||
const messageItem: ResponsesOutputMessage = item as ResponsesOutputMessage;
|
||||
const messageItem: ResponsesOutputMessage =
|
||||
item as ResponsesOutputMessage;
|
||||
const { textParts: messageTextParts, toolCalls: messageToolCalls } =
|
||||
this.extractOutputContent(messageItem);
|
||||
if (messageTextParts.length) {
|
||||
@@ -175,9 +184,8 @@ export class OpenAIClient implements LLMClient {
|
||||
}
|
||||
|
||||
if (this.isFunctionCallOutput(item)) {
|
||||
const mappedCall: OpenAIToolCall | null = this.mapFunctionCallOutput(
|
||||
item,
|
||||
);
|
||||
const mappedCall: OpenAIToolCall | null =
|
||||
this.mapFunctionCallOutput(item);
|
||||
if (mappedCall) {
|
||||
toolCalls.push(mappedCall);
|
||||
}
|
||||
@@ -235,11 +243,15 @@ export class OpenAIClient implements LLMClient {
|
||||
});
|
||||
}
|
||||
|
||||
private mapRoleToResponsesRole(role: ChatMessage["role"]): ResponsesMessage["role"] {
|
||||
private mapRoleToResponsesRole(
|
||||
role: ChatMessage["role"],
|
||||
): ResponsesMessage["role"] {
|
||||
return (role === "tool" ? "user" : role) as ResponsesMessage["role"];
|
||||
}
|
||||
|
||||
private createContentBlocksForMessage(message: ChatMessage): Array<ResponsesContentBlock> {
|
||||
private createContentBlocksForMessage(
|
||||
message: ChatMessage,
|
||||
): Array<ResponsesContentBlock> {
|
||||
if (message.role === "tool") {
|
||||
return [
|
||||
{
|
||||
@@ -283,7 +295,9 @@ export class OpenAIClient implements LLMClient {
|
||||
await this.delay(delayMs);
|
||||
}
|
||||
|
||||
private createAbortTimeout(controller: AbortController): NodeJS.Timeout | null {
|
||||
private createAbortTimeout(
|
||||
controller: AbortController,
|
||||
): NodeJS.Timeout | null {
|
||||
if (!this.options.timeoutMs || this.options.timeoutMs <= 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -351,9 +365,7 @@ export class OpenAIClient implements LLMClient {
|
||||
return (item as ResponsesFunctionCallOutput).type === "function_call";
|
||||
}
|
||||
|
||||
private extractOutputContent(
|
||||
outputMessage: ResponsesOutputMessage,
|
||||
): {
|
||||
private extractOutputContent(outputMessage: ResponsesOutputMessage): {
|
||||
textParts: Array<string>;
|
||||
toolCalls: Array<OpenAIToolCall>;
|
||||
} {
|
||||
|
||||
@@ -229,7 +229,9 @@ export class ApplyPatchTool extends StructuredTool<ApplyPatchArgs> {
|
||||
);
|
||||
|
||||
await fs.mkdir(path.dirname(block.absolutePath), { recursive: true });
|
||||
await fs.writeFile(block.absolutePath, patchedContent, { encoding: "utf8" });
|
||||
await fs.writeFile(block.absolutePath, patchedContent, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
}
|
||||
|
||||
private async readFileWithMeta(filePath: string): Promise<{
|
||||
@@ -253,7 +255,8 @@ export class ApplyPatchTool extends StructuredTool<ApplyPatchArgs> {
|
||||
relativePath: string,
|
||||
): string {
|
||||
const normalizedOriginal: string = originalContent.replace(/\r\n/g, "\n");
|
||||
const originalHadTrailingNewline: boolean = normalizedOriginal.endsWith("\n");
|
||||
const originalHadTrailingNewline: boolean =
|
||||
normalizedOriginal.endsWith("\n");
|
||||
const originalLines: Array<string> = normalizedOriginal.length
|
||||
? normalizedOriginal
|
||||
.slice(0, originalHadTrailingNewline ? -1 : undefined)
|
||||
@@ -267,8 +270,12 @@ export class ApplyPatchTool extends StructuredTool<ApplyPatchArgs> {
|
||||
|
||||
for (const hunk of hunks) {
|
||||
const matchSequence: Array<string> = hunk
|
||||
.filter((line: string) => line.startsWith(" ") || line.startsWith("-"))
|
||||
.map((line: string) => line.slice(1));
|
||||
.filter((line: string) => {
|
||||
return line.startsWith(" ") || line.startsWith("-");
|
||||
})
|
||||
.map((line: string) => {
|
||||
return line.slice(1);
|
||||
});
|
||||
const startIndex: number = this.findMatchSequence(
|
||||
originalLines,
|
||||
cursor,
|
||||
@@ -296,11 +303,21 @@ export class ApplyPatchTool extends StructuredTool<ApplyPatchArgs> {
|
||||
const value: string = line.length > 1 ? line.slice(1) : "";
|
||||
|
||||
if (op === " ") {
|
||||
this.assertLineMatches(originalLines, localIndex, value, relativePath);
|
||||
this.assertLineMatches(
|
||||
originalLines,
|
||||
localIndex,
|
||||
value,
|
||||
relativePath,
|
||||
);
|
||||
outputLines.push(value);
|
||||
localIndex += 1;
|
||||
} else if (op === "-") {
|
||||
this.assertLineMatches(originalLines, localIndex, value, relativePath);
|
||||
this.assertLineMatches(
|
||||
originalLines,
|
||||
localIndex,
|
||||
value,
|
||||
relativePath,
|
||||
);
|
||||
localIndex += 1;
|
||||
} else if (op === "+") {
|
||||
outputLines.push(value);
|
||||
@@ -369,7 +386,11 @@ export class ApplyPatchTool extends StructuredTool<ApplyPatchArgs> {
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
for (let index: number = startIndex; index <= originalLines.length - sequence.length; index += 1) {
|
||||
for (
|
||||
let index: number = startIndex;
|
||||
index <= originalLines.length - sequence.length;
|
||||
index += 1
|
||||
) {
|
||||
let matched: boolean = true;
|
||||
for (let offset: number = 0; offset < sequence.length; offset += 1) {
|
||||
if (originalLines[index + offset] !== sequence[offset]) {
|
||||
|
||||
Reference in New Issue
Block a user