@swarmclawai/swarmclaw 0.2.0
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/README.md +577 -0
- package/bin/server-cmd.js +359 -0
- package/bin/swarmclaw.js +29 -0
- package/bin/swarmclaw.mjs +1504 -0
- package/next.config.ts +33 -0
- package/package.json +112 -0
- package/postcss.config.mjs +7 -0
- package/public/branding/swarmclaw-org-avatar.png +0 -0
- package/public/branding/swarmclaw-org-avatar.svg +58 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/next.svg +1 -0
- package/public/screenshots/agents.png +0 -0
- package/public/screenshots/connectors.png +0 -0
- package/public/screenshots/dashboard.png +0 -0
- package/public/screenshots/new-session-openclaw.png +0 -0
- package/public/screenshots/providers.png +0 -0
- package/public/screenshots/schedules.png +0 -0
- package/public/screenshots/tasks.png +0 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/src/app/api/agents/[id]/route.ts +30 -0
- package/src/app/api/agents/[id]/thread/route.ts +66 -0
- package/src/app/api/agents/generate/route.ts +42 -0
- package/src/app/api/agents/route.ts +33 -0
- package/src/app/api/auth/route.ts +25 -0
- package/src/app/api/claude-skills/route.ts +42 -0
- package/src/app/api/clawhub/install/route.ts +39 -0
- package/src/app/api/clawhub/search/route.ts +11 -0
- package/src/app/api/connectors/[id]/route.ts +79 -0
- package/src/app/api/connectors/route.ts +60 -0
- package/src/app/api/credentials/[id]/route.ts +14 -0
- package/src/app/api/credentials/route.ts +31 -0
- package/src/app/api/daemon/health-check/route.ts +11 -0
- package/src/app/api/daemon/route.ts +22 -0
- package/src/app/api/dirs/pick/route.ts +60 -0
- package/src/app/api/dirs/route.ts +29 -0
- package/src/app/api/documents/[id]/route.ts +47 -0
- package/src/app/api/documents/route.ts +93 -0
- package/src/app/api/files/serve/route.ts +69 -0
- package/src/app/api/generate/info/route.ts +12 -0
- package/src/app/api/generate/route.ts +106 -0
- package/src/app/api/ip/route.ts +6 -0
- package/src/app/api/knowledge/[id]/route.ts +61 -0
- package/src/app/api/knowledge/route.ts +48 -0
- package/src/app/api/knowledge/upload/route.ts +86 -0
- package/src/app/api/logs/route.ts +65 -0
- package/src/app/api/mcp-servers/[id]/route.ts +32 -0
- package/src/app/api/mcp-servers/[id]/test/route.ts +23 -0
- package/src/app/api/mcp-servers/[id]/tools/route.ts +32 -0
- package/src/app/api/mcp-servers/route.ts +27 -0
- package/src/app/api/memory/[id]/route.ts +126 -0
- package/src/app/api/memory/maintenance/route.ts +63 -0
- package/src/app/api/memory/route.ts +111 -0
- package/src/app/api/memory-images/[filename]/route.ts +36 -0
- package/src/app/api/orchestrator/run/route.ts +43 -0
- package/src/app/api/plugins/install/route.ts +58 -0
- package/src/app/api/plugins/marketplace/route.ts +33 -0
- package/src/app/api/plugins/route.ts +21 -0
- package/src/app/api/preview-server/route.ts +339 -0
- package/src/app/api/providers/[id]/models/route.ts +29 -0
- package/src/app/api/providers/[id]/route.ts +34 -0
- package/src/app/api/providers/configs/route.ts +7 -0
- package/src/app/api/providers/ollama/route.ts +30 -0
- package/src/app/api/providers/openclaw/health/route.ts +23 -0
- package/src/app/api/providers/route.ts +28 -0
- package/src/app/api/runs/[id]/route.ts +9 -0
- package/src/app/api/runs/route.ts +13 -0
- package/src/app/api/schedules/[id]/route.ts +28 -0
- package/src/app/api/schedules/[id]/run/route.ts +104 -0
- package/src/app/api/schedules/route.ts +78 -0
- package/src/app/api/secrets/[id]/route.ts +29 -0
- package/src/app/api/secrets/route.ts +42 -0
- package/src/app/api/sessions/[id]/browser/route.ts +13 -0
- package/src/app/api/sessions/[id]/chat/route.ts +96 -0
- package/src/app/api/sessions/[id]/clear/route.ts +19 -0
- package/src/app/api/sessions/[id]/deploy/route.ts +34 -0
- package/src/app/api/sessions/[id]/devserver/route.ts +69 -0
- package/src/app/api/sessions/[id]/mailbox/route.ts +70 -0
- package/src/app/api/sessions/[id]/main-loop/route.ts +94 -0
- package/src/app/api/sessions/[id]/messages/route.ts +9 -0
- package/src/app/api/sessions/[id]/retry/route.ts +28 -0
- package/src/app/api/sessions/[id]/route.ts +103 -0
- package/src/app/api/sessions/[id]/stop/route.ts +13 -0
- package/src/app/api/sessions/heartbeat/route.ts +26 -0
- package/src/app/api/sessions/route.ts +85 -0
- package/src/app/api/settings/route.ts +58 -0
- package/src/app/api/setup/check-provider/route.ts +326 -0
- package/src/app/api/setup/doctor/route.ts +250 -0
- package/src/app/api/skills/[id]/route.ts +40 -0
- package/src/app/api/skills/import/route.ts +69 -0
- package/src/app/api/skills/route.ts +28 -0
- package/src/app/api/tasks/[id]/route.ts +102 -0
- package/src/app/api/tasks/route.ts +115 -0
- package/src/app/api/tts/route.ts +40 -0
- package/src/app/api/upload/route.ts +18 -0
- package/src/app/api/uploads/[filename]/route.ts +59 -0
- package/src/app/api/usage/route.ts +35 -0
- package/src/app/api/version/route.ts +81 -0
- package/src/app/api/version/update/route.ts +95 -0
- package/src/app/api/webhooks/[id]/history/route.ts +13 -0
- package/src/app/api/webhooks/[id]/route.ts +204 -0
- package/src/app/api/webhooks/route.ts +37 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +370 -0
- package/src/app/layout.tsx +52 -0
- package/src/app/page.tsx +172 -0
- package/src/cli/index.js +1232 -0
- package/src/cli/index.test.js +281 -0
- package/src/cli/index.ts +1158 -0
- package/src/cli/spec.js +284 -0
- package/src/components/agents/agent-card.tsx +219 -0
- package/src/components/agents/agent-chat-list.tsx +165 -0
- package/src/components/agents/agent-list.tsx +110 -0
- package/src/components/agents/agent-sheet.tsx +1220 -0
- package/src/components/auth/access-key-gate.tsx +248 -0
- package/src/components/auth/setup-wizard.tsx +940 -0
- package/src/components/auth/user-picker.tsx +88 -0
- package/src/components/chat/chat-area.tsx +406 -0
- package/src/components/chat/chat-header.tsx +491 -0
- package/src/components/chat/chat-tool-toggles.tsx +161 -0
- package/src/components/chat/code-block.tsx +146 -0
- package/src/components/chat/dev-server-bar.tsx +39 -0
- package/src/components/chat/message-bubble.tsx +486 -0
- package/src/components/chat/message-list.tsx +299 -0
- package/src/components/chat/session-debug-panel.tsx +196 -0
- package/src/components/chat/streaming-bubble.tsx +85 -0
- package/src/components/chat/thinking-indicator.tsx +26 -0
- package/src/components/chat/tool-call-bubble.tsx +438 -0
- package/src/components/chat/tool-request-banner.tsx +103 -0
- package/src/components/connectors/connector-list.tsx +196 -0
- package/src/components/connectors/connector-sheet.tsx +804 -0
- package/src/components/input/chat-input.tsx +235 -0
- package/src/components/knowledge/knowledge-list.tsx +206 -0
- package/src/components/knowledge/knowledge-sheet.tsx +316 -0
- package/src/components/layout/app-layout.tsx +1016 -0
- package/src/components/layout/daemon-indicator.tsx +56 -0
- package/src/components/layout/mobile-header.tsx +31 -0
- package/src/components/layout/network-banner.tsx +17 -0
- package/src/components/layout/update-banner.tsx +130 -0
- package/src/components/logs/log-list.tsx +358 -0
- package/src/components/mcp-servers/mcp-server-list.tsx +122 -0
- package/src/components/mcp-servers/mcp-server-sheet.tsx +243 -0
- package/src/components/memory/memory-card.tsx +63 -0
- package/src/components/memory/memory-detail.tsx +339 -0
- package/src/components/memory/memory-list.tsx +198 -0
- package/src/components/memory/memory-sheet.tsx +70 -0
- package/src/components/plugins/plugin-list.tsx +60 -0
- package/src/components/plugins/plugin-sheet.tsx +311 -0
- package/src/components/providers/provider-list.tsx +96 -0
- package/src/components/providers/provider-sheet.tsx +542 -0
- package/src/components/runs/run-list.tsx +231 -0
- package/src/components/schedules/schedule-card.tsx +63 -0
- package/src/components/schedules/schedule-list.tsx +76 -0
- package/src/components/schedules/schedule-sheet.tsx +336 -0
- package/src/components/secrets/secret-sheet.tsx +180 -0
- package/src/components/secrets/secrets-list.tsx +91 -0
- package/src/components/sessions/new-session-sheet.tsx +478 -0
- package/src/components/sessions/session-card.tsx +144 -0
- package/src/components/sessions/session-list.tsx +202 -0
- package/src/components/shared/ai-gen-block.tsx +77 -0
- package/src/components/shared/avatar.tsx +48 -0
- package/src/components/shared/bottom-sheet.tsx +30 -0
- package/src/components/shared/confirm-dialog.tsx +47 -0
- package/src/components/shared/connector-platform-icon.tsx +113 -0
- package/src/components/shared/dir-browser.tsx +285 -0
- package/src/components/shared/dropdown.tsx +55 -0
- package/src/components/shared/icon-button.tsx +25 -0
- package/src/components/shared/settings/plugin-manager.tsx +207 -0
- package/src/components/shared/settings/section-capability-policy.tsx +93 -0
- package/src/components/shared/settings/section-embedding.tsx +99 -0
- package/src/components/shared/settings/section-heartbeat.tsx +168 -0
- package/src/components/shared/settings/section-memory.tsx +77 -0
- package/src/components/shared/settings/section-orchestrator.tsx +108 -0
- package/src/components/shared/settings/section-providers.tsx +181 -0
- package/src/components/shared/settings/section-runtime-loop.tsx +183 -0
- package/src/components/shared/settings/section-secrets.tsx +132 -0
- package/src/components/shared/settings/section-user-preferences.tsx +24 -0
- package/src/components/shared/settings/section-voice.tsx +53 -0
- package/src/components/shared/settings/settings-sheet.tsx +88 -0
- package/src/components/shared/settings/types.ts +7 -0
- package/src/components/shared/settings/utils.ts +13 -0
- package/src/components/shared/settings-sheet.tsx +1 -0
- package/src/components/shared/skeleton.tsx +19 -0
- package/src/components/shared/usage-badge.tsx +28 -0
- package/src/components/skills/clawhub-browser.tsx +225 -0
- package/src/components/skills/skill-list.tsx +70 -0
- package/src/components/skills/skill-sheet.tsx +254 -0
- package/src/components/tasks/task-board.tsx +96 -0
- package/src/components/tasks/task-card.tsx +179 -0
- package/src/components/tasks/task-column.tsx +73 -0
- package/src/components/tasks/task-list.tsx +118 -0
- package/src/components/tasks/task-sheet.tsx +415 -0
- package/src/components/ui/avatar.tsx +109 -0
- package/src/components/ui/badge.tsx +48 -0
- package/src/components/ui/button.tsx +64 -0
- package/src/components/ui/card.tsx +92 -0
- package/src/components/ui/dialog.tsx +158 -0
- package/src/components/ui/dropdown-menu.tsx +257 -0
- package/src/components/ui/input.tsx +21 -0
- package/src/components/ui/scroll-area.tsx +58 -0
- package/src/components/ui/select.tsx +190 -0
- package/src/components/ui/separator.tsx +28 -0
- package/src/components/ui/sheet.tsx +143 -0
- package/src/components/ui/sonner.tsx +22 -0
- package/src/components/ui/textarea.tsx +18 -0
- package/src/components/ui/tooltip.tsx +56 -0
- package/src/components/usage/usage-list.tsx +105 -0
- package/src/components/webhooks/webhook-list.tsx +166 -0
- package/src/components/webhooks/webhook-sheet.tsx +402 -0
- package/src/hooks/use-auto-resize.ts +20 -0
- package/src/hooks/use-media-query.ts +21 -0
- package/src/hooks/use-speech-recognition.ts +83 -0
- package/src/instrumentation.ts +8 -0
- package/src/lib/agents.ts +13 -0
- package/src/lib/api-client.ts +100 -0
- package/src/lib/chat.ts +60 -0
- package/src/lib/memory.ts +42 -0
- package/src/lib/openclaw-endpoint.test.ts +48 -0
- package/src/lib/openclaw-endpoint.ts +67 -0
- package/src/lib/provider-config.ts +13 -0
- package/src/lib/providers/anthropic.ts +135 -0
- package/src/lib/providers/claude-cli.ts +202 -0
- package/src/lib/providers/codex-cli.ts +260 -0
- package/src/lib/providers/index.ts +351 -0
- package/src/lib/providers/ollama.ts +131 -0
- package/src/lib/providers/openai.ts +164 -0
- package/src/lib/providers/openclaw.ts +330 -0
- package/src/lib/providers/opencode-cli.ts +164 -0
- package/src/lib/runtime-loop.ts +15 -0
- package/src/lib/schedule-dedupe.test.ts +84 -0
- package/src/lib/schedule-dedupe.ts +174 -0
- package/src/lib/schedule-name.ts +62 -0
- package/src/lib/schedules.ts +16 -0
- package/src/lib/server/agent-registry.ts +70 -0
- package/src/lib/server/api-routes.test.ts +362 -0
- package/src/lib/server/autonomy-contract.ts +200 -0
- package/src/lib/server/build-llm.ts +155 -0
- package/src/lib/server/capability-router.test.ts +21 -0
- package/src/lib/server/capability-router.ts +172 -0
- package/src/lib/server/chat-execution.ts +894 -0
- package/src/lib/server/clawhub-client.test.ts +161 -0
- package/src/lib/server/clawhub-client.ts +26 -0
- package/src/lib/server/connectors/connector-routing.test.ts +243 -0
- package/src/lib/server/connectors/discord.ts +116 -0
- package/src/lib/server/connectors/googlechat.ts +66 -0
- package/src/lib/server/connectors/manager.ts +559 -0
- package/src/lib/server/connectors/matrix.ts +78 -0
- package/src/lib/server/connectors/media.ts +149 -0
- package/src/lib/server/connectors/openclaw.test.ts +375 -0
- package/src/lib/server/connectors/openclaw.ts +1132 -0
- package/src/lib/server/connectors/signal.ts +183 -0
- package/src/lib/server/connectors/slack.ts +258 -0
- package/src/lib/server/connectors/teams.ts +94 -0
- package/src/lib/server/connectors/telegram.ts +221 -0
- package/src/lib/server/connectors/types.ts +62 -0
- package/src/lib/server/connectors/whatsapp.ts +349 -0
- package/src/lib/server/context-manager.ts +232 -0
- package/src/lib/server/cost.ts +31 -0
- package/src/lib/server/daemon-state.ts +354 -0
- package/src/lib/server/data-dir.ts +3 -0
- package/src/lib/server/embeddings.ts +111 -0
- package/src/lib/server/execution-log.ts +257 -0
- package/src/lib/server/gateway/protocol.test.ts +54 -0
- package/src/lib/server/gateway/protocol.ts +114 -0
- package/src/lib/server/heartbeat-service.ts +366 -0
- package/src/lib/server/knowledge-db.test.ts +441 -0
- package/src/lib/server/logger.ts +47 -0
- package/src/lib/server/main-agent-loop.ts +1017 -0
- package/src/lib/server/mcp-client.test.ts +342 -0
- package/src/lib/server/mcp-client.ts +130 -0
- package/src/lib/server/memory-db.ts +1078 -0
- package/src/lib/server/memory-graph.test.ts +153 -0
- package/src/lib/server/memory-graph.ts +138 -0
- package/src/lib/server/openclaw-health.ts +245 -0
- package/src/lib/server/orchestrator-lg.ts +431 -0
- package/src/lib/server/orchestrator.ts +364 -0
- package/src/lib/server/playwright-proxy.mjs +70 -0
- package/src/lib/server/plugins.ts +229 -0
- package/src/lib/server/process-manager.ts +327 -0
- package/src/lib/server/provider-health.ts +113 -0
- package/src/lib/server/queue.ts +859 -0
- package/src/lib/server/runtime-settings.ts +119 -0
- package/src/lib/server/scheduler.ts +196 -0
- package/src/lib/server/session-mailbox.ts +129 -0
- package/src/lib/server/session-run-manager.ts +512 -0
- package/src/lib/server/session-tools/connector.ts +124 -0
- package/src/lib/server/session-tools/context-mgmt.ts +103 -0
- package/src/lib/server/session-tools/context.ts +114 -0
- package/src/lib/server/session-tools/crud.ts +673 -0
- package/src/lib/server/session-tools/delegate.ts +708 -0
- package/src/lib/server/session-tools/file.ts +264 -0
- package/src/lib/server/session-tools/index.ts +164 -0
- package/src/lib/server/session-tools/memory.ts +230 -0
- package/src/lib/server/session-tools/session-info.ts +422 -0
- package/src/lib/server/session-tools/session-tools-wiring.test.ts +166 -0
- package/src/lib/server/session-tools/shell.ts +171 -0
- package/src/lib/server/session-tools/web.ts +408 -0
- package/src/lib/server/session-tools.ts +9 -0
- package/src/lib/server/skills-normalize.ts +130 -0
- package/src/lib/server/storage-mcp.test.ts +161 -0
- package/src/lib/server/storage.ts +670 -0
- package/src/lib/server/stream-agent-chat.ts +571 -0
- package/src/lib/server/task-reports.ts +122 -0
- package/src/lib/server/task-result.ts +161 -0
- package/src/lib/server/task-validation.test.ts +27 -0
- package/src/lib/server/task-validation.ts +90 -0
- package/src/lib/server/tool-capability-policy.test.ts +58 -0
- package/src/lib/server/tool-capability-policy.ts +262 -0
- package/src/lib/sessions.ts +68 -0
- package/src/lib/tasks.ts +20 -0
- package/src/lib/tts.ts +42 -0
- package/src/lib/upload.ts +10 -0
- package/src/lib/utils.ts +6 -0
- package/src/proxy.ts +43 -0
- package/src/stores/use-app-store.ts +468 -0
- package/src/stores/use-chat-store.ts +323 -0
- package/src/types/index.ts +621 -0
- package/tsconfig.json +34 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { tool, type StructuredToolInterface } from '@langchain/core/tools'
|
|
3
|
+
import { loadSessions, saveSessions } from '../storage'
|
|
4
|
+
import type { ToolBuildContext } from './context'
|
|
5
|
+
|
|
6
|
+
export function buildContextTools(bctx: ToolBuildContext): StructuredToolInterface[] {
|
|
7
|
+
const tools: StructuredToolInterface[] = []
|
|
8
|
+
const { ctx, activeTools, resolveCurrentSession } = bctx
|
|
9
|
+
|
|
10
|
+
if (activeTools.length > 0) {
|
|
11
|
+
tools.push(
|
|
12
|
+
tool(
|
|
13
|
+
async () => {
|
|
14
|
+
try {
|
|
15
|
+
const { getContextStatus } = await import('../context-manager')
|
|
16
|
+
const session = resolveCurrentSession()
|
|
17
|
+
if (!session) return 'Error: no current session context.'
|
|
18
|
+
const messages = session.messages || []
|
|
19
|
+
const systemPromptTokens = 2000
|
|
20
|
+
const status = getContextStatus(messages, systemPromptTokens, session.provider, session.model)
|
|
21
|
+
return JSON.stringify(status)
|
|
22
|
+
} catch (err: any) {
|
|
23
|
+
return `Error: ${err.message || String(err)}`
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'context_status',
|
|
28
|
+
description: 'Check current context window usage for this session. Returns estimated tokens used, provider context limit, percentage used, and compaction strategy recommendation.',
|
|
29
|
+
schema: z.object({}),
|
|
30
|
+
},
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
tools.push(
|
|
35
|
+
tool(
|
|
36
|
+
async ({ keepLastN }) => {
|
|
37
|
+
try {
|
|
38
|
+
const { summarizeAndCompact } = await import('../context-manager')
|
|
39
|
+
const session = resolveCurrentSession()
|
|
40
|
+
if (!session) return 'Error: no current session context.'
|
|
41
|
+
if (!ctx?.sessionId) return 'Error: no session id in context.'
|
|
42
|
+
const messages = session.messages || []
|
|
43
|
+
const keep = Math.max(2, Math.min(keepLastN || 10, messages.length))
|
|
44
|
+
|
|
45
|
+
if (messages.length <= keep) {
|
|
46
|
+
return JSON.stringify({ status: 'no_action', reason: 'Not enough messages to compact', messageCount: messages.length })
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const generateSummary = async (text: string): Promise<string> => {
|
|
50
|
+
const lines = text.split('\n\n').filter(Boolean)
|
|
51
|
+
const keyLines: string[] = []
|
|
52
|
+
for (const line of lines) {
|
|
53
|
+
if (line.length > 20) {
|
|
54
|
+
keyLines.push(line.slice(0, 200))
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
let summary = ''
|
|
58
|
+
for (const line of keyLines) {
|
|
59
|
+
if (summary.length + line.length > 2000) break
|
|
60
|
+
summary += line + '\n'
|
|
61
|
+
}
|
|
62
|
+
return summary.trim() || 'Previous conversation context was pruned.'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const result = await summarizeAndCompact({
|
|
66
|
+
messages,
|
|
67
|
+
keepLastN: keep,
|
|
68
|
+
agentId: ctx?.agentId || session.agentId || null,
|
|
69
|
+
sessionId: ctx.sessionId,
|
|
70
|
+
generateSummary,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const sessions = loadSessions()
|
|
74
|
+
const target = sessions[ctx.sessionId]
|
|
75
|
+
if (target) {
|
|
76
|
+
target.messages = result.messages
|
|
77
|
+
saveSessions(sessions)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return JSON.stringify({
|
|
81
|
+
status: 'compacted',
|
|
82
|
+
prunedCount: result.prunedCount,
|
|
83
|
+
memoriesStored: result.memoriesStored,
|
|
84
|
+
summaryAdded: result.summaryAdded,
|
|
85
|
+
remainingMessages: result.messages.length,
|
|
86
|
+
})
|
|
87
|
+
} catch (err: any) {
|
|
88
|
+
return `Error: ${err.message || String(err)}`
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'context_summarize',
|
|
93
|
+
description: 'Summarize and compact the conversation history to free context window space. Old messages are consolidated to memory (preserving decisions, key facts, results) and replaced with a summary. Use context_status first to check if compaction is needed.',
|
|
94
|
+
schema: z.object({
|
|
95
|
+
keepLastN: z.number().optional().describe('Number of recent messages to keep (default 10, min 2).'),
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
),
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return tools
|
|
103
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { StructuredToolInterface } from '@langchain/core/tools'
|
|
2
|
+
|
|
3
|
+
export const MAX_OUTPUT = 50 * 1024 // 50KB
|
|
4
|
+
export const MAX_FILE = 100 * 1024 // 100KB
|
|
5
|
+
|
|
6
|
+
export interface ToolContext {
|
|
7
|
+
agentId?: string | null
|
|
8
|
+
sessionId?: string | null
|
|
9
|
+
platformAssignScope?: 'self' | 'all'
|
|
10
|
+
mcpServerIds?: string[]
|
|
11
|
+
mcpDisabledTools?: string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SessionToolsResult {
|
|
15
|
+
tools: StructuredToolInterface[]
|
|
16
|
+
cleanup: () => Promise<void>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ToolBuildContext {
|
|
20
|
+
cwd: string
|
|
21
|
+
ctx: ToolContext | undefined
|
|
22
|
+
hasTool: (name: string) => boolean
|
|
23
|
+
cleanupFns: (() => Promise<void>)[]
|
|
24
|
+
commandTimeoutMs: number
|
|
25
|
+
claudeTimeoutMs: number
|
|
26
|
+
cliProcessTimeoutMs: number
|
|
27
|
+
persistDelegateResumeId: (key: 'claudeCode' | 'codex' | 'opencode', id: string | null | undefined) => void
|
|
28
|
+
readStoredDelegateResumeId: (key: 'claudeCode' | 'codex' | 'opencode') => string | null
|
|
29
|
+
resolveCurrentSession: () => any | null
|
|
30
|
+
activeTools: string[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function safePath(cwd: string, filePath: string): string {
|
|
34
|
+
const resolved = require('path').resolve(cwd, filePath)
|
|
35
|
+
if (!resolved.startsWith(require('path').resolve(cwd))) {
|
|
36
|
+
throw new Error('Path traversal not allowed')
|
|
37
|
+
}
|
|
38
|
+
return resolved
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function truncate(text: string, max: number): string {
|
|
42
|
+
if (text.length <= max) return text
|
|
43
|
+
return text.slice(0, max) + `\n... [truncated at ${max} bytes]`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function tail(text: string, max = 4000): string {
|
|
47
|
+
if (!text) return ''
|
|
48
|
+
return text.length <= max ? text : text.slice(text.length - max)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function extractResumeIdentifier(text: string): string | null {
|
|
52
|
+
if (!text) return null
|
|
53
|
+
const patterns = [
|
|
54
|
+
/session[_\s-]?id["'\s]*[:=]\s*["']?([A-Za-z0-9._:-]{6,})/i,
|
|
55
|
+
/thread[_\s-]?id["'\s]*[:=]\s*["']?([A-Za-z0-9._:-]{6,})/i,
|
|
56
|
+
/resume(?:\s+with)?\s+([A-Za-z0-9._:-]{6,})/i,
|
|
57
|
+
]
|
|
58
|
+
for (const pattern of patterns) {
|
|
59
|
+
const m = text.match(pattern)
|
|
60
|
+
if (m?.[1]) return m[1]
|
|
61
|
+
}
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const binaryLookupCache = new Map<string, { checkedAt: number; path: string | null }>()
|
|
66
|
+
const BINARY_LOOKUP_TTL_MS = 30_000
|
|
67
|
+
|
|
68
|
+
export function findBinaryOnPath(binaryName: string): string | null {
|
|
69
|
+
const now = Date.now()
|
|
70
|
+
const cached = binaryLookupCache.get(binaryName)
|
|
71
|
+
if (cached && now - cached.checkedAt < BINARY_LOOKUP_TTL_MS) return cached.path
|
|
72
|
+
|
|
73
|
+
const { spawnSync } = require('child_process')
|
|
74
|
+
const probe = spawnSync('/bin/zsh', ['-lc', `command -v ${binaryName} 2>/dev/null`], {
|
|
75
|
+
encoding: 'utf-8',
|
|
76
|
+
timeout: 2000,
|
|
77
|
+
})
|
|
78
|
+
const resolved = (probe.stdout || '').trim() || null
|
|
79
|
+
binaryLookupCache.set(binaryName, { checkedAt: now, path: resolved })
|
|
80
|
+
return resolved
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function coerceEnvMap(value: unknown): Record<string, string> | undefined {
|
|
84
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined
|
|
85
|
+
const out: Record<string, string> = {}
|
|
86
|
+
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
87
|
+
if (typeof v === 'string') out[k] = v
|
|
88
|
+
}
|
|
89
|
+
return Object.keys(out).length ? out : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function listDirRecursive(dir: string, depth: number, maxDepth: number): string[] {
|
|
93
|
+
if (depth > maxDepth) return []
|
|
94
|
+
const fs = require('fs')
|
|
95
|
+
const path = require('path')
|
|
96
|
+
const entries: string[] = []
|
|
97
|
+
try {
|
|
98
|
+
const items = fs.readdirSync(dir, { withFileTypes: true })
|
|
99
|
+
for (const item of items) {
|
|
100
|
+
if (item.name.startsWith('.') || item.name === 'node_modules') continue
|
|
101
|
+
const rel = item.name
|
|
102
|
+
if (item.isDirectory()) {
|
|
103
|
+
entries.push(rel + '/')
|
|
104
|
+
const sub = listDirRecursive(path.join(dir, item.name), depth + 1, maxDepth)
|
|
105
|
+
entries.push(...sub.map((s: string) => ` ${rel}/${s}`))
|
|
106
|
+
} else {
|
|
107
|
+
entries.push(rel)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
// permission error etc
|
|
112
|
+
}
|
|
113
|
+
return entries
|
|
114
|
+
}
|