Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 79 additions & 55 deletions app/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ async function submit(formData?: FormData, skip?: boolean) {
message.role !== 'tool' &&
message.type !== 'followup' &&
message.type !== 'related' &&
message.type !== 'end'
message.type !== 'end' &&
message.type !== 'resolution_search_result'
);

// The user's prompt for this action is static.
Expand All @@ -82,74 +83,96 @@ async function submit(formData?: FormData, skip?: boolean) {
});
messages.push({ role: 'user', content });

// Call the simplified agent, which now returns data directly.
const analysisResult = await resolutionSearch(messages) as any;
// Create a streamable value for the summary.
const summaryStream = createStreamableValue<string>('');

// Create a streamable value for the summary and mark it as done.
const summaryStream = createStreamableValue<string>();
summaryStream.done(analysisResult.summary || 'Analysis complete.');
async function processResolutionSearch() {
try {
// Call the simplified agent, which now returns data directly.
const analysisResult = await resolutionSearch(messages) as any;

// Update the UI stream with the BotMessage component.
uiStream.update(
<BotMessage content={summaryStream.value} />
);
// Mark the summary stream as done with the result.
summaryStream.done(analysisResult.summary || 'Analysis complete.');

messages.push({ role: 'assistant', content: analysisResult.summary || 'Analysis complete.' });
// Append the GeoJSON layer to the UI stream so it appears on the map immediately.
if (analysisResult.geoJson) {
uiStream.append(
<GeoJsonLayer id={nanoid()} data={analysisResult.geoJson} />
);
}

const sanitizedMessages: CoreMessage[] = messages.map(m => {
if (Array.isArray(m.content)) {
return {
...m,
content: m.content.filter(part => part.type !== 'image')
} as CoreMessage
}
return m
})
messages.push({ role: 'assistant', content: analysisResult.summary || 'Analysis complete.' });

const relatedQueries = await querySuggestor(uiStream, sanitizedMessages);
uiStream.append(
<Section title="Follow-up">
const sanitizedMessages: CoreMessage[] = messages.map(m => {
if (Array.isArray(m.content)) {
return {
...m,
content: m.content.filter(part => part.type !== 'image')
} as CoreMessage
}
return m
})

const relatedQueries = await querySuggestor(uiStream, sanitizedMessages);
uiStream.append(
<Section title="Follow-up">
<FollowupPanel />
</Section>
);
</Section>
);

await new Promise(resolve => setTimeout(resolve, 500));
await new Promise(resolve => setTimeout(resolve, 500));

const groupeId = nanoid();
const groupeId = nanoid();

aiState.done({
...aiState.get(),
messages: [
aiState.done({
...aiState.get(),
messages: [
...aiState.get().messages,
{
id: groupeId,
role: 'assistant',
content: analysisResult.summary || 'Analysis complete.',
type: 'response'
id: groupeId,
role: 'assistant',
content: analysisResult.summary || 'Analysis complete.',
type: 'response'
},
{
id: groupeId,
role: 'assistant',
content: JSON.stringify(analysisResult),
type: 'resolution_search_result'
id: groupeId,
role: 'assistant',
content: JSON.stringify(analysisResult),
type: 'resolution_search_result'
},
{
id: groupeId,
role: 'assistant',
content: JSON.stringify(relatedQueries),
type: 'related'
id: groupeId,
role: 'assistant',
content: JSON.stringify(relatedQueries),
type: 'related'
},
{
id: groupeId,
role: 'assistant',
content: 'followup',
type: 'followup'
id: groupeId,
role: 'assistant',
content: 'followup',
type: 'followup'
}
]
});
]
});
} catch (error) {
console.error('Error in resolution search:', error);
summaryStream.error(error);
} finally {
isGenerating.done(false);
uiStream.done();
}
}

// Start the background process without awaiting it.
processResolutionSearch();

// Immediately update the UI stream with the BotMessage component.
uiStream.update(
<Section title="response">
<BotMessage content={summaryStream.value} />
</Section>
);

isGenerating.done(false);
uiStream.done();
return {
id: nanoid(),
isGenerating: isGenerating.value,
Expand All @@ -163,7 +186,8 @@ async function submit(formData?: FormData, skip?: boolean) {
message.role !== 'tool' &&
message.type !== 'followup' &&
message.type !== 'related' &&
message.type !== 'end'
message.type !== 'end' &&
message.type !== 'resolution_search_result'
)

const groupeId = nanoid()
Expand Down Expand Up @@ -198,7 +222,7 @@ async function submit(formData?: FormData, skip?: boolean) {
],
});

const definitionStream = createStreamableValue();
const definitionStream = createStreamableValue('');
definitionStream.done(definition);

const answerSection = (
Expand Down Expand Up @@ -295,7 +319,7 @@ async function submit(formData?: FormData, skip?: boolean) {
const hasImage = messageParts.some(part => part.type === 'image')
// Properly type the content based on whether it contains images
const content: CoreMessage['content'] = hasImage
? messageParts as CoreMessage['content']
? (messageParts as any)
: messageParts.map(part => part.text).join('\n')

const type = skip
Expand All @@ -321,7 +345,7 @@ async function submit(formData?: FormData, skip?: boolean) {
})
messages.push({
role: 'user',
content
content: content as any
} as CoreMessage)
}

Expand Down Expand Up @@ -371,7 +395,7 @@ async function submit(formData?: FormData, skip?: boolean) {
let answer = ''
let toolOutputs: ToolResultPart[] = []
let errorOccurred = false
const streamText = createStreamableValue<string>()
const streamText = createStreamableValue<string>('')
uiStream.update(<Spinner />)

while (
Expand Down
Loading