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
2 changes: 1 addition & 1 deletion examples/deep-survey/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.1.25",
"type": "module",
"scripts": {
"dev": "pnpm run --parallel \"/^dev:/\"",
"dev": "pnpm run dev:server & pnpm run dev:ui",
"dev:server": "tsx watch src/server/index.ts",
"dev:ui": "vite",
"build:ui": "vite build",
Expand Down
35 changes: 29 additions & 6 deletions examples/deep-survey/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { createHash } from 'node:crypto'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { parseEnv } from 'node:util'

// Load repo root .env first, then local .env (local values take precedence)
const envPaths = [
path.resolve(import.meta.dirname, `../../../../.env`),
path.resolve(import.meta.dirname, `../../.env`),
]
for (const envPath of envPaths) {
function loadEnvFile(envPath: string, override: boolean): void {
try {
process.loadEnvFile(envPath)
const env = parseEnv(readFileSync(envPath, `utf8`))
for (const [key, value] of Object.entries(env)) {
if (override || process.env[key] === undefined) {
process.env[key] = value
}
}
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== `ENOENT`) {
console.error(`Failed to load .env file:`, err)
}
}
}

loadEnvFile(path.resolve(import.meta.dirname, `../../../../.env`), false)
loadEnvFile(path.resolve(import.meta.dirname, `../../.env`), true)

function envFingerprint(name: string): string {
const value = process.env[name]?.trim()
if (!value) return `${name}=unset`

const fingerprint = createHash(`sha256`)
.update(value)
.digest(`hex`)
.slice(0, 8)
return `${name}=set sha256:${fingerprint}`
}

import {
createEntityRegistry,
createRuntimeHandler,
Expand Down Expand Up @@ -136,5 +154,10 @@ server.listen(PORT, async () => {
await runtime.registerTypes()
console.log(`Deep Survey server ready on port ${PORT}`)
console.log(`DARIX: ${DARIX_URL}`)
console.log(
`Model credentials: ${envFingerprint(`ANTHROPIC_API_KEY`)}, ${envFingerprint(
`MOONSHOT_API_KEY`
)}`
)
console.log(`${runtime.typeNames.length} entity types registered`)
})
14 changes: 6 additions & 8 deletions examples/deep-survey/src/server/orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
ChildStatusEntry,
EntityRegistry,
HandlerContext,
ManifestEntry,
SharedStateHandle,
} from '@electric-ax/agents-runtime'
import { db } from '@electric-ax/agents-runtime'
import { queryOnce } from '@durable-streams/state/db'
import { Type } from '@sinclair/typebox'
import { exec, execFile } from 'node:child_process'
import type { AgentTool } from '@mariozechner/pi-agent-core'
Expand Down Expand Up @@ -141,9 +142,7 @@ function createExploreCorpusTool(
topics: string[]
}

const existing = await queryOnce((q) =>
q.from({ manifests: ctx.db.collections.manifests })
)
const existing = ctx.db.collections.manifests.toArray as ManifestEntry[]
const hasChildren = existing.some(
(m) => m.kind === `child` && m.entity_type === SURVEY_WORKER_ENTITY_TYPE
)
Expand Down Expand Up @@ -269,12 +268,11 @@ function createSwarmStatusTool(
const entries = readWiki(shared)
const xrefs = readXrefs(shared)

const children = await queryOnce((q) =>
q.from({ cs: ctx.db.collections.childStatus })
)
const children = ctx.db.collections.childStatus
.toArray as ChildStatusEntry[]
const byStatus = { spawning: 0, running: 0, idle: 0, stopped: 0 }
for (const c of children) {
const s = (c as any).status as string
const s = c.status
if (s in byStatus) byStatus[s as keyof typeof byStatus]++
}
const total = children.length
Expand Down
Loading