summaryrefslogtreecommitdiffhomepage
path: root/packages/transport-http/src/extension.ts
blob: 4ab43ce8a4a5d8eae1740123942965ef21f86480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { Extension, HostAPI, Manifest } from "@dispatch/kernel";
import { createApp } from "./app.js";
import {
	type ComputerService,
	cacheWarmHandle,
	compactionHandle,
	computerServiceHandle,
	conversationStoreHandle,
	credentialStoreHandle,
	lspServiceHandle,
	mcpServiceHandle,
	sessionOrchestratorHandle,
	systemPromptHandle,
	throughputStoreHandle,
} from "./seam.js";

export const manifest: Manifest = {
	id: "transport-http",
	name: "Transport HTTP",
	version: "0.0.0",
	apiVersion: "^0.1.0",
	trust: "bundled",
	dependsOn: [
		"conversation-store",
		"credential-store",
		"lsp",
		"mcp",
		"session-orchestrator",
		"throughput-store",
	],
	capabilities: { network: true },
	contributes: {
		routes: [
			"/chat",
			"/chat/warm",
			"/computers",
			"/computers/:alias",
			"/computers/:alias/status",
			"/computers/:alias/test",
			"/conversations",
			"/conversations/:id",
			"/conversations/:id/close",
			"/conversations/:id/compact",
			"/conversations/:id/compact-percent",
			"/conversations/:id/computer",
			"/conversations/:id/cwd",
			"/conversations/:id/last",
			"/conversations/:id/lsp",
			"/conversations/:id/mcp",
			"/conversations/:id/open",
			"/conversations/:id/queue",
			"/conversations/:id/reasoning-effort",
			"/conversations/:id/status",
			"/conversations/:id/stop",
			"/conversations/:id/title",
			"/health",
			"/models",
			"/metrics/throughput",
			"/system-prompt",
			"/system-prompt/variables",
			"/workspaces",
			"/workspaces/:id",
			"/workspaces/:id/title",
			"/workspaces/:id/default-cwd",
			"/workspaces/:id/default-computer",
		],
	},
	activation: "eager",
};

export function createTransportHttpExtension(): Extension & {
	readonly _testServer: ReturnType<typeof Bun.serve> | undefined;
} {
	let server: ReturnType<typeof Bun.serve> | undefined;

	return {
		get _testServer() {
			return server;
		},
		manifest,
		async activate(host: HostAPI) {
			const conversationStore = host.getService(conversationStoreHandle);
			const orchestrator = host.getService(sessionOrchestratorHandle);
			const credentialStore = host.getService(credentialStoreHandle);
			const throughputStore = host.getService(throughputStoreHandle);
			const warmService = host.getService(cacheWarmHandle);
			const compactionService = host.getService(compactionHandle);
			const lspService = host.getService(lspServiceHandle);
			const mcpService = host.getService(mcpServiceHandle);
			const systemPromptService = host.getService(systemPromptHandle);
			// Optional: the `ssh` extension provides ComputerService. It is NOT in
			// dependsOn (ssh may be absent), so resolve defensively — when no
			// provider registered the handle, the computer routes degrade to
			// empty/disconnected (see app.ts). Wrapped because getService throws
			// for an unregistered handle.
			let computerService: ComputerService | undefined;
			try {
				computerService = host.getService(computerServiceHandle);
			} catch {
				computerService = undefined;
			}
			const logger = host.logger;

			const app = createApp({
				conversationStore,
				orchestrator,
				credentialStore,
				throughputStore,
				warmService,
				compactionService,
				lspService,
				mcpService,
				systemPromptService,
				...(computerService !== undefined ? { computerService } : {}),
				logger,
				emit: host.emit.bind(host),
				...(process.env.DISPATCH_WEB_DIR !== undefined
					? { webDir: process.env.DISPATCH_WEB_DIR }
					: {}),
			});

			const port = host.config.get<number>("httpPort") ?? 24203;

			server = Bun.serve({
				port,
				fetch: app.fetch,
				idleTimeout: 0,
			});

			logger.info("transport-http: listening", { port });
		},

		deactivate() {
			if (server) {
				server.stop();
				server = undefined;
			}
		},
	};
}