LLM-Bridge 1.11.3__py3-none-any.whl → 1.11.3b0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ from anthropic import BetaMessageStreamEvent, AsyncAnthropic
2
+ from anthropic.types.beta import BetaRawContentBlockDelta, BetaThinkingDelta, BetaTextDelta, BetaInputJSONDelta, \
3
+ BetaBashCodeExecutionToolResultBlock, \
4
+ BetaTextEditorCodeExecutionToolResultBlock
5
+ from anthropic.types.beta.beta_raw_content_block_start_event import ContentBlock
6
+
7
+ from llm_bridge.client.implementations.claude.claude_token_counter import count_claude_output_tokens
8
+ from llm_bridge.type.chat_response import ChatResponse
9
+
10
+
11
+ class ClaudeStreamResponseHandler:
12
+ async def process_claude_stream_response(
13
+ self,
14
+ event: BetaMessageStreamEvent,
15
+ input_tokens: int,
16
+ client: AsyncAnthropic,
17
+ model: str,
18
+ ) -> ChatResponse:
19
+ text = ""
20
+ thought = ""
21
+ code = ""
22
+ code_output = ""
23
+
24
+ if event.type == "content_block_delta":
25
+ event_delta: BetaRawContentBlockDelta = event.delta
26
+
27
+ if event_delta.type == "text_delta":
28
+ text_delta: BetaTextDelta = event_delta
29
+ text += text_delta.text
30
+
31
+ elif event_delta.type == "thinking_delta":
32
+ thinking_delta: BetaThinkingDelta = event_delta
33
+ thought += thinking_delta.thinking
34
+
35
+ elif event_delta.type == "input_json_delta":
36
+ input_json_delta: BetaInputJSONDelta = event_delta
37
+ code += input_json_delta.partial_json
38
+
39
+ if event.type == "content_block_start":
40
+ event_content_block: ContentBlock = event.content_block
41
+
42
+ if event_content_block.type == "bash_code_execution_tool_result":
43
+ bash_code_execution_tool_result_block: BetaBashCodeExecutionToolResultBlock = event_content_block
44
+ if bash_code_execution_tool_result_block.content.type == "bash_code_execution_result":
45
+ code_output += event_content_block.content.stdout
46
+
47
+ elif event_content_block.type == "text_editor_code_execution_tool_result":
48
+ text_editor_code_execution_tool_result: BetaTextEditorCodeExecutionToolResultBlock = event_content_block
49
+ if text_editor_code_execution_tool_result.content.type == "text_editor_code_execution_view_result":
50
+ code_output += event_content_block.content.content
51
+
52
+ chat_response = ChatResponse(
53
+ text=text,
54
+ thought=thought,
55
+ code=code,
56
+ code_output=code_output,
57
+ )
58
+ output_tokens = await count_claude_output_tokens(
59
+ client=client,
60
+ model=model,
61
+ chat_response=chat_response,
62
+ )
63
+ return ChatResponse(
64
+ text=text,
65
+ thought=thought,
66
+ code=code,
67
+ code_output=code_output,
68
+ input_tokens=input_tokens,
69
+ output_tokens=output_tokens,
70
+ )
@@ -7,7 +7,6 @@ from anthropic.types.beta import BetaMessage, BetaBashCodeExecutionToolResultBlo
7
7
  BetaServerToolUseBlock
8
8
  from fastapi import HTTPException
9
9
 
10
- from llm_bridge.client.implementations.claude.claude_response_handler import process_content_block
11
10
  from llm_bridge.client.implementations.claude.claude_token_counter import count_claude_output_tokens
12
11
  from llm_bridge.client.model_client.claude_client import ClaudeClient
13
12
  from llm_bridge.type.chat_response import ChatResponse
