Add Trajectory.from_messages/2 for building trajectories without a live chain#560
Merged
Conversation
…ve chain
`from_chain/1` is the only constructor that reads live data, and it requires
an `%LLMChain{}`. Callers that only have a bare list of messages (for example
a persisted agent state, a workflow, or a sliced "last turn") had no entry
point, even though the real work — tool-call extraction, token aggregation,
and metadata — is a pure function of a `[Message.t()]` plus an optional llm.
Add `Trajectory.from_messages/2` exposing that seam, with `llm` optional
(defaulting to `nil`, which yields empty metadata via the existing
`extract_metadata/1` fallback). Refactor `from_chain/1` to delegate to it so
there is a single implementation and the two entry points cannot drift.
Tests cover bare-list construction with and without an llm, token
aggregation over a hand-built list, `matches?/3` on a no-llm trajectory, and
parity (`from_chain(chain) == from_messages(chain.exchanged_messages, chain.llm)`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LangChain.Trajectory.from_chain/1is the only constructor that reads live run data, and it requires an%LLMChain{}. But the real work it does — tool-call extraction, token aggregation, and metadata — is a pure function of a[Message.t()]plus an optional llm.Callers that only have a bare list of messages had no entry point: a persisted agent state, a workflow, or a sliced "last turn" of a conversation. There was no way to build a trajectory from those without reconstructing a chain.
Solution
Add
Trajectory.from_messages/2, exposing the message list as a first-class seam:llmis optional and defaults tonil, which yields empty metadata via the existingextract_metadata/1fallback clause — no helper needed to change. Metadata does not survive a JSON roundtrip anyway, so omitting it is safe when you only have a stored message list.from_chain/1is refactored to delegate tofrom_messages/2, so there is a single implementation and the two entry points cannot drift apart.Changes
lib/trajectory.ex— Addfrom_messages/2(with optionalllm); refactorfrom_chain/1to delegate to itlib/trajectory.ex(@moduledoc+@doc) — Document the new constructor and the "exchanged messages for an operation" framingtest/trajectory_test.exs— Add afrom_messages/2describe block: bare-list construction with and without an llm (metadata present vs%{}), token aggregation over a hand-built list,matches?/3on a no-llm trajectory, and a parity test assertingfrom_chain(chain) == from_messages(chain.exchanged_messages, chain.llm)Testing
mix precommitpasses clean: 31 doctests, 1773 tests, 0 failures (143 excluded live-API tests), plus a clean Sobelow scan. The 5 new tests are intest/trajectory_test.exsand run async with no external API calls. The parity test guards the delegation refactor by asserting full struct equality between the two entry points.🤖 Generated with Claude Code