Skip to content

Commit 5bb7c19

Browse files
committed
Updated chatterlang_serve to support api key as a configuration setting
1 parent 67867ac commit 5bb7c19

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
is displayed with a full traceback, and the prompt continues accepting input. This makes interactive
2424
pipelines more robust and user-friendly. The error-resilient behavior is enabled by default but can
2525
be disabled by setting `error_resilient=False`.
26+
- Updated **chatterlang_serve** to support API key configuration via the configuration system. When
27+
`--api-key` is not specified on the command line, it will check for `API_KEY` in the configuration,
28+
which automatically checks the `TALKPIPE_API_KEY` environment variable. This makes it easier to
29+
deploy chatterlang_serve in Docker and CI/CD environments without exposing API keys in command history.
2630

2731
## 0.9.3
2832
### Improvements

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ ChatterLang is an external DSL and is defined under talkpipe/chatterlang
1616
Other packages in the project other than util include implementations of pipe sources and segments
1717
Util contains various generally usable utility functions, expecially for basic data manipulation and configuration.
1818
talkpipe/README.md contains a longer overview
19+
After making a change, update CHANGELOG.md
20+
When making a change, write a unit test that fails without the change, then make the change and test that the test now passes. Notify the user if it is not reasonable to write the unit test.
1921

2022
## Important Commands
2123
python -m build for creating a new release

src/talkpipe/app/chatterlang_serve.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,19 +1788,24 @@ def go():
17881788
help='Property of the input json to display in the stream interface as user input.')
17891789

17901790
args, unknown_args = parser.parse_known_args()
1791-
1791+
17921792
# Parse unknown arguments and add to configuration so they're accessible via $key syntax
17931793
constants = parse_unknown_args(unknown_args)
1794-
1794+
17951795
# Add command-line constants to the configuration
17961796
if constants:
17971797
add_config_values(constants, override=True)
17981798
print(f"Added command-line values to configuration: {list(constants.keys())}")
1799-
1799+
18001800
if args.load_module:
18011801
for module_file in args.load_module:
18021802
load_module_file(fname=module_file, fail_on_missing=False)
18031803

1804+
# Get API key from command line, or fall back to configuration (which checks environment variable)
1805+
api_key = args.api_key
1806+
if api_key is None:
1807+
api_key = get_config().get('API_KEY')
1808+
18041809
script_content = None
18051810
if args.script:
18061811
script_content = load_script(args.script)
@@ -1823,7 +1828,7 @@ def go():
18231828
receiver = ChatterlangServer(
18241829
host=args.host,
18251830
port=args.port,
1826-
api_key=args.api_key,
1831+
api_key=api_key,
18271832
require_auth=args.require_auth,
18281833
title=args.title,
18291834
form_config=form_config,

tests/talkpipe/app/test_chatterlang_serve.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,50 @@ def test_go_with_constants(self, mock_server_class):
939939
override=True
940940
)
941941

942+
@patch('talkpipe.app.chatterlang_serve.ChatterlangServer')
943+
@patch('talkpipe.app.chatterlang_serve.get_config')
944+
@patch('sys.argv', ['script.py'])
945+
def test_go_api_key_from_config(self, mock_get_config, mock_server_class):
946+
"""Test go function loads API key from configuration when not provided on command line."""
947+
mock_server = Mock()
948+
mock_server_class.return_value = mock_server
949+
950+
# Mock configuration with API_KEY
951+
mock_config = {'API_KEY': 'config-api-key'}
952+
mock_get_config.return_value = mock_config
953+
954+
with patch('talkpipe.app.chatterlang_serve.parse_unknown_args', return_value={}), \
955+
patch('talkpipe.app.chatterlang_serve.add_config_values'), \
956+
patch('talkpipe.app.chatterlang_serve.load_script', return_value=None):
957+
958+
go()
959+
960+
# Verify server was created with API key from config
961+
call_args = mock_server_class.call_args[1]
962+
assert call_args["api_key"] == "config-api-key"
963+
964+
@patch('talkpipe.app.chatterlang_serve.ChatterlangServer')
965+
@patch('talkpipe.app.chatterlang_serve.get_config')
966+
@patch('sys.argv', ['script.py', '--api-key', 'cmdline-api-key'])
967+
def test_go_api_key_cmdline_overrides_config(self, mock_get_config, mock_server_class):
968+
"""Test go function uses command line API key over configuration."""
969+
mock_server = Mock()
970+
mock_server_class.return_value = mock_server
971+
972+
# Mock configuration with API_KEY
973+
mock_config = {'API_KEY': 'config-api-key'}
974+
mock_get_config.return_value = mock_config
975+
976+
with patch('talkpipe.app.chatterlang_serve.parse_unknown_args', return_value={}), \
977+
patch('talkpipe.app.chatterlang_serve.add_config_values'), \
978+
patch('talkpipe.app.chatterlang_serve.load_script', return_value=None):
979+
980+
go()
981+
982+
# Verify server was created with command line API key (takes precedence)
983+
call_args = mock_server_class.call_args[1]
984+
assert call_args["api_key"] == "cmdline-api-key"
985+
942986

943987
class TestAsyncFunctionality:
944988
"""Test async functionality."""

0 commit comments

Comments
 (0)