zoe-agent 0.3.1
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/CHANGELOG.md +154 -0
- package/LICENSE +96 -0
- package/README.md +568 -0
- package/dist/adapters/cli/agent.d.ts +59 -0
- package/dist/adapters/cli/agent.js +232 -0
- package/dist/adapters/cli/bootstrap.d.ts +25 -0
- package/dist/adapters/cli/bootstrap.js +204 -0
- package/dist/adapters/cli/commands/build-registry.d.ts +14 -0
- package/dist/adapters/cli/commands/build-registry.js +88 -0
- package/dist/adapters/cli/commands/clear.d.ts +7 -0
- package/dist/adapters/cli/commands/clear.js +10 -0
- package/dist/adapters/cli/commands/compact.d.ts +13 -0
- package/dist/adapters/cli/commands/compact.js +96 -0
- package/dist/adapters/cli/commands/exit.d.ts +7 -0
- package/dist/adapters/cli/commands/exit.js +9 -0
- package/dist/adapters/cli/commands/gateway.d.ts +7 -0
- package/dist/adapters/cli/commands/gateway.js +152 -0
- package/dist/adapters/cli/commands/help.d.ts +9 -0
- package/dist/adapters/cli/commands/help.js +12 -0
- package/dist/adapters/cli/commands/models.d.ts +10 -0
- package/dist/adapters/cli/commands/models.js +32 -0
- package/dist/adapters/cli/commands/registry.d.ts +70 -0
- package/dist/adapters/cli/commands/registry.js +111 -0
- package/dist/adapters/cli/commands/settings-utils.d.ts +38 -0
- package/dist/adapters/cli/commands/settings-utils.js +182 -0
- package/dist/adapters/cli/commands/settings.d.ts +9 -0
- package/dist/adapters/cli/commands/settings.js +395 -0
- package/dist/adapters/cli/commands/skills.d.ts +7 -0
- package/dist/adapters/cli/commands/skills.js +21 -0
- package/dist/adapters/cli/config-loader.d.ts +27 -0
- package/dist/adapters/cli/config-loader.js +48 -0
- package/dist/adapters/cli/docker-utils.d.ts +37 -0
- package/dist/adapters/cli/docker-utils.js +90 -0
- package/dist/adapters/cli/index.d.ts +2 -0
- package/dist/adapters/cli/index.js +88 -0
- package/dist/adapters/cli/repl.d.ts +22 -0
- package/dist/adapters/cli/repl.js +256 -0
- package/dist/adapters/cli/setup.d.ts +19 -0
- package/dist/adapters/cli/setup.js +613 -0
- package/dist/adapters/cli/system-prompts.d.ts +56 -0
- package/dist/adapters/cli/system-prompts.js +131 -0
- package/dist/adapters/cli/tui/app.d.ts +58 -0
- package/dist/adapters/cli/tui/app.js +314 -0
- package/dist/adapters/cli/tui/components/assistant-message.d.ts +5 -0
- package/dist/adapters/cli/tui/components/assistant-message.js +9 -0
- package/dist/adapters/cli/tui/components/autocomplete.d.ts +19 -0
- package/dist/adapters/cli/tui/components/autocomplete.js +75 -0
- package/dist/adapters/cli/tui/components/command-palette.d.ts +15 -0
- package/dist/adapters/cli/tui/components/command-palette.js +50 -0
- package/dist/adapters/cli/tui/components/diff-viewer.d.ts +5 -0
- package/dist/adapters/cli/tui/components/diff-viewer.js +109 -0
- package/dist/adapters/cli/tui/components/error-message.d.ts +5 -0
- package/dist/adapters/cli/tui/components/error-message.js +8 -0
- package/dist/adapters/cli/tui/components/footer.d.ts +20 -0
- package/dist/adapters/cli/tui/components/footer.js +19 -0
- package/dist/adapters/cli/tui/components/goal-status.d.ts +12 -0
- package/dist/adapters/cli/tui/components/goal-status.js +22 -0
- package/dist/adapters/cli/tui/components/info-message.d.ts +5 -0
- package/dist/adapters/cli/tui/components/info-message.js +8 -0
- package/dist/adapters/cli/tui/components/logo-banner.d.ts +7 -0
- package/dist/adapters/cli/tui/components/logo-banner.js +33 -0
- package/dist/adapters/cli/tui/components/markdown.d.ts +9 -0
- package/dist/adapters/cli/tui/components/markdown.js +92 -0
- package/dist/adapters/cli/tui/components/message-area.d.ts +19 -0
- package/dist/adapters/cli/tui/components/message-area.js +55 -0
- package/dist/adapters/cli/tui/components/permission-prompt.d.ts +13 -0
- package/dist/adapters/cli/tui/components/permission-prompt.js +32 -0
- package/dist/adapters/cli/tui/components/prompt-area.d.ts +22 -0
- package/dist/adapters/cli/tui/components/prompt-area.js +68 -0
- package/dist/adapters/cli/tui/components/text-input.d.ts +27 -0
- package/dist/adapters/cli/tui/components/text-input.js +142 -0
- package/dist/adapters/cli/tui/components/tool-call-block.d.ts +11 -0
- package/dist/adapters/cli/tui/components/tool-call-block.js +68 -0
- package/dist/adapters/cli/tui/components/user-message.d.ts +5 -0
- package/dist/adapters/cli/tui/components/user-message.js +8 -0
- package/dist/adapters/cli/tui/diff/file-write-meta.d.ts +11 -0
- package/dist/adapters/cli/tui/diff/file-write-meta.js +11 -0
- package/dist/adapters/cli/tui/diff/line-diff.d.ts +17 -0
- package/dist/adapters/cli/tui/diff/line-diff.js +44 -0
- package/dist/adapters/cli/tui/feed-serializer.d.ts +29 -0
- package/dist/adapters/cli/tui/feed-serializer.js +70 -0
- package/dist/adapters/cli/tui/file-index.d.ts +8 -0
- package/dist/adapters/cli/tui/file-index.js +41 -0
- package/dist/adapters/cli/tui/hooks/use-agent.d.ts +54 -0
- package/dist/adapters/cli/tui/hooks/use-agent.js +177 -0
- package/dist/adapters/cli/tui/hooks/use-feed.d.ts +16 -0
- package/dist/adapters/cli/tui/hooks/use-feed.js +25 -0
- package/dist/adapters/cli/tui/hooks/use-file-watcher.d.ts +10 -0
- package/dist/adapters/cli/tui/hooks/use-file-watcher.js +43 -0
- package/dist/adapters/cli/tui/hooks/use-keybindings.d.ts +16 -0
- package/dist/adapters/cli/tui/hooks/use-keybindings.js +25 -0
- package/dist/adapters/cli/tui/hooks/use-theme.d.ts +8 -0
- package/dist/adapters/cli/tui/hooks/use-theme.js +12 -0
- package/dist/adapters/cli/tui/index.d.ts +19 -0
- package/dist/adapters/cli/tui/index.js +206 -0
- package/dist/adapters/cli/tui/ink-reset.d.ts +29 -0
- package/dist/adapters/cli/tui/ink-reset.js +57 -0
- package/dist/adapters/cli/tui/layout.d.ts +15 -0
- package/dist/adapters/cli/tui/layout.js +15 -0
- package/dist/adapters/cli/tui/logo/gradient.d.ts +11 -0
- package/dist/adapters/cli/tui/logo/gradient.js +31 -0
- package/dist/adapters/cli/tui/overlays/help-dialog.d.ts +4 -0
- package/dist/adapters/cli/tui/overlays/help-dialog.js +26 -0
- package/dist/adapters/cli/tui/overlays/model-selector.d.ts +14 -0
- package/dist/adapters/cli/tui/overlays/model-selector.js +43 -0
- package/dist/adapters/cli/tui/overlays/session-selector.d.ts +35 -0
- package/dist/adapters/cli/tui/overlays/session-selector.js +162 -0
- package/dist/adapters/cli/tui/overlays/settings-overlay.d.ts +24 -0
- package/dist/adapters/cli/tui/overlays/settings-overlay.js +126 -0
- package/dist/adapters/cli/tui/session-export.d.ts +21 -0
- package/dist/adapters/cli/tui/session-export.js +63 -0
- package/dist/adapters/cli/tui/theme.d.ts +23 -0
- package/dist/adapters/cli/tui/theme.js +22 -0
- package/dist/adapters/cli/tui/types.d.ts +52 -0
- package/dist/adapters/cli/tui/types.js +12 -0
- package/dist/adapters/sdk/agent.d.ts +20 -0
- package/dist/adapters/sdk/agent.js +356 -0
- package/dist/adapters/sdk/http.d.ts +43 -0
- package/dist/adapters/sdk/http.js +61 -0
- package/dist/adapters/sdk/index.d.ts +58 -0
- package/dist/adapters/sdk/index.js +209 -0
- package/dist/adapters/sdk/settings.d.ts +18 -0
- package/dist/adapters/sdk/settings.js +57 -0
- package/dist/adapters/sdk/tools.d.ts +7 -0
- package/dist/adapters/sdk/tools.js +13 -0
- package/dist/adapters/server/auth.d.ts +53 -0
- package/dist/adapters/server/auth.js +168 -0
- package/dist/adapters/server/index.d.ts +40 -0
- package/dist/adapters/server/index.js +255 -0
- package/dist/adapters/server/rest-gateway.d.ts +13 -0
- package/dist/adapters/server/rest-gateway.js +218 -0
- package/dist/adapters/server/rest.d.ts +37 -0
- package/dist/adapters/server/rest.js +341 -0
- package/dist/adapters/server/server-core.d.ts +55 -0
- package/dist/adapters/server/server-core.js +121 -0
- package/dist/adapters/server/session-store.d.ts +81 -0
- package/dist/adapters/server/session-store.js +272 -0
- package/dist/adapters/server/settings-handlers.d.ts +24 -0
- package/dist/adapters/server/settings-handlers.js +360 -0
- package/dist/adapters/server/standalone.d.ts +19 -0
- package/dist/adapters/server/standalone.js +113 -0
- package/dist/adapters/server/websocket.d.ts +26 -0
- package/dist/adapters/server/websocket.js +68 -0
- package/dist/adapters/server/ws-handlers.d.ts +32 -0
- package/dist/adapters/server/ws-handlers.js +523 -0
- package/dist/adapters/server/ws-types.d.ts +304 -0
- package/dist/adapters/server/ws-types.js +7 -0
- package/dist/core/agent-loop.d.ts +68 -0
- package/dist/core/agent-loop.js +423 -0
- package/dist/core/config.d.ts +115 -0
- package/dist/core/config.js +189 -0
- package/dist/core/errors.d.ts +58 -0
- package/dist/core/errors.js +88 -0
- package/dist/core/hooks.d.ts +35 -0
- package/dist/core/hooks.js +49 -0
- package/dist/core/index.d.ts +23 -0
- package/dist/core/index.js +29 -0
- package/dist/core/message-convert.d.ts +41 -0
- package/dist/core/message-convert.js +94 -0
- package/dist/core/middleware/auth.d.ts +24 -0
- package/dist/core/middleware/auth.js +28 -0
- package/dist/core/middleware/logging.d.ts +23 -0
- package/dist/core/middleware/logging.js +28 -0
- package/dist/core/middleware/rate-limit.d.ts +27 -0
- package/dist/core/middleware/rate-limit.js +38 -0
- package/dist/core/middleware/semantic-tools.d.ts +10 -0
- package/dist/core/middleware/semantic-tools.js +43 -0
- package/dist/core/middleware.d.ts +48 -0
- package/dist/core/middleware.js +38 -0
- package/dist/core/permission.d.ts +25 -0
- package/dist/core/permission.js +50 -0
- package/dist/core/provider-config.d.ts +129 -0
- package/dist/core/provider-config.js +273 -0
- package/dist/core/provider-env.d.ts +39 -0
- package/dist/core/provider-env.js +142 -0
- package/dist/core/provider-resolver.d.ts +12 -0
- package/dist/core/provider-resolver.js +12 -0
- package/dist/core/session-store.d.ts +75 -0
- package/dist/core/session-store.js +245 -0
- package/dist/core/settings-manager.d.ts +57 -0
- package/dist/core/settings-manager.js +359 -0
- package/dist/core/settings-schema.d.ts +38 -0
- package/dist/core/settings-schema.js +171 -0
- package/dist/core/skill-catalog.d.ts +6 -0
- package/dist/core/skill-catalog.js +17 -0
- package/dist/core/skill-invoker.d.ts +127 -0
- package/dist/core/skill-invoker.js +182 -0
- package/dist/core/stream-accumulator.d.ts +21 -0
- package/dist/core/stream-accumulator.js +51 -0
- package/dist/core/stream-manager.d.ts +58 -0
- package/dist/core/stream-manager.js +212 -0
- package/dist/core/tool-executor.d.ts +84 -0
- package/dist/core/tool-executor.js +256 -0
- package/dist/core/types.d.ts +259 -0
- package/dist/core/types.js +11 -0
- package/dist/gateway/gateway.d.ts +52 -0
- package/dist/gateway/gateway.js +537 -0
- package/dist/gateway/index.d.ts +21 -0
- package/dist/gateway/index.js +31 -0
- package/dist/gateway/openapi-importer.d.ts +15 -0
- package/dist/gateway/openapi-importer.js +66 -0
- package/dist/gateway/semantic-scorer.d.ts +7 -0
- package/dist/gateway/semantic-scorer.js +24 -0
- package/dist/gateway/settings-adapter.d.ts +49 -0
- package/dist/gateway/settings-adapter.js +137 -0
- package/dist/gateway/tool-factory.d.ts +9 -0
- package/dist/gateway/tool-factory.js +414 -0
- package/dist/gateway/types.d.ts +68 -0
- package/dist/gateway/types.js +7 -0
- package/dist/models-catalog.js +46 -0
- package/dist/providers/anthropic.d.ts +22 -0
- package/dist/providers/anthropic.js +148 -0
- package/dist/providers/factory.d.ts +10 -0
- package/dist/providers/factory.js +25 -0
- package/dist/providers/openai.d.ts +15 -0
- package/dist/providers/openai.js +71 -0
- package/dist/providers/types.d.ts +48 -0
- package/dist/providers/types.js +1 -0
- package/dist/skills/args.d.ts +37 -0
- package/dist/skills/args.js +99 -0
- package/dist/skills/index.d.ts +11 -0
- package/dist/skills/index.js +23 -0
- package/dist/skills/loader.d.ts +3 -0
- package/dist/skills/loader.js +59 -0
- package/dist/skills/parser.d.ts +7 -0
- package/dist/skills/parser.js +152 -0
- package/dist/skills/registry.d.ts +13 -0
- package/dist/skills/registry.js +74 -0
- package/dist/skills/resolver.d.ts +19 -0
- package/dist/skills/resolver.js +116 -0
- package/dist/skills/types.d.ts +74 -0
- package/dist/skills/types.js +50 -0
- package/dist/tools/browser.d.ts +2 -0
- package/dist/tools/browser.js +68 -0
- package/dist/tools/core.d.ts +20 -0
- package/dist/tools/core.js +244 -0
- package/dist/tools/email.d.ts +2 -0
- package/dist/tools/email.js +61 -0
- package/dist/tools/image.d.ts +2 -0
- package/dist/tools/image.js +257 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.js +88 -0
- package/dist/tools/interface.d.ts +22 -0
- package/dist/tools/interface.js +1 -0
- package/dist/tools/notify.d.ts +2 -0
- package/dist/tools/notify.js +100 -0
- package/dist/tools/prompt-optimizer.d.ts +2 -0
- package/dist/tools/prompt-optimizer.js +65 -0
- package/dist/tools/screenshot.d.ts +2 -0
- package/dist/tools/screenshot.js +184 -0
- package/dist/tools/search.d.ts +2 -0
- package/dist/tools/search.js +78 -0
- package/dist/tools/todos.d.ts +10 -0
- package/dist/tools/todos.js +50 -0
- package/package.json +119 -0
- package/skills/docker-ops/SKILL.md +329 -0
- package/skills/k8s-deploy/SKILL.md +397 -0
- package/skills/log-analyzer/SKILL.md +331 -0
- package/skills/speckit-analyze/SKILL.md +260 -0
- package/skills/speckit-checklist/SKILL.md +374 -0
- package/skills/speckit-clarify/SKILL.md +286 -0
- package/skills/speckit-constitution/SKILL.md +157 -0
- package/skills/speckit-implement/SKILL.md +224 -0
- package/skills/speckit-plan/SKILL.md +171 -0
- package/skills/speckit-specify/SKILL.md +346 -0
- package/skills/speckit-tasks/SKILL.md +215 -0
- package/skills/speckit-taskstoissues/SKILL.md +107 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Zoe Server — Standalone Entry Point
|
|
4
|
+
*
|
|
5
|
+
* Starts the Zoe remote server as a standalone process.
|
|
6
|
+
* Suitable as a Docker CMD/ENTRYPOINT or direct CLI invocation.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* node dist/adapters/server/standalone.js
|
|
10
|
+
* node dist/adapters/server/standalone.js --generate-api-key
|
|
11
|
+
*
|
|
12
|
+
* Environment variables:
|
|
13
|
+
* ZOE_PORT / PORT — Port to listen on (default: 7337)
|
|
14
|
+
* ZOE_HOST — Host to bind to (default: "0.0.0.0")
|
|
15
|
+
* ZOE_SESSION_DIR — Directory for session storage
|
|
16
|
+
* ZOE_SESSION_TTL — Session TTL in seconds (default: 86400)
|
|
17
|
+
* ZOE_API_KEYS_FILE — Path to API key store file
|
|
18
|
+
*/
|
|
19
|
+
import * as fs from "fs";
|
|
20
|
+
import * as path from "path";
|
|
21
|
+
import { startServer, initializeSkills } from "./index.js";
|
|
22
|
+
import { generateApiKey } from "./auth.js";
|
|
23
|
+
// ── Version ────────────────────────────────────────────────────────────
|
|
24
|
+
function resolveVersion() {
|
|
25
|
+
try {
|
|
26
|
+
const pkgPath = path.join(import.meta.dirname ?? ".", "..", "..", "package.json");
|
|
27
|
+
const raw = fs.readFileSync(pkgPath, "utf-8");
|
|
28
|
+
return JSON.parse(raw).version ?? "0.0.0";
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return "0.0.0";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// ── API Key Generation ─────────────────────────────────────────────────
|
|
35
|
+
function handleGenerateApiKey() {
|
|
36
|
+
const filePath = process.env.ZOE_API_KEYS_FILE || undefined;
|
|
37
|
+
const entry = generateApiKey(["agent:run", "admin"], {
|
|
38
|
+
label: "cli-generated",
|
|
39
|
+
filePath,
|
|
40
|
+
});
|
|
41
|
+
process.stdout.write(`Generated API key:\n\n ${entry.key}\n\n`);
|
|
42
|
+
process.stdout.write(`Scopes: ${entry.scopes.join(", ")}\n` +
|
|
43
|
+
`Created: ${entry.created}\n` +
|
|
44
|
+
`Stored in: ${filePath ?? "~/.zoe/server-keys.json"}\n`);
|
|
45
|
+
process.exit(0);
|
|
46
|
+
}
|
|
47
|
+
// ── Main ────────────────────────────────────────────────────────────────
|
|
48
|
+
async function main() {
|
|
49
|
+
// Handle --generate-api-key flag
|
|
50
|
+
if (process.argv.includes("--generate-api-key")) {
|
|
51
|
+
handleGenerateApiKey();
|
|
52
|
+
return; // unreachable, but satisfies type checker
|
|
53
|
+
}
|
|
54
|
+
const version = resolveVersion();
|
|
55
|
+
// Resolve configuration from environment
|
|
56
|
+
const port = parseInt(process.env.ZOE_PORT ?? process.env.PORT ?? "", 10);
|
|
57
|
+
const host = process.env.ZOE_HOST ?? "0.0.0.0";
|
|
58
|
+
const sessionTTL = parseInt(process.env.ZOE_SESSION_TTL ?? "", 10);
|
|
59
|
+
const apiKeysFile = process.env.ZOE_API_KEYS_FILE;
|
|
60
|
+
// Expose API keys file path for the auth module if provided
|
|
61
|
+
if (apiKeysFile) {
|
|
62
|
+
process.env.ZOE_API_KEYS_FILE = apiKeysFile;
|
|
63
|
+
}
|
|
64
|
+
const options = {
|
|
65
|
+
host,
|
|
66
|
+
...(isNaN(port) || port <= 0 ? {} : { port }),
|
|
67
|
+
...(isNaN(sessionTTL) || sessionTTL <= 0 ? {} : { sessionTTL }),
|
|
68
|
+
};
|
|
69
|
+
process.stdout.write(`[zoe] Starting Zoe server v${version}\n`);
|
|
70
|
+
try {
|
|
71
|
+
// Initialize skills registry
|
|
72
|
+
await initializeSkills();
|
|
73
|
+
// Start server
|
|
74
|
+
const server = await startServer(options);
|
|
75
|
+
const actualPort = server.address()?.port ?? options.port ?? 7337;
|
|
76
|
+
process.stdout.write(`[zoe] Listening on ${host}:${actualPort}\n` +
|
|
77
|
+
`[zoe] Session TTL: ${isNaN(sessionTTL) || sessionTTL <= 0 ? 86400 : sessionTTL}s\n` +
|
|
78
|
+
`[zoe] API keys: ${apiKeysFile ?? "~/.zoe/server-keys.json"}\n`);
|
|
79
|
+
// Graceful shutdown
|
|
80
|
+
const shutdown = (signal) => {
|
|
81
|
+
process.stdout.write(`[zoe] Received ${signal}, shutting down...\n`);
|
|
82
|
+
server.close(() => {
|
|
83
|
+
process.stdout.write("[zoe] Server stopped.\n");
|
|
84
|
+
process.exit(0);
|
|
85
|
+
});
|
|
86
|
+
// Force exit after 5 seconds if connections don't drain
|
|
87
|
+
setTimeout(() => {
|
|
88
|
+
process.stdout.write("[zoe] Force exiting after 5s timeout.\n");
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}, 5000);
|
|
91
|
+
};
|
|
92
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
93
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
97
|
+
if (message.includes("EADDRINUSE") ||
|
|
98
|
+
message.includes("EACCES")) {
|
|
99
|
+
process.stdout.write(`[zoe] Fatal: ${message}\n` +
|
|
100
|
+
`[zoe] Check that port ${isNaN(port) || port <= 0 ? 7337 : port} is available and you have permission to bind.\n`);
|
|
101
|
+
}
|
|
102
|
+
else if (message.includes("ENOENT") &&
|
|
103
|
+
message.includes("sessions")) {
|
|
104
|
+
process.stdout.write(`[zoe] Fatal: Cannot create session directory.\n` +
|
|
105
|
+
`[zoe] Ensure ZOE_SESSION_DIR (${process.env.ZOE_SESSION_DIR ?? "<cwd>/.zoe/sessions"}) is writable.\n`);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
process.stdout.write(`[zoe] Fatal error: ${message}\n`);
|
|
109
|
+
}
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
main();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zoe Server — WebSocket Protocol Handler
|
|
3
|
+
*
|
|
4
|
+
* Re-export hub. Setup and teardown functions live here;
|
|
5
|
+
* types and handlers are split into ws-types.ts and ws-handlers.ts.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: Requires the `ws` npm package for Node.js. Install it via:
|
|
8
|
+
* npm install ws
|
|
9
|
+
* npm install -D @types/ws
|
|
10
|
+
*
|
|
11
|
+
* The module uses a dynamic import so it fails gracefully if `ws` is missing.
|
|
12
|
+
*/
|
|
13
|
+
import type { WSServer, WebSocketHandlerContext } from "./ws-types.js";
|
|
14
|
+
export type { WebSocketHandlerContext } from "./ws-types.js";
|
|
15
|
+
export { getActiveConnectionCount } from "./ws-handlers.js";
|
|
16
|
+
/**
|
|
17
|
+
* Initialize the WebSocket server.
|
|
18
|
+
*
|
|
19
|
+
* Uses a dynamic import for the `ws` package. If it's not installed,
|
|
20
|
+
* logs a warning and returns null.
|
|
21
|
+
*/
|
|
22
|
+
export declare function setupWebSocket(server: import("http").Server, ctx: WebSocketHandlerContext): Promise<WSServer | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Close the WebSocket server and all active connections.
|
|
25
|
+
*/
|
|
26
|
+
export declare function closeWebSocket(): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zoe Server — WebSocket Protocol Handler
|
|
3
|
+
*
|
|
4
|
+
* Re-export hub. Setup and teardown functions live here;
|
|
5
|
+
* types and handlers are split into ws-types.ts and ws-handlers.ts.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: Requires the `ws` npm package for Node.js. Install it via:
|
|
8
|
+
* npm install ws
|
|
9
|
+
* npm install -D @types/ws
|
|
10
|
+
*
|
|
11
|
+
* The module uses a dynamic import so it fails gracefully if `ws` is missing.
|
|
12
|
+
*/
|
|
13
|
+
import { authMiddleware } from "./auth.js";
|
|
14
|
+
import { handleConnection, closeAllConnections } from "./ws-handlers.js";
|
|
15
|
+
export { getActiveConnectionCount } from "./ws-handlers.js";
|
|
16
|
+
// ── Exported setup function ──────────────────────────────────────────
|
|
17
|
+
let wss = null;
|
|
18
|
+
/**
|
|
19
|
+
* Initialize the WebSocket server.
|
|
20
|
+
*
|
|
21
|
+
* Uses a dynamic import for the `ws` package. If it's not installed,
|
|
22
|
+
* logs a warning and returns null.
|
|
23
|
+
*/
|
|
24
|
+
export async function setupWebSocket(server, ctx) {
|
|
25
|
+
let wsModule;
|
|
26
|
+
try {
|
|
27
|
+
// @ts-expect-error — ws is an optional peer dependency
|
|
28
|
+
wsModule = (await import("ws"));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
console.warn("[ws] The 'ws' package is not installed. WebSocket support is disabled.\n" +
|
|
32
|
+
" Install it with: npm install ws");
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
wss = new wsModule.WebSocketServer({ noServer: true, path: "/ws" });
|
|
36
|
+
wss.on("connection", (ws, req) => {
|
|
37
|
+
handleConnection(ws, req, ctx);
|
|
38
|
+
});
|
|
39
|
+
// Handle HTTP upgrade requests
|
|
40
|
+
server.on("upgrade", (req, socket, head) => {
|
|
41
|
+
// Only handle /ws upgrades
|
|
42
|
+
const url = req.url?.split("?")[0];
|
|
43
|
+
if (url !== "/ws") {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Authenticate the upgrade request
|
|
47
|
+
const key = authMiddleware(req);
|
|
48
|
+
if (!key) {
|
|
49
|
+
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
|
50
|
+
socket.destroy();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
54
|
+
wss.emit("connection", ws, req);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
return wss;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Close the WebSocket server and all active connections.
|
|
61
|
+
*/
|
|
62
|
+
export function closeWebSocket() {
|
|
63
|
+
if (wss) {
|
|
64
|
+
closeAllConnections();
|
|
65
|
+
wss.close();
|
|
66
|
+
wss = null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zoe Server — WebSocket Protocol Handlers
|
|
3
|
+
*
|
|
4
|
+
* All handler functions, safeSend helper, and active connections registry.
|
|
5
|
+
* Extracted from websocket.ts for single-responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import type { WebSocket, ServerMessage, WebSocketHandlerContext, ConnectionState } from "./ws-types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Get the number of currently active WebSocket connections.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getActiveConnectionCount(): number;
|
|
12
|
+
/**
|
|
13
|
+
* Get all connected WS clients (excluding the given one).
|
|
14
|
+
* Used by settings broadcast to notify other connections of changes.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getOtherClients(excludeWs?: WebSocket): Array<{
|
|
17
|
+
ws: WebSocket;
|
|
18
|
+
state: ConnectionState;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function safeSend(ws: WebSocket, message: ServerMessage): void;
|
|
21
|
+
export declare function handleConnection(ws: WebSocket, req: import("http").IncomingMessage, ctx: WebSocketHandlerContext): void;
|
|
22
|
+
/**
|
|
23
|
+
* Create an `approveTool` callback for the server adapter.
|
|
24
|
+
* Sends a `tool_approval_request` to the client and waits for a
|
|
25
|
+
* `tool_approval_response`. Falls back to auto-deny on timeout.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createServerApproveTool(ws: WebSocket): import("../../core/types.js").ApproveToolFn;
|
|
28
|
+
/**
|
|
29
|
+
* Close all active connections and clear the registry.
|
|
30
|
+
* Used by closeWebSocket() during shutdown.
|
|
31
|
+
*/
|
|
32
|
+
export declare function closeAllConnections(): void;
|