@dobby.ai/dobby 0.1.0 → 0.1.2

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.
Files changed (156) hide show
  1. package/README.md +84 -39
  2. package/dist/src/agent/event-forwarder.js +185 -16
  3. package/dist/src/cli/commands/cron.js +39 -35
  4. package/dist/src/cli/commands/doctor.js +81 -2
  5. package/dist/src/cli/commands/extension.js +3 -1
  6. package/dist/src/cli/commands/init.js +43 -173
  7. package/dist/src/cli/commands/topology.js +38 -14
  8. package/dist/src/cli/program.js +15 -137
  9. package/dist/src/cli/shared/config-io.js +3 -31
  10. package/dist/src/cli/shared/config-mutators.js +33 -9
  11. package/dist/src/cli/shared/configure-sections.js +52 -12
  12. package/dist/src/cli/shared/init-catalog.js +89 -46
  13. package/dist/src/cli/shared/local-extension-specs.js +85 -0
  14. package/dist/src/cli/shared/schema-prompts.js +26 -2
  15. package/dist/src/core/gateway.js +3 -1
  16. package/dist/src/core/routing.js +53 -38
  17. package/dist/src/core/types.js +2 -0
  18. package/dist/src/cron/config.js +2 -2
  19. package/dist/src/cron/service.js +87 -23
  20. package/dist/src/cron/store.js +1 -1
  21. package/dist/src/main.js +0 -0
  22. package/dist/src/shared/dobby-repo.js +40 -0
  23. package/package.json +11 -4
  24. package/.env.example +0 -9
  25. package/AGENTS.md +0 -267
  26. package/ROADMAP.md +0 -34
  27. package/config/cron.example.json +0 -9
  28. package/config/gateway.example.json +0 -128
  29. package/config/models.custom.example.json +0 -27
  30. package/dist/src/agent/tests/event-forwarder.test.js +0 -113
  31. package/dist/src/cli/shared/config-path.js +0 -207
  32. package/dist/src/cli/shared/init-models-file.js +0 -65
  33. package/dist/src/cli/shared/presets.js +0 -86
  34. package/dist/src/cli/tests/config-command.test.js +0 -42
  35. package/dist/src/cli/tests/config-io.test.js +0 -64
  36. package/dist/src/cli/tests/config-mutators.test.js +0 -47
  37. package/dist/src/cli/tests/config-path.test.js +0 -21
  38. package/dist/src/cli/tests/discord-config.test.js +0 -23
  39. package/dist/src/cli/tests/doctor.test.js +0 -107
  40. package/dist/src/cli/tests/init-catalog.test.js +0 -87
  41. package/dist/src/cli/tests/presets.test.js +0 -41
  42. package/dist/src/cli/tests/program-options.test.js +0 -92
  43. package/dist/src/cli/tests/routing-config.test.js +0 -199
  44. package/dist/src/cli/tests/routing-legacy.test.js +0 -191
  45. package/dist/src/core/tests/control-command.test.js +0 -17
  46. package/dist/src/core/tests/gateway-update-strategy.test.js +0 -167
  47. package/dist/src/core/tests/runtime-registry.test.js +0 -116
  48. package/dist/src/core/tests/typing-controller.test.js +0 -103
  49. package/docs/BOXLITE_SANDBOX_FEASIBILITY.md +0 -175
  50. package/docs/CRON_SCHEDULER_DESIGN.md +0 -374
  51. package/docs/DOCKER_SANDBOX_vs_BOXLITE.md +0 -77
  52. package/docs/EXTENSION_SYSTEM_ARCHITECTURE.md +0 -119
  53. package/docs/MVP.md +0 -135
  54. package/docs/RUNBOOK.md +0 -242
  55. package/docs/TEAMWORK_HANDOFF_DESIGN.md +0 -440
  56. package/plugins/connector-discord/dobby.manifest.json +0 -18
  57. package/plugins/connector-discord/index.js +0 -1
  58. package/plugins/connector-discord/package-lock.json +0 -360
  59. package/plugins/connector-discord/package.json +0 -38
  60. package/plugins/connector-discord/src/connector.ts +0 -350
  61. package/plugins/connector-discord/src/contribution.ts +0 -21
  62. package/plugins/connector-discord/src/mapper.ts +0 -102
  63. package/plugins/connector-discord/tsconfig.json +0 -19
  64. package/plugins/connector-feishu/dobby.manifest.json +0 -18
  65. package/plugins/connector-feishu/index.js +0 -1
  66. package/plugins/connector-feishu/package-lock.json +0 -618
  67. package/plugins/connector-feishu/package.json +0 -38
  68. package/plugins/connector-feishu/src/connector.ts +0 -343
  69. package/plugins/connector-feishu/src/contribution.ts +0 -26
  70. package/plugins/connector-feishu/src/mapper.ts +0 -401
  71. package/plugins/connector-feishu/tsconfig.json +0 -19
  72. package/plugins/plugin-sdk/index.d.ts +0 -261
  73. package/plugins/plugin-sdk/index.js +0 -1
  74. package/plugins/plugin-sdk/package-lock.json +0 -12
  75. package/plugins/plugin-sdk/package.json +0 -22
  76. package/plugins/provider-claude/dobby.manifest.json +0 -17
  77. package/plugins/provider-claude/index.js +0 -1
  78. package/plugins/provider-claude/package-lock.json +0 -3398
  79. package/plugins/provider-claude/package.json +0 -39
  80. package/plugins/provider-claude/src/contribution.ts +0 -1018
  81. package/plugins/provider-claude/tsconfig.json +0 -19
  82. package/plugins/provider-claude-cli/dobby.manifest.json +0 -17
  83. package/plugins/provider-claude-cli/index.js +0 -1
  84. package/plugins/provider-claude-cli/package-lock.json +0 -2898
  85. package/plugins/provider-claude-cli/package.json +0 -38
  86. package/plugins/provider-claude-cli/src/contribution.ts +0 -1673
  87. package/plugins/provider-claude-cli/tsconfig.json +0 -19
  88. package/plugins/provider-pi/dobby.manifest.json +0 -17
  89. package/plugins/provider-pi/index.js +0 -1
  90. package/plugins/provider-pi/package-lock.json +0 -3877
  91. package/plugins/provider-pi/package.json +0 -40
  92. package/plugins/provider-pi/src/contribution.ts +0 -476
  93. package/plugins/provider-pi/tsconfig.json +0 -19
  94. package/plugins/sandbox-core/boxlite.js +0 -1
  95. package/plugins/sandbox-core/dobby.manifest.json +0 -17
  96. package/plugins/sandbox-core/docker.js +0 -1
  97. package/plugins/sandbox-core/package-lock.json +0 -136
  98. package/plugins/sandbox-core/package.json +0 -39
  99. package/plugins/sandbox-core/src/boxlite-context.ts +0 -2
  100. package/plugins/sandbox-core/src/boxlite-contribution.ts +0 -53
  101. package/plugins/sandbox-core/src/boxlite-executor.ts +0 -911
  102. package/plugins/sandbox-core/src/docker-contribution.ts +0 -43
  103. package/plugins/sandbox-core/src/docker-executor.ts +0 -217
  104. package/plugins/sandbox-core/tsconfig.json +0 -19
  105. package/scripts/local-extensions.mjs +0 -168
  106. package/src/agent/event-forwarder.ts +0 -414
  107. package/src/cli/commands/config.ts +0 -328
  108. package/src/cli/commands/configure.ts +0 -92
  109. package/src/cli/commands/cron.ts +0 -410
  110. package/src/cli/commands/doctor.ts +0 -230
  111. package/src/cli/commands/extension.ts +0 -205
  112. package/src/cli/commands/init.ts +0 -396
  113. package/src/cli/commands/start.ts +0 -223
  114. package/src/cli/commands/topology.ts +0 -383
  115. package/src/cli/index.ts +0 -9
  116. package/src/cli/program.ts +0 -465
  117. package/src/cli/shared/config-io.ts +0 -277
  118. package/src/cli/shared/config-mutators.ts +0 -440
  119. package/src/cli/shared/config-schema.ts +0 -228
  120. package/src/cli/shared/config-types.ts +0 -121
  121. package/src/cli/shared/configure-sections.ts +0 -551
  122. package/src/cli/shared/discord-config.ts +0 -14
  123. package/src/cli/shared/init-catalog.ts +0 -189
  124. package/src/cli/shared/init-models-file.ts +0 -77
  125. package/src/cli/shared/runtime.ts +0 -33
  126. package/src/cli/shared/schema-prompts.ts +0 -414
  127. package/src/cli/tests/config-command.test.ts +0 -56
  128. package/src/cli/tests/config-io.test.ts +0 -92
  129. package/src/cli/tests/config-mutators.test.ts +0 -59
  130. package/src/cli/tests/doctor.test.ts +0 -120
  131. package/src/cli/tests/init-catalog.test.ts +0 -96
  132. package/src/cli/tests/program-options.test.ts +0 -113
  133. package/src/cli/tests/routing-config.test.ts +0 -209
  134. package/src/core/control-command.ts +0 -12
  135. package/src/core/dedup-store.ts +0 -103
  136. package/src/core/gateway.ts +0 -607
  137. package/src/core/routing.ts +0 -379
  138. package/src/core/runtime-registry.ts +0 -141
  139. package/src/core/tests/control-command.test.ts +0 -20
  140. package/src/core/tests/runtime-registry.test.ts +0 -140
  141. package/src/core/tests/typing-controller.test.ts +0 -129
  142. package/src/core/types.ts +0 -318
  143. package/src/core/typing-controller.ts +0 -119
  144. package/src/cron/config.ts +0 -154
  145. package/src/cron/schedule.ts +0 -61
  146. package/src/cron/service.ts +0 -249
  147. package/src/cron/store.ts +0 -155
  148. package/src/cron/types.ts +0 -60
  149. package/src/extension/loader.ts +0 -145
  150. package/src/extension/manager.ts +0 -355
  151. package/src/extension/manifest.ts +0 -26
  152. package/src/extension/registry.ts +0 -229
  153. package/src/main.ts +0 -8
  154. package/src/sandbox/executor.ts +0 -44
  155. package/src/sandbox/host-executor.ts +0 -118
  156. package/tsconfig.json +0 -18
