One string field of a structured answer, read as the answer streams (#2921): feed it each piece of text as it arrives - an agent's AgentEvents.TextDelta, a client's StreamedChunk - and it returns what the field gained, so an application can show, say, a narration field as the model writes it without parsing incomplete JSON itself.
val narration = StreamedField("narration")
agent.stream(threadId, query) {
case AgentEvents.ModelCallStarted(_) => narration.reset()
case AgentEvents.TextDelta(d) => print(narration.feed(d.text))
case _ => ()
}
The text so far is read with PartialJson, from its first { or [ (" for a string answer), so a code fence or prose before the JSON is skipped. An escape split across two pieces is held back until it is whole. The field's text only grows: what feed returns is always the text after what it returned before. A path of more than one key reads a nested field (StreamedField("scene", "description")); the field of a union answer is under its answer envelope.
'''A new answer.''' Each model call streams an answer of its own: a corrected answer after a failed output check, a retry, the next step after a tool call, the next turn. Call reset on every AgentEvents.ModelCallStarted, and clear what was shown then. (A delta's attempt cannot tell: each model call counts its attempts from 1.)
Each feed parses the text so far again, so the work for an answer is quadratic in its length: fine for an answer of a few tens of kilobytes. Not thread-safe: feed it from the one thread that receives the stream.
Value parameters
- path
-
the keys from the answer's root to the string field
Attributes
- Companion
- object
- Source
- StreamedField.scala
- Graph
-
- Supertypes
