mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
* add documentation for ai agents * docs: consolidate CLAUDE.md and CONTRIBUTING.md, add style guidelines - Refactor CLAUDE.md to reference CONTRIBUTING.md for build/test/format info - Expand CONTRIBUTING.md with comprehensive build targets, env vars, and testing - Add code style guidelines section with conventional commits - Standardize SDK prettier config to use single quotes (matching web) - Add project-level Claude Code settings to disable co-author attribution * style(sdk): apply prettier with single quotes Run prettier across sdk/base and sdk/package to apply the standardized quote style (single quotes matching web). * docs: add USER.md for per-developer TODO filtering - Add agents/USER.md to .gitignore (contains user identifier) - Document session startup flow in CLAUDE.md: - Create USER.md if missing, prompting for identifier - Filter TODOs by @username tags - Offer relevant TODOs on session start * docs: add i18n documentation task to agent TODOs * docs: document i18n ID patterns in core/ Add agents/i18n-patterns.md covering rust-i18n setup, translation file format, t!() macro usage, key naming conventions, and locale selection. Remove completed TODO item and add reference in CLAUDE.md. * chore: clarify that all builds work on any OS with Docker
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
import { Effects, HealthCheckId } from '../../../base/lib/types'
|
|
import { HealthCheckResult } from './checkFns/HealthCheckResult'
|
|
import { Trigger } from '../trigger'
|
|
import { TriggerInput } from '../trigger/TriggerInput'
|
|
import { defaultTrigger } from '../trigger/defaultTrigger'
|
|
import { once, asError, Drop } from '../util'
|
|
import { object, unknown } from 'ts-matches'
|
|
|
|
export type HealthCheckParams = {
|
|
id: HealthCheckId
|
|
name: string
|
|
trigger?: Trigger
|
|
gracePeriod?: number
|
|
fn(): Promise<HealthCheckResult> | HealthCheckResult
|
|
}
|
|
|
|
export class HealthCheck extends Drop {
|
|
private started: number | null = null
|
|
private setStarted = (started: number | null) => {
|
|
this.started = started
|
|
}
|
|
private exited = false
|
|
private exit = () => {
|
|
this.exited = true
|
|
}
|
|
private currentValue: TriggerInput = {}
|
|
private promise: Promise<void>
|
|
private constructor(effects: Effects, o: HealthCheckParams) {
|
|
super()
|
|
this.promise = Promise.resolve().then(async () => {
|
|
const getCurrentValue = () => this.currentValue
|
|
const gracePeriod = o.gracePeriod ?? 10_000
|
|
const trigger = (o.trigger ?? defaultTrigger)(getCurrentValue)
|
|
const checkStarted = () =>
|
|
[
|
|
this.started,
|
|
new Promise<void>((resolve) => {
|
|
this.setStarted = (started: number | null) => {
|
|
this.started = started
|
|
resolve()
|
|
}
|
|
this.exit = () => {
|
|
this.exited = true
|
|
resolve()
|
|
}
|
|
}),
|
|
] as const
|
|
let triggered = false
|
|
while (!this.exited) {
|
|
const [started, changed] = checkStarted()
|
|
let race:
|
|
| [Promise<void>]
|
|
| [Promise<void>, Promise<IteratorResult<unknown, unknown>>] = [
|
|
changed,
|
|
]
|
|
if (started) {
|
|
race = [...race, trigger.next()]
|
|
if (triggered) {
|
|
try {
|
|
let { result, message } = await o.fn()
|
|
if (
|
|
result === 'failure' &&
|
|
performance.now() - started <= gracePeriod
|
|
)
|
|
result = 'starting'
|
|
await effects.setHealth({
|
|
name: o.name,
|
|
id: o.id,
|
|
result,
|
|
message: message || '',
|
|
})
|
|
this.currentValue.lastResult = result
|
|
} catch (e) {
|
|
await effects.setHealth({
|
|
name: o.name,
|
|
id: o.id,
|
|
result:
|
|
performance.now() - started <= gracePeriod
|
|
? 'starting'
|
|
: 'failure',
|
|
message: asMessage(e) || '',
|
|
})
|
|
this.currentValue.lastResult = 'failure'
|
|
}
|
|
}
|
|
} else triggered = false
|
|
const raced = await Promise.race(race)
|
|
if (raced) {
|
|
if (raced.done) break
|
|
triggered = true
|
|
}
|
|
}
|
|
})
|
|
}
|
|
static of(effects: Effects, options: HealthCheckParams): HealthCheck {
|
|
return new HealthCheck(effects, options)
|
|
}
|
|
start() {
|
|
if (this.started) return
|
|
this.setStarted(performance.now())
|
|
}
|
|
stop() {
|
|
if (!this.started) return
|
|
this.setStarted(null)
|
|
}
|
|
onDrop(): void {
|
|
this.exit()
|
|
}
|
|
}
|
|
|
|
function asMessage(e: unknown) {
|
|
if (object({ message: unknown }).test(e)) return String(e.message)
|
|
const value = String(e)
|
|
if (value.length == null) return null
|
|
return value
|
|
}
|