@@ -1,379 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs";
2
- import { readFile } from "node:fs/promises";
3
- import { homedir } from "node:os";
4
- import { dirname, isAbsolute, resolve } from "node:path";
5
- import { z } from "zod";
6
- import { BUILTIN_HOST_SANDBOX_ID } from "./types.js";
7
- import type {
8
- BindingConfig,
9
- BindingResolution,
10
- BindingSource,
11
- ConnectorsConfig,
12
- ExtensionInstanceConfig,
13
- ExtensionsConfig,
14
- GatewayConfig,
15
- ProvidersConfig,
16
- RouteDefaultsConfig,
17
- RouteProfile,
18
- RouteResolution,
19
- RoutesConfig,
20
- SandboxesConfig,
21
- } from "./types.js";
22
-
23
- const extensionItemSchema = z.object({
24
- type: z.string().trim().min(1),
25
- }).catchall(z.unknown());
26
-
27
- const routeDefaultsSchema = z.object({
28
- provider: z.string().trim().min(1).optional(),
29
- sandbox: z.string().trim().min(1).optional(),
30
- tools: z.enum(["full", "readonly"]).optional(),
31
- mentions: z.enum(["required", "optional"]).optional(),
32
- }).strict();
33
-
34
- const routeItemSchema = z.object({
35
- projectRoot: z.string().trim().min(1),
36
- tools: z.enum(["full", "readonly"]).optional(),
37
- mentions: z.enum(["required", "optional"]).optional(),
38
- provider: z.string().trim().min(1).optional(),
39
- sandbox: z.string().trim().min(1).optional(),
40
- systemPromptFile: z
41
- .string()
42
- .optional()
43
- .transform((value) => (value && value.trim().length > 0 ? value : undefined)),
44
- }).strict();
45
-
46
- const bindingSourceSchema = z.object({
47
- type: z.enum(["channel", "chat"]),
48
- id: z.string().trim().min(1),
49
- }).strict();
50
-
51
- const bindingItemSchema = z.object({
52
- connector: z.string().trim().min(1),
53
- source: bindingSourceSchema,
54
- route: z.string().trim().min(1),
55
- }).strict();
56
-
57
- const gatewayConfigSchema = z.object({
58
- extensions: z.object({
59
- allowList: z
60
- .array(
61
- z.object({
62
- package: z.string().trim().min(1),
63
- enabled: z.boolean().default(true),
64
- }).strict(),
65
- )
66
- .default([]),
67
- }).strict(),
68
- providers: z.object({
69
- default: z.string().trim().min(1),
70
- items: z.record(z.string(), extensionItemSchema),
71
- }).strict(),
72
- connectors: z.object({
73
- items: z.record(z.string(), extensionItemSchema),
74
- }).strict(),
75
- sandboxes: z.object({
76
- default: z.string().trim().min(1).optional(),
77
- items: z.record(z.string(), extensionItemSchema).default({}),
78
- }).strict(),
79
- routes: z.object({
80
- defaults: routeDefaultsSchema.default({}),
81
- items: z.record(z.string(), routeItemSchema),
82
- }).strict(),
83
- bindings: z.object({
84
- items: z.record(z.string(), bindingItemSchema).default({}),
85
- }).strict(),
86
- data: z.object({
87
- rootDir: z.string().default("./data"),
88
- dedupTtlMs: z.number().int().positive().default(7 * 24 * 60 * 60 * 1000),
89
- }).strict(),
90
- }).strict();
91
-
92
- type ParsedGatewayConfig = z.infer<typeof gatewayConfigSchema>;
93
- type ParsedRouteItem = z.infer<typeof routeItemSchema>;
94
- type ParsedExtensionItem = z.infer<typeof extensionItemSchema>;
95
-
96
- const FORBIDDEN_CONNECTOR_CONFIG_KEYS: Record<string, string> = {
97
- botChannelMap: "Use bindings.items to map connector sources to routes.",
98
- chatRouteMap: "Use bindings.items to map connector sources to routes.",
99
- botTokenEnv: "Set botToken directly in connector config or inject it before the config is loaded.",
100
- };
101
-
102
- function isDobbyRepoRoot(candidateDir: string): boolean {
103
- const packageJsonPath = resolve(candidateDir, "package.json");
104
- const repoConfigPath = resolve(candidateDir, "config", "gateway.json");
105
- const localExtensionsScriptPath = resolve(candidateDir, "scripts", "local-extensions.mjs");
106
-
107
- if (!existsSync(packageJsonPath) || !existsSync(repoConfigPath) || !existsSync(localExtensionsScriptPath)) {
108
- return false;
109
- }
110
-
111
- try {
112
- const packageJsonRaw = readFileSync(packageJsonPath, "utf-8");
113
- const parsed = JSON.parse(packageJsonRaw) as { name?: unknown };
114
- return parsed.name === "dobby";
115
- } catch {
116
- return false;
117
- }
118
- }
119
-
120
- function resolveConfigBaseDir(configPath: string): string {
121
- const absoluteConfigPath = resolve(configPath);
122
- const configDir = dirname(absoluteConfigPath);
123
- const repoRoot = dirname(configDir);
124
-
125
- if (absoluteConfigPath === resolve(repoRoot, "config", "gateway.json") && isDobbyRepoRoot(repoRoot)) {
126
- return repoRoot;
127
- }
128
-
129
- return configDir;
130
- }
131
-
132
- function resolveMaybeAbsolute(baseDir: string, value: string): string {
133
- const expanded = expandHome(value);
134
- return isAbsolute(expanded) ? resolve(expanded) : resolve(baseDir, expanded);
135
- }
136
-
137
- function expandHome(value: string): string {
138
- if (value === "~") {
139
- return homedir();
140
- }
141
-
142
- if (value.startsWith("~/") || value.startsWith("~\\")) {
143
- return resolve(homedir(), value.slice(2));
144
- }
145
-
146
- return value;
147
- }
148
-
149
- function normalizeInstanceItem(item: ParsedExtensionItem): ExtensionInstanceConfig {
150
- const { type, ...config } = item;
151
- return {
152
- type,
153
- config,
154
- };
155
- }
156
-
157
- function normalizeInstances(parsedItems: Record<string, ParsedExtensionItem>): Record<string, ExtensionInstanceConfig> {
158
- const normalized: Record<string, ExtensionInstanceConfig> = {};
159
- for (const [id, item] of Object.entries(parsedItems)) {
160
- normalized[id] = normalizeInstanceItem(item);
161
- }
162
- return normalized;
163
- }
164
-
165
- function normalizeExtensions(parsed: ParsedGatewayConfig["extensions"]): ExtensionsConfig {
166
- return {
167
- allowList: parsed.allowList.map((item) => ({
168
- package: item.package,
169
- enabled: item.enabled,
170
- })),
171
- };
172
- }
173
-
174
- function normalizeProviders(parsed: ParsedGatewayConfig["providers"]): ProvidersConfig {
175
- return {
176
- default: parsed.default,
177
- items: normalizeInstances(parsed.items),
178
- };
179
- }
180
-
181
- function normalizeConnectors(parsed: ParsedGatewayConfig["connectors"]): ConnectorsConfig {
182
- return {
183
- items: normalizeInstances(parsed.items),
184
- };
185
- }
186
-
187
- function normalizeSandboxes(parsed: ParsedGatewayConfig["sandboxes"]): SandboxesConfig {
188
- return {
189
- ...(parsed.default ? { default: parsed.default } : {}),
190
- items: normalizeInstances(parsed.items),
191
- };
192
- }
193
-
194
- function normalizeRouteProfile(
195
- baseDir: string,
196
- profile: ParsedRouteItem,
197
- defaults: RouteDefaultsConfig,
198
- ): RouteProfile {
199
- const normalized: RouteProfile = {
200
- projectRoot: resolveMaybeAbsolute(baseDir, profile.projectRoot),
201
- tools: profile.tools ?? defaults.tools,
202
- mentions: profile.mentions ?? defaults.mentions,
203
- provider: profile.provider ?? defaults.provider,
204
- sandbox: profile.sandbox ?? defaults.sandbox,
205
- };
206
-
207
- if (profile.systemPromptFile) {
208
- normalized.systemPromptFile = resolveMaybeAbsolute(baseDir, profile.systemPromptFile);
209
- }
210
-
211
- return normalized;
212
- }
213
-
214
- function normalizeRoutes(parsed: ParsedGatewayConfig["routes"], baseDir: string, defaults: RouteDefaultsConfig): RoutesConfig {
215
- const items: Record<string, RouteProfile> = {};
216
- for (const [routeId, profile] of Object.entries(parsed.items)) {
217
- items[routeId] = normalizeRouteProfile(baseDir, profile, defaults);
218
- }
219
-
220
- return {
221
- defaults,
222
- items,
223
- };
224
- }
225
-
226
- function normalizeBindings(parsed: ParsedGatewayConfig["bindings"]): GatewayConfig["bindings"] {
227
- const items: Record<string, BindingConfig> = {};
228
- for (const [bindingId, binding] of Object.entries(parsed.items)) {
229
- items[bindingId] = {
230
- connector: binding.connector,
231
- source: {
232
- type: binding.source.type,
233
- id: binding.source.id,
234
- },
235
- route: binding.route,
236
- };
237
- }
238
-
239
- return { items };
240
- }
241
-
242
- function validateConnectorConfigKeys(parsed: ParsedGatewayConfig["connectors"]): void {
243
- for (const [instanceId, item] of Object.entries(parsed.items)) {
244
- for (const [key, message] of Object.entries(FORBIDDEN_CONNECTOR_CONFIG_KEYS)) {
245
- if (Object.prototype.hasOwnProperty.call(item, key)) {
246
- throw new Error(`connectors.items['${instanceId}'] must not include '${key}'. ${message}`);
247
- }
248
- }
249
- }
250
- }
251
-
252
- function validateReferences(parsed: ParsedGatewayConfig, normalizedRoutes: RoutesConfig): void {
253
- if (!parsed.providers.items[parsed.providers.default]) {
254
- throw new Error(`providers.default '${parsed.providers.default}' does not exist in providers.items`);
255
- }
256
-
257
- const defaultSandbox = parsed.sandboxes.default ?? BUILTIN_HOST_SANDBOX_ID;
258
- if (defaultSandbox !== BUILTIN_HOST_SANDBOX_ID && !parsed.sandboxes.items[defaultSandbox]) {
259
- throw new Error(`sandboxes.default '${defaultSandbox}' does not exist in sandboxes.items`);
260
- }
261
-
262
- const resolvedDefaults: RouteDefaultsConfig = {
263
- provider: parsed.routes.defaults.provider ?? parsed.providers.default,
264
- sandbox: parsed.routes.defaults.sandbox ?? parsed.sandboxes.default ?? BUILTIN_HOST_SANDBOX_ID,
265
- tools: parsed.routes.defaults.tools ?? "full",
266
- mentions: parsed.routes.defaults.mentions ?? "required",
267
- };
268
-
269
- if (!parsed.providers.items[resolvedDefaults.provider]) {
270
- throw new Error(`routes.defaults.provider references unknown provider '${resolvedDefaults.provider}'`);
271
- }
272
- if (resolvedDefaults.sandbox !== BUILTIN_HOST_SANDBOX_ID && !parsed.sandboxes.items[resolvedDefaults.sandbox]) {
273
- throw new Error(`routes.defaults.sandbox references unknown sandbox '${resolvedDefaults.sandbox}'`);
274
- }
275
-
276
- for (const [routeId, profile] of Object.entries(normalizedRoutes.items)) {
277
- if (!parsed.providers.items[profile.provider]) {
278
- throw new Error(`routes.items['${routeId}'].provider references unknown provider '${profile.provider}'`);
279
- }
280
- if (profile.sandbox !== BUILTIN_HOST_SANDBOX_ID && !parsed.sandboxes.items[profile.sandbox]) {
281
- throw new Error(`routes.items['${routeId}'].sandbox references unknown sandbox '${profile.sandbox}'`);
282
- }
283
- }
284
-
285
- const seenSources = new Map<string, string>();
286
- for (const [bindingId, binding] of Object.entries(parsed.bindings.items)) {
287
- if (!parsed.connectors.items[binding.connector]) {
288
- throw new Error(`bindings.items['${bindingId}'].connector references unknown connector '${binding.connector}'`);
289
- }
290
- if (!normalizedRoutes.items[binding.route]) {
291
- throw new Error(`bindings.items['${bindingId}'].route references unknown route '${binding.route}'`);
292
- }
293
-
294
- const bindingKey = `${binding.connector}:${binding.source.type}:${binding.source.id}`;
295
- const existingBindingId = seenSources.get(bindingKey);
296
- if (existingBindingId) {
297
- throw new Error(
298
- `bindings.items['${bindingId}'] duplicates source '${bindingKey}' already used by bindings.items['${existingBindingId}']`,
299
- );
300
- }
301
- seenSources.set(bindingKey, bindingId);
302
- }
303
- }
304
-
305
- export async function loadGatewayConfig(configPath: string): Promise<GatewayConfig> {
306
- const absoluteConfigPath = resolve(configPath);
307
- const configBaseDir = resolveConfigBaseDir(absoluteConfigPath);
308
- const raw = await readFile(absoluteConfigPath, "utf-8");
309
- const parsed = gatewayConfigSchema.parse(JSON.parse(raw) as unknown);
310
- validateConnectorConfigKeys(parsed.connectors);
311
-
312
- const routeDefaults: RouteDefaultsConfig = {
313
- provider: parsed.routes.defaults.provider ?? parsed.providers.default,
314
- sandbox: parsed.routes.defaults.sandbox ?? parsed.sandboxes.default ?? BUILTIN_HOST_SANDBOX_ID,
315
- tools: parsed.routes.defaults.tools ?? "full",
316
- mentions: parsed.routes.defaults.mentions ?? "required",
317
- };
318
-
319
- const normalizedRoutes = normalizeRoutes(parsed.routes, configBaseDir, routeDefaults);
320
- validateReferences(parsed, normalizedRoutes);
321
-
322
- const rootDir = resolveMaybeAbsolute(configBaseDir, parsed.data.rootDir);
323
-
324
- return {
325
- extensions: normalizeExtensions(parsed.extensions),
326
- providers: normalizeProviders(parsed.providers),
327
- connectors: normalizeConnectors(parsed.connectors),
328
- sandboxes: normalizeSandboxes(parsed.sandboxes),
329
- routes: normalizedRoutes,
330
- bindings: normalizeBindings(parsed.bindings),
331
- data: {
332
- rootDir,
333
- sessionsDir: resolve(rootDir, "sessions"),
334
- attachmentsDir: resolve(rootDir, "attachments"),
335
- logsDir: resolve(rootDir, "logs"),
336
- stateDir: resolve(rootDir, "state"),
337
- dedupTtlMs: parsed.data.dedupTtlMs,
338
- },
339
- };
340
- }
341
-
342
- export class RouteResolver {
343
- constructor(private readonly routes: RoutesConfig) {}
344
-
345
- resolve(routeId: string): RouteResolution | null {
346
- const normalizedRouteId = routeId.trim();
347
- if (!normalizedRouteId) return null;
348
-
349
- const profile = this.routes.items[normalizedRouteId];
350
- if (!profile) return null;
351
-
352
- return { routeId: normalizedRouteId, profile };
353
- }
354
- }
355
-
356
- export class BindingResolver {
357
- private readonly bindingsBySource = new Map<string, BindingResolution>();
358
-
359
- constructor(bindings: GatewayConfig["bindings"]) {
360
- for (const [bindingId, binding] of Object.entries(bindings.items)) {
361
- this.bindingsBySource.set(this.buildKey(binding.connector, binding.source), {
362
- bindingId,
363
- config: binding,
364
- });
365
- }
366
- }
367
-
368
- resolve(connectorId: string, source: BindingSource): BindingResolution | null {
369
- if (!connectorId.trim() || !source.id.trim()) {
370
- return null;
371
- }
372
-
373
- return this.bindingsBySource.get(this.buildKey(connectorId, source)) ?? null;
374
- }
375
-
376
- private buildKey(connectorId: string, source: BindingSource): string {
377
- return `${connectorId.trim()}:${source.type}:${source.id.trim()}`;
378
- }
379
- }
@@ -1,141 +0,0 @@
1
- import type { ConversationRuntime, GatewayLogger } from "./types.js";
2
-
3
- interface RuntimeEntry {
4
- runtime: ConversationRuntime | undefined;
5
- tail: Promise<void>;
6
- epoch: number;
7
- scheduledTasks: number;
8
- }
9
-
10
- export class RuntimeRegistry {
11
- private readonly entries = new Map<string, RuntimeEntry>();
12
-
13
- constructor(private readonly logger: GatewayLogger) {}
14
-
15
- async run(
16
- key: string,
17
- createFn: () => Promise<ConversationRuntime>,
18
- task: (runtime: ConversationRuntime) => Promise<void>,
19
- ): Promise<void> {
20
- const entry = this.getOrCreateEntry(key);
21
- const scheduledEpoch = entry.epoch;
22
- entry.scheduledTasks += 1;
23
- const run = entry.tail.then(async () => {
24
- if (scheduledEpoch !== entry.epoch) return;
25
-
26
- let runtime = entry.runtime;
27
- if (!runtime) {
28
- const created = await createFn();
29
- if (scheduledEpoch !== entry.epoch) {
30
- await this.closeRuntime(key, created, "Discarding runtime created for stale queued task");
31
- return;
32
- }
33
-
34
- entry.runtime = created;
35
- runtime = created;
36
- }
37
-
38
- await task(runtime);
39
- });
40
-
41
- const managedRun = run.finally(() => {
42
- entry.scheduledTasks = Math.max(0, entry.scheduledTasks - 1);
43
- });
44
-
45
- this.attachTail(key, entry, managedRun, "Queued task failed");
46
- await managedRun;
47
- }
48
-
49
- async abort(key: string): Promise<boolean> {
50
- const entry = this.entries.get(key);
51
- if (!entry?.runtime) return false;
52
- return this.abortRuntime(key, entry.runtime, "Failed to abort runtime");
53
- }
54
-
55
- async cancel(key: string): Promise<boolean> {
56
- const entry = this.entries.get(key);
57
- if (!entry) return false;
58
- if (entry.scheduledTasks === 0) return false;
59
-
60
- entry.epoch += 1;
61
- if (!entry.runtime) return true;
62
- return this.abortRuntime(key, entry.runtime, "Failed to cancel runtime");
63
- }
64
-
65
- async reset(key: string): Promise<boolean> {
66
- const entry = this.entries.get(key);
67
- if (!entry) return false;
68
-
69
- entry.epoch += 1;
70
- if (entry.runtime) {
71
- await this.abortRuntime(key, entry.runtime, "Failed to abort runtime during reset");
72
- }
73
-
74
- const close = entry.tail.then(async () => {
75
- const runtime = entry.runtime;
76
- entry.runtime = undefined;
77
- if (!runtime) return;
78
- await this.closeRuntime(key, runtime, "Failed to close runtime during reset");
79
- });
80
-
81
- this.attachTail(key, entry, close);
82
- await close;
83
- return true;
84
- }
85
-
86
- async closeAll(): Promise<void> {
87
- const keys = [...this.entries.keys()];
88
- await Promise.all(keys.map((key) => this.reset(key)));
89
- this.entries.clear();
90
- }
91
-
92
- private getOrCreateEntry(key: string): RuntimeEntry {
93
- const existing = this.entries.get(key);
94
- if (existing) return existing;
95
-
96
- const entry: RuntimeEntry = {
97
- runtime: undefined,
98
- tail: Promise.resolve(),
99
- epoch: 0,
100
- scheduledTasks: 0,
101
- };
102
- this.entries.set(key, entry);
103
- return entry;
104
- }
105
-
106
- private attachTail(
107
- key: string,
108
- entry: RuntimeEntry,
109
- run: Promise<void>,
110
- errorMessage = "Queued task failed",
111
- ): void {
112
- const nextTail = run.catch((error) => {
113
- this.logger.error({ err: error, conversationKey: key }, errorMessage);
114
- });
115
- entry.tail = nextTail;
116
- void nextTail.finally(() => {
117
- if (this.entries.get(key) !== entry) return;
118
- if (entry.runtime !== undefined) return;
119
- if (entry.tail !== nextTail) return;
120
- this.entries.delete(key);
121
- });
122
- }
123
-
124
- private async abortRuntime(key: string, runtime: ConversationRuntime, errorMessage: string): Promise<boolean> {
125
- try {
126
- await runtime.runtime.abort();
127
- return true;
128
- } catch (error) {
129
- this.logger.error({ err: error, conversationKey: key }, errorMessage);
130
- return false;
131
- }
132
- }
133
-
134
- private async closeRuntime(key: string, runtime: ConversationRuntime, errorMessage: string): Promise<void> {
135
- try {
136
- await runtime.close();
137
- } catch (error) {
138
- this.logger.error({ err: error, conversationKey: key }, errorMessage);
139
- }
140
- }
141
- }
@@ -1,20 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { parseControlCommand } from "../control-command.js";
4
-
5
- test("parseControlCommand recognizes cancel aliases", () => {
6
- assert.equal(parseControlCommand("stop"), "cancel");
7
- assert.equal(parseControlCommand(" /STOP "), "cancel");
8
- assert.equal(parseControlCommand("/cancel"), "cancel");
9
- });
10
-
11
- test("parseControlCommand recognizes new session aliases", () => {
12
- assert.equal(parseControlCommand("/new"), "new_session");
13
- assert.equal(parseControlCommand(" /reset "), "new_session");
14
- });
15
-
16
- test("parseControlCommand ignores regular messages", () => {
17
- assert.equal(parseControlCommand("please /new"), null);
18
- assert.equal(parseControlCommand(""), null);
19
- assert.equal(parseControlCommand("hello"), null);
20
- });
@@ -1,140 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { RuntimeRegistry } from "../runtime-registry.js";
4
- import type { ConversationRuntime, GatewayLogger } from "../types.js";
5
-
6
- function deferred<T = void>(): { promise: Promise<T>; resolve: (value: T) => void } {
7
- let resolve!: (value: T) => void;
8
- const promise = new Promise<T>((res) => {
9
- resolve = res;
10
- });
11
- return { promise, resolve };
12
- }
13
-
14
- function createLogger(): GatewayLogger {
15
- return {
16
- error() {},
17
- warn() {},
18
- info() {},
19
- debug() {},
20
- } as unknown as GatewayLogger;
21
- }
22
-
23
- function createConversationRuntime(id: string, abortCalls: string[], closeCalls: string[]): ConversationRuntime {
24
- return {
25
- key: id,
26
- routeId: "route.main",
27
- route: {
28
- projectRoot: "/tmp/project",
29
- tools: "full",
30
- mentions: "required",
31
- provider: "provider.main",
32
- sandbox: "host.builtin",
33
- },
34
- providerId: "provider.main",
35
- sandboxId: "host.builtin",
36
- runtime: {
37
- async prompt() {},
38
- subscribe() {
39
- return () => {};
40
- },
41
- async abort() {
42
- abortCalls.push(id);
43
- },
44
- dispose() {},
45
- },
46
- async close() {
47
- closeCalls.push(id);
48
- },
49
- };
50
- }
51
-
52
- test("cancel aborts the active run and drops queued turns", async () => {
53
- const registry = new RuntimeRegistry(createLogger());
54
- const abortCalls: string[] = [];
55
- const closeCalls: string[] = [];
56
- let runtimeCount = 0;
57
- const createRuntime = async (): Promise<ConversationRuntime> => {
58
- runtimeCount += 1;
59
- return createConversationRuntime(`runtime-${runtimeCount}`, abortCalls, closeCalls);
60
- };
61
-
62
- const firstStarted = deferred();
63
- const releaseFirst = deferred();
64
- const started: string[] = [];
65
-
66
- const firstTurn = registry.run("conversation", createRuntime, async (runtime) => {
67
- started.push(runtime.key);
68
- firstStarted.resolve(undefined);
69
- await releaseFirst.promise;
70
- });
71
-
72
- await firstStarted.promise;
73
-
74
- const secondTurn = registry.run("conversation", createRuntime, async (runtime) => {
75
- started.push(runtime.key);
76
- });
77
- const thirdTurn = registry.run("conversation", createRuntime, async (runtime) => {
78
- started.push(runtime.key);
79
- });
80
-
81
- assert.equal(await registry.cancel("conversation"), true);
82
- assert.deepEqual(abortCalls, ["runtime-1"]);
83
-
84
- releaseFirst.resolve(undefined);
85
- await Promise.all([firstTurn, secondTurn, thirdTurn]);
86
-
87
- assert.deepEqual(started, ["runtime-1"]);
88
- assert.deepEqual(closeCalls, []);
89
-
90
- await registry.run("conversation", createRuntime, async (runtime) => {
91
- started.push(runtime.key);
92
- });
93
-
94
- assert.deepEqual(started, ["runtime-1", "runtime-1"]);
95
- assert.equal(runtimeCount, 1);
96
- });
97
-
98
- test("reset closes the current runtime and recreates it on the next turn", async () => {
99
- const registry = new RuntimeRegistry(createLogger());
100
- const abortCalls: string[] = [];
101
- const closeCalls: string[] = [];
102
- let runtimeCount = 0;
103
- const createRuntime = async (): Promise<ConversationRuntime> => {
104
- runtimeCount += 1;
105
- return createConversationRuntime(`runtime-${runtimeCount}`, abortCalls, closeCalls);
106
- };
107
-
108
- const firstStarted = deferred();
109
- const releaseFirst = deferred();
110
- const started: string[] = [];
111
-
112
- const firstTurn = registry.run("conversation", createRuntime, async (runtime) => {
113
- started.push(runtime.key);
114
- firstStarted.resolve(undefined);
115
- await releaseFirst.promise;
116
- });
117
-
118
- await firstStarted.promise;
119
-
120
- const queuedTurn = registry.run("conversation", createRuntime, async (runtime) => {
121
- started.push(runtime.key);
122
- });
123
-
124
- const resetPromise = registry.reset("conversation");
125
- releaseFirst.resolve(undefined);
126
-
127
- assert.equal(await resetPromise, true);
128
- await Promise.all([firstTurn, queuedTurn]);
129
-
130
- assert.deepEqual(abortCalls, ["runtime-1"]);
131
- assert.deepEqual(closeCalls, ["runtime-1"]);
132
- assert.deepEqual(started, ["runtime-1"]);
133
-
134
- await registry.run("conversation", createRuntime, async (runtime) => {
135
- started.push(runtime.key);
136
- });
137
-
138
- assert.deepEqual(started, ["runtime-1", "runtime-2"]);
139
- assert.equal(runtimeCount, 2);
140
- });