Files
2026-07-24 18:22:51 +00:00

223 lines
16 KiB
HTML

<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Google Analytics tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DKHZS27PHP"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "G-DKHZS27PHP");
</script>
<!-- Generated by javadoc (26) -->
<title>InstructionUtils (Google Agent Development Kit Maven Parent POM 1.7.0 API)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="description" content="declaration: package: com.google.adk.utils, class: InstructionUtils">
<meta name="generator" content="javadoc/ClassWriter">
<link rel="stylesheet" type="text/css" href="../../../../resource-files/jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="../../../../resource-files/stylesheet.css">
<script type="text/javascript" src="../../../../script-files/script.js"></script>
<script type="text/javascript" src="../../../../script-files/jquery-3.7.1.min.js"></script>
<script type="text/javascript" src="../../../../script-files/jquery-ui.min.js"></script>
</head>
<body class="class-declaration-page">
<script type="text/javascript">const pathtoroot = "../../../../";
loadScripts();
initTheme();
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<header role="banner">
<nav role="navigation">
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="top-nav" id="navbar-top">
<div class="nav-content">
<div class="nav-menu-button"><button id="navbar-toggle-button" aria-controls="navbar-top" aria-expanded="false" aria-label="Toggle navigation links"><span class="nav-bar-toggle-icon">&nbsp;</span><span class="nav-bar-toggle-icon">&nbsp;</span><span class="nav-bar-toggle-icon">&nbsp;</span></button></div>
<div class="skip-nav"><a href="#skip-navbar-top" title="Skip navigation links">Skip navigation links</a></div>
<ul id="navbar-top-firstrow" class="nav-list" title="Navigation">
<li><a href="../../../../index.html">Overview</a></li>
<li class="nav-bar-cell1-rev">Class</li>
<li><a href="class-use/InstructionUtils.html">Use</a></li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../search.html">Search</a></li>
<li><button id="theme-button" aria-label="Select Theme" title="Select Theme"></button></li>
</ul>
<div id="theme-panel">
<div>Select Theme</div>
<div><label for="theme-light"><input type="radio" id="theme-light" disabled name="theme" value="theme-light"><span>Light</span></label><label for="theme-dark"><input type="radio" id="theme-dark" disabled name="theme" value="theme-dark"><span>Dark</span></label><label for="theme-os"><input type="radio" id="theme-os" disabled name="theme" value="theme-os"><span>System Setting</span></label></div>
</div>
</div>
</div>
<div class="sub-nav">
<div class="nav-content">
<ol class="sub-nav-list">
<li><a href="package-summary.html">com.google.adk.utils</a></li>
<li><a href="InstructionUtils.html" class="current-selection">InstructionUtils</a></li>
</ol>
<div class="nav-list-search"><input type="text" id="search-input" disabled placeholder="Search documentation (type /)" aria-label="Search in documentation" autocomplete="off" spellcheck="false"><input type="reset" id="reset-search" disabled value="Reset"></div>
</div>
</div>
<!-- ========= END OF TOP NAVBAR ========= -->
<span class="skip-nav" id="skip-navbar-top"></span></nav>
</header>
<div class="main-grid">
<nav role="navigation" class="toc" aria-label="Table of contents">
<div class="toc-header">Contents&nbsp;<input type="text" class="filter-input" disabled placeholder="Filter contents (type .)" aria-label="Filter table of contents" autocomplete="off" spellcheck="false"><input type="reset" class="reset-filter" disabled tabindex="-1" value="Reset">&nbsp;<button class="toc-sort-toggle" id="toc-lexical-order-toggle"><img src="../../../../resource-files/sort-a-z.svg" alt="Sort member details in lexicographical order"></button></div>
<ol class="toc-list" tabindex="-1">
<li><a href="#" tabindex="0">Description</a></li>
<li><a href="#method-summary" tabindex="0">Method Summary</a></li>
<li><a href="#method-detail" tabindex="0">Method Details</a>
<ol class="toc-list">
<li><a href="#injectSessionState(com.google.adk.agents.InvocationContext,java.lang.String)" tabindex="0">injectSessionState(InvocationContext, String)</a></li>
</ol>
</li>
</ol>
<button class="hide-sidebar"><span>Hide sidebar&nbsp;</span><img src="../../../../resource-files/left.svg" alt="Hide sidebar"></button><button class="show-sidebar"><img src="../../../../resource-files/right.svg" alt="Show sidebar"><span>&nbsp;Show sidebar</span></button></nav>
<main role="main">
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<h1 title="Class InstructionUtils" class="title">Class InstructionUtils</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">com.google.adk.utils.InstructionUtils</div>
</div>
<section class="class-description" id="class-description">
<hr>
<div class="horizontal-scroll">
<div class="type-signature"><span class="modifiers">public final class </span><span class="element-name type-name-label">InstructionUtils</span>
<span class="extends-implements">extends <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class in java.lang" class="external-link">Object</a></span></div>
<div class="block">Utility methods for handling instruction templates.</div>
</div>
</section>
<section class="summary">
<ul class="summary-list">
<!-- ========== METHOD SUMMARY =========== -->
<li>
<section class="method-summary" id="method-summary">
<h2>Method Summary</h2>
<div id="method-summary-table">
<div class="table-tabs" role="tablist" aria-orientation="horizontal"><button id="method-summary-table-tab0" role="tab" aria-selected="true" aria-controls="method-summary-table.tabpanel" tabindex="0" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table', 3)" class="active-table-tab">All Methods</button><button id="method-summary-table-tab1" role="tab" aria-selected="false" aria-controls="method-summary-table.tabpanel" tabindex="-1" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table-tab1', 3)" class="table-tab">Static Methods</button><button id="method-summary-table-tab4" role="tab" aria-selected="false" aria-controls="method-summary-table.tabpanel" tabindex="-1" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table-tab4', 3)" class="table-tab">Concrete Methods</button></div>
<div id="method-summary-table.tabpanel" role="tabpanel" aria-labelledby="method-summary-table-tab0">
<div class="summary-table three-column-summary">
<div class="table-header col-first">Modifier and Type</div>
<div class="table-header col-second">Method</div>
<div class="table-header col-last">Description</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code>static io.reactivex.rxjava3.core.Single<wbr>&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class in java.lang" class="external-link">String</a>&gt;</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code><a href="#injectSessionState(com.google.adk.agents.InvocationContext,java.lang.String)" class="member-name-link">injectSessionState</a><wbr>(<a href="../agents/InvocationContext.html" title="class in com.google.adk.agents">InvocationContext</a>&nbsp;context,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class in java.lang" class="external-link">String</a>&nbsp;template)</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4">
<div class="block">Populates placeholders in an instruction template string with values from the session state or
loaded artifacts.</div>
</div>
</div>
</div>
</div>
<div class="inherited-list">
<h3 id="methods-inherited-from-class-java.lang.Object">Methods inherited from class&nbsp;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#method-summary" title="class in java.lang" class="external-link">Object</a></h3>
<code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#clone()" title="clone()" class="external-link">clone</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)" title="equals(Object)" class="external-link">equals</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#finalize()" title="finalize()" class="external-link">finalize</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#getClass()" title="getClass()" class="external-link">getClass</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#hashCode()" title="hashCode()" class="external-link">hashCode</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#notify()" title="notify()" class="external-link">notify</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#notifyAll()" title="notifyAll()" class="external-link">notifyAll</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString()" title="toString()" class="external-link">toString</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait()" title="wait()" class="external-link">wait</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait(long)" title="wait(long)" class="external-link">wait</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait(long,int)" title="wait(long, int)" class="external-link">wait</a></code></div>
</section>
</li>
</ul>
</section>
<section class="details">
<ul class="details-list">
<!-- ============ METHOD DETAIL ========== -->
<li>
<section class="method-details" id="method-detail">
<h2>Method Details</h2>
<ul class="member-list">
<li>
<section class="detail" id="injectSessionState(com.google.adk.agents.InvocationContext,java.lang.String)">
<h3>injectSessionState</h3>
<div class="horizontal-scroll">
<div class="member-signature"><span class="modifiers">public static</span>&nbsp;<span class="return-type">io.reactivex.rxjava3.core.Single&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class in java.lang" class="external-link">String</a>&gt;</span>&nbsp;<span class="element-name">injectSessionState</span><wbr><span class="parameters">(<a href="../agents/InvocationContext.html" title="class in com.google.adk.agents">InvocationContext</a>&nbsp;context,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class in java.lang" class="external-link">String</a>&nbsp;template)</span></div>
<div class="block">Populates placeholders in an instruction template string with values from the session state or
loaded artifacts.
<p><b>Placeholder Syntax:</b>
<p>Placeholders are enclosed by one or more curly braces at the start and end, e.g., <code>
{key}</code> or <code>{{key}}</code>. The core <code>key</code> is extracted from whatever is between the
innermost pair of braces after trimming whitespace and possibly removing the <code>?</code> which
denotes optionality (e.g. <code>{key?}</code>). The <code>key</code> itself must not contain curly
braces. For typical usage, a single pair of braces like <code>{my_variable}</code> is standard.
<p>The extracted <code>key</code> determines the source and name of the value:
<ul>
<li><b>Session State Variables:</b> The <code>key</code> (e.g., <code>"variable_name"</code> or <code>
"prefix:variable_name"</code>) refers to a variable in session state.
<ul>
<li>Simple name: <code>{variable_name}</code>. The <code>variable_name</code> part must be a
valid identifier as per <code>isValidStateName(String)</code>. Invalid names will
result in the placeholder being returned as is.
<li>Prefixed name: <code>{prefix:variable_name}</code>. Valid prefixes are: <a href="../sessions/State.html#APP_PREFIX">"app:"</a>, <a href="../sessions/State.html#USER_PREFIX">"user:"</a>, and <a href="../sessions/State.html#TEMP_PREFIX">"temp:"</a> The part of the name following the
prefix must also be a valid identifier. Invalid prefixes will result in the
placeholder being returned as is.
</ul>
<li><b>Artifacts:</b> The <code>key</code> starts with "<code>artifact.</code>" (e.g., <code>
"artifact.file_name"</code>).
<li><b>Optional Placeholders:</b> A <code>key</code> can be marked as optional by appending a
question mark <code>?</code> at its very end, inside the braces.
<ul>
<li>Example: <code>{optional_variable?}</code>, <code>{{artifact.optional_file.txt?}}</code>
<li>If an optional placeholder cannot be resolved (e.g., variable not found, artifact
not found), it is replaced with an empty string.
</ul>
</ul>
<b>Example Usage:</b>
<pre><code>InvocationContext context = ...; // Assume this is initialized with session and artifact service
Session session = context.session();
session.state().put("user:name", "Alice");
context.artifactService().saveArtifact(
session.appName(), session.userId(), session.id(), "knowledge.txt", Part.fromText("Origins of the universe: At first, there was-"));
String template = "You are {user:name}'s assistant. Answer questions based on your knowledge. Your knowledge: {artifact.knowledge.txt}." +
" Your extra knowledge: {artifact.missing_artifact.txt?}";
Single&lt;String&gt; populatedStringSingle = InstructionUtils.injectSessionState(context, template);
populatedStringSingle.subscribe(
result -&gt; System.out.println(result),
// Expected: "You are Alice's assistant. Answer questions based on your knowledge. Your knowledge: Origins of the universe: At first, there was-. Your extra knowledge: "
error -&gt; System.err.println("Error populating template: " + error.getMessage())
);
</code></pre></div>
<dl class="notes">
<dt>Parameters:</dt>
<dd><code>context</code> - The invocation context providing access to session state and artifact services.</dd>
<dd><code>template</code> - The instruction template string containing placeholders to be populated.</dd>
<dt>Returns:</dt>
<dd>A <code>Single</code> that will emit the populated instruction string upon successful
resolution of all non-optional placeholders. Emits the original template if it is empty or
contains no placeholders that are processed.</dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/NullPointerException.html" title="class in java.lang" class="external-link">NullPointerException</a></code> - if the template or context is null.</dd>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalArgumentException.html" title="class in java.lang" class="external-link">IllegalArgumentException</a></code> - if a non-optional variable or artifact is not found.</dd>
</dl>
</div>
</section>
</li>
</ul>
</section>
</li>
</ul>
</section>
<!-- ========= END OF CLASS DATA ========= -->
<footer>
<hr>
<p class="legal-copy"><small>Copyright &#169; 1980. All rights reserved.</small></p>
</footer>
</main>
</div>
</body>
</html>