Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
.PHONY: sync format format-check lint mypy pyright tests gen-docs build-docs serve-docs deploy-docs check
.PHONY: sync install format format-check lint mypy pyright test build serve-docs deploy-docs check

install:
uv sync --all-extras --all-packages --group dev
UV_ENV := env -i PATH="$(PATH)" HOME="$(HOME)"
UV := $(UV_ENV) uv --no-config
UV_LOCK_ARGS := --locked --default-index https://pypi.org/simple

sync:
$(UV) sync $(UV_LOCK_ARGS) --all-extras --all-packages --group dev

install: sync

format:
uv run ruff format
uv run ruff check --fix
$(UV) run $(UV_LOCK_ARGS) ruff format
$(UV) run $(UV_LOCK_ARGS) ruff check --fix

format-check:
uv run ruff format --check
$(UV) run $(UV_LOCK_ARGS) ruff format --check

lint:
uv run ruff check
$(UV) run $(UV_LOCK_ARGS) ruff check

mypy:
uv run mypy .
$(UV) run $(UV_LOCK_ARGS) mypy .

pyright:
uv run pyright
$(UV) run $(UV_LOCK_ARGS) pyright

test:
PYTHONPATH=. uv run pytest
$(UV_ENV) PYTHONPATH=. uv --no-config run $(UV_LOCK_ARGS) pytest

build:
uv build
$(UV) build --default-index https://pypi.org/simple

serve-docs:
uv run mkdocs serve
$(UV) run $(UV_LOCK_ARGS) mkdocs serve

deploy-docs:
uv run mkdocs gh-deploy --force --verbose
$(UV) run $(UV_LOCK_ARGS) mkdocs gh-deploy --force --verbose

check: format-check lint pyright test
check: format-check lint pyright test
33 changes: 23 additions & 10 deletions chatkit/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from openai.types.responses import (
EasyInputMessageParam,
ResponseFunctionToolCall,
ResponseFunctionToolCallParam,
ResponseInputContentParam,
ResponseInputImageParam,
Expand Down Expand Up @@ -455,6 +456,24 @@ async def _convert_annotation(
return None


def _function_call_metadata(raw_item: object) -> tuple[str | None, str | None]:
if isinstance(raw_item, dict):
if raw_item.get("type") != "function_call":
return None, None

call_id = raw_item.get("call_id")
item_id = raw_item.get("id")
return (
call_id if isinstance(call_id, str) else None,
item_id if isinstance(item_id, str) else None,
)

if isinstance(raw_item, ResponseFunctionToolCall):
return raw_item.call_id, raw_item.id

return None, None


async def stream_agent_response(
context: AgentContext,
result: RunResultStreaming,
Expand Down Expand Up @@ -554,16 +573,10 @@ def end_workflow(item: WorkflowItem):
if event.type == "run_item_stream_event":
event = event.item
if event.type == "tool_call_item":
raw_item = event.raw_item
if isinstance(raw_item, dict):
if raw_item.get("type") == "function_call":
current_tool_call = event.call_id
current_item_id = raw_item.get("id")
assert current_item_id
produced_items.add(current_item_id)
elif raw_item.type == "function_call":
current_tool_call = event.call_id
current_item_id = raw_item.id
current_tool_call, current_item_id = _function_call_metadata(
event.raw_item
)
if current_tool_call:
assert current_item_id
produced_items.add(current_item_id)
continue
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openai-chatkit"
version = "1.6.4"
version = "1.6.5"
description = "A ChatKit backend SDK."
readme = "README.md"
requires-python = ">=3.10"
Expand Down
44 changes: 44 additions & 0 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from openai.types.responses import (
EasyInputMessageParam,
ResponseFileSearchToolCall,
ResponseFunctionToolCall,
ResponseInputContentParam,
ResponseInputTextParam,
ResponseOutputItemAddedEvent,
Expand Down Expand Up @@ -59,6 +60,7 @@

from chatkit.agents import (
AgentContext,
ClientToolCall,
ResponseStreamConverter,
ThreadItemConverter,
accumulate_text,
Expand Down Expand Up @@ -311,6 +313,48 @@ async def widget_generator():
)


async def test_client_tool_call_uses_raw_function_call_id():
context = AgentContext(
previous_response_id=None,
thread=thread,
store=mock_store,
request_context=None,
client_tool_call=ClientToolCall(name="get_selection", arguments={}),
)
result = make_result()
raw_item = ResponseFunctionToolCall(
id="fc_123",
type="function_call",
call_id="call_123",
name="get_selection",
arguments="{}",
status="completed",
)
result.add_event(
RunItemStreamEvent(
name="tool_called",
item=ToolCallItem(
agent=Agent("Assistant"),
raw_item=raw_item,
),
)
)
result.done()

events = await all_events(
stream_agent_response(
context=context,
result=result,
)
)

assert len(events) == 1
assert isinstance(events[0], ThreadItemDoneEvent)
assert isinstance(events[0].item, ClientToolCallItem)
assert events[0].item.id == "fc_123"
assert events[0].item.call_id == "call_123"


async def test_accumulate_text():
def delta(text: str) -> RawResponsesStreamEvent:
return RawResponsesStreamEvent(
Expand Down
5 changes: 1 addition & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading