tests/testthat/test-session-agent.R

test_that("ChatSession initializes from Agent", {
  # Create a mock agent
  agent <- Agent$new(
    name = "TestAgent",
    description = "A test agent",
    system_prompt = "You are a test agent.",
    tools = list(
      Tool$new(
        name = "test_tool", 
        description = "A test tool", 
        parameters = z_empty_object(description = "No parameters"),
        execute = function() "result"
      )
    )
  )

  # Initialize session from agent
  session <- ChatSession$new(agent = agent)
  
  # Check system prompt
  # Note: ChatSession$initialize merges agent prompt with session prompt (if any).
  # Since we didn't provide a session prompt, it should be just the agent's prompt.
  # Accessing private fields is tricky in R6 from outside, but we can check via behavior or if there's a getter.
  # ChatSession doesn't have a getter for system_prompt, but it has as_list()
  
  session_state <- session$as_list()
  expect_equal(session_state$system_prompt, "You are a test agent.")
  expect_equal(session_state$tool_names, c("test_tool"))
})

test_that("Agent$create_session returns valid session", {
  agent <- Agent$new(
    name = "TestAgent",
    description = "A test agent",
    system_prompt = "Agent Prompt"
  )
  
  session <- agent$create_session()
  expect_s3_class(session, "ChatSession")
  expect_equal(session$as_list()$system_prompt, "Agent Prompt")
})

test_that("Session from Agent maintains history", {
  # Setup mock model
  mock_model <- MockModel$new()
  mock_model$add_response(text = "Response 1")
  mock_model$add_response(text = "Response 2")
  
  agent <- Agent$new(
    name = "TestAgent",
    description = "A test agent",
    system_prompt = "Agent System Prompt"
  )
  
  session <- agent$create_session(model = mock_model)
  
  # First turn
  session$send("Hello")
  
  # Verify first call params
  expect_equal(length(mock_model$last_params$messages), 2) # System + User
  expect_equal(mock_model$last_params$messages[[1]]$role, "system")
  expect_equal(mock_model$last_params$messages[[1]]$content, "Agent System Prompt")
  expect_equal(mock_model$last_params$messages[[2]]$role, "user")
  expect_equal(mock_model$last_params$messages[[2]]$content, "Hello")
  
  # Second turn (what user "continue" scenario effectively is)
  session$send("Continue")
  
  # Verify second call params - should have history
  # System + User1 + Assistant1 + User2
  expect_equal(length(mock_model$last_params$messages), 4)
  expect_equal(mock_model$last_params$messages[[3]]$role, "assistant")
  expect_equal(mock_model$last_params$messages[[3]]$content, "Response 1")
  expect_equal(mock_model$last_params$messages[[4]]$role, "user")
  expect_equal(mock_model$last_params$messages[[4]]$content, "Continue")
})

test_that("ChatSession send can append a turn-specific system prompt", {
  mock_model <- MockModel$new()
  mock_model$add_response(text = "Response 1")

  session <- create_chat_session(model = mock_model, system_prompt = "Base system")
  session$send("Hello", turn_system_prompt = "Matched skill context")

  expect_equal(mock_model$last_params$messages[[1]]$role, "system")
  expect_match(mock_model$last_params$messages[[1]]$content, "Base system", fixed = TRUE)
  expect_match(mock_model$last_params$messages[[1]]$content, "Matched skill context", fixed = TRUE)
})

test_that("ChatSession send forwards the live session to tool execution", {
  mock_model <- MockModel$new()
  mock_model$add_response(
    tool_calls = list(list(
      id = "call_1",
      name = "store_value",
      arguments = list(value = 42)
    ))
  )
  mock_model$add_response(text = "Stored")

  store_tool <- Tool$new(
    name = "store_value",
    description = "Store a value in the session environment",
    parameters = z_object(
      value = z_number("Value to store")
    ),
    execute = function(args) {
      assign("stored_value", args$value, envir = args$.envir)
      paste("Stored", args$value)
    }
  )

  session <- create_chat_session(model = mock_model, tools = list(store_tool))
  session$send("Store 42")

  expect_true(exists("stored_value", envir = session$get_envir()))
  expect_equal(session$get_envir()$stored_value, 42)
})

Try the aisdk package in your browser

Any scripts or data that you put into this service are public.

aisdk documentation built on May 29, 2026, 9:07 a.m.