@@ -26,11 +25,27 @@ async def process_claude_non_stream_response(
26
25
  code_output = ""
27
26
 
28
27
  for content_block in message.content:
29
- content_block_chat_response = process_content_block(content_block)
30
- text += content_block_chat_response.text
31
- thought += content_block_chat_response.thought
32
- code += content_block_chat_response.code
33
- code_output += content_block_chat_response.code_output
28
+ if content_block.type == "text":
29
+ text_block: BetaTextBlock = content_block
30
+ text += text_block.text
31
+
32
+ elif content_block.type == "thinking":
33
+ thinking_block: BetaThinkingBlock = content_block
34
+ thought += thinking_block.thinking
35
+
36
+ elif content_block.type == "server_tool_use":
37
+ server_tool_use_block: BetaServerToolUseBlock = content_block
38
+ code += server_tool_use_block.input
39
+
40
+ elif content_block.type == "bash_code_execution_tool_result":
41
+ bash_code_execution_tool_result_block: BetaBashCodeExecutionToolResultBlock = content_block
42
+ if bash_code_execution_tool_result_block.content.type == "bash_code_execution_result":
43
+ code_output += bash_code_execution_tool_result_block.content.stdout
44
+
45
+ elif content_block.type == "text_editor_code_execution_tool_result":
46
+ text_editor_code_execution_tool_result: BetaBashCodeExecutionToolResultBlock = content_block
47
+ if text_editor_code_execution_tool_result.content.type == "text_editor_code_execution_view_result":
48
+ code_output += content_block.content.content
34
49
 
35
50
  chat_response = ChatResponse(
36
51
  text=text,
@@ -5,7 +5,7 @@ from typing import AsyncGenerator
5
5
  import httpx
6
6
  from fastapi import HTTPException
7
7
 
8
- from llm_bridge.client.implementations.claude.claude_response_handler import process_claude_stream_response
8
+ from llm_bridge.client.implementations.claude.claude_stream_response_handler import ClaudeStreamResponseHandler
9
9
  from llm_bridge.client.model_client.claude_client import ClaudeClient
10
10
  from llm_bridge.type.chat_response import ChatResponse
11
11
  from llm_bridge.type.serializer import serialize
@@ -26,8 +26,9 @@ class StreamClaudeClient(ClaudeClient):
26
26
  betas=self.betas,
27
27
  tools=self.tools,
28
28
  ) as stream:
29
+ stream_response_handler = ClaudeStreamResponseHandler()
29
30
  async for event in stream:
30
- yield await process_claude_stream_response(
31
+ yield await stream_response_handler.process_claude_stream_response(
31
32
  event=event,
32
33
  input_tokens=self.input_tokens,
33
34
  client=self.client,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: LLM-Bridge
3
- Version: 1.11.3
3
+ Version: 1.11.3b0
4
4
  Summary: A Bridge for LLMs
5
5
  Author-email: windsnow1025 <windsnow1025@gmail.com>
6
6
  License-Expression: MIT
@@ -4,10 +4,10 @@ llm_bridge/client/chat_client.py,sha256=XISF2BM-WkZJNbnvcLfMcbSzlrE0XMDulyE_VG9z
4
4
  llm_bridge/client/implementations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  llm_bridge/client/implementations/printing_status.py,sha256=ok3ihBRIEan3qMbc62HeXTThDx1L6Zbs_IT0HPLPspI,102
6
6
  llm_bridge/client/implementations/claude/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- llm_bridge/client/implementations/claude/claude_response_handler.py,sha256=d5e1rlxfao_rjhgT1Rky-xlCRJIK2M-e9LKNUATOczc,4143
7
+ llm_bridge/client/implementations/claude/claude_stream_response_handler.py,sha256=3fRyaQ_uZiPC-UyNq5OXCH6a_TFlqL3x1um1kd4knwc,2886
8
8
  llm_bridge/client/implementations/claude/claude_token_counter.py,sha256=m_aoLJkFPJqSBA3Thzv5vg3GnaucZh41SAgT28sLeBA,1324
9
- llm_bridge/client/implementations/claude/non_stream_claude_client.py,sha256=1khCk0vJkCQ09Q8wuCqX1ZUV54qcwtTGa21ij8ziyak,2990
10
- llm_bridge/client/implementations/claude/stream_claude_client.py,sha256=gOvdoSa_pNAbZ882pG4NAOOwNtjth-X4M3Gt34orXww,2005
9
+ llm_bridge/client/implementations/claude/non_stream_claude_client.py,sha256=OljVF37RaP98WhPnSgLRgqTMPSWJ4dGjeashg3f5dRo,3779
10
+ llm_bridge/client/implementations/claude/stream_claude_client.py,sha256=q4w1UYc1yZJw5UFOtnxCoeg8MFp5soc1d57YiCTCCGE,2109
11
11
  llm_bridge/client/implementations/gemini/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
12
  llm_bridge/client/implementations/gemini/gemini_response_handler.py,sha256=LADXq5h_bbuCclp4RTf70YtQ0_9JFRRzo7j4V5Ts7y4,4388
13
13
  llm_bridge/client/implementations/gemini/gemini_token_counter.py,sha256=GdnwJWPhGZMB_xC0fz88zQRparIHzTemkQoqfDcxVEA,687
@@ -55,8 +55,8 @@ llm_bridge/type/model_message/claude_message.py,sha256=gYJUTbLUeifQMva3Axarc-VFe
55
55
  llm_bridge/type/model_message/gemini_message.py,sha256=mh8pf929g7_NkBzSOwnLXyrwSzTT4yt2FmyX7NZn0sM,4302
56
56
  llm_bridge/type/model_message/openai_message.py,sha256=xFaLY-cZoSwNd7E9BSWQjBNcRfCVH11X9s2yxXlctR0,453
57
57
  llm_bridge/type/model_message/openai_responses_message.py,sha256=be1q2euA0ybjj4NO6NxOGIRB9eJuXSb4ssUm_bM4Ocs,1529
58
- llm_bridge-1.11.3.dist-info/licenses/LICENSE,sha256=m6uon-6P_CaiqcBfApMfjG9YRtDxcr40Z52JcqUCEAE,1069
59
- llm_bridge-1.11.3.dist-info/METADATA,sha256=iyJlwk0I2O3Qm6hEwEGhTFpsYxKK7GUJZE8X9Zw9Z9c,7849
60
- llm_bridge-1.11.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
61
- llm_bridge-1.11.3.dist-info/top_level.txt,sha256=PtxyrgNX1lSa1Ab_qswg0sekSXejG5zrS6b_v3Po05g,11
62
- llm_bridge-1.11.3.dist-info/RECORD,,
58
+ llm_bridge-1.11.3b0.dist-info/licenses/LICENSE,sha256=m6uon-6P_CaiqcBfApMfjG9YRtDxcr40Z52JcqUCEAE,1069
59
+ llm_bridge-1.11.3b0.dist-info/METADATA,sha256=Skjps0NM7c883d_Mx5PQ0cpbDrEiYLXLu6nbc9qsWdI,7851
60
+ llm_bridge-1.11.3b0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
61
+ llm_bridge-1.11.3b0.dist-info/top_level.txt,sha256=PtxyrgNX1lSa1Ab_qswg0sekSXejG5zrS6b_v3Po05g,11
62
+ llm_bridge-1.11.3b0.dist-info/RECORD,,
@@ -1,106 +0,0 @@
1
- from anthropic import BetaMessageStreamEvent, AsyncAnthropic
2
- from anthropic.types.beta import BetaRawContentBlockDelta, BetaThinkingDelta, BetaTextDelta, BetaInputJSONDelta, \
3
- BetaBashCodeExecutionToolResultBlock, \
4
- BetaTextEditorCodeExecutionToolResultBlock, BetaTextEditorCodeExecutionViewResultBlock, \
5
- BetaTextEditorCodeExecutionStrReplaceResultBlock, \
6
- BetaServerToolUseBlock, BetaBashCodeExecutionResultBlock, BetaTextBlock, BetaThinkingBlock
7
- from anthropic.types.beta.beta_raw_content_block_start_event import ContentBlock
8
-
9
- from llm_bridge.client.implementations.claude.claude_token_counter import count_claude_output_tokens
10
- from llm_bridge.type.chat_response import ChatResponse
11
-
12
-
13
- def process_content_block(content_block: ContentBlock) -> ChatResponse:
14
- text = ""
15
- thought = ""
16
- code = ""
17
- code_output = ""
18
-
19
- if content_block.type == "text":
20
- text_block: BetaTextBlock = content_block
21
- text += text_block.text
22
-
23
- elif content_block.type == "thinking":
24
- thinking_block: BetaThinkingBlock = content_block
25
- thought += thinking_block.thinking
26
-
27
- elif content_block.type == "server_tool_use":
28
- server_tool_use_block: BetaServerToolUseBlock = content_block
29
- code += str(server_tool_use_block.input)
30
-
31
- elif content_block.type == "bash_code_execution_tool_result":
32
- bash_code_execution_tool_result_block: BetaBashCodeExecutionToolResultBlock = content_block
33
- if bash_code_execution_tool_result_block.content.type == "bash_code_execution_result":
34
- content: BetaBashCodeExecutionResultBlock = content_block.content
35
- code_output += content.stdout
36
-
37
- elif content_block.type == "text_editor_code_execution_tool_result":
38
- text_editor_code_execution_tool_result: BetaTextEditorCodeExecutionToolResultBlock = content_block
39
- if text_editor_code_execution_tool_result.content.type == "text_editor_code_execution_view_result":
40
- content: BetaTextEditorCodeExecutionViewResultBlock = content_block.content
41
- code_output += content.content
42
- elif text_editor_code_execution_tool_result.content.type == "text_editor_code_execution_str_replace_result":
43
- content: BetaTextEditorCodeExecutionStrReplaceResultBlock = content_block.content
44
- code_output += content.lines
45
-
46
- return ChatResponse(
47
- text=text,
48
- thought=thought,
49
- code=code,
50
- code_output=code_output,
51
- )
52
-
53
-
54
- async def process_claude_stream_response(
55
- event: BetaMessageStreamEvent,
56
- input_tokens: int,
57
- client: AsyncAnthropic,
58
- model: str,
59
- ) -> ChatResponse:
60
- text = ""
61
- thought = ""
62
- code = ""
63
- code_output = ""
64
-
65
- if event.type == "content_block_delta":
66
- event_delta: BetaRawContentBlockDelta = event.delta
67
-
68
- if event_delta.type == "text_delta":
69
- text_delta: BetaTextDelta = event_delta
70
- text += text_delta.text
71
-
72
- elif event_delta.type == "thinking_delta":
73
- thinking_delta: BetaThinkingDelta = event_delta
74
- thought += thinking_delta.thinking
75
-
76
- elif event_delta.type == "input_json_delta":
77
- input_json_delta: BetaInputJSONDelta = event_delta
78
- code += input_json_delta.partial_json
79
-
80
- if event.type == "content_block_start":
81
- content_block: ContentBlock = event.content_block
82
- content_block_chat_response = process_content_block(content_block)
83
- text += content_block_chat_response.text
84
- thought += content_block_chat_response.thought
85
- code += content_block_chat_response.code
86
- code_output += content_block_chat_response.code_output
87
-
88
- chat_response = ChatResponse(
89
- text=text,
90
- thought=thought,
91
- code=code,
92
- code_output=code_output,
93
- )
94
- output_tokens = await count_claude_output_tokens(
95
- client=client,
96
- model=model,
97
- chat_response=chat_response,
98
- )
99
- return ChatResponse(
100
- text=text,
101
- thought=thought,
102
- code=code,
103
- code_output=code_output,
104
- input_tokens=input_tokens,
105
- output_tokens=output_tokens,
106
- )