Skip to content

Commit 88e048f

Browse files
authored
Debug mode (#121)
1 parent af963a6 commit 88e048f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test = [
4949
release = ["build>=1.4.2", "jinja2>=3.1.6", "sphinx>=9.1.0", "twine>=6.2.0"]
5050
lint = ["basedpyright>=1.38.4", "ruff>=0.15.8"]
5151
dev = [
52+
"rich>=14.3.3",
5253
"splunk-sdk[openai, anthropic]",
5354
{ include-group = "test" },
5455
{ include-group = "lint" },

splunklib/ai/engines/langchain.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import json
1616
import logging
17+
import os
1718
import uuid
1819
from collections.abc import Awaitable, Callable, Sequence
1920
from dataclasses import asdict, dataclass
@@ -119,6 +120,16 @@
119120
LC_AgentMiddleware = Langchain_AgentMiddleware[Any, "InvokeContext", Any]
120121
LC_ModelRequest = Langchain_ModelRequest["InvokeContext"]
121122

123+
# Set to True to enable debugging mode.
124+
_DEBUG = False
125+
126+
# Disallow _DEBUG == True in CI.
127+
# Github actions sets the CI env var.
128+
if _DEBUG and os.environ.get("CI") is not None:
129+
raise Exception(
130+
"_DEBUG can only be used in a local dev env and shouldn't ever be committed!"
131+
)
132+
122133
# Represents a prefix reserved only for internal use.
123134
# No user-visible tool or subagent name can be prefixed with it.
124135
RESERVED_LC_TOOL_PREFIX = "__"
@@ -475,6 +486,48 @@ def unpack_tool_call(self, call: LC_ToolCall) -> LC_ToolCall:
475486
lc_middleware.append(_ThreadIDMiddleware())
476487
lc_middleware.append(_SubagentArgumentPacker())
477488

489+
class _DEBUGMiddleware(LC_AgentMiddleware):
490+
@override
491+
async def awrap_model_call(
492+
self,
493+
request: LC_ModelRequest,
494+
handler: Callable[[LC_ModelRequest], Awaitable[LC_ModelCallResult]],
495+
) -> LC_ModelCallResult:
496+
from rich import print
497+
498+
print("LLM CALL", request)
499+
try:
500+
resp = await handler(request)
501+
except Exception as e:
502+
print("LLM FAILURE", e)
503+
raise
504+
505+
print("LLM RESPONSE", resp)
506+
return resp
507+
508+
@override
509+
async def awrap_tool_call(
510+
self,
511+
request: LC_ToolCallRequest,
512+
handler: Callable[
513+
[LC_ToolCallRequest], Awaitable[LC_ToolMessage | LC_Command[None]]
514+
],
515+
) -> LC_ToolMessage | LC_Command[None]:
516+
from rich import print
517+
518+
print("TOOL CALL", request)
519+
try:
520+
resp = await handler(request)
521+
except Exception as e:
522+
print("TOOL FAILURE", e)
523+
raise
524+
525+
print("TOOL RESPONSE", resp)
526+
return resp
527+
528+
if _DEBUG:
529+
lc_middleware.append(_DEBUGMiddleware())
530+
478531
response_format = None
479532
if agent.output_schema is not None:
480533
if _supports_provider_strategy(model_impl):

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)