mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 17:11:17 +00:00
Compare commits
3 Commits
feat/weath
...
feat/time-
| Author | SHA1 | Date | |
|---|---|---|---|
|
c9be3b190c
|
|||
|
0f8b1ec4eb
|
|||
|
4f55439f77
|
@@ -1,87 +0,0 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
|
||||||
|
|
||||||
import type { FeedItem, Slot } from "./feed"
|
|
||||||
|
|
||||||
describe("FeedItem slots", () => {
|
|
||||||
test("FeedItem without slots is valid", () => {
|
|
||||||
const item: FeedItem<"test", { value: number }> = {
|
|
||||||
id: "test-1",
|
|
||||||
type: "test",
|
|
||||||
timestamp: new Date(),
|
|
||||||
data: { value: 42 },
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(item.slots).toBeUndefined()
|
|
||||||
})
|
|
||||||
|
|
||||||
test("FeedItem with unfilled slots", () => {
|
|
||||||
const item: FeedItem<"weather", { temp: number }> = {
|
|
||||||
id: "weather-1",
|
|
||||||
type: "weather",
|
|
||||||
timestamp: new Date(),
|
|
||||||
data: { temp: 18 },
|
|
||||||
slots: {
|
|
||||||
insight: {
|
|
||||||
description: "A short contextual insight about the current weather",
|
|
||||||
content: null,
|
|
||||||
},
|
|
||||||
"cross-source": {
|
|
||||||
description: "Connection between weather and calendar events",
|
|
||||||
content: null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(item.slots).toBeDefined()
|
|
||||||
expect(Object.keys(item.slots!)).toEqual(["insight", "cross-source"])
|
|
||||||
expect(item.slots!.insight!.content).toBeNull()
|
|
||||||
expect(item.slots!["cross-source"]!.content).toBeNull()
|
|
||||||
})
|
|
||||||
|
|
||||||
test("FeedItem with filled slots", () => {
|
|
||||||
const item: FeedItem<"weather", { temp: number }> = {
|
|
||||||
id: "weather-1",
|
|
||||||
type: "weather",
|
|
||||||
timestamp: new Date(),
|
|
||||||
data: { temp: 18 },
|
|
||||||
slots: {
|
|
||||||
insight: {
|
|
||||||
description: "A short contextual insight about the current weather",
|
|
||||||
content: "Rain after 3pm — grab a jacket before your walk",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(item.slots!.insight!.content).toBe("Rain after 3pm — grab a jacket before your walk")
|
|
||||||
})
|
|
||||||
|
|
||||||
test("Slot interface enforces required fields", () => {
|
|
||||||
const slot: Slot = {
|
|
||||||
description: "Test slot description",
|
|
||||||
content: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(slot.description).toBe("Test slot description")
|
|
||||||
expect(slot.content).toBeNull()
|
|
||||||
|
|
||||||
const filledSlot: Slot = {
|
|
||||||
description: "Test slot description",
|
|
||||||
content: "Filled content",
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(filledSlot.content).toBe("Filled content")
|
|
||||||
})
|
|
||||||
|
|
||||||
test("FeedItem with empty slots record", () => {
|
|
||||||
const item: FeedItem<"test", { value: number }> = {
|
|
||||||
id: "test-1",
|
|
||||||
type: "test",
|
|
||||||
timestamp: new Date(),
|
|
||||||
data: { value: 1 },
|
|
||||||
slots: {},
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(item.slots).toEqual({})
|
|
||||||
expect(Object.keys(item.slots!)).toHaveLength(0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -23,20 +23,6 @@ export interface FeedItemSignals {
|
|||||||
timeRelevance?: TimeRelevance
|
timeRelevance?: TimeRelevance
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A named slot for LLM-fillable content on a feed item.
|
|
||||||
*
|
|
||||||
* Sources declare slots with a description that tells the LLM what content
|
|
||||||
* to generate. The enhancement harness fills `content` asynchronously;
|
|
||||||
* until then it remains `null`.
|
|
||||||
*/
|
|
||||||
export interface Slot {
|
|
||||||
/** Tells the LLM what this slot wants — written by the source */
|
|
||||||
description: string
|
|
||||||
/** LLM-filled text content, null until enhanced */
|
|
||||||
content: string | null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A single item in the feed.
|
* A single item in the feed.
|
||||||
*
|
*
|
||||||
@@ -50,12 +36,6 @@ export interface Slot {
|
|||||||
* timestamp: new Date(),
|
* timestamp: new Date(),
|
||||||
* data: { temp: 18, condition: "cloudy" },
|
* data: { temp: 18, condition: "cloudy" },
|
||||||
* signals: { urgency: 0.5, timeRelevance: "ambient" },
|
* signals: { urgency: 0.5, timeRelevance: "ambient" },
|
||||||
* slots: {
|
|
||||||
* insight: {
|
|
||||||
* description: "A short contextual insight about the current weather",
|
|
||||||
* content: null,
|
|
||||||
* },
|
|
||||||
* },
|
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@@ -73,6 +53,4 @@ export interface FeedItem<
|
|||||||
data: TData
|
data: TData
|
||||||
/** Source-provided hints for post-processors. Optional — omit if no signals apply. */
|
/** Source-provided hints for post-processors. Optional — omit if no signals apply. */
|
||||||
signals?: FeedItemSignals
|
signals?: FeedItemSignals
|
||||||
/** Named slots for LLM-fillable content. Keys are slot names. */
|
|
||||||
slots?: Record<string, Slot>
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export type { ActionDefinition } from "./action"
|
|||||||
export { UnknownActionError } from "./action"
|
export { UnknownActionError } from "./action"
|
||||||
|
|
||||||
// Feed
|
// Feed
|
||||||
export type { FeedItem, FeedItemSignals, Slot } from "./feed"
|
export type { FeedItem, FeedItemSignals } from "./feed"
|
||||||
export { TimeRelevance } from "./feed"
|
export { TimeRelevance } from "./feed"
|
||||||
|
|
||||||
// Feed Source
|
// Feed Source
|
||||||
|
|||||||
@@ -1,181 +0,0 @@
|
|||||||
#!/usr/bin/env bun
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interactive CLI script to query WeatherKit directly.
|
|
||||||
* Prompts for credentials, coordinates, and optional settings,
|
|
||||||
* then prints the raw API response and processed feed items.
|
|
||||||
* Caches credentials locally and writes response JSON to a file.
|
|
||||||
*
|
|
||||||
* Usage: bun packages/aris-source-weatherkit/scripts/query.ts
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
|
|
||||||
import { join } from "node:path"
|
|
||||||
import { createInterface } from "node:readline/promises"
|
|
||||||
|
|
||||||
import { Context } from "@aris/core"
|
|
||||||
import { LocationKey } from "@aris/source-location"
|
|
||||||
|
|
||||||
import { DefaultWeatherKitClient } from "../src/weatherkit"
|
|
||||||
import { WeatherSource, Units } from "../src/weather-source"
|
|
||||||
|
|
||||||
const SCRIPT_DIR = import.meta.dirname
|
|
||||||
const CACHE_DIR = join(SCRIPT_DIR, ".cache")
|
|
||||||
const CREDS_PATH = join(CACHE_DIR, "credentials.json")
|
|
||||||
|
|
||||||
interface CachedCredentials {
|
|
||||||
teamId: string
|
|
||||||
serviceId: string
|
|
||||||
keyId: string
|
|
||||||
privateKey: string
|
|
||||||
lat?: number
|
|
||||||
lng?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadCachedCredentials(): CachedCredentials | null {
|
|
||||||
if (!existsSync(CREDS_PATH)) return null
|
|
||||||
try {
|
|
||||||
return JSON.parse(readFileSync(CREDS_PATH, "utf-8")) as CachedCredentials
|
|
||||||
} catch {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveCachedCredentials(creds: CachedCredentials): void {
|
|
||||||
mkdirSync(CACHE_DIR, { recursive: true })
|
|
||||||
writeFileSync(CREDS_PATH, JSON.stringify(creds))
|
|
||||||
}
|
|
||||||
|
|
||||||
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
||||||
|
|
||||||
async function prompt(question: string, defaultValue?: string): Promise<string> {
|
|
||||||
const suffix = defaultValue ? ` [${defaultValue}]` : ""
|
|
||||||
const answer = await rl.question(`${question}${suffix}: `)
|
|
||||||
return answer.trim() || defaultValue || ""
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
|
||||||
console.log("=== WeatherKit Query Tool ===\n")
|
|
||||||
|
|
||||||
const cached = loadCachedCredentials()
|
|
||||||
|
|
||||||
let teamId: string
|
|
||||||
let serviceId: string
|
|
||||||
let keyId: string
|
|
||||||
let privateKey: string
|
|
||||||
|
|
||||||
if (cached) {
|
|
||||||
console.log(`Using cached credentials from ${CREDS_PATH}`)
|
|
||||||
console.log(` Team ID: ${cached.teamId}`)
|
|
||||||
console.log(` Service ID: ${cached.serviceId}`)
|
|
||||||
console.log(` Key ID: ${cached.keyId}\n`)
|
|
||||||
|
|
||||||
const useCached = await prompt("Use cached credentials? (y/n)", "y")
|
|
||||||
if (useCached.toLowerCase() === "y") {
|
|
||||||
teamId = cached.teamId
|
|
||||||
serviceId = cached.serviceId
|
|
||||||
keyId = cached.keyId
|
|
||||||
privateKey = cached.privateKey
|
|
||||||
} else {
|
|
||||||
;({ teamId, serviceId, keyId, privateKey } = await promptCredentials())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log(`Credentials will be cached to ${CREDS_PATH}\n`)
|
|
||||||
;({ teamId, serviceId, keyId, privateKey } = await promptCredentials())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Location
|
|
||||||
const defaultLat = cached?.lat?.toString() ?? "37.7749"
|
|
||||||
const defaultLng = cached?.lng?.toString() ?? "-122.4194"
|
|
||||||
const lat = parseFloat(await prompt("Latitude", defaultLat))
|
|
||||||
const lng = parseFloat(await prompt("Longitude", defaultLng))
|
|
||||||
|
|
||||||
if (Number.isNaN(lat) || Number.isNaN(lng)) {
|
|
||||||
console.error("Invalid coordinates")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const credentials = { privateKey, keyId, teamId, serviceId }
|
|
||||||
saveCachedCredentials({ ...credentials, lat, lng })
|
|
||||||
|
|
||||||
// Options
|
|
||||||
const unitsInput = await prompt("Units (metric/imperial)", "metric")
|
|
||||||
const units = unitsInput === "imperial" ? Units.imperial : Units.metric
|
|
||||||
|
|
||||||
// Raw API query
|
|
||||||
console.log("\n--- Raw WeatherKit Response ---\n")
|
|
||||||
const client = new DefaultWeatherKitClient(credentials)
|
|
||||||
const raw = await client.fetch({ lat, lng })
|
|
||||||
console.log(JSON.stringify(raw, null, 2))
|
|
||||||
|
|
||||||
// Write JSON to file
|
|
||||||
const outPath = join(CACHE_DIR, "response.json")
|
|
||||||
writeFileSync(outPath, JSON.stringify(raw))
|
|
||||||
console.log(`\nResponse written to ${outPath}`)
|
|
||||||
|
|
||||||
// Processed feed items via WeatherSource
|
|
||||||
console.log("\n--- Processed Feed Items ---\n")
|
|
||||||
const source = new WeatherSource({ client, units })
|
|
||||||
const context = new Context()
|
|
||||||
context.set([[LocationKey, { lat, lng, accuracy: 10, timestamp: new Date() }]])
|
|
||||||
|
|
||||||
const items = await source.fetchItems(context)
|
|
||||||
for (const item of items) {
|
|
||||||
console.log(`[${item.type}] ${item.id}`)
|
|
||||||
console.log(` signals: ${JSON.stringify(item.signals)}`)
|
|
||||||
if (item.slots) {
|
|
||||||
console.log(` slots:`)
|
|
||||||
for (const [name, slot] of Object.entries(item.slots)) {
|
|
||||||
console.log(` ${name}: "${slot.description}" -> ${slot.content ?? "(unfilled)"}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(` data: ${JSON.stringify(item.data, null, 4)}`)
|
|
||||||
console.log()
|
|
||||||
}
|
|
||||||
|
|
||||||
const feedPath = join(CACHE_DIR, "feed-items.json")
|
|
||||||
writeFileSync(feedPath, JSON.stringify(items, null, 2))
|
|
||||||
console.log(`Feed items written to ${feedPath}`)
|
|
||||||
console.log(`Total: ${items.length} items`)
|
|
||||||
rl.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
async function promptCredentials(): Promise<CachedCredentials> {
|
|
||||||
const teamId = await prompt("Apple Team ID")
|
|
||||||
if (!teamId) {
|
|
||||||
console.error("Team ID is required")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const serviceId = await prompt("Service ID")
|
|
||||||
if (!serviceId) {
|
|
||||||
console.error("Service ID is required")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const keyId = await prompt("Key ID")
|
|
||||||
if (!keyId) {
|
|
||||||
console.error("Key ID is required")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("\nPaste your private key (PEM format). Enter an empty line when done:")
|
|
||||||
const keyLines: string[] = []
|
|
||||||
for await (const line of rl) {
|
|
||||||
if (line.trim() === "") break
|
|
||||||
keyLines.push(line)
|
|
||||||
}
|
|
||||||
const privateKey = keyLines.join("\n")
|
|
||||||
if (!privateKey) {
|
|
||||||
console.error("Private key is required")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return { teamId, serviceId, keyId, privateKey }
|
|
||||||
}
|
|
||||||
|
|
||||||
main().catch((err) => {
|
|
||||||
console.error("Error:", err)
|
|
||||||
rl.close()
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
Max 12 words. Plain language, no hedging. Now + what's next.
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
- "Clear tonight, warming up. Rain by Saturday."
|
|
||||||
- "Clearing soon with strong winds overnight. Light rain Thursday."
|
|
||||||
- "Sunny all day. Grab sunscreen."
|
|
||||||
- "Cloudy tonight, warming to 15°. Rain Monday."
|
|
||||||
@@ -176,34 +176,6 @@ describe("WeatherSource", () => {
|
|||||||
|
|
||||||
expect(uniqueIds.size).toBe(ids.length)
|
expect(uniqueIds.size).toBe(ids.length)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("current weather item has insight slot", async () => {
|
|
||||||
const source = new WeatherSource({ client: mockClient })
|
|
||||||
const context = createMockContext({ lat: 37.7749, lng: -122.4194 })
|
|
||||||
|
|
||||||
const items = await source.fetchItems(context)
|
|
||||||
const currentItem = items.find((i) => i.type === WeatherFeedItemType.Current)
|
|
||||||
|
|
||||||
expect(currentItem).toBeDefined()
|
|
||||||
expect(currentItem!.slots).toBeDefined()
|
|
||||||
expect(currentItem!.slots!.insight).toBeDefined()
|
|
||||||
expect(currentItem!.slots!.insight!.description).toBeString()
|
|
||||||
expect(currentItem!.slots!.insight!.description.length).toBeGreaterThan(0)
|
|
||||||
expect(currentItem!.slots!.insight!.content).toBeNull()
|
|
||||||
})
|
|
||||||
|
|
||||||
test("non-current items do not have slots", async () => {
|
|
||||||
const source = new WeatherSource({ client: mockClient })
|
|
||||||
const context = createMockContext({ lat: 37.7749, lng: -122.4194 })
|
|
||||||
|
|
||||||
const items = await source.fetchItems(context)
|
|
||||||
const nonCurrentItems = items.filter((i) => i.type !== WeatherFeedItemType.Current)
|
|
||||||
|
|
||||||
expect(nonCurrentItems.length).toBeGreaterThan(0)
|
|
||||||
for (const item of nonCurrentItems) {
|
|
||||||
expect(item.slots).toBeUndefined()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("no reactive methods", () => {
|
describe("no reactive methods", () => {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { Context, TimeRelevance, UnknownActionError } from "@aris/core"
|
|||||||
import { LocationKey } from "@aris/source-location"
|
import { LocationKey } from "@aris/source-location"
|
||||||
|
|
||||||
import { WeatherFeedItemType, type WeatherFeedItem } from "./feed-items"
|
import { WeatherFeedItemType, type WeatherFeedItem } from "./feed-items"
|
||||||
import currentWeatherInsightPrompt from "./prompts/current-weather-insight.txt"
|
|
||||||
import { WeatherKey, type Weather } from "./weather-context"
|
import { WeatherKey, type Weather } from "./weather-context"
|
||||||
import {
|
import {
|
||||||
DefaultWeatherKitClient,
|
DefaultWeatherKitClient,
|
||||||
@@ -310,12 +309,6 @@ function createCurrentWeatherFeedItem(
|
|||||||
windSpeed: convertSpeed(current.windSpeed, units),
|
windSpeed: convertSpeed(current.windSpeed, units),
|
||||||
},
|
},
|
||||||
signals,
|
signals,
|
||||||
slots: {
|
|
||||||
insight: {
|
|
||||||
description: currentWeatherInsightPrompt,
|
|
||||||
content: null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user