Skip to content

An AI-powered search engine with a generative UI

fix: add optional chaining and default values for tool call parameters

Yoshiki Miurac9eca0316650
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 41c710e4..f890832f 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -27,8 +27,8 @@ export function SearchSection({
const isToolLoading = tool.state === 'call'
const searchResults: TypeSearchResults =
tool.state === 'result' ? tool.result : undefined
- const query = tool.args.query as string | undefined
- const includeDomains = tool.args.includeDomains as string[] | undefined
+ const query = tool.args?.query as string | undefined
+ const includeDomains = tool.args?.includeDomains as string[] | undefined
const includeDomainsString = includeDomains
? ` [${includeDomains.join(', ')}]`
: ''
diff --git a/lib/streaming/parse-tool-call.ts b/lib/streaming/parse-tool-call.ts
index 260da598..28396129 100644
--- a/lib/streaming/parse-tool-call.ts
+++ b/lib/streaming/parse-tool-call.ts
@@ -39,15 +39,17 @@ export function parseToolCallXml<T>(
// Parse parameters using the provided schema
const parameters = schema.parse({
...rawParameters,
- // Convert comma-separated strings to arrays for array fields
- include_domains: rawParameters.include_domains
- ?.split(',')
- .map(d => d.trim())
- .filter(Boolean),
- exclude_domains: rawParameters.exclude_domains
- ?.split(',')
- .map(d => d.trim())
- .filter(Boolean),
+ // Convert comma-separated strings to arrays for array fields with default empty arrays
+ include_domains:
+ rawParameters.include_domains
+ ?.split(',')
+ .map(d => d.trim())
+ .filter(Boolean) ?? [],
+ exclude_domains:
+ rawParameters.exclude_domains
+ ?.split(',')
+ .map(d => d.trim())
+ .filter(Boolean) ?? [],
// Convert string to number for numeric fields
max_results: rawParameters.max_results
? parseInt(rawParameters.max_results, 10)
diff --git a/lib/streaming/tool-execution.ts b/lib/streaming/tool-execution.ts
index 8947f349..b7b70f04 100644
--- a/lib/streaming/tool-execution.ts
+++ b/lib/streaming/tool-execution.ts
@@ -91,8 +91,8 @@ export async function executeToolCall(
toolCall.parameters?.query ?? '',
toolCall.parameters?.max_results,
toolCall.parameters?.search_depth as 'basic' | 'advanced',
- toolCall.parameters?.include_domains,
- toolCall.parameters?.exclude_domains
+ toolCall.parameters?.include_domains ?? [],
+ toolCall.parameters?.exclude_domains ?? []
)
const updatedToolCallAnnotation = {