@lobu/gateway 3.0.5 → 3.0.6
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/package.json +2 -2
- package/src/__tests__/agent-config-routes.test.ts +254 -0
- package/src/__tests__/agent-history-routes.test.ts +72 -0
- package/src/__tests__/agent-routes.test.ts +68 -0
- package/src/__tests__/agent-schedules-routes.test.ts +59 -0
- package/src/__tests__/agent-settings-store.test.ts +323 -0
- package/src/__tests__/chat-instance-manager-slack.test.ts +204 -0
- package/src/__tests__/chat-response-bridge.test.ts +131 -0
- package/src/__tests__/config-memory-plugins.test.ts +92 -0
- package/src/__tests__/config-request-store.test.ts +127 -0
- package/src/__tests__/connection-routes.test.ts +144 -0
- package/src/__tests__/core-services-store-selection.test.ts +92 -0
- package/src/__tests__/docker-deployment.test.ts +1211 -0
- package/src/__tests__/embedded-deployment.test.ts +342 -0
- package/src/__tests__/grant-store.test.ts +148 -0
- package/src/__tests__/http-proxy.test.ts +281 -0
- package/src/__tests__/instruction-service.test.ts +37 -0
- package/src/__tests__/link-buttons.test.ts +112 -0
- package/src/__tests__/lobu.test.ts +32 -0
- package/src/__tests__/mcp-config-service.test.ts +347 -0
- package/src/__tests__/mcp-proxy.test.ts +696 -0
- package/src/__tests__/message-handler-bridge.test.ts +17 -0
- package/src/__tests__/model-selection.test.ts +172 -0
- package/src/__tests__/oauth-templates.test.ts +39 -0
- package/src/__tests__/platform-adapter-slack-send.test.ts +114 -0
- package/src/__tests__/platform-helpers-model-resolution.test.ts +253 -0
- package/src/__tests__/provider-inheritance.test.ts +212 -0
- package/src/__tests__/routes/cli-auth.test.ts +337 -0
- package/src/__tests__/routes/interactions.test.ts +121 -0
- package/src/__tests__/secret-proxy.test.ts +85 -0
- package/src/__tests__/session-manager.test.ts +572 -0
- package/src/__tests__/setup.ts +133 -0
- package/src/__tests__/skill-and-mcp-registry.test.ts +203 -0
- package/src/__tests__/slack-routes.test.ts +161 -0
- package/src/__tests__/system-config-resolver.test.ts +75 -0
- package/src/__tests__/system-message-limiter.test.ts +89 -0
- package/src/__tests__/system-skills-service.test.ts +362 -0
- package/src/__tests__/transcription-service.test.ts +222 -0
- package/src/__tests__/utils/rate-limiter.test.ts +102 -0
- package/src/__tests__/worker-connection-manager.test.ts +497 -0
- package/src/__tests__/worker-job-router.test.ts +722 -0
- package/src/api/index.ts +1 -0
- package/src/api/platform.ts +292 -0
- package/src/api/response-renderer.ts +157 -0
- package/src/auth/agent-metadata-store.ts +168 -0
- package/src/auth/api-auth-middleware.ts +69 -0
- package/src/auth/api-key-provider-module.ts +213 -0
- package/src/auth/base-provider-module.ts +201 -0
- package/src/auth/chatgpt/chatgpt-oauth-module.ts +185 -0
- package/src/auth/chatgpt/device-code-client.ts +218 -0
- package/src/auth/chatgpt/index.ts +1 -0
- package/src/auth/claude/oauth-module.ts +280 -0
- package/src/auth/cli/token-service.ts +249 -0
- package/src/auth/external/client.ts +560 -0
- package/src/auth/external/device-code-client.ts +225 -0
- package/src/auth/mcp/config-service.ts +392 -0
- package/src/auth/mcp/proxy.ts +1088 -0
- package/src/auth/mcp/string-substitution.ts +17 -0
- package/src/auth/mcp/tool-cache.ts +90 -0
- package/src/auth/oauth/base-client.ts +267 -0
- package/src/auth/oauth/client.ts +153 -0
- package/src/auth/oauth/credentials.ts +7 -0
- package/src/auth/oauth/providers.ts +69 -0
- package/src/auth/oauth/state-store.ts +150 -0
- package/src/auth/oauth-templates.ts +179 -0
- package/src/auth/provider-catalog.ts +220 -0
- package/src/auth/provider-model-options.ts +41 -0
- package/src/auth/settings/agent-settings-store.ts +565 -0
- package/src/auth/settings/auth-profiles-manager.ts +216 -0
- package/src/auth/settings/index.ts +12 -0
- package/src/auth/settings/model-preference-store.ts +52 -0
- package/src/auth/settings/model-selection.ts +135 -0
- package/src/auth/settings/resolved-settings-view.ts +298 -0
- package/src/auth/settings/template-utils.ts +44 -0
- package/src/auth/settings/token-service.ts +88 -0
- package/src/auth/system-env-store.ts +98 -0
- package/src/auth/user-agents-store.ts +68 -0
- package/src/channels/binding-service.ts +214 -0
- package/src/channels/index.ts +4 -0
- package/src/cli/gateway.ts +1304 -0
- package/src/cli/index.ts +74 -0
- package/src/commands/built-in-commands.ts +80 -0
- package/src/commands/command-dispatcher.ts +94 -0
- package/src/commands/command-reply-adapters.ts +27 -0
- package/src/config/file-loader.ts +618 -0
- package/src/config/index.ts +588 -0
- package/src/config/network-allowlist.ts +71 -0
- package/src/connections/chat-instance-manager.ts +1284 -0
- package/src/connections/chat-response-bridge.ts +618 -0
- package/src/connections/index.ts +7 -0
- package/src/connections/interaction-bridge.ts +831 -0
- package/src/connections/message-handler-bridge.ts +415 -0
- package/src/connections/platform-auth-methods.ts +15 -0
- package/src/connections/types.ts +84 -0
- package/src/gateway/connection-manager.ts +291 -0
- package/src/gateway/index.ts +700 -0
- package/src/gateway/job-router.ts +201 -0
- package/src/gateway-main.ts +200 -0
- package/src/index.ts +41 -0
- package/src/infrastructure/queue/index.ts +12 -0
- package/src/infrastructure/queue/queue-producer.ts +148 -0
- package/src/infrastructure/queue/redis-queue.ts +361 -0
- package/src/infrastructure/queue/types.ts +133 -0
- package/src/infrastructure/redis/system-message-limiter.ts +94 -0
- package/src/interactions/config-request-store.ts +198 -0
- package/src/interactions.ts +363 -0
- package/src/lobu.ts +311 -0
- package/src/metrics/prometheus.ts +159 -0
- package/src/modules/module-system.ts +179 -0
- package/src/orchestration/base-deployment-manager.ts +900 -0
- package/src/orchestration/deployment-utils.ts +98 -0
- package/src/orchestration/impl/docker-deployment.ts +620 -0
- package/src/orchestration/impl/embedded-deployment.ts +268 -0
- package/src/orchestration/impl/index.ts +8 -0
- package/src/orchestration/impl/k8s/deployment.ts +1061 -0
- package/src/orchestration/impl/k8s/helpers.ts +610 -0
- package/src/orchestration/impl/k8s/index.ts +1 -0
- package/src/orchestration/index.ts +333 -0
- package/src/orchestration/message-consumer.ts +584 -0
- package/src/orchestration/scheduled-wakeup.ts +704 -0
- package/src/permissions/approval-policy.ts +36 -0
- package/src/permissions/grant-store.ts +219 -0
- package/src/platform/file-handler.ts +66 -0
- package/src/platform/link-buttons.ts +57 -0
- package/src/platform/renderer-utils.ts +44 -0
- package/src/platform/response-renderer.ts +84 -0
- package/src/platform/unified-thread-consumer.ts +187 -0
- package/src/platform.ts +318 -0
- package/src/proxy/http-proxy.ts +752 -0
- package/src/proxy/proxy-manager.ts +81 -0
- package/src/proxy/secret-proxy.ts +402 -0
- package/src/proxy/token-refresh-job.ts +143 -0
- package/src/routes/internal/audio.ts +141 -0
- package/src/routes/internal/device-auth.ts +566 -0
- package/src/routes/internal/files.ts +226 -0
- package/src/routes/internal/history.ts +69 -0
- package/src/routes/internal/images.ts +127 -0
- package/src/routes/internal/interactions.ts +84 -0
- package/src/routes/internal/middleware.ts +23 -0
- package/src/routes/internal/schedule.ts +226 -0
- package/src/routes/internal/types.ts +22 -0
- package/src/routes/openapi-auto.ts +239 -0
- package/src/routes/public/agent-access.ts +23 -0
- package/src/routes/public/agent-config.ts +675 -0
- package/src/routes/public/agent-history.ts +422 -0
- package/src/routes/public/agent-schedules.ts +296 -0
- package/src/routes/public/agent.ts +1086 -0
- package/src/routes/public/agents.ts +373 -0
- package/src/routes/public/channels.ts +191 -0
- package/src/routes/public/cli-auth.ts +883 -0
- package/src/routes/public/connections.ts +574 -0
- package/src/routes/public/landing.ts +16 -0
- package/src/routes/public/oauth.ts +147 -0
- package/src/routes/public/settings-auth.ts +104 -0
- package/src/routes/public/slack.ts +173 -0
- package/src/routes/shared/agent-ownership.ts +101 -0
- package/src/routes/shared/token-verifier.ts +34 -0
- package/src/services/core-services.ts +1053 -0
- package/src/services/image-generation-service.ts +257 -0
- package/src/services/instruction-service.ts +318 -0
- package/src/services/mcp-registry.ts +94 -0
- package/src/services/platform-helpers.ts +287 -0
- package/src/services/session-manager.ts +262 -0
- package/src/services/settings-resolver.ts +74 -0
- package/src/services/system-config-resolver.ts +90 -0
- package/src/services/system-skills-service.ts +229 -0
- package/src/services/transcription-service.ts +684 -0
- package/src/session.ts +110 -0
- package/src/spaces/index.ts +1 -0
- package/src/spaces/space-resolver.ts +17 -0
- package/src/stores/in-memory-agent-store.ts +403 -0
- package/src/stores/redis-agent-store.ts +279 -0
- package/src/utils/public-url.ts +44 -0
- package/src/utils/rate-limiter.ts +94 -0
- package/tsconfig.json +33 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent history routes — proxy session data from worker HTTP server,
|
|
3
|
+
* with direct session-file fallback for embedded/Docker dev mode.
|
|
4
|
+
* Auth: settings session cookie (verifySettingsSession).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
8
|
+
import { join, resolve } from "node:path";
|
|
9
|
+
import type { AgentConfigStore } from "@lobu/core";
|
|
10
|
+
import { createLogger } from "@lobu/core";
|
|
11
|
+
import type { Context } from "hono";
|
|
12
|
+
import { Hono } from "hono";
|
|
13
|
+
import type { UserAgentsStore } from "../../auth/user-agents-store";
|
|
14
|
+
import type { ChatInstanceManager } from "../../connections/chat-instance-manager";
|
|
15
|
+
import type { WorkerConnectionManager } from "../../gateway/connection-manager";
|
|
16
|
+
import { createTokenVerifier } from "../shared/token-verifier";
|
|
17
|
+
import { verifySettingsSession } from "./settings-auth";
|
|
18
|
+
|
|
19
|
+
const logger = createLogger("agent-history-routes");
|
|
20
|
+
|
|
21
|
+
/** Alphanumeric, hyphens, and underscores only — no path separators or dots. */
|
|
22
|
+
const SAFE_AGENT_ID = /^[a-zA-Z0-9_-]+$/;
|
|
23
|
+
|
|
24
|
+
function isSafeAgentId(id: string): boolean {
|
|
25
|
+
return SAFE_AGENT_ID.test(id);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ─── Direct session file reader (fallback) ─────────────────────────────────
|
|
29
|
+
|
|
30
|
+
interface SessionEntry {
|
|
31
|
+
type: string;
|
|
32
|
+
id: string;
|
|
33
|
+
parentId: string | null;
|
|
34
|
+
timestamp: string;
|
|
35
|
+
message?: {
|
|
36
|
+
role: string;
|
|
37
|
+
content: unknown;
|
|
38
|
+
usage?: { inputTokens?: number; outputTokens?: number };
|
|
39
|
+
};
|
|
40
|
+
summary?: string;
|
|
41
|
+
provider?: string;
|
|
42
|
+
modelId?: string;
|
|
43
|
+
customType?: string;
|
|
44
|
+
content?: unknown;
|
|
45
|
+
display?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ParsedMessage {
|
|
49
|
+
id: string;
|
|
50
|
+
type: string;
|
|
51
|
+
role?: string;
|
|
52
|
+
content: unknown;
|
|
53
|
+
model?: string;
|
|
54
|
+
timestamp: string;
|
|
55
|
+
isVerbose?: boolean;
|
|
56
|
+
usage?: { inputTokens?: number; outputTokens?: number };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function findSessionFile(agentId: string): Promise<string | null> {
|
|
60
|
+
if (!isSafeAgentId(agentId)) return null;
|
|
61
|
+
const workspacesRoot = resolve("workspaces");
|
|
62
|
+
const workspaceDir = resolve(workspacesRoot, agentId);
|
|
63
|
+
if (!workspaceDir.startsWith(`${workspacesRoot}/`)) return null;
|
|
64
|
+
|
|
65
|
+
// Direct: workspaces/{agentId}/.openclaw/session.jsonl
|
|
66
|
+
const directPath = join(workspaceDir, ".openclaw", "session.jsonl");
|
|
67
|
+
try {
|
|
68
|
+
await stat(directPath);
|
|
69
|
+
return directPath;
|
|
70
|
+
} catch {
|
|
71
|
+
// Not found
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Search subdirectories (up to 3 levels deep for nested workspace layouts)
|
|
75
|
+
try {
|
|
76
|
+
const search = async (
|
|
77
|
+
dir: string,
|
|
78
|
+
depth: number
|
|
79
|
+
): Promise<string | null> => {
|
|
80
|
+
if (depth > 3) return null;
|
|
81
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
82
|
+
for (const entry of entries) {
|
|
83
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
84
|
+
const sessionPath = join(dir, entry.name, ".openclaw", "session.jsonl");
|
|
85
|
+
try {
|
|
86
|
+
await stat(sessionPath);
|
|
87
|
+
return sessionPath;
|
|
88
|
+
} catch {
|
|
89
|
+
// Try deeper
|
|
90
|
+
const deeper = await search(join(dir, entry.name), depth + 1);
|
|
91
|
+
if (deeper) return deeper;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
96
|
+
return await search(workspaceDir, 0);
|
|
97
|
+
} catch {
|
|
98
|
+
// Workspace dir doesn't exist
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function parseSessionEntries(content: string): {
|
|
105
|
+
entries: SessionEntry[];
|
|
106
|
+
sessionId?: string;
|
|
107
|
+
} {
|
|
108
|
+
const lines = content.split("\n").filter((l) => l.trim());
|
|
109
|
+
const entries: SessionEntry[] = [];
|
|
110
|
+
let sessionId: string | undefined;
|
|
111
|
+
for (const line of lines) {
|
|
112
|
+
try {
|
|
113
|
+
const parsed = JSON.parse(line);
|
|
114
|
+
if (parsed.type === "session") {
|
|
115
|
+
sessionId = parsed.id;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
entries.push(parsed);
|
|
119
|
+
} catch {
|
|
120
|
+
// Skip malformed
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { entries, sessionId };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function entryToMessage(entry: SessionEntry): ParsedMessage | null {
|
|
127
|
+
if (entry.type === "message" && entry.message) {
|
|
128
|
+
return {
|
|
129
|
+
id: entry.id,
|
|
130
|
+
type: "message",
|
|
131
|
+
role: entry.message.role,
|
|
132
|
+
content: entry.message.content,
|
|
133
|
+
timestamp: entry.timestamp,
|
|
134
|
+
isVerbose: entry.message.role === "toolResult",
|
|
135
|
+
usage: entry.message.usage,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (entry.type === "compaction") {
|
|
139
|
+
return {
|
|
140
|
+
id: entry.id,
|
|
141
|
+
type: "compaction",
|
|
142
|
+
content: entry.summary || "",
|
|
143
|
+
timestamp: entry.timestamp,
|
|
144
|
+
isVerbose: true,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (entry.type === "model_change") {
|
|
148
|
+
return {
|
|
149
|
+
id: entry.id,
|
|
150
|
+
type: "model_change",
|
|
151
|
+
content: `${entry.provider}/${entry.modelId}`,
|
|
152
|
+
model: `${entry.provider}/${entry.modelId}`,
|
|
153
|
+
timestamp: entry.timestamp,
|
|
154
|
+
isVerbose: true,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
if (entry.type === "custom_message") {
|
|
158
|
+
return {
|
|
159
|
+
id: entry.id,
|
|
160
|
+
type: "custom_message",
|
|
161
|
+
role: "user",
|
|
162
|
+
content: entry.content,
|
|
163
|
+
timestamp: entry.timestamp,
|
|
164
|
+
isVerbose: !entry.display,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function readSessionMessages(
|
|
171
|
+
agentId: string,
|
|
172
|
+
cursorParam: string,
|
|
173
|
+
limit: number
|
|
174
|
+
) {
|
|
175
|
+
const sessionPath = await findSessionFile(agentId);
|
|
176
|
+
if (!sessionPath) {
|
|
177
|
+
return {
|
|
178
|
+
messages: [],
|
|
179
|
+
nextCursor: null,
|
|
180
|
+
hasMore: false,
|
|
181
|
+
sessionId: "none",
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const content = await readFile(sessionPath, "utf-8");
|
|
185
|
+
const { entries, sessionId } = parseSessionEntries(content);
|
|
186
|
+
|
|
187
|
+
const allMessages: ParsedMessage[] = [];
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
const msg = entryToMessage(entry);
|
|
190
|
+
if (msg) allMessages.push(msg);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let startIndex = 0;
|
|
194
|
+
if (cursorParam) {
|
|
195
|
+
const idx = allMessages.findIndex((m) => m.id === cursorParam);
|
|
196
|
+
if (idx >= 0) startIndex = idx + 1;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const pageMessages = allMessages.slice(startIndex, startIndex + limit);
|
|
200
|
+
const hasMore = startIndex + limit < allMessages.length;
|
|
201
|
+
const nextCursor = hasMore ? pageMessages[pageMessages.length - 1]?.id : null;
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
messages: pageMessages,
|
|
205
|
+
nextCursor,
|
|
206
|
+
hasMore,
|
|
207
|
+
sessionId: sessionId || "unknown",
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function readSessionStats(agentId: string) {
|
|
212
|
+
const sessionPath = await findSessionFile(agentId);
|
|
213
|
+
if (!sessionPath) {
|
|
214
|
+
return {
|
|
215
|
+
sessionId: "none",
|
|
216
|
+
messageCount: 0,
|
|
217
|
+
userMessages: 0,
|
|
218
|
+
assistantMessages: 0,
|
|
219
|
+
totalInputTokens: 0,
|
|
220
|
+
totalOutputTokens: 0,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const content = await readFile(sessionPath, "utf-8");
|
|
224
|
+
const { entries, sessionId } = parseSessionEntries(content);
|
|
225
|
+
|
|
226
|
+
let messageCount = 0;
|
|
227
|
+
let userMessages = 0;
|
|
228
|
+
let assistantMessages = 0;
|
|
229
|
+
let totalInputTokens = 0;
|
|
230
|
+
let totalOutputTokens = 0;
|
|
231
|
+
let currentModel: string | undefined;
|
|
232
|
+
|
|
233
|
+
for (const entry of entries) {
|
|
234
|
+
if (entry.type === "message" && entry.message) {
|
|
235
|
+
messageCount++;
|
|
236
|
+
if (entry.message.role === "user") userMessages++;
|
|
237
|
+
if (entry.message.role === "assistant") assistantMessages++;
|
|
238
|
+
if (entry.message.usage) {
|
|
239
|
+
const u = entry.message.usage as any;
|
|
240
|
+
totalInputTokens += u.inputTokens || u.input || 0;
|
|
241
|
+
totalOutputTokens += u.outputTokens || u.output || 0;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (entry.type === "model_change") {
|
|
245
|
+
currentModel = `${entry.provider}/${entry.modelId}`;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
sessionId: sessionId || "unknown",
|
|
251
|
+
messageCount,
|
|
252
|
+
userMessages,
|
|
253
|
+
assistantMessages,
|
|
254
|
+
totalInputTokens,
|
|
255
|
+
totalOutputTokens,
|
|
256
|
+
currentModel,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ─── Routes ─────────────────────────────────────────────────────────────────
|
|
261
|
+
|
|
262
|
+
export function createAgentHistoryRoutes(deps: {
|
|
263
|
+
connectionManager: WorkerConnectionManager;
|
|
264
|
+
chatInstanceManager?: ChatInstanceManager;
|
|
265
|
+
agentConfigStore?: Pick<AgentConfigStore, "listSandboxes" | "getMetadata">;
|
|
266
|
+
userAgentsStore?: UserAgentsStore;
|
|
267
|
+
}) {
|
|
268
|
+
const app = new Hono();
|
|
269
|
+
const { connectionManager, chatInstanceManager, agentConfigStore } = deps;
|
|
270
|
+
const verifyToken = createTokenVerifier({
|
|
271
|
+
userAgentsStore: deps.userAgentsStore,
|
|
272
|
+
agentMetadataStore: deps.agentConfigStore,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
async function getAuthorizedAgentId(c: Context): Promise<string | null> {
|
|
276
|
+
const session = verifySettingsSession(c);
|
|
277
|
+
if (!session) return null;
|
|
278
|
+
const agentId = c.req.param("agentId") || session.agentId || null;
|
|
279
|
+
if (!agentId || !isSafeAgentId(agentId)) return null;
|
|
280
|
+
const verified = await verifyToken(session, agentId);
|
|
281
|
+
return verified ? agentId : null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Resolve the first active sandbox agentId that has a running deployment.
|
|
286
|
+
* When a template agent (e.g. "lobu") has no direct deployments,
|
|
287
|
+
* we look at its connections' sandbox agents for a running worker.
|
|
288
|
+
*/
|
|
289
|
+
async function resolveActiveAgent(
|
|
290
|
+
agentId: string
|
|
291
|
+
): Promise<{ connected: boolean; resolvedAgentId: string }> {
|
|
292
|
+
if (connectionManager.getDeploymentsForAgent(agentId).length > 0) {
|
|
293
|
+
return { connected: true, resolvedAgentId: agentId };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (chatInstanceManager && agentConfigStore) {
|
|
297
|
+
try {
|
|
298
|
+
const connections = await chatInstanceManager.listConnections({
|
|
299
|
+
templateAgentId: agentId,
|
|
300
|
+
});
|
|
301
|
+
for (const conn of connections) {
|
|
302
|
+
const sandboxes = await agentConfigStore.listSandboxes(conn.id);
|
|
303
|
+
for (const sb of sandboxes) {
|
|
304
|
+
if (
|
|
305
|
+
connectionManager.getDeploymentsForAgent(sb.agentId).length > 0
|
|
306
|
+
) {
|
|
307
|
+
return { connected: true, resolvedAgentId: sb.agentId };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
} catch (e) {
|
|
312
|
+
logger.debug("Failed to resolve sandbox agents", { error: e });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return { connected: false, resolvedAgentId: agentId };
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Try proxying to worker HTTP server, fall back to direct file read.
|
|
321
|
+
*/
|
|
322
|
+
async function proxyOrFallback<T>(
|
|
323
|
+
agentId: string,
|
|
324
|
+
workerPath: string,
|
|
325
|
+
fallback: (agentId: string) => Promise<T>
|
|
326
|
+
): Promise<{ data: T; proxied: boolean } | null> {
|
|
327
|
+
const { resolvedAgentId } = await resolveActiveAgent(agentId);
|
|
328
|
+
const httpUrl = connectionManager.getHttpUrl(resolvedAgentId);
|
|
329
|
+
|
|
330
|
+
if (httpUrl) {
|
|
331
|
+
try {
|
|
332
|
+
const response = await fetch(`${httpUrl}${workerPath}`, {
|
|
333
|
+
signal: AbortSignal.timeout(5000),
|
|
334
|
+
});
|
|
335
|
+
if (response.ok) {
|
|
336
|
+
return { data: (await response.json()) as T, proxied: true };
|
|
337
|
+
}
|
|
338
|
+
} catch {
|
|
339
|
+
// Worker HTTP not reachable, fall through to file read
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Fallback: read session file directly
|
|
344
|
+
try {
|
|
345
|
+
return { data: await fallback(resolvedAgentId), proxied: false };
|
|
346
|
+
} catch (e) {
|
|
347
|
+
logger.debug("Session file fallback failed", {
|
|
348
|
+
error: e,
|
|
349
|
+
agentId: resolvedAgentId,
|
|
350
|
+
});
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Agent status
|
|
356
|
+
app.get("/status", async (c) => {
|
|
357
|
+
const agentId = await getAuthorizedAgentId(c);
|
|
358
|
+
if (!agentId) return c.json({ error: "Unauthorized" }, 401);
|
|
359
|
+
|
|
360
|
+
const { connected, resolvedAgentId } = await resolveActiveAgent(agentId);
|
|
361
|
+
|
|
362
|
+
// Even if worker HTTP is unreachable, check if session file exists on disk
|
|
363
|
+
const hasSessionFile = !!(await findSessionFile(resolvedAgentId));
|
|
364
|
+
|
|
365
|
+
return c.json({
|
|
366
|
+
connected: connected || hasSessionFile,
|
|
367
|
+
hasHttpServer: !!connectionManager.getHttpUrl(resolvedAgentId),
|
|
368
|
+
deploymentCount:
|
|
369
|
+
connectionManager.getDeploymentsForAgent(resolvedAgentId).length,
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// Session messages
|
|
374
|
+
app.get("/session/messages", async (c) => {
|
|
375
|
+
const agentId = await getAuthorizedAgentId(c);
|
|
376
|
+
if (!agentId) return c.json({ error: "Unauthorized" }, 401);
|
|
377
|
+
|
|
378
|
+
const cursor = c.req.query("cursor") || "";
|
|
379
|
+
const limit = Math.min(parseInt(c.req.query("limit") || "50", 10), 200);
|
|
380
|
+
|
|
381
|
+
const result = await proxyOrFallback(
|
|
382
|
+
agentId,
|
|
383
|
+
`/session/messages?cursor=${cursor}&limit=${limit}`,
|
|
384
|
+
(resolved) => readSessionMessages(resolved, cursor, limit)
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
if (!result) {
|
|
388
|
+
return c.json(
|
|
389
|
+
{
|
|
390
|
+
error: "Agent offline",
|
|
391
|
+
connected: false,
|
|
392
|
+
messages: [],
|
|
393
|
+
nextCursor: null,
|
|
394
|
+
hasMore: false,
|
|
395
|
+
},
|
|
396
|
+
503
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return c.json(result.data);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Session stats
|
|
404
|
+
app.get("/session/stats", async (c) => {
|
|
405
|
+
const agentId = await getAuthorizedAgentId(c);
|
|
406
|
+
if (!agentId) return c.json({ error: "Unauthorized" }, 401);
|
|
407
|
+
|
|
408
|
+
const result = await proxyOrFallback(
|
|
409
|
+
agentId,
|
|
410
|
+
"/session/stats",
|
|
411
|
+
readSessionStats
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
if (!result) {
|
|
415
|
+
return c.json({ error: "Agent offline", connected: false }, 503);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return c.json(result.data);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
return app;
|
|
422
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Schedules Routes
|
|
3
|
+
*
|
|
4
|
+
* Schedule management endpoints mounted under /api/v1/agents/{agentId}/schedules
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
8
|
+
import type { AgentConfigStore } from "@lobu/core";
|
|
9
|
+
import { createLogger } from "@lobu/core";
|
|
10
|
+
import type { ExternalAuthClient } from "../../auth/external/client";
|
|
11
|
+
import type { SettingsTokenPayload } from "../../auth/settings/token-service";
|
|
12
|
+
import type { UserAgentsStore } from "../../auth/user-agents-store";
|
|
13
|
+
import type { ScheduledWakeupService } from "../../orchestration/scheduled-wakeup";
|
|
14
|
+
import { createTokenVerifier } from "../shared/token-verifier";
|
|
15
|
+
import { verifySettingsSessionOrToken } from "./settings-auth";
|
|
16
|
+
|
|
17
|
+
const logger = createLogger("agent-schedules");
|
|
18
|
+
|
|
19
|
+
const TAG = "Schedules";
|
|
20
|
+
const ErrorResponse = z.object({ error: z.string() });
|
|
21
|
+
const TokenQuery = z.object({ token: z.string().optional() });
|
|
22
|
+
|
|
23
|
+
// --- Route Definitions ---
|
|
24
|
+
|
|
25
|
+
const listSchedulesRoute = createRoute({
|
|
26
|
+
method: "get",
|
|
27
|
+
path: "/",
|
|
28
|
+
tags: [TAG],
|
|
29
|
+
summary: "List agent schedules",
|
|
30
|
+
request: { query: TokenQuery },
|
|
31
|
+
responses: {
|
|
32
|
+
200: {
|
|
33
|
+
description: "Schedules",
|
|
34
|
+
content: {
|
|
35
|
+
"application/json": {
|
|
36
|
+
schema: z.object({
|
|
37
|
+
schedules: z.array(
|
|
38
|
+
z.object({
|
|
39
|
+
scheduleId: z.string(),
|
|
40
|
+
conversationId: z.string(),
|
|
41
|
+
task: z.string(),
|
|
42
|
+
scheduledAt: z.number(),
|
|
43
|
+
scheduledFor: z.number(),
|
|
44
|
+
status: z.string(),
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
401: {
|
|
52
|
+
description: "Unauthorized",
|
|
53
|
+
content: { "application/json": { schema: ErrorResponse } },
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const cancelScheduleRoute = createRoute({
|
|
59
|
+
method: "delete",
|
|
60
|
+
path: "/{scheduleId}",
|
|
61
|
+
tags: [TAG],
|
|
62
|
+
summary: "Cancel agent schedule",
|
|
63
|
+
request: {
|
|
64
|
+
query: TokenQuery,
|
|
65
|
+
params: z.object({ scheduleId: z.string() }),
|
|
66
|
+
},
|
|
67
|
+
responses: {
|
|
68
|
+
200: {
|
|
69
|
+
description: "Cancelled",
|
|
70
|
+
content: {
|
|
71
|
+
"application/json": {
|
|
72
|
+
schema: z.object({
|
|
73
|
+
success: z.boolean(),
|
|
74
|
+
message: z.string().optional(),
|
|
75
|
+
}),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
401: {
|
|
80
|
+
description: "Unauthorized",
|
|
81
|
+
content: { "application/json": { schema: ErrorResponse } },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const createScheduleRoute = createRoute({
|
|
87
|
+
method: "post",
|
|
88
|
+
path: "/",
|
|
89
|
+
tags: [TAG],
|
|
90
|
+
summary: "Create agent schedule",
|
|
91
|
+
request: {
|
|
92
|
+
query: TokenQuery,
|
|
93
|
+
body: {
|
|
94
|
+
content: {
|
|
95
|
+
"application/json": {
|
|
96
|
+
schema: z.object({
|
|
97
|
+
task: z.string().max(2000),
|
|
98
|
+
cron: z.string().optional(),
|
|
99
|
+
delayMinutes: z.number().min(1).max(1440).optional(),
|
|
100
|
+
maxIterations: z.number().min(1).optional(),
|
|
101
|
+
context: z.record(z.string(), z.unknown()).optional(),
|
|
102
|
+
source: z.string().optional(),
|
|
103
|
+
}),
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
responses: {
|
|
109
|
+
200: {
|
|
110
|
+
description: "Schedule created",
|
|
111
|
+
content: {
|
|
112
|
+
"application/json": {
|
|
113
|
+
schema: z.object({
|
|
114
|
+
scheduleId: z.string(),
|
|
115
|
+
scheduledFor: z.string(),
|
|
116
|
+
isRecurring: z.boolean(),
|
|
117
|
+
cron: z.string().optional(),
|
|
118
|
+
maxIterations: z.number(),
|
|
119
|
+
}),
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
400: {
|
|
124
|
+
description: "Bad request",
|
|
125
|
+
content: { "application/json": { schema: ErrorResponse } },
|
|
126
|
+
},
|
|
127
|
+
401: {
|
|
128
|
+
description: "Unauthorized",
|
|
129
|
+
content: { "application/json": { schema: ErrorResponse } },
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
export interface AgentSchedulesRoutesConfig {
|
|
135
|
+
scheduledWakeupService?: ScheduledWakeupService;
|
|
136
|
+
externalAuthClient?: ExternalAuthClient;
|
|
137
|
+
userAgentsStore?: UserAgentsStore;
|
|
138
|
+
agentMetadataStore?: Pick<AgentConfigStore, "getMetadata">;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function createAgentSchedulesRoutes(
|
|
142
|
+
config: AgentSchedulesRoutesConfig
|
|
143
|
+
): OpenAPIHono {
|
|
144
|
+
const app = new OpenAPIHono();
|
|
145
|
+
const verifySessionToken = createTokenVerifier({
|
|
146
|
+
userAgentsStore: config.userAgentsStore,
|
|
147
|
+
agentMetadataStore: config.agentMetadataStore,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Auth: settings session (cookie/authProvider) OR external OAuth Bearer token
|
|
152
|
+
* (validated via MEMORY_URL userinfo endpoint).
|
|
153
|
+
*/
|
|
154
|
+
async function requireAuth(
|
|
155
|
+
c: any,
|
|
156
|
+
agentId: string
|
|
157
|
+
): Promise<SettingsTokenPayload | null> {
|
|
158
|
+
// 1. Try settings session (cookie or injected authProvider)
|
|
159
|
+
const session = verifySettingsSessionOrToken(c);
|
|
160
|
+
if (session) {
|
|
161
|
+
if (session.isAdmin || session.settingsMode === "admin") {
|
|
162
|
+
return {
|
|
163
|
+
...session,
|
|
164
|
+
isAdmin: true,
|
|
165
|
+
settingsMode: "admin",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return verifySessionToken(session, agentId);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 2. Try external OAuth Bearer token (validated via MEMORY_URL)
|
|
172
|
+
if (config.externalAuthClient) {
|
|
173
|
+
const authHeader = c.req.header("Authorization");
|
|
174
|
+
if (authHeader?.startsWith("Bearer ")) {
|
|
175
|
+
const token = authHeader.substring(7);
|
|
176
|
+
try {
|
|
177
|
+
const userInfo = await config.externalAuthClient.fetchUserInfo(token);
|
|
178
|
+
if (userInfo?.sub) {
|
|
179
|
+
return verifySessionToken(
|
|
180
|
+
{
|
|
181
|
+
userId: userInfo.sub,
|
|
182
|
+
oauthUserId: userInfo.sub,
|
|
183
|
+
platform: "external",
|
|
184
|
+
exp: Date.now() + 60_000,
|
|
185
|
+
},
|
|
186
|
+
agentId
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
} catch (err) {
|
|
190
|
+
logger.debug({ err }, "Bearer token validation failed");
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// POST / — create schedule
|
|
199
|
+
app.openapi(createScheduleRoute, async (c): Promise<any> => {
|
|
200
|
+
const agentId = c.req.param("agentId") || "";
|
|
201
|
+
if (!(await requireAuth(c, agentId))) {
|
|
202
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!config.scheduledWakeupService) {
|
|
206
|
+
return c.json({ error: "Scheduling not configured" }, 500);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const body = c.req.valid("json");
|
|
210
|
+
if (!body.cron && !body.delayMinutes) {
|
|
211
|
+
return c.json({ error: "Must specify either cron or delayMinutes" }, 400);
|
|
212
|
+
}
|
|
213
|
+
if (body.cron && body.delayMinutes) {
|
|
214
|
+
return c.json(
|
|
215
|
+
{ error: "Cannot specify both cron and delayMinutes" },
|
|
216
|
+
400
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
const schedule = await config.scheduledWakeupService.scheduleExternal({
|
|
222
|
+
agentId,
|
|
223
|
+
task: body.task,
|
|
224
|
+
context: body.context,
|
|
225
|
+
cron: body.cron,
|
|
226
|
+
delayMinutes: body.delayMinutes,
|
|
227
|
+
maxIterations: body.maxIterations,
|
|
228
|
+
source: body.source,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return c.json({
|
|
232
|
+
scheduleId: schedule.id,
|
|
233
|
+
scheduledFor: schedule.triggerAt,
|
|
234
|
+
isRecurring: schedule.isRecurring,
|
|
235
|
+
cron: schedule.cron,
|
|
236
|
+
maxIterations: schedule.maxIterations,
|
|
237
|
+
});
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return c.json(
|
|
240
|
+
{
|
|
241
|
+
error:
|
|
242
|
+
error instanceof Error
|
|
243
|
+
? error.message
|
|
244
|
+
: "Failed to create schedule",
|
|
245
|
+
},
|
|
246
|
+
400
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
app.openapi(listSchedulesRoute, async (c): Promise<any> => {
|
|
252
|
+
const agentId = c.req.param("agentId") || "";
|
|
253
|
+
if (!(await requireAuth(c, agentId))) {
|
|
254
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (!config.scheduledWakeupService) return c.json({ schedules: [] });
|
|
258
|
+
|
|
259
|
+
const schedules =
|
|
260
|
+
await config.scheduledWakeupService.listPendingForAgent(agentId);
|
|
261
|
+
return c.json({
|
|
262
|
+
schedules: schedules.map((s) => ({
|
|
263
|
+
scheduleId: s.id,
|
|
264
|
+
conversationId: s.conversationId,
|
|
265
|
+
task: s.task,
|
|
266
|
+
scheduledAt: s.scheduledAt,
|
|
267
|
+
scheduledFor: s.triggerAt,
|
|
268
|
+
status: s.status,
|
|
269
|
+
})),
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
app.openapi(cancelScheduleRoute, async (c): Promise<any> => {
|
|
274
|
+
const agentId = c.req.param("agentId") || "";
|
|
275
|
+
if (!(await requireAuth(c, agentId))) {
|
|
276
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (!config.scheduledWakeupService) {
|
|
280
|
+
return c.json({ error: "Not configured" }, 500);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const { scheduleId } = c.req.valid("param");
|
|
284
|
+
const success = await config.scheduledWakeupService.cancelByAgent(
|
|
285
|
+
scheduleId,
|
|
286
|
+
agentId
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return c.json({
|
|
290
|
+
success,
|
|
291
|
+
message: success ? undefined : "Not found or already triggered",
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return app;
|
|
296
|
+
}
|