mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-06 14:42:11 +00:00
[open-swe] feat: Add LangGraph thread and run ID tracking to shared logger (#256)
* Apply patch * Apply patch * Apply patch * formatting * resuse code --------- Co-authored-by: open-swe-dev[bot] <open-swe-dev@users.noreply.github.com> Co-authored-by: bracesproul <braceasproul@gmail.com>
This commit is contained in:
parent
a71f17f1d6
commit
4f88413002
1 changed files with 34 additions and 20 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
import { getConfig } from "@langchain/langgraph";
|
||||||
|
|
||||||
export enum LogLevel {
|
export enum LogLevel {
|
||||||
DEBUG = "debug",
|
DEBUG = "debug",
|
||||||
|
|
@ -41,6 +42,35 @@ function simpleHash(str: string): number {
|
||||||
return Math.abs(hash); // Ensure positive for modulo index
|
return Math.abs(hash); // Ensure positive for modulo index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to safely extract thread_id and run_id from LangGraph config
|
||||||
|
function getThreadAndRunIds(): { thread_id?: string; run_id?: string } {
|
||||||
|
try {
|
||||||
|
const config = getConfig();
|
||||||
|
return {
|
||||||
|
thread_id: config.configurable?.thread_id,
|
||||||
|
run_id: config.configurable?.run_id,
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
// If getConfig throws an error or config.configurable is undefined,
|
||||||
|
// return empty object and proceed as normal
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function logWithOptionalIds(styledPrefix: string, message: string, data?: any) {
|
||||||
|
const ids = getThreadAndRunIds();
|
||||||
|
if (Object.keys(ids).length > 0) {
|
||||||
|
const logData = data !== undefined ? { ...data, ...ids } : ids;
|
||||||
|
console.log(`${styledPrefix} ${message}`, logData);
|
||||||
|
} else {
|
||||||
|
if (data !== undefined) {
|
||||||
|
console.log(`${styledPrefix} ${message}`, data);
|
||||||
|
} else {
|
||||||
|
console.log(`${styledPrefix} ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createLogger(level: LogLevel, prefix: string) {
|
export function createLogger(level: LogLevel, prefix: string) {
|
||||||
const hash = simpleHash(prefix);
|
const hash = simpleHash(prefix);
|
||||||
const color = COLORS[hash % COLORS.length];
|
const color = COLORS[hash % COLORS.length];
|
||||||
|
|
@ -49,20 +79,12 @@ export function createLogger(level: LogLevel, prefix: string) {
|
||||||
return {
|
return {
|
||||||
debug: (message: string, data?: any) => {
|
debug: (message: string, data?: any) => {
|
||||||
if (level === LogLevel.DEBUG) {
|
if (level === LogLevel.DEBUG) {
|
||||||
if (data !== undefined) {
|
logWithOptionalIds(styledPrefix, message, data);
|
||||||
console.log(`${styledPrefix} ${message}`, data);
|
|
||||||
} else {
|
|
||||||
console.log(`${styledPrefix} ${message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
info: (message: string, data?: any) => {
|
info: (message: string, data?: any) => {
|
||||||
if (level === LogLevel.INFO || level === LogLevel.DEBUG) {
|
if (level === LogLevel.INFO || level === LogLevel.DEBUG) {
|
||||||
if (data !== undefined) {
|
logWithOptionalIds(styledPrefix, message, data);
|
||||||
console.log(`${styledPrefix} ${message}`, data);
|
|
||||||
} else {
|
|
||||||
console.log(`${styledPrefix} ${message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
warn: (message: string, data?: any) => {
|
warn: (message: string, data?: any) => {
|
||||||
|
|
@ -71,11 +93,7 @@ export function createLogger(level: LogLevel, prefix: string) {
|
||||||
level === LogLevel.INFO ||
|
level === LogLevel.INFO ||
|
||||||
level === LogLevel.DEBUG
|
level === LogLevel.DEBUG
|
||||||
) {
|
) {
|
||||||
if (data !== undefined) {
|
logWithOptionalIds(styledPrefix, message, data);
|
||||||
console.log(`${styledPrefix} ${message}`, data);
|
|
||||||
} else {
|
|
||||||
console.log(`${styledPrefix} ${message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: (message: string, data?: any) => {
|
error: (message: string, data?: any) => {
|
||||||
|
|
@ -85,11 +103,7 @@ export function createLogger(level: LogLevel, prefix: string) {
|
||||||
level === LogLevel.INFO ||
|
level === LogLevel.INFO ||
|
||||||
level === LogLevel.DEBUG
|
level === LogLevel.DEBUG
|
||||||
) {
|
) {
|
||||||
if (data !== undefined) {
|
logWithOptionalIds(styledPrefix, message, data);
|
||||||
console.log(`${styledPrefix} ${message}`, data);
|
|
||||||
} else {
|
|
||||||
console.log(`${styledPrefix} ${message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue