agents-city 0.3.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +16 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.es.md +2021 -0
- package/README.md +1999 -0
- package/benchmarks/committee/README.md +39 -0
- package/benchmarks/committee/metrics.py +51 -0
- package/benchmarks/committee/run.py +64 -0
- package/benchmarks/committee/traces/01-single.json +8 -0
- package/benchmarks/committee/traces/02-mesh.json +16 -0
- package/benchmarks/committee/traces/03-chair.json +15 -0
- package/benchmarks/latency/README.md +35 -0
- package/benchmarks/latency/fake-claude-cli.mjs +119 -0
- package/benchmarks/latency/fake-native-server.mjs +340 -0
- package/benchmarks/latency/fake-tui.py +91 -0
- package/benchmarks/latency/live.py +380 -0
- package/benchmarks/stress/README.md +20 -0
- package/benchmarks/stress/run.py +487 -0
- package/bin/agents +4 -0
- package/bin/agents-city.js +116 -0
- package/bin/benchmark +17 -0
- package/bin/bus +6 -0
- package/bin/cities +3 -0
- package/bin/city +86 -0
- package/bin/committee +6 -0
- package/bin/demo +200 -0
- package/bin/exit +5 -0
- package/bin/hall +73 -0
- package/bin/hall.html +426 -0
- package/bin/logs +3 -0
- package/bin/report.py +14 -0
- package/bin/reset +3 -0
- package/bin/road +3 -0
- package/bin/seat +10 -0
- package/bin/serve.py +1428 -0
- package/bin/setup.py +753 -0
- package/bin/skills +3 -0
- package/bin/test +131 -0
- package/bin/test-adapter.py +183 -0
- package/bin/test-admision.py +99 -0
- package/bin/test-avatar.py +58 -0
- package/bin/test-benchmark.py +40 -0
- package/bin/test-broker.py +334 -0
- package/bin/test-cage.py +206 -0
- package/bin/test-card.py +402 -0
- package/bin/test-channel.py +379 -0
- package/bin/test-cities.py +453 -0
- package/bin/test-claude-runtime.py +252 -0
- package/bin/test-committee.py +340 -0
- package/bin/test-contracts.py +544 -0
- package/bin/test-crecimiento.py +164 -0
- package/bin/test-demo.py +308 -0
- package/bin/test-doctor.py +106 -0
- package/bin/test-domains.py +151 -0
- package/bin/test-evidencia.py +50 -0
- package/bin/test-exit.py +128 -0
- package/bin/test-hall-protocol.py +93 -0
- package/bin/test-launch.py +169 -0
- package/bin/test-live-feed.py +508 -0
- package/bin/test-pairing.py +99 -0
- package/bin/test-parcels.py +283 -0
- package/bin/test-runtime-failures.py +252 -0
- package/bin/test-runtime-ui.py +488 -0
- package/bin/test-runtime.py +268 -0
- package/bin/test-rutas.py +80 -0
- package/bin/test-seat.py +1386 -0
- package/bin/test-security.py +150 -0
- package/bin/test-serve.py +1057 -0
- package/bin/test-stress.py +64 -0
- package/bin/test-widgets.py +143 -0
- package/bin/test-workspace.py +188 -0
- package/bin/testlib.py +95 -0
- package/bin/tokens.py +14 -0
- package/bus/scripts/deploy.sh +60 -0
- package/bus/scripts/mint-token.sh +68 -0
- package/bus/scripts/setup-dev.sh +79 -0
- package/bus/scripts/test-channel.ts +8 -0
- package/bus/scripts/test-hub.ts +266 -0
- package/bus/scripts/test-local.sh +40 -0
- package/bus/scripts/test-queue.ts +132 -0
- package/bus/worker/package-lock.json +1971 -0
- package/bus/worker/package.json +17 -0
- package/bus/worker/src/index.ts +485 -0
- package/bus/worker/tsconfig.json +13 -0
- package/bus/worker/wrangler.toml +32 -0
- package/city/oven/README.md +59 -0
- package/city/oven/collect.py +43 -0
- package/city/oven/oven.html +4 -0
- package/city/scripts/history.py +120 -0
- package/city/scripts/seed.py +442 -0
- package/city/web/assets/modelos/License.txt +28 -0
- package/city/web/assets/modelos/PROCEDENCIA.txt +3 -0
- package/city/web/assets/modelos/building-a.glb +0 -0
- package/city/web/assets/modelos/building-b.glb +0 -0
- package/city/web/assets/modelos/building-c.glb +0 -0
- package/city/web/assets/modelos/building-d.glb +0 -0
- package/city/web/assets/modelos/building-e.glb +0 -0
- package/city/web/assets/modelos/building-f.glb +0 -0
- package/city/web/assets/modelos/building-g.glb +0 -0
- package/city/web/assets/modelos/building-h.glb +0 -0
- package/city/web/assets/sprites/building-a.png +0 -0
- package/city/web/assets/sprites/building-b.png +0 -0
- package/city/web/assets/sprites/building-c.png +0 -0
- package/city/web/assets/sprites/building-d.png +0 -0
- package/city/web/assets/sprites/building-e.png +0 -0
- package/city/web/assets/sprites/building-f.png +0 -0
- package/city/web/assets/sprites/building-g.png +0 -0
- package/city/web/assets/sprites/building-h.png +0 -0
- package/city/web/assets/sprites/building-i.png +0 -0
- package/city/web/assets/sprites/building-j.png +0 -0
- package/city/web/assets/sprites/building-k.png +0 -0
- package/city/web/assets/sprites/building-l.png +0 -0
- package/city/web/assets/sprites/building-m.png +0 -0
- package/city/web/assets/sprites/building-n.png +0 -0
- package/city/web/assets/sprites/building-skyscraper-a.png +0 -0
- package/city/web/assets/sprites/building-skyscraper-b.png +0 -0
- package/city/web/assets/sprites/building-skyscraper-c.png +0 -0
- package/city/web/assets/sprites/building-skyscraper-d.png +0 -0
- package/city/web/assets/sprites/building-skyscraper-e.png +0 -0
- package/city/web/assets/sprites/catalogo.json +47 -0
- package/city/web/assets/sprites/detail-awning-wide.png +0 -0
- package/city/web/assets/sprites/detail-awning.png +0 -0
- package/city/web/assets/sprites/detail-overhang-wide.png +0 -0
- package/city/web/assets/sprites/detail-overhang.png +0 -0
- package/city/web/assets/sprites/detail-parasol-a.png +0 -0
- package/city/web/assets/sprites/detail-parasol-b.png +0 -0
- package/city/web/assets/sprites/low-detail-building-a.png +0 -0
- package/city/web/assets/sprites/low-detail-building-b.png +0 -0
- package/city/web/assets/sprites/low-detail-building-c.png +0 -0
- package/city/web/assets/sprites/low-detail-building-d.png +0 -0
- package/city/web/assets/sprites/low-detail-building-e.png +0 -0
- package/city/web/assets/sprites/low-detail-building-f.png +0 -0
- package/city/web/assets/sprites/low-detail-building-g.png +0 -0
- package/city/web/assets/sprites/low-detail-building-h.png +0 -0
- package/city/web/assets/sprites/low-detail-building-i.png +0 -0
- package/city/web/assets/sprites/low-detail-building-j.png +0 -0
- package/city/web/assets/sprites/low-detail-building-k.png +0 -0
- package/city/web/assets/sprites/low-detail-building-l.png +0 -0
- package/city/web/assets/sprites/low-detail-building-m.png +0 -0
- package/city/web/assets/sprites/low-detail-building-n.png +0 -0
- package/city/web/assets/sprites/low-detail-building-wide-a.png +0 -0
- package/city/web/assets/sprites/low-detail-building-wide-b.png +0 -0
- package/city/web/assets/sprites/medidas.json +248 -0
- package/city/web/dist/city.js +939 -0
- package/city/web/dist/index.html +294 -0
- package/city/web/dist/modelos/License.txt +28 -0
- package/city/web/dist/modelos/PROCEDENCIA.txt +3 -0
- package/city/web/dist/modelos/building-a.glb +0 -0
- package/city/web/dist/modelos/building-b.glb +0 -0
- package/city/web/dist/modelos/building-c.glb +0 -0
- package/city/web/dist/modelos/building-d.glb +0 -0
- package/city/web/dist/modelos/building-e.glb +0 -0
- package/city/web/dist/modelos/building-f.glb +0 -0
- package/city/web/dist/modelos/building-g.glb +0 -0
- package/city/web/dist/modelos/building-h.glb +0 -0
- package/city/web/dist/sprites/building-a.png +0 -0
- package/city/web/dist/sprites/building-b.png +0 -0
- package/city/web/dist/sprites/building-c.png +0 -0
- package/city/web/dist/sprites/building-d.png +0 -0
- package/city/web/dist/sprites/building-e.png +0 -0
- package/city/web/dist/sprites/building-f.png +0 -0
- package/city/web/dist/sprites/building-g.png +0 -0
- package/city/web/dist/sprites/building-h.png +0 -0
- package/city/web/dist/sprites/building-i.png +0 -0
- package/city/web/dist/sprites/building-j.png +0 -0
- package/city/web/dist/sprites/building-k.png +0 -0
- package/city/web/dist/sprites/building-l.png +0 -0
- package/city/web/dist/sprites/building-m.png +0 -0
- package/city/web/dist/sprites/building-n.png +0 -0
- package/city/web/dist/sprites/building-skyscraper-a.png +0 -0
- package/city/web/dist/sprites/building-skyscraper-b.png +0 -0
- package/city/web/dist/sprites/building-skyscraper-c.png +0 -0
- package/city/web/dist/sprites/building-skyscraper-d.png +0 -0
- package/city/web/dist/sprites/building-skyscraper-e.png +0 -0
- package/city/web/dist/sprites/catalogo.json +47 -0
- package/city/web/dist/sprites/detail-awning-wide.png +0 -0
- package/city/web/dist/sprites/detail-awning.png +0 -0
- package/city/web/dist/sprites/detail-overhang-wide.png +0 -0
- package/city/web/dist/sprites/detail-overhang.png +0 -0
- package/city/web/dist/sprites/detail-parasol-a.png +0 -0
- package/city/web/dist/sprites/detail-parasol-b.png +0 -0
- package/city/web/dist/sprites/low-detail-building-a.png +0 -0
- package/city/web/dist/sprites/low-detail-building-b.png +0 -0
- package/city/web/dist/sprites/low-detail-building-c.png +0 -0
- package/city/web/dist/sprites/low-detail-building-d.png +0 -0
- package/city/web/dist/sprites/low-detail-building-e.png +0 -0
- package/city/web/dist/sprites/low-detail-building-f.png +0 -0
- package/city/web/dist/sprites/low-detail-building-g.png +0 -0
- package/city/web/dist/sprites/low-detail-building-h.png +0 -0
- package/city/web/dist/sprites/low-detail-building-i.png +0 -0
- package/city/web/dist/sprites/low-detail-building-j.png +0 -0
- package/city/web/dist/sprites/low-detail-building-k.png +0 -0
- package/city/web/dist/sprites/low-detail-building-l.png +0 -0
- package/city/web/dist/sprites/low-detail-building-m.png +0 -0
- package/city/web/dist/sprites/low-detail-building-n.png +0 -0
- package/city/web/dist/sprites/low-detail-building-wide-a.png +0 -0
- package/city/web/dist/sprites/low-detail-building-wide-b.png +0 -0
- package/city/web/dist/sprites/medidas.json +248 -0
- package/city/web/dist-hall/hall.js +264 -0
- package/city/web/index.html +294 -0
- package/city/web/package.json +18 -0
- package/city/web/sella.py +21 -0
- package/city/web/src/activity-actors.ts +47 -0
- package/city/web/src/activity.ts +172 -0
- package/city/web/src/ayuntamiento.ts +580 -0
- package/city/web/src/draw.ts +640 -0
- package/city/web/src/game-speech.ts +79 -0
- package/city/web/src/hall.ts +1867 -0
- package/city/web/src/main.ts +2225 -0
- package/city/web/src/oven.ts +124 -0
- package/city/web/src/people.ts +244 -0
- package/city/web/src/presencia.ts +193 -0
- package/city/web/src/puertas.ts +180 -0
- package/city/web/src/repo-roles.ts +59 -0
- package/city/worker/package.json +16 -0
- package/city/worker/schema.sql +117 -0
- package/city/worker/src/index.ts +702 -0
- package/city/worker/src/square.ts +218 -0
- package/city/worker/wrangler.toml +58 -0
- package/demo/ada.md +50 -0
- package/demo/bruno.md +62 -0
- package/demo/camila.md +56 -0
- package/demo/city/ada.md +40 -0
- package/demo/city/city.yml +8 -0
- package/demo/city/parcels.yml +15 -0
- package/demo/city/roads.json +4 -0
- package/demo/city/units.yml +10 -0
- package/demo/clinica/city.yml +8 -0
- package/demo/clinica/parcels.yml +15 -0
- package/demo/clinica/roads.json +4 -0
- package/demo/clinica/units.yml +9 -0
- package/demo/clinica/vera.md +46 -0
- package/demo/dante.md +54 -0
- package/demo/despacho/city.yml +8 -0
- package/demo/despacho/marta.md +46 -0
- package/demo/despacho/parcels.yml +15 -0
- package/demo/despacho/roads.json +4 -0
- package/demo/despacho/units.yml +9 -0
- package/demo/elsa.md +50 -0
- package/demo/farid.md +52 -0
- package/demo/greta.md +50 -0
- package/demo/hugo.md +49 -0
- package/demo/iris.md +49 -0
- package/demo/jonas.md +51 -0
- package/demo/kira.md +51 -0
- package/demo/luca.md +49 -0
- package/demo/parcels.yml +92 -0
- package/demo/seed.py +20 -0
- package/demo/show.py +161 -0
- package/demo/stories.py +824 -0
- package/demo/units.yml +33 -0
- package/docs/agents-first.md +99 -0
- package/docs/glossary.md +55 -0
- package/docs/map-live-layers.md +73 -0
- package/docs/security.md +231 -0
- package/docs/self-host.md +151 -0
- package/docs/testing.md +205 -0
- package/package.json +73 -0
- package/plugin/.claude-plugin/plugin.json +62 -0
- package/plugin/.mcp.json +15 -0
- package/plugin/channel/activity-cli.ts +16 -0
- package/plugin/channel/adapter-prompts.ts +113 -0
- package/plugin/channel/adapter.js +4292 -0
- package/plugin/channel/adapter.ts +133 -0
- package/plugin/channel/bus.js +20188 -0
- package/plugin/channel/bus.ts +131 -0
- package/plugin/channel/city-config.ts +125 -0
- package/plugin/channel/claude-channel.ts +58 -0
- package/plugin/channel/cli-args.ts +75 -0
- package/plugin/channel/client.js +4200 -0
- package/plugin/channel/client.ts +38 -0
- package/plugin/channel/committee/activity.ts +256 -0
- package/plugin/channel/committee/collection.ts +154 -0
- package/plugin/channel/committee/decision.ts +175 -0
- package/plugin/channel/committee/floor.ts +135 -0
- package/plugin/channel/committee/guards.ts +30 -0
- package/plugin/channel/committee/history.ts +38 -0
- package/plugin/channel/committee/render.ts +102 -0
- package/plugin/channel/committee/service.ts +72 -0
- package/plugin/channel/committee/storage.ts +92 -0
- package/plugin/channel/committee/types.ts +163 -0
- package/plugin/channel/committee/view.ts +63 -0
- package/plugin/channel/committee-cli.ts +109 -0
- package/plugin/channel/delivery-metrics.ts +61 -0
- package/plugin/channel/delivery-queue.ts +118 -0
- package/plugin/channel/hub/activity-controller.ts +72 -0
- package/plugin/channel/hub/activity-feed.ts +169 -0
- package/plugin/channel/hub/committee-controller.ts +60 -0
- package/plugin/channel/hub/connections.ts +48 -0
- package/plugin/channel/hub/diagnostics.ts +67 -0
- package/plugin/channel/hub/envelope-validity.ts +109 -0
- package/plugin/channel/hub/envelopes.ts +66 -0
- package/plugin/channel/hub/lifecycle.ts +91 -0
- package/plugin/channel/hub/local-roads.ts +89 -0
- package/plugin/channel/hub/remote-roads.ts +165 -0
- package/plugin/channel/hub/road-controller.ts +99 -0
- package/plugin/channel/hub-client.ts +149 -0
- package/plugin/channel/local-hub.js +5995 -0
- package/plugin/channel/local-hub.ts +349 -0
- package/plugin/channel/map-reporter.ts +48 -0
- package/plugin/channel/package-lock.json +1731 -0
- package/plugin/channel/package.json +19 -0
- package/plugin/channel/protocol.ts +98 -0
- package/plugin/channel/road-cli.ts +18 -0
- package/plugin/channel/run.sh +15 -0
- package/plugin/channel/runtime/claude.ts +458 -0
- package/plugin/channel/runtime/codex-config.ts +77 -0
- package/plugin/channel/runtime/codex.ts +669 -0
- package/plugin/channel/runtime/command.ts +62 -0
- package/plugin/channel/runtime/factory.ts +15 -0
- package/plugin/channel/runtime/json-rpc.ts +154 -0
- package/plugin/channel/runtime/kimi.ts +277 -0
- package/plugin/channel/runtime/opencode.ts +248 -0
- package/plugin/channel/runtime/process.ts +84 -0
- package/plugin/channel/runtime/types.ts +45 -0
- package/plugin/channel/runtime-files.ts +73 -0
- package/plugin/channel/runtime-gateway.js +6396 -0
- package/plugin/channel/runtime-gateway.ts +358 -0
- package/plugin/channel/runtime-metrics.ts +52 -0
- package/plugin/channel/runtime-subscription.ts +180 -0
- package/plugin/channel/terminal-delivery.ts +131 -0
- package/plugin/channel/untrusted.ts +60 -0
- package/plugin/commands/committee.md +31 -0
- package/plugin/commands/exit.md +32 -0
- package/plugin/commands/goals.md +20 -0
- package/plugin/commands/join.md +18 -0
- package/plugin/commands/notice.md +20 -0
- package/plugin/commands/propose.md +19 -0
- package/plugin/commands/round.md +17 -0
- package/plugin/commands/session.md +22 -0
- package/plugin/commands/settings.md +25 -0
- package/plugin/commands/setup.md +21 -0
- package/plugin/commands/team.md +23 -0
- package/plugin/domains/custom.md +27 -0
- package/plugin/domains/finance.md +31 -0
- package/plugin/domains/healthcare.md +34 -0
- package/plugin/domains/legal.md +31 -0
- package/plugin/domains/marketing.md +33 -0
- package/plugin/domains/operations.md +31 -0
- package/plugin/domains/research.md +32 -0
- package/plugin/domains/sales.md +30 -0
- package/plugin/domains/software.md +37 -0
- package/plugin/hooks/activity.sh +6 -0
- package/plugin/hooks/digging.sh +68 -0
- package/plugin/hooks/growth.sh +42 -0
- package/plugin/hooks/hooks.json +106 -0
- package/plugin/hooks/notice-on-pr.sh +35 -0
- package/plugin/hooks/notice-on-stop.sh +80 -0
- package/plugin/hooks/notice-pending.sh +58 -0
- package/plugin/hooks/solo-en-ciudad.sh +23 -0
- package/plugin/hooks/tokens.sh +47 -0
- package/plugin/roles/examples/account-executive.md +25 -0
- package/plugin/roles/examples/ai-manager.md +34 -0
- package/plugin/roles/examples/associate.md +24 -0
- package/plugin/roles/examples/brand-lead.md +20 -0
- package/plugin/roles/examples/cfo.md +20 -0
- package/plugin/roles/examples/city-lead.md +25 -0
- package/plugin/roles/examples/clinical-director.md +26 -0
- package/plugin/roles/examples/clinical-ops.md +25 -0
- package/plugin/roles/examples/clinician.md +25 -0
- package/plugin/roles/examples/compliance.md +27 -0
- package/plugin/roles/examples/content.md +24 -0
- package/plugin/roles/examples/controller.md +27 -0
- package/plugin/roles/examples/cpto.md +41 -0
- package/plugin/roles/examples/customer-success.md +25 -0
- package/plugin/roles/examples/data-engineer.md +37 -0
- package/plugin/roles/examples/data.md +60 -0
- package/plugin/roles/examples/dev.md +37 -0
- package/plugin/roles/examples/devops.md +39 -0
- package/plugin/roles/examples/enablement.md +24 -0
- package/plugin/roles/examples/ethics.md +24 -0
- package/plugin/roles/examples/fin-analytics.md +27 -0
- package/plugin/roles/examples/health-compliance.md +25 -0
- package/plugin/roles/examples/health-data.md +26 -0
- package/plugin/roles/examples/knowledge.md +25 -0
- package/plugin/roles/examples/lifecycle.md +26 -0
- package/plugin/roles/examples/llm-engineer.md +34 -0
- package/plugin/roles/examples/managing-partner.md +20 -0
- package/plugin/roles/examples/methods.md +23 -0
- package/plugin/roles/examples/operations-lead.md +23 -0
- package/plugin/roles/examples/ops.md +24 -0
- package/plugin/roles/examples/patient-safety.md +26 -0
- package/plugin/roles/examples/performance.md +27 -0
- package/plugin/roles/examples/po.md +36 -0
- package/plugin/roles/examples/process-owner.md +23 -0
- package/plugin/roles/examples/product-design.md +39 -0
- package/plugin/roles/examples/program-manager.md +24 -0
- package/plugin/roles/examples/quality.md +25 -0
- package/plugin/roles/examples/research-director.md +24 -0
- package/plugin/roles/examples/research-ops.md +25 -0
- package/plugin/roles/examples/researcher.md +25 -0
- package/plugin/roles/examples/revenue-lead.md +25 -0
- package/plugin/roles/examples/revops.md +26 -0
- package/plugin/roles/examples/seo.md +27 -0
- package/plugin/roles/examples/specialist.md +24 -0
- package/plugin/scripts/admision.py +165 -0
- package/plugin/scripts/apaga.py +232 -0
- package/plugin/scripts/avatar.py +211 -0
- package/plugin/scripts/broker.py +531 -0
- package/plugin/scripts/cage.py +252 -0
- package/plugin/scripts/capabilities.py +197 -0
- package/plugin/scripts/card.py +312 -0
- package/plugin/scripts/cities.py +520 -0
- package/plugin/scripts/city-env.sh +90 -0
- package/plugin/scripts/city-runtime.sh +87 -0
- package/plugin/scripts/city-session.sh +526 -0
- package/plugin/scripts/city_env.py +67 -0
- package/plugin/scripts/crecimiento.py +140 -0
- package/plugin/scripts/deliberations.py +48 -0
- package/plugin/scripts/doctor.py +172 -0
- package/plugin/scripts/domains.py +201 -0
- package/plugin/scripts/evidencia.py +64 -0
- package/plugin/scripts/find-repos.sh +111 -0
- package/plugin/scripts/gh.py +159 -0
- package/plugin/scripts/hall_protocol.py +147 -0
- package/plugin/scripts/hook_activity.py +139 -0
- package/plugin/scripts/launch.py +94 -0
- package/plugin/scripts/logs.py +98 -0
- package/plugin/scripts/pairing.py +185 -0
- package/plugin/scripts/parcels.py +132 -0
- package/plugin/scripts/read-card.py +51 -0
- package/plugin/scripts/report.py +201 -0
- package/plugin/scripts/reset.py +180 -0
- package/plugin/scripts/roads.py +230 -0
- package/plugin/scripts/roles.py +274 -0
- package/plugin/scripts/runtime_log.py +69 -0
- package/plugin/scripts/runtime_processes.py +132 -0
- package/plugin/scripts/rutas.py +83 -0
- package/plugin/scripts/seat.py +1349 -0
- package/plugin/scripts/tokens.py +194 -0
- package/plugin/scripts/trust-repos.py +59 -0
- package/plugin/scripts/ui.py +280 -0
- package/plugin/scripts/units.py +80 -0
- package/plugin/scripts/workspace.py +364 -0
- package/plugin/skills/city/SKILL.md +191 -0
- package/templates/blank.md +39 -0
- package/templates/finance.md +44 -0
- package/templates/legal.md +45 -0
- package/templates/marketing.md +48 -0
- package/templates/product.md +45 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
|
|
3
|
+
import { join, resolve } from 'path';
|
|
4
|
+
import { createInterface } from 'readline';
|
|
5
|
+
import { promptFor } from './adapter-prompts.js';
|
|
6
|
+
import { loadCityContext } from './city-config.js';
|
|
7
|
+
import { diagnosticLog } from './hub/diagnostics.js';
|
|
8
|
+
import { busCommand } from './hub-client.js';
|
|
9
|
+
import { BUS_PROTOCOL, BusEnvelope, isoNow, randomId, safeSegment } from './protocol.js';
|
|
10
|
+
import { runtimeFor } from './runtime/command.js';
|
|
11
|
+
import { createConnector } from './runtime/factory.js';
|
|
12
|
+
import { spawnNativeUi, terminate } from './runtime/process.js';
|
|
13
|
+
import { NativeRuntime, NativeUiCommand, RuntimeActivity } from './runtime/types.js';
|
|
14
|
+
import { atomicJson } from './runtime-files.js';
|
|
15
|
+
import { RuntimeSubscription, subscribeRuntime } from './runtime-subscription.js';
|
|
16
|
+
|
|
17
|
+
const options = parse(process.argv.slice(2));
|
|
18
|
+
if (!options.actor || !options.cwd || !options.command) {
|
|
19
|
+
throw new Error('runtime gateway needs --actor, --cwd and --command');
|
|
20
|
+
}
|
|
21
|
+
const context = loadCityContext(options.data || process.env.AGENTS_CITY_DATA);
|
|
22
|
+
const diagnostics = diagnosticLog(context, `gateway:${options.actor || 'unknown'}`);
|
|
23
|
+
if (!context.actors[options.actor])
|
|
24
|
+
throw new Error(`${options.actor} is not an actor in this city`);
|
|
25
|
+
const cwd = resolve(options.cwd);
|
|
26
|
+
if (!existsSync(cwd)) throw new Error(`runtime working directory does not exist: ${cwd}`);
|
|
27
|
+
const detected = runtimeFor(options.command);
|
|
28
|
+
if (!['claude', 'codex', 'opencode', 'kimi'].includes(detected)) {
|
|
29
|
+
throw new Error(`no native gateway exists for runtime command: ${options.command}`);
|
|
30
|
+
}
|
|
31
|
+
const runtime = detected as NativeRuntime;
|
|
32
|
+
const interactive = truthy(options.interactive || '');
|
|
33
|
+
const gatewayDir = join(context.runtimeDir, 'gateways');
|
|
34
|
+
const pidPath = join(gatewayDir, `${safeSegment(options.actor)}.pid`);
|
|
35
|
+
const statusPath = join(gatewayDir, `${safeSegment(options.actor)}.json`);
|
|
36
|
+
claimPid(pidPath);
|
|
37
|
+
|
|
38
|
+
// Provider servers execute the model's tool calls in their own child process.
|
|
39
|
+
// Give that process the same authenticated city identity as this gateway so a
|
|
40
|
+
// committee response cannot accidentally be submitted as the chair (`seat`).
|
|
41
|
+
process.env.AGENTS_CITY_DATA = context.dataDir;
|
|
42
|
+
process.env.AGENTS_CITY_HOME = context.appHome;
|
|
43
|
+
process.env.CITY_BUS_ACTOR = options.actor;
|
|
44
|
+
|
|
45
|
+
let activityQueue: Promise<void> = Promise.resolve();
|
|
46
|
+
let stopping = false;
|
|
47
|
+
function reportActivity(activity: RuntimeActivity): void {
|
|
48
|
+
const { thread, ...payload } = activity;
|
|
49
|
+
activityQueue = activityQueue
|
|
50
|
+
.then(async () => {
|
|
51
|
+
await busCommand(
|
|
52
|
+
'activity.publish',
|
|
53
|
+
payload as unknown as Record<string, unknown>,
|
|
54
|
+
thread || undefined,
|
|
55
|
+
options.actor,
|
|
56
|
+
context,
|
|
57
|
+
);
|
|
58
|
+
})
|
|
59
|
+
.catch((error) => {
|
|
60
|
+
diagnostics('activity.publish.failed', {
|
|
61
|
+
actor: options.actor,
|
|
62
|
+
outcome: 'failed',
|
|
63
|
+
message: (error as Error).message,
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const connector = createConnector(runtime, {
|
|
69
|
+
actor: options.actor,
|
|
70
|
+
cwd,
|
|
71
|
+
command: options.command,
|
|
72
|
+
autoApprove: truthy(options.auto || process.env.CITY_RUNTIME_AUTO || ''),
|
|
73
|
+
interactive,
|
|
74
|
+
onActivity: reportActivity,
|
|
75
|
+
onDiagnostic: (event, fields = {}) => {
|
|
76
|
+
diagnostics(event, { ...fields, actor: options.actor, mode: runtime });
|
|
77
|
+
},
|
|
78
|
+
onFatal: (error) => {
|
|
79
|
+
if (stopping) return;
|
|
80
|
+
diagnostics('gateway.provider.exited', {
|
|
81
|
+
actor: options.actor,
|
|
82
|
+
mode: runtime,
|
|
83
|
+
outcome: 'failed',
|
|
84
|
+
message: error.message,
|
|
85
|
+
});
|
|
86
|
+
process.stderr.write(`[city-gateway:${options.actor}] ${error.message}\n`);
|
|
87
|
+
void stop().finally(() => process.exit(1));
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
let subscription: RuntimeSubscription | null = null;
|
|
91
|
+
let nativeUiChild: import('child_process').ChildProcess | null = null;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
diagnostics('gateway.starting', { actor: options.actor, mode: runtime, outcome: 'starting' });
|
|
95
|
+
await connector.start();
|
|
96
|
+
diagnostics('gateway.provider.ready', {
|
|
97
|
+
actor: options.actor,
|
|
98
|
+
mode: runtime,
|
|
99
|
+
outcome: 'ready',
|
|
100
|
+
transport: connector.transport,
|
|
101
|
+
});
|
|
102
|
+
let nativeUi: NativeUiCommand | null = null;
|
|
103
|
+
if (interactive) {
|
|
104
|
+
nativeUi = connector.nativeUi?.() || null;
|
|
105
|
+
if (nativeUi) {
|
|
106
|
+
startNativeUi(nativeUi);
|
|
107
|
+
await connector.waitUntilReady?.();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
subscription = subscribeRuntime({
|
|
111
|
+
actor: options.actor,
|
|
112
|
+
context,
|
|
113
|
+
label: `city-gateway:${options.actor}`,
|
|
114
|
+
deliver: (envelope: BusEnvelope) => {
|
|
115
|
+
const role = context.actors[options.actor]?.operatingRole || 'blank';
|
|
116
|
+
if (runtime === 'claude') {
|
|
117
|
+
// Claude's gateway owns the streaming transport, while the same semantic
|
|
118
|
+
// thread is used for the public answer in the Hall. Never expose the raw
|
|
119
|
+
// assignment wrapper as spectator dialogue.
|
|
120
|
+
atomicJson(
|
|
121
|
+
join(context.runtimeDir, 'claude-threads', `${safeSegment(options.actor)}.json`),
|
|
122
|
+
{
|
|
123
|
+
thread: envelope.thread || envelope.id,
|
|
124
|
+
envelopeId: envelope.id,
|
|
125
|
+
kind: envelope.kind,
|
|
126
|
+
at: new Date().toISOString(),
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return connector.accept(promptFor(envelope, role), envelope);
|
|
131
|
+
},
|
|
132
|
+
onAccepted: (metric) => {
|
|
133
|
+
diagnostics('gateway.assignment.accepted', {
|
|
134
|
+
actor: options.actor,
|
|
135
|
+
mode: runtime,
|
|
136
|
+
thread: metric.envelopeId,
|
|
137
|
+
outcome: 'accepted',
|
|
138
|
+
latencyMs: metric.totalToNativeAcceptMs,
|
|
139
|
+
});
|
|
140
|
+
process.stderr.write(
|
|
141
|
+
`[city-gateway:${options.actor}] accepted ${metric.envelopeId} in ` +
|
|
142
|
+
`${metric.totalToNativeAcceptMs}ms via ${metric.transport}; no terminal paste\n`,
|
|
143
|
+
);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
await subscription.ready;
|
|
147
|
+
// A provider being ready is not enough: publish the gateway as online only
|
|
148
|
+
// after its authenticated runtime socket has joined the city bus.
|
|
149
|
+
atomicJson(statusPath, {
|
|
150
|
+
actor: options.actor,
|
|
151
|
+
runtime,
|
|
152
|
+
transport: connector.transport,
|
|
153
|
+
cwd,
|
|
154
|
+
pid: process.pid,
|
|
155
|
+
startedAt: new Date().toISOString(),
|
|
156
|
+
terminalInjection: false,
|
|
157
|
+
});
|
|
158
|
+
process.stderr.write(
|
|
159
|
+
`[city-gateway:${options.actor}] authenticated on ${context.city.address}; ` +
|
|
160
|
+
`${connector.transport} ready\n`,
|
|
161
|
+
);
|
|
162
|
+
diagnostics('gateway.bus.ready', {
|
|
163
|
+
actor: options.actor,
|
|
164
|
+
mode: runtime,
|
|
165
|
+
outcome: 'ready',
|
|
166
|
+
transport: connector.transport,
|
|
167
|
+
});
|
|
168
|
+
reportActivity({
|
|
169
|
+
sourceId: `gateway:${process.pid}:ready`,
|
|
170
|
+
kind: 'runtime.gateway.ready',
|
|
171
|
+
phase: 'ready',
|
|
172
|
+
tone: 'system',
|
|
173
|
+
title: `${options.actor} is online`,
|
|
174
|
+
summary: `${runtime} connected through ${connector.transport}.`,
|
|
175
|
+
});
|
|
176
|
+
if (interactive && !nativeUi) startConsole();
|
|
177
|
+
} catch (error) {
|
|
178
|
+
diagnostics('gateway.start.failed', {
|
|
179
|
+
actor: options.actor,
|
|
180
|
+
mode: runtime,
|
|
181
|
+
outcome: 'failed',
|
|
182
|
+
message: (error as Error).message,
|
|
183
|
+
});
|
|
184
|
+
reportActivity({
|
|
185
|
+
sourceId: `gateway:${process.pid}:start-failed`,
|
|
186
|
+
kind: 'runtime.gateway.failed',
|
|
187
|
+
phase: 'failed',
|
|
188
|
+
tone: 'error',
|
|
189
|
+
title: `${options.actor} failed to start`,
|
|
190
|
+
summary: (error as Error).message,
|
|
191
|
+
});
|
|
192
|
+
await stop();
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function startNativeUi(command: NativeUiCommand): void {
|
|
197
|
+
connector.setNativeUiAttached?.(true);
|
|
198
|
+
diagnostics('native-ui.starting', { actor: options.actor, mode: runtime });
|
|
199
|
+
nativeUiChild = spawnNativeUi(command.executable, command.args, command.cwd, process.env);
|
|
200
|
+
nativeUiChild.once('error', (error) => {
|
|
201
|
+
diagnostics('native-ui.start.failed', {
|
|
202
|
+
actor: options.actor,
|
|
203
|
+
mode: runtime,
|
|
204
|
+
outcome: 'failed',
|
|
205
|
+
message: error.message,
|
|
206
|
+
});
|
|
207
|
+
reportActivity({
|
|
208
|
+
sourceId: `gateway:${process.pid}:native-ui-error`,
|
|
209
|
+
kind: 'runtime.ui.failed',
|
|
210
|
+
phase: 'failed',
|
|
211
|
+
tone: 'error',
|
|
212
|
+
title: `${options.actor} interface failed`,
|
|
213
|
+
summary: error.message,
|
|
214
|
+
});
|
|
215
|
+
process.stderr.write(
|
|
216
|
+
`[city-gateway:${options.actor}] could not open ${runtime} TUI: ${error.message}\n`,
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
nativeUiChild.once('close', (code, signal) => {
|
|
220
|
+
nativeUiChild = null;
|
|
221
|
+
connector.setNativeUiAttached?.(false);
|
|
222
|
+
if (stopping) return;
|
|
223
|
+
diagnostics('native-ui.exited', {
|
|
224
|
+
actor: options.actor,
|
|
225
|
+
mode: runtime,
|
|
226
|
+
outcome: code && code !== 0 ? 'failed' : 'closed',
|
|
227
|
+
exitCode: code,
|
|
228
|
+
signal: signal || '',
|
|
229
|
+
});
|
|
230
|
+
if (code && code !== 0) {
|
|
231
|
+
reportActivity({
|
|
232
|
+
sourceId: `gateway:${process.pid}:native-ui-exit:${code}`,
|
|
233
|
+
kind: 'runtime.ui.failed',
|
|
234
|
+
phase: 'failed',
|
|
235
|
+
tone: 'error',
|
|
236
|
+
title: `${options.actor} interface exited`,
|
|
237
|
+
summary: `${runtime} exited with ${signal || code}.`,
|
|
238
|
+
});
|
|
239
|
+
process.stderr.write(
|
|
240
|
+
`[city-gateway:${options.actor}] ${runtime} TUI exited with ${signal || code}\n`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
void stop().finally(() => process.exit(code || 0));
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function startConsole(): void {
|
|
248
|
+
const terminal = createInterface({
|
|
249
|
+
input: process.stdin,
|
|
250
|
+
output: process.stdout,
|
|
251
|
+
prompt: 'city> ',
|
|
252
|
+
});
|
|
253
|
+
terminal.prompt();
|
|
254
|
+
terminal.on('line', (line) => {
|
|
255
|
+
const prompt = line.trim();
|
|
256
|
+
if (!prompt) return terminal.prompt();
|
|
257
|
+
if (prompt === '/exit' || prompt === '/quit') {
|
|
258
|
+
terminal.close();
|
|
259
|
+
return void stop().finally(() => process.exit(0));
|
|
260
|
+
}
|
|
261
|
+
const role = context.actors[options.actor]?.role || 'member';
|
|
262
|
+
const envelope: BusEnvelope = {
|
|
263
|
+
protocol: BUS_PROTOCOL,
|
|
264
|
+
id: randomId('console'),
|
|
265
|
+
kind: 'console.prompt',
|
|
266
|
+
scope: 'internal',
|
|
267
|
+
thread: null,
|
|
268
|
+
from: { city: context.city.address, actor: options.actor, role },
|
|
269
|
+
to: { city: context.city.address, actor: options.actor },
|
|
270
|
+
createdAt: isoNow(),
|
|
271
|
+
payload: { text: prompt },
|
|
272
|
+
};
|
|
273
|
+
if (runtime !== 'claude')
|
|
274
|
+
reportActivity({
|
|
275
|
+
sourceId: `${runtime}:${options.actor}:${envelope.id}:user`,
|
|
276
|
+
kind: 'conversation.user',
|
|
277
|
+
thread: envelope.id,
|
|
278
|
+
phase: 'asked',
|
|
279
|
+
tone: 'question',
|
|
280
|
+
title: `${options.actor} asked`,
|
|
281
|
+
summary: prompt,
|
|
282
|
+
});
|
|
283
|
+
void connector
|
|
284
|
+
.accept(prompt, envelope)
|
|
285
|
+
.then(() => terminal.prompt())
|
|
286
|
+
.catch((error) => {
|
|
287
|
+
process.stderr.write(`[city-gateway:${options.actor}] ${(error as Error).message}\n`);
|
|
288
|
+
terminal.prompt();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
terminal.on('close', () => {
|
|
292
|
+
if (!stopping) void stop().finally(() => process.exit(0));
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async function stop(): Promise<void> {
|
|
297
|
+
if (stopping) return;
|
|
298
|
+
stopping = true;
|
|
299
|
+
subscription?.close();
|
|
300
|
+
const ui = nativeUiChild;
|
|
301
|
+
nativeUiChild = null;
|
|
302
|
+
await terminate(ui);
|
|
303
|
+
await connector.close();
|
|
304
|
+
await Promise.race([activityQueue, new Promise<void>((resolve) => setTimeout(resolve, 2_000))]);
|
|
305
|
+
try {
|
|
306
|
+
unlinkSync(pidPath);
|
|
307
|
+
} catch {}
|
|
308
|
+
try {
|
|
309
|
+
unlinkSync(statusPath);
|
|
310
|
+
} catch {}
|
|
311
|
+
diagnostics('gateway.stopped', { actor: options.actor, mode: runtime, outcome: 'stopped' });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) {
|
|
315
|
+
process.on(signal, () => {
|
|
316
|
+
void stop().finally(() => process.exit(0));
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
process.on('exit', () => {
|
|
320
|
+
try {
|
|
321
|
+
unlinkSync(pidPath);
|
|
322
|
+
} catch {}
|
|
323
|
+
try {
|
|
324
|
+
unlinkSync(statusPath);
|
|
325
|
+
} catch {}
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
function claimPid(path: string): void {
|
|
329
|
+
mkdirSync(gatewayDir, { recursive: true, mode: 0o700 });
|
|
330
|
+
try {
|
|
331
|
+
const old = Number(readFileSync(path, 'utf8').trim());
|
|
332
|
+
if (old > 0) {
|
|
333
|
+
process.kill(old, 0);
|
|
334
|
+
throw new Error(`runtime gateway for ${options.actor} is already running as pid ${old}`);
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (
|
|
338
|
+
(error as NodeJS.ErrnoException).code !== 'ESRCH' &&
|
|
339
|
+
!(error as NodeJS.ErrnoException).code
|
|
340
|
+
) {
|
|
341
|
+
throw error;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
writeFileSync(path, String(process.pid) + '\n', { mode: 0o600 });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function parse(args: string[]): Record<string, string> {
|
|
348
|
+
const out: Record<string, string> = {};
|
|
349
|
+
for (let index = 0; index < args.length; index += 2) {
|
|
350
|
+
const key = args[index]?.replace(/^--/, '');
|
|
351
|
+
if (key) out[key] = args[index + 1] || '';
|
|
352
|
+
}
|
|
353
|
+
return out;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function truthy(value: string): boolean {
|
|
357
|
+
return ['1', 'true', 'yes', 'on'].includes(value.toLowerCase());
|
|
358
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { BusEnvelope } from './protocol.js';
|
|
4
|
+
|
|
5
|
+
export interface NativeAcceptance {
|
|
6
|
+
acceptedAt: string;
|
|
7
|
+
runtime: string;
|
|
8
|
+
transport: string;
|
|
9
|
+
providerRequestId?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RuntimeDeliveryMetric extends NativeAcceptance {
|
|
13
|
+
protocol: 'agents-city-runtime-delivery/1';
|
|
14
|
+
envelopeId: string;
|
|
15
|
+
thread: string | null;
|
|
16
|
+
actor: string;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
gatewayReceivedAt: string;
|
|
19
|
+
busToGatewayMs: number;
|
|
20
|
+
gatewayToNativeAcceptMs: number;
|
|
21
|
+
totalToNativeAcceptMs: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function recordNativeAcceptance(
|
|
25
|
+
runtimeDir: string,
|
|
26
|
+
envelope: BusEnvelope,
|
|
27
|
+
actor: string,
|
|
28
|
+
gatewayReceivedAt: string,
|
|
29
|
+
acceptance: NativeAcceptance,
|
|
30
|
+
): RuntimeDeliveryMetric {
|
|
31
|
+
const metric: RuntimeDeliveryMetric = {
|
|
32
|
+
protocol: 'agents-city-runtime-delivery/1',
|
|
33
|
+
envelopeId: envelope.id,
|
|
34
|
+
thread: envelope.thread,
|
|
35
|
+
actor,
|
|
36
|
+
createdAt: envelope.createdAt,
|
|
37
|
+
gatewayReceivedAt,
|
|
38
|
+
...acceptance,
|
|
39
|
+
busToGatewayMs: elapsed(envelope.createdAt, gatewayReceivedAt),
|
|
40
|
+
gatewayToNativeAcceptMs: elapsed(gatewayReceivedAt, acceptance.acceptedAt),
|
|
41
|
+
totalToNativeAcceptMs: elapsed(envelope.createdAt, acceptance.acceptedAt),
|
|
42
|
+
};
|
|
43
|
+
mkdirSync(runtimeDir, { recursive: true, mode: 0o700 });
|
|
44
|
+
appendFileSync(join(runtimeDir, 'runtime-latency.jsonl'), JSON.stringify(metric) + '\n', {
|
|
45
|
+
mode: 0o600,
|
|
46
|
+
});
|
|
47
|
+
return metric;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function elapsed(start: string, end: string): number {
|
|
51
|
+
return Math.max(0, Date.parse(end) - Date.parse(start));
|
|
52
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import WebSocket from 'ws';
|
|
4
|
+
import { CityContext } from './city-config.js';
|
|
5
|
+
import { openActorSocket } from './hub-client.js';
|
|
6
|
+
import { BusEnvelope, randomId, safeSegment } from './protocol.js';
|
|
7
|
+
import { atomicJson } from './runtime-files.js';
|
|
8
|
+
import {
|
|
9
|
+
NativeAcceptance,
|
|
10
|
+
RuntimeDeliveryMetric,
|
|
11
|
+
recordNativeAcceptance,
|
|
12
|
+
} from './runtime-metrics.js';
|
|
13
|
+
|
|
14
|
+
interface RuntimeSubscriptionOptions {
|
|
15
|
+
actor: string;
|
|
16
|
+
context: CityContext;
|
|
17
|
+
label: string;
|
|
18
|
+
deliver: (envelope: BusEnvelope) => Promise<NativeAcceptance>;
|
|
19
|
+
onAccepted?: (metric: RuntimeDeliveryMetric) => void;
|
|
20
|
+
onError?: (error: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RuntimeSubscription {
|
|
24
|
+
ready: Promise<void>;
|
|
25
|
+
close: () => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Subscribe one authenticated actor to its durable outbox.
|
|
30
|
+
*
|
|
31
|
+
* Native acceptance is persisted before the bus ACK. If the socket or process
|
|
32
|
+
* dies in that narrow interval, a reconnect ACKs the receipt without invoking
|
|
33
|
+
* the model twice.
|
|
34
|
+
*/
|
|
35
|
+
export function subscribeRuntime(options: RuntimeSubscriptionOptions): RuntimeSubscription {
|
|
36
|
+
let stopped = false;
|
|
37
|
+
let socket: WebSocket | null = null;
|
|
38
|
+
let tail = Promise.resolve();
|
|
39
|
+
let resolveReady: () => void = () => {};
|
|
40
|
+
let readyResolved = false;
|
|
41
|
+
const ready = new Promise<void>((resolve) => {
|
|
42
|
+
resolveReady = resolve;
|
|
43
|
+
});
|
|
44
|
+
const ackWaiters = new Map<
|
|
45
|
+
string,
|
|
46
|
+
{ resolve: () => void; reject: (error: Error) => void; timer: NodeJS.Timeout }
|
|
47
|
+
>();
|
|
48
|
+
|
|
49
|
+
const report = (error: unknown): void => {
|
|
50
|
+
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
51
|
+
if (options.onError) options.onError(normalized);
|
|
52
|
+
else console.error(`[${options.label}] ${normalized.message}`);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const connect = async (): Promise<void> => {
|
|
56
|
+
if (stopped) return;
|
|
57
|
+
try {
|
|
58
|
+
const opened = await openActorSocket('runtime', options.actor, options.context, (raw) => {
|
|
59
|
+
let message: Record<string, unknown>;
|
|
60
|
+
try {
|
|
61
|
+
message = JSON.parse(String(raw));
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (message.type === 'result') {
|
|
66
|
+
const waiter = ackWaiters.get(String(message.requestId || ''));
|
|
67
|
+
if (!waiter) return;
|
|
68
|
+
clearTimeout(waiter.timer);
|
|
69
|
+
ackWaiters.delete(String(message.requestId || ''));
|
|
70
|
+
if (message.ok) waiter.resolve();
|
|
71
|
+
else waiter.reject(new Error(String(message.error || 'bus acknowledgement failed')));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (message.type !== 'envelope') return;
|
|
75
|
+
const envelope = message.envelope as BusEnvelope;
|
|
76
|
+
const receivedAt = new Date().toISOString();
|
|
77
|
+
tail = tail.then(() => accept(envelope, receivedAt)).catch(report);
|
|
78
|
+
});
|
|
79
|
+
socket = opened.ws;
|
|
80
|
+
if (!readyResolved) {
|
|
81
|
+
readyResolved = true;
|
|
82
|
+
resolveReady();
|
|
83
|
+
}
|
|
84
|
+
socket.on('close', () => {
|
|
85
|
+
socket = null;
|
|
86
|
+
rejectAcks(new Error('city bus disconnected before acknowledgement'));
|
|
87
|
+
if (!stopped) setTimeout(() => void connect(), 500);
|
|
88
|
+
});
|
|
89
|
+
socket.on('error', () => {});
|
|
90
|
+
} catch (error) {
|
|
91
|
+
report(error);
|
|
92
|
+
if (!stopped) setTimeout(() => void connect(), 500);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const accept = async (envelope: BusEnvelope, receivedAt: string): Promise<void> => {
|
|
97
|
+
const marker = receiptPath(options.context, options.actor, envelope.id);
|
|
98
|
+
let acceptance = readReceipt(marker, envelope.id);
|
|
99
|
+
if (!acceptance) {
|
|
100
|
+
acceptance = await options.deliver(envelope);
|
|
101
|
+
atomicJson(marker, { envelopeId: envelope.id, acceptance });
|
|
102
|
+
const metric = recordNativeAcceptance(
|
|
103
|
+
options.context.runtimeDir,
|
|
104
|
+
envelope,
|
|
105
|
+
options.actor,
|
|
106
|
+
receivedAt,
|
|
107
|
+
acceptance,
|
|
108
|
+
);
|
|
109
|
+
options.onAccepted?.(metric);
|
|
110
|
+
}
|
|
111
|
+
await acknowledge(envelope.id);
|
|
112
|
+
try {
|
|
113
|
+
unlinkSync(marker);
|
|
114
|
+
} catch {}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const acknowledge = (envelopeId: string): Promise<void> => {
|
|
118
|
+
const current = socket;
|
|
119
|
+
if (!current || current.readyState !== WebSocket.OPEN) {
|
|
120
|
+
return Promise.reject(new Error('city bus is offline; native receipt retained for retry'));
|
|
121
|
+
}
|
|
122
|
+
const requestId = randomId('ack');
|
|
123
|
+
return new Promise<void>((resolve, reject) => {
|
|
124
|
+
const timer = setTimeout(() => {
|
|
125
|
+
ackWaiters.delete(requestId);
|
|
126
|
+
reject(new Error('city bus acknowledgement timed out'));
|
|
127
|
+
}, 5_000);
|
|
128
|
+
ackWaiters.set(requestId, { resolve, reject, timer });
|
|
129
|
+
current.send(JSON.stringify({ type: 'ack', requestId, envelopeId }), (error) => {
|
|
130
|
+
if (!error) return;
|
|
131
|
+
const waiter = ackWaiters.get(requestId);
|
|
132
|
+
if (!waiter) return;
|
|
133
|
+
clearTimeout(waiter.timer);
|
|
134
|
+
ackWaiters.delete(requestId);
|
|
135
|
+
waiter.reject(error);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const rejectAcks = (error: Error): void => {
|
|
141
|
+
for (const [requestId, waiter] of ackWaiters) {
|
|
142
|
+
clearTimeout(waiter.timer);
|
|
143
|
+
waiter.reject(error);
|
|
144
|
+
ackWaiters.delete(requestId);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
void connect();
|
|
149
|
+
return {
|
|
150
|
+
ready,
|
|
151
|
+
close: () => {
|
|
152
|
+
stopped = true;
|
|
153
|
+
rejectAcks(new Error('runtime subscription stopped'));
|
|
154
|
+
try {
|
|
155
|
+
socket?.close();
|
|
156
|
+
} catch {}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function receiptPath(context: CityContext, actor: string, envelopeId: string): string {
|
|
162
|
+
const directory = join(context.runtimeDir, 'accepted', safeSegment(actor));
|
|
163
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
164
|
+
return join(directory, `${safeSegment(envelopeId, 'message')}.json`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readReceipt(path: string, envelopeId: string): NativeAcceptance | null {
|
|
168
|
+
if (!existsSync(path)) return null;
|
|
169
|
+
try {
|
|
170
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8')) as {
|
|
171
|
+
envelopeId?: string;
|
|
172
|
+
acceptance?: NativeAcceptance;
|
|
173
|
+
};
|
|
174
|
+
return parsed.envelopeId === envelopeId && parsed.acceptance?.acceptedAt
|
|
175
|
+
? parsed.acceptance
|
|
176
|
+
: null;
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|