mirror of
https://github.com/kennethnym/aris.git
synced 2026-04-01 07:31:17 +01:00
feat: combine hourly weather into single feed item
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -32,7 +32,7 @@ export interface CurrentWeatherFeedItem extends FeedItem<
|
||||
CurrentWeatherData
|
||||
> {}
|
||||
|
||||
export type HourlyWeatherData = {
|
||||
export type HourlyWeatherEntry = {
|
||||
forecastTime: Date
|
||||
conditionCode: ConditionCode
|
||||
daylight: boolean
|
||||
@@ -48,6 +48,10 @@ export type HourlyWeatherData = {
|
||||
windSpeed: number
|
||||
}
|
||||
|
||||
export type HourlyWeatherData = {
|
||||
hours: HourlyWeatherEntry[]
|
||||
}
|
||||
|
||||
export interface HourlyWeatherFeedItem extends FeedItem<
|
||||
typeof WeatherFeedItemType.Hourly,
|
||||
HourlyWeatherData
|
||||
|
||||
@@ -8,6 +8,7 @@ export {
|
||||
type CurrentWeatherData,
|
||||
type HourlyWeatherFeedItem,
|
||||
type HourlyWeatherData,
|
||||
type HourlyWeatherEntry,
|
||||
type DailyWeatherFeedItem,
|
||||
type DailyWeatherData,
|
||||
type WeatherAlertFeedItem,
|
||||
|
||||
@@ -131,10 +131,26 @@ describe("WeatherSource", () => {
|
||||
const hourlyItems = items.filter((i) => i.type === WeatherFeedItemType.Hourly)
|
||||
const dailyItems = items.filter((i) => i.type === WeatherFeedItemType.Daily)
|
||||
|
||||
expect(hourlyItems.length).toBe(3)
|
||||
expect(hourlyItems.length).toBe(1)
|
||||
expect((hourlyItems[0]!.data as { hours: unknown[] }).hours.length).toBe(3)
|
||||
expect(dailyItems.length).toBe(2)
|
||||
})
|
||||
|
||||
test("produces a single hourly item with hours array", async () => {
|
||||
const source = new WeatherSource({ client: mockClient })
|
||||
const context = createMockContext({ lat: 37.7749, lng: -122.4194 })
|
||||
|
||||
const items = await source.fetchItems(context)
|
||||
|
||||
const hourlyItems = items.filter((i) => i.type === WeatherFeedItemType.Hourly)
|
||||
expect(hourlyItems.length).toBe(1)
|
||||
|
||||
const hourlyData = hourlyItems[0]!.data as { hours: unknown[] }
|
||||
expect(Array.isArray(hourlyData.hours)).toBe(true)
|
||||
expect(hourlyData.hours.length).toBeGreaterThan(0)
|
||||
expect(hourlyData.hours.length).toBeLessThanOrEqual(12)
|
||||
})
|
||||
|
||||
test("sets timestamp from context.time", async () => {
|
||||
const source = new WeatherSource({ client: mockClient })
|
||||
const queryTime = new Date("2026-01-17T12:00:00Z")
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { ActionDefinition, ContextEntry, FeedItemSignals, FeedSource } from
|
||||
import { Context, TimeRelevance, UnknownActionError } from "@aelis/core"
|
||||
import { LocationKey } from "@aelis/source-location"
|
||||
|
||||
import { WeatherFeedItemType, type WeatherFeedItem } from "./feed-items"
|
||||
import { WeatherFeedItemType, type HourlyWeatherEntry, type WeatherFeedItem } from "./feed-items"
|
||||
import currentWeatherInsightPrompt from "./prompts/current-weather-insight.txt"
|
||||
import { WeatherKey, type Weather } from "./weather-context"
|
||||
import {
|
||||
@@ -174,11 +174,8 @@ export class WeatherSource implements FeedSource<WeatherFeedItem> {
|
||||
|
||||
if (response.forecastHourly?.hours) {
|
||||
const hours = response.forecastHourly.hours.slice(0, this.hourlyLimit)
|
||||
for (let i = 0; i < hours.length; i++) {
|
||||
const hour = hours[i]
|
||||
if (hour) {
|
||||
items.push(createHourlyWeatherFeedItem(hour, i, timestamp, this.units, this.id))
|
||||
}
|
||||
if (hours.length > 0) {
|
||||
items.push(createHourlyForecastFeedItem(hours, timestamp, this.units, this.id))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,24 +320,17 @@ function createCurrentWeatherFeedItem(
|
||||
}
|
||||
}
|
||||
|
||||
function createHourlyWeatherFeedItem(
|
||||
hourly: HourlyForecast,
|
||||
index: number,
|
||||
function createHourlyForecastFeedItem(
|
||||
hourlyForecasts: HourlyForecast[],
|
||||
timestamp: Date,
|
||||
units: Units,
|
||||
sourceId: string,
|
||||
): WeatherFeedItem {
|
||||
const signals: FeedItemSignals = {
|
||||
urgency: adjustUrgencyForCondition(BASE_URGENCY.hourly, hourly.conditionCode),
|
||||
timeRelevance: timeRelevanceForCondition(hourly.conditionCode),
|
||||
}
|
||||
const hours: HourlyWeatherEntry[] = []
|
||||
let totalUrgency = 0
|
||||
|
||||
return {
|
||||
id: `weather-hourly-${timestamp.getTime()}-${index}`,
|
||||
sourceId,
|
||||
type: WeatherFeedItemType.Hourly,
|
||||
timestamp,
|
||||
data: {
|
||||
for (const hourly of hourlyForecasts) {
|
||||
hours.push({
|
||||
forecastTime: new Date(hourly.forecastStart),
|
||||
conditionCode: hourly.conditionCode,
|
||||
daylight: hourly.daylight,
|
||||
@@ -354,7 +344,21 @@ function createHourlyWeatherFeedItem(
|
||||
windDirection: hourly.windDirection,
|
||||
windGust: convertSpeed(hourly.windGust, units),
|
||||
windSpeed: convertSpeed(hourly.windSpeed, units),
|
||||
},
|
||||
})
|
||||
totalUrgency += adjustUrgencyForCondition(BASE_URGENCY.hourly, hourly.conditionCode)
|
||||
}
|
||||
|
||||
const signals: FeedItemSignals = {
|
||||
urgency: totalUrgency / hours.length,
|
||||
timeRelevance: TimeRelevance.Ambient,
|
||||
}
|
||||
|
||||
return {
|
||||
id: `weather-hourly-${timestamp.getTime()}`,
|
||||
sourceId,
|
||||
type: WeatherFeedItemType.Hourly,
|
||||
timestamp,
|
||||
data: { hours },
|
||||
signals,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user