test_fromconfig.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Tests for lambdagent.fromconfig compiler."""
  2. import os
  3. import json
  4. import tempfile
  5. import pytest
  6. import yaml
  7. from lambdagent.fromconfig import from_config, lint_config, to_lambda_expr, describe_config
  8. from lambdagent.fromconfig.errors import SchemaError
  9. from lambdagent.fromconfig.schema import validate_schema
  10. from lambdagent.primitives import Lam, Loop, Compose
  11. from lambdagent.extensions import Memory, Guard, Route
  12. from lambdagent.conversation import ConversationLam
  13. def _write_config(config):
  14. f = tempfile.NamedTemporaryFile(mode='w', suffix='.yml', delete=False, encoding='utf-8')
  15. yaml.dump(config, f, allow_unicode=True)
  16. f.close()
  17. return f.name
  18. def test_compile_simple():
  19. path = _write_config({"type": "simple", "systemPrompt": "Hello", "model": {"name": "test"}})
  20. try:
  21. term = from_config(path)
  22. # The compiler may emit either Lam (stateless) or ConversationLam
  23. # (history-aware) for a "simple" agent depending on provider config.
  24. # Both are valid Lambda abstractions — accept either.
  25. assert isinstance(term, (Lam, ConversationLam)), \
  26. f"expected Lam or ConversationLam, got {type(term).__name__}"
  27. finally:
  28. os.unlink(path)
  29. def test_compile_chain():
  30. path = _write_config({
  31. "type": "chain", "model": {"name": "test"},
  32. "chain": {"steps": [
  33. {"name": "s1", "prompt": "Step 1"},
  34. {"name": "s2", "prompt": "Step 2"},
  35. ]}
  36. })
  37. try:
  38. term = from_config(path)
  39. assert isinstance(term, Compose)
  40. assert len(term.stages) == 2
  41. finally:
  42. os.unlink(path)
  43. def test_compile_react():
  44. path = _write_config({
  45. "type": "react", "systemPrompt": "Think",
  46. "model": {"name": "test"},
  47. "react": {"maxSteps": 5},
  48. "mcp": {"localTools": ["terminate"]},
  49. })
  50. try:
  51. term = from_config(path)
  52. assert isinstance(term, Loop)
  53. assert term.max_steps == 5
  54. finally:
  55. os.unlink(path)
  56. def test_compile_with_memory():
  57. path = _write_config({
  58. "type": "simple", "systemPrompt": "Hello",
  59. "model": {"name": "test"},
  60. "memory": {"enabled": True, "strategy": "local", "size": 10, "ttl": 3600},
  61. })
  62. try:
  63. term = from_config(path)
  64. assert isinstance(term, Memory)
  65. finally:
  66. os.unlink(path)
  67. def test_compile_with_guard():
  68. path = _write_config({
  69. "type": "simple", "systemPrompt": "Hello",
  70. "model": {"name": "test"},
  71. "guard": {"validator": "len(x) > 5", "retry": 1},
  72. })
  73. try:
  74. term = from_config(path)
  75. assert isinstance(term, Guard)
  76. finally:
  77. os.unlink(path)
  78. def test_schema_validation_missing_type():
  79. errors = validate_schema({})
  80. assert any(e[0] == "ERROR" for e in errors)
  81. def test_lint_no_terminate():
  82. results = lint_config({
  83. "type": "react", "systemPrompt": "x",
  84. "react": {"maxSteps": 10},
  85. "mcp": {"localTools": []},
  86. })
  87. rules = [r.rule for r in results]
  88. assert any("L004" in r for r in rules)
  89. def test_lint_clean():
  90. results = lint_config({
  91. "type": "react", "systemPrompt": "x",
  92. "model": {"name": "test"},
  93. "react": {"maxSteps": 10},
  94. "mcp": {"localTools": ["terminate"]},
  95. })
  96. errors = [r for r in results if r.level == "ERROR"]
  97. assert len(errors) == 0
  98. def test_to_lambda_expr():
  99. expr = to_lambda_expr({"type": "simple", "name": "test", "systemPrompt": "hi"})
  100. assert "test" in expr
  101. assert "lambda" in expr
  102. def test_describe_config():
  103. desc = describe_config({"type": "react", "name": "agent", "react": {"maxSteps": 10},
  104. "mcp": {"onlineTool": {"s": ["search"]}, "localTools": ["terminate"]}})
  105. assert "Y_10" in desc or "Loop" in desc