[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:
open-swe[bot] 2025-06-19 20:06:16 +00:00 • committed by GitHub
parent a71f17f1d6
commit 4f88413002
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,5 @@
/* eslint-disable no-console */
import { getConfig } from "@langchain/langgraph";
export enum LogLevel {
DEBUG = "debug",
@ -41,6 +42,35 @@ function simpleHash(str: string): number {
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) {
const hash = simpleHash(prefix);
const color = COLORS[hash % COLORS.length];
@ -49,20 +79,12 @@ export function createLogger(level: LogLevel, prefix: string) {
return {
debug: (message: string, data?: any) => {
if (level === LogLevel.DEBUG) {
if (data !== undefined) {
console.log(`${styledPrefix} ${message}`, data);
} else {
console.log(`${styledPrefix} ${message}`);
}
logWithOptionalIds(styledPrefix, message, data);
}
},
info: (message: string, data?: any) => {
if (level === LogLevel.INFO || level === LogLevel.DEBUG) {
if (data !== undefined) {
console.log(`${styledPrefix} ${message}`, data);
} else {
console.log(`${styledPrefix} ${message}`);
}
logWithOptionalIds(styledPrefix, message, data);
}
},
warn: (message: string, data?: any) => {
@ -71,11 +93,7 @@ export function createLogger(level: LogLevel, prefix: string) {
level === LogLevel.INFO ||
level === LogLevel.DEBUG
) {
if (data !== undefined) {
console.log(`${styledPrefix} ${message}`, data);
} else {
console.log(`${styledPrefix} ${message}`);
}
logWithOptionalIds(styledPrefix, message, data);
}
},
error: (message: string, data?: any) => {
@ -85,11 +103,7 @@ export function createLogger(level: LogLevel, prefix: string) {
level === LogLevel.INFO ||
level === LogLevel.DEBUG
) {
if (data !== undefined) {
console.log(`${styledPrefix} ${message}`, data);
} else {
console.log(`${styledPrefix} ${message}`);
}
logWithOptionalIds(styledPrefix, message, data);
}
},
};