2026-03-01 22:52:41 +00:00
|
|
|
import type { ActionDefinition, ContextEntry, FeedSource } from "@aris/core"
|
2026-01-19 00:28:03 +00:00
|
|
|
|
2026-03-01 22:52:41 +00:00
|
|
|
import { Context, UnknownActionError, contextKey, type ContextKey } from "@aris/core"
|
2026-02-15 12:26:23 +00:00
|
|
|
import { type } from "arktype"
|
2026-01-19 00:28:03 +00:00
|
|
|
|
2026-02-15 12:26:23 +00:00
|
|
|
import { Location, type LocationSourceOptions } from "./types.ts"
|
2026-01-19 00:28:03 +00:00
|
|
|
|
2026-03-01 22:52:41 +00:00
|
|
|
export const LocationKey: ContextKey<Location> = contextKey("aris.location", "location")
|
2026-01-19 00:28:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A FeedSource that provides location context.
|
|
|
|
|
*
|
|
|
|
|
* This source accepts external location pushes and does not query location itself.
|
|
|
|
|
* Use `pushLocation` to update the location from an external provider (e.g., GPS, network).
|
|
|
|
|
*
|
|
|
|
|
* Does not produce feed items - always returns empty array from `fetchItems`.
|
|
|
|
|
*/
|
|
|
|
|
export class LocationSource implements FeedSource {
|
2026-02-15 12:26:23 +00:00
|
|
|
readonly id = "aris.location"
|
2026-01-19 00:28:03 +00:00
|
|
|
|
|
|
|
|
private readonly historySize: number
|
|
|
|
|
private locations: Location[] = []
|
2026-03-01 22:52:41 +00:00
|
|
|
private listeners = new Set<(entries: readonly ContextEntry[]) => void>()
|
2026-01-19 00:28:03 +00:00
|
|
|
|
|
|
|
|
constructor(options: LocationSourceOptions = {}) {
|
|
|
|
|
this.historySize = options.historySize ?? 1
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 12:26:23 +00:00
|
|
|
async listActions(): Promise<Record<string, ActionDefinition>> {
|
|
|
|
|
return {
|
|
|
|
|
"update-location": {
|
|
|
|
|
id: "update-location",
|
|
|
|
|
description: "Push a new location update",
|
|
|
|
|
input: Location,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async executeAction(actionId: string, params: unknown): Promise<void> {
|
|
|
|
|
switch (actionId) {
|
|
|
|
|
case "update-location": {
|
|
|
|
|
const result = Location(params)
|
|
|
|
|
if (result instanceof type.errors) {
|
|
|
|
|
throw new Error(result.summary)
|
|
|
|
|
}
|
|
|
|
|
this.pushLocation(result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
throw new UnknownActionError(actionId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 00:28:03 +00:00
|
|
|
/**
|
|
|
|
|
* Push a new location update. Notifies all context listeners.
|
|
|
|
|
*/
|
|
|
|
|
pushLocation(location: Location): void {
|
|
|
|
|
this.locations.push(location)
|
|
|
|
|
if (this.locations.length > this.historySize) {
|
|
|
|
|
this.locations.shift()
|
|
|
|
|
}
|
2026-03-01 22:52:41 +00:00
|
|
|
const entries: readonly ContextEntry[] = [[LocationKey, location]]
|
2026-01-19 00:28:03 +00:00
|
|
|
this.listeners.forEach((listener) => {
|
2026-03-01 22:52:41 +00:00
|
|
|
listener(entries)
|
2026-01-19 00:28:03 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Most recent location, or null if none pushed.
|
|
|
|
|
*/
|
|
|
|
|
get lastLocation(): Location | null {
|
|
|
|
|
return this.locations[this.locations.length - 1] ?? null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Location history, oldest first. Length limited by `historySize`.
|
|
|
|
|
*/
|
|
|
|
|
get locationHistory(): readonly Location[] {
|
|
|
|
|
return this.locations
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:52:41 +00:00
|
|
|
onContextUpdate(callback: (entries: readonly ContextEntry[]) => void): () => void {
|
2026-01-19 00:28:03 +00:00
|
|
|
this.listeners.add(callback)
|
|
|
|
|
return () => {
|
|
|
|
|
this.listeners.delete(callback)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 22:52:41 +00:00
|
|
|
async fetchContext(): Promise<readonly ContextEntry[] | null> {
|
2026-01-19 00:28:03 +00:00
|
|
|
if (this.lastLocation) {
|
2026-03-01 22:52:41 +00:00
|
|
|
return [[LocationKey, this.lastLocation]]
|
2026-01-19 00:28:03 +00:00
|
|
|
}
|
2026-02-14 16:20:24 +00:00
|
|
|
return null
|
2026-01-19 00:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fetchItems(): Promise<[]> {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
}
|