nexus-ai-pro 2.1.0 → 2.3.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/API_STABILITY.md +77 -0
- package/CHANGELOG.md +286 -1
- package/README.md +19 -9
- package/dist/agent/capabilities.d.ts +39 -0
- package/dist/agent/capabilities.js +36 -0
- package/dist/agent/create-agent.d.ts +61 -11
- package/dist/agent/create-agent.js +136 -22
- package/dist/agent/index.d.ts +3 -2
- package/dist/agent/index.js +1 -1
- package/dist/agent/middleware/approval.d.ts +26 -0
- package/dist/agent/middleware/approval.js +23 -0
- package/dist/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
- package/dist/agent/{middleware.js → middleware/basic.js} +3 -5
- package/dist/agent/middleware/context-editor.d.ts +52 -0
- package/dist/agent/middleware/context-editor.js +66 -0
- package/dist/agent/middleware/filesystem.d.ts +44 -0
- package/dist/agent/middleware/filesystem.js +83 -0
- package/dist/agent/middleware/index.d.ts +19 -0
- package/dist/agent/middleware/index.js +9 -0
- package/dist/agent/middleware/limits.d.ts +42 -0
- package/dist/agent/middleware/limits.js +52 -0
- package/dist/agent/middleware/models.d.ts +48 -0
- package/dist/agent/middleware/models.js +44 -0
- package/dist/agent/middleware/pii.d.ts +67 -0
- package/dist/agent/middleware/pii.js +173 -0
- package/dist/agent/middleware/retry.d.ts +66 -0
- package/dist/agent/middleware/retry.js +59 -0
- package/dist/agent/middleware/shared.d.ts +41 -0
- package/dist/agent/middleware/shared.js +73 -0
- package/dist/agent/middleware/tool-selector.d.ts +64 -0
- package/dist/agent/middleware/tool-selector.js +106 -0
- package/dist/agent/permissions.d.ts +113 -0
- package/dist/agent/permissions.js +180 -0
- package/dist/agent/sandbox.d.ts +151 -0
- package/dist/agent/sandbox.js +257 -0
- package/dist/agent/tool.d.ts +8 -3
- package/dist/agent/tool.js +14 -3
- package/dist/cache/memory-cache.d.ts +8 -0
- package/dist/cache/memory-cache.js +9 -0
- package/dist/cache/semantic-cache.d.ts +6 -3
- package/dist/cache/semantic-cache.js +5 -2
- package/dist/cli/args.js +3 -0
- package/dist/cli/db.d.ts +16 -4
- package/dist/cli/db.js +169 -22
- package/dist/cli/doctor.d.ts +17 -0
- package/dist/cli/doctor.js +53 -0
- package/dist/cli.js +12 -1
- package/dist/core/lifecycle.js +11 -6
- package/dist/core/nexus.d.ts +6 -0
- package/dist/core/nexus.js +103 -22
- package/dist/deep-agent/index.d.ts +109 -0
- package/dist/deep-agent/index.js +216 -0
- package/dist/doctor/index.d.ts +90 -0
- package/dist/doctor/index.js +237 -0
- package/dist/graph/functional.d.ts +89 -7
- package/dist/graph/functional.js +156 -36
- package/dist/graph/graph.d.ts +15 -5
- package/dist/graph/graph.js +95 -22
- package/dist/graph/lint.d.ts +4 -1
- package/dist/graph/lint.js +35 -0
- package/dist/images/evals.js +4 -2
- package/dist/operations/adapters.d.ts +52 -4
- package/dist/operations/adapters.js +194 -7
- package/dist/operations/runner.js +1 -0
- package/dist/operations/stats.js +8 -1
- package/dist/operations/store.d.ts +5 -1
- package/dist/operations/store.js +7 -0
- package/dist/ops/circuit-breaker.d.ts +52 -0
- package/dist/ops/circuit-breaker.js +4 -0
- package/dist/ops/circuit-store.d.ts +13 -2
- package/dist/ops/circuit-store.js +68 -0
- package/dist/ops/circuit-sync.d.ts +17 -0
- package/dist/ops/circuit-sync.js +86 -0
- package/dist/ops/rate-limiter.d.ts +2 -0
- package/dist/ops/rate-limiter.js +2 -0
- package/dist/postgres/circuits.d.ts +21 -4
- package/dist/postgres/circuits.js +65 -4
- package/dist/postgres/client.d.ts +0 -2
- package/dist/postgres/client.js +0 -4
- package/dist/postgres/evaluate.d.ts +16 -4
- package/dist/postgres/evaluate.js +17 -5
- package/dist/postgres/fulltext.d.ts +67 -0
- package/dist/postgres/fulltext.js +91 -0
- package/dist/postgres/index.d.ts +32 -11
- package/dist/postgres/index.js +65 -31
- package/dist/postgres/migrations.d.ts +60 -0
- package/dist/postgres/migrations.js +121 -0
- package/dist/postgres/operations.d.ts +14 -3
- package/dist/postgres/operations.js +45 -15
- package/dist/postgres/prompts.d.ts +11 -2
- package/dist/postgres/prompts.js +14 -3
- package/dist/postgres/rollups.d.ts +35 -0
- package/dist/postgres/rollups.js +129 -0
- package/dist/postgres/store.d.ts +11 -2
- package/dist/postgres/store.js +14 -3
- package/dist/postgres/traces.d.ts +13 -3
- package/dist/postgres/traces.js +25 -3
- package/dist/postgres/vectors.d.ts +11 -2
- package/dist/postgres/vectors.js +14 -3
- package/dist/rag/elasticsearch.d.ts +71 -0
- package/dist/rag/elasticsearch.js +130 -0
- package/dist/rag/pipeline.d.ts +140 -0
- package/dist/rag/pipeline.js +187 -0
- package/dist/rag/rerankers.d.ts +92 -0
- package/dist/rag/rerankers.js +97 -0
- package/dist/rag/retrievers.d.ts +12 -1
- package/dist/router/auto-router.d.ts +34 -7
- package/dist/router/auto-router.js +201 -68
- package/dist/server/assistant.d.ts +9 -1
- package/dist/server/assistant.js +10 -0
- package/dist/server/auth.d.ts +250 -0
- package/dist/server/auth.js +392 -0
- package/dist/server/errors.d.ts +12 -0
- package/dist/server/errors.js +7 -0
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/runs.d.ts +24 -1
- package/dist/server/runs.js +90 -14
- package/dist/sqlite/index.d.ts +16 -3
- package/dist/sqlite/index.js +16 -3
- package/dist/sqlite/migrations.d.ts +26 -0
- package/dist/sqlite/migrations.js +63 -0
- package/dist/sqlite/operations.d.ts +14 -3
- package/dist/sqlite/operations.js +42 -15
- package/dist/sqlite/store.d.ts +9 -2
- package/dist/sqlite/store.js +13 -3
- package/dist/sqlite/vectors.d.ts +11 -2
- package/dist/sqlite/vectors.js +13 -2
- package/dist/store/memory.d.ts +10 -0
- package/dist/store/memory.js +10 -0
- package/dist/store/tenant.d.ts +12 -0
- package/dist/store/tenant.js +29 -0
- package/dist/tenancy/index.d.ts +113 -0
- package/dist/tenancy/index.js +233 -0
- package/dist/tracing/instrument.js +2 -0
- package/dist/types/config.d.ts +24 -2
- package/dist/types/graph.d.ts +49 -1
- package/dist/types/messages.d.ts +41 -2
- package/dist/types/operations.d.ts +6 -0
- package/dist/types/principal.d.ts +21 -0
- package/dist/types/principal.js +1 -0
- package/dist/types/response.d.ts +20 -0
- package/dist/types/server.d.ts +12 -10
- package/dist/utils/schema-migrations.d.ts +97 -0
- package/dist/utils/schema-migrations.js +91 -0
- package/dist/utils/signals.d.ts +17 -0
- package/dist/utils/signals.js +26 -0
- package/dist/utils/tenant.d.ts +25 -0
- package/dist/utils/tenant.js +30 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist-cjs/agent/capabilities.d.ts +39 -0
- package/dist-cjs/agent/capabilities.js +41 -0
- package/dist-cjs/agent/create-agent.d.ts +61 -11
- package/dist-cjs/agent/create-agent.js +136 -22
- package/dist-cjs/agent/index.d.ts +3 -2
- package/dist-cjs/agent/index.js +4 -4
- package/dist-cjs/agent/middleware/approval.d.ts +26 -0
- package/dist-cjs/agent/middleware/approval.js +26 -0
- package/dist-cjs/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
- package/dist-cjs/agent/{middleware.js → middleware/basic.js} +3 -5
- package/dist-cjs/agent/middleware/context-editor.d.ts +52 -0
- package/dist-cjs/agent/middleware/context-editor.js +70 -0
- package/dist-cjs/agent/middleware/filesystem.d.ts +44 -0
- package/dist-cjs/agent/middleware/filesystem.js +86 -0
- package/dist-cjs/agent/middleware/index.d.ts +19 -0
- package/dist-cjs/agent/middleware/index.js +28 -0
- package/dist-cjs/agent/middleware/limits.d.ts +42 -0
- package/dist-cjs/agent/middleware/limits.js +57 -0
- package/dist-cjs/agent/middleware/models.d.ts +48 -0
- package/dist-cjs/agent/middleware/models.js +48 -0
- package/dist-cjs/agent/middleware/pii.d.ts +67 -0
- package/dist-cjs/agent/middleware/pii.js +178 -0
- package/dist-cjs/agent/middleware/retry.d.ts +66 -0
- package/dist-cjs/agent/middleware/retry.js +63 -0
- package/dist-cjs/agent/middleware/shared.d.ts +41 -0
- package/dist-cjs/agent/middleware/shared.js +83 -0
- package/dist-cjs/agent/middleware/tool-selector.d.ts +64 -0
- package/dist-cjs/agent/middleware/tool-selector.js +109 -0
- package/dist-cjs/agent/permissions.d.ts +113 -0
- package/dist-cjs/agent/permissions.js +189 -0
- package/dist-cjs/agent/sandbox.d.ts +151 -0
- package/dist-cjs/agent/sandbox.js +297 -0
- package/dist-cjs/agent/tool.d.ts +8 -3
- package/dist-cjs/agent/tool.js +14 -3
- package/dist-cjs/cache/memory-cache.d.ts +8 -0
- package/dist-cjs/cache/memory-cache.js +9 -0
- package/dist-cjs/cache/semantic-cache.d.ts +6 -3
- package/dist-cjs/cache/semantic-cache.js +5 -2
- package/dist-cjs/cli/args.js +3 -0
- package/dist-cjs/cli/db.d.ts +16 -4
- package/dist-cjs/cli/db.js +200 -20
- package/dist-cjs/cli/doctor.d.ts +17 -0
- package/dist-cjs/cli/doctor.js +56 -0
- package/dist-cjs/core/lifecycle.js +11 -6
- package/dist-cjs/core/nexus.d.ts +6 -0
- package/dist-cjs/core/nexus.js +103 -22
- package/dist-cjs/deep-agent/index.d.ts +109 -0
- package/dist-cjs/deep-agent/index.js +219 -0
- package/dist-cjs/doctor/index.d.ts +90 -0
- package/dist-cjs/doctor/index.js +276 -0
- package/dist-cjs/graph/functional.d.ts +89 -7
- package/dist-cjs/graph/functional.js +155 -35
- package/dist-cjs/graph/graph.d.ts +15 -5
- package/dist-cjs/graph/graph.js +95 -22
- package/dist-cjs/graph/lint.d.ts +4 -1
- package/dist-cjs/graph/lint.js +35 -0
- package/dist-cjs/images/evals.js +4 -2
- package/dist-cjs/operations/adapters.d.ts +52 -4
- package/dist-cjs/operations/adapters.js +193 -6
- package/dist-cjs/operations/runner.js +1 -0
- package/dist-cjs/operations/stats.js +8 -1
- package/dist-cjs/operations/store.d.ts +5 -1
- package/dist-cjs/operations/store.js +7 -0
- package/dist-cjs/ops/circuit-breaker.d.ts +52 -0
- package/dist-cjs/ops/circuit-breaker.js +4 -0
- package/dist-cjs/ops/circuit-store.d.ts +13 -2
- package/dist-cjs/ops/circuit-store.js +68 -0
- package/dist-cjs/ops/circuit-sync.d.ts +17 -0
- package/dist-cjs/ops/circuit-sync.js +87 -0
- package/dist-cjs/ops/rate-limiter.d.ts +2 -0
- package/dist-cjs/ops/rate-limiter.js +2 -0
- package/dist-cjs/postgres/circuits.d.ts +21 -4
- package/dist-cjs/postgres/circuits.js +98 -3
- package/dist-cjs/postgres/client.d.ts +0 -2
- package/dist-cjs/postgres/client.js +0 -5
- package/dist-cjs/postgres/evaluate.d.ts +16 -4
- package/dist-cjs/postgres/evaluate.js +50 -4
- package/dist-cjs/postgres/fulltext.d.ts +67 -0
- package/dist-cjs/postgres/fulltext.js +129 -0
- package/dist-cjs/postgres/index.d.ts +32 -11
- package/dist-cjs/postgres/index.js +69 -20
- package/dist-cjs/postgres/migrations.d.ts +60 -0
- package/dist-cjs/postgres/migrations.js +129 -0
- package/dist-cjs/postgres/operations.d.ts +14 -3
- package/dist-cjs/postgres/operations.js +78 -14
- package/dist-cjs/postgres/prompts.d.ts +11 -2
- package/dist-cjs/postgres/prompts.js +47 -2
- package/dist-cjs/postgres/rollups.d.ts +35 -0
- package/dist-cjs/postgres/rollups.js +168 -0
- package/dist-cjs/postgres/store.d.ts +11 -2
- package/dist-cjs/postgres/store.js +47 -2
- package/dist-cjs/postgres/traces.d.ts +13 -3
- package/dist-cjs/postgres/traces.js +58 -2
- package/dist-cjs/postgres/vectors.d.ts +11 -2
- package/dist-cjs/postgres/vectors.js +47 -2
- package/dist-cjs/rag/elasticsearch.d.ts +71 -0
- package/dist-cjs/rag/elasticsearch.js +135 -0
- package/dist-cjs/rag/pipeline.d.ts +140 -0
- package/dist-cjs/rag/pipeline.js +191 -0
- package/dist-cjs/rag/rerankers.d.ts +92 -0
- package/dist-cjs/rag/rerankers.js +107 -0
- package/dist-cjs/rag/retrievers.d.ts +12 -1
- package/dist-cjs/router/auto-router.d.ts +34 -7
- package/dist-cjs/router/auto-router.js +201 -68
- package/dist-cjs/server/assistant.d.ts +9 -1
- package/dist-cjs/server/assistant.js +10 -0
- package/dist-cjs/server/auth.d.ts +250 -0
- package/dist-cjs/server/auth.js +402 -0
- package/dist-cjs/server/errors.d.ts +12 -0
- package/dist-cjs/server/errors.js +9 -1
- package/dist-cjs/server/index.d.ts +1 -1
- package/dist-cjs/server/index.js +2 -1
- package/dist-cjs/server/runs.d.ts +24 -1
- package/dist-cjs/server/runs.js +89 -13
- package/dist-cjs/sqlite/index.d.ts +16 -3
- package/dist-cjs/sqlite/index.js +31 -10
- package/dist-cjs/sqlite/migrations.d.ts +26 -0
- package/dist-cjs/sqlite/migrations.js +71 -0
- package/dist-cjs/sqlite/operations.d.ts +14 -3
- package/dist-cjs/sqlite/operations.js +76 -15
- package/dist-cjs/sqlite/store.d.ts +9 -2
- package/dist-cjs/sqlite/store.js +47 -3
- package/dist-cjs/sqlite/vectors.d.ts +11 -2
- package/dist-cjs/sqlite/vectors.js +47 -2
- package/dist-cjs/store/memory.d.ts +10 -0
- package/dist-cjs/store/memory.js +10 -0
- package/dist-cjs/store/tenant.d.ts +12 -0
- package/dist-cjs/store/tenant.js +33 -0
- package/dist-cjs/tenancy/index.d.ts +113 -0
- package/dist-cjs/tenancy/index.js +250 -0
- package/dist-cjs/tracing/instrument.js +2 -0
- package/dist-cjs/types/config.d.ts +24 -2
- package/dist-cjs/types/graph.d.ts +49 -1
- package/dist-cjs/types/messages.d.ts +41 -2
- package/dist-cjs/types/operations.d.ts +6 -0
- package/dist-cjs/types/principal.d.ts +21 -0
- package/dist-cjs/types/principal.js +2 -0
- package/dist-cjs/types/response.d.ts +20 -0
- package/dist-cjs/types/server.d.ts +12 -10
- package/dist-cjs/utils/schema-migrations.d.ts +97 -0
- package/dist-cjs/utils/schema-migrations.js +102 -0
- package/dist-cjs/utils/signals.d.ts +17 -0
- package/dist-cjs/utils/signals.js +29 -0
- package/dist-cjs/utils/tenant.d.ts +25 -0
- package/dist-cjs/utils/tenant.js +37 -0
- package/dist-cjs/version.d.ts +1 -1
- package/dist-cjs/version.js +1 -1
- package/llms.txt +6 -4
- package/package.json +188 -2
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { agentInput, createAgent, } from '../agent/create-agent.js';
|
|
2
|
+
import { contextEditor } from '../agent/middleware/context-editor.js';
|
|
3
|
+
import { filesystemContext } from '../agent/middleware/filesystem.js';
|
|
4
|
+
import { toolSelector } from '../agent/middleware/tool-selector.js';
|
|
5
|
+
import { permissionPolicy } from '../agent/permissions.js';
|
|
6
|
+
import { sandboxTools } from '../agent/sandbox.js';
|
|
7
|
+
import { tool } from '../agent/tool.js';
|
|
8
|
+
const MOUNT = '/workspace';
|
|
9
|
+
const CORE_TOOLS = [
|
|
10
|
+
'write_todos',
|
|
11
|
+
'delegate',
|
|
12
|
+
'load_skill',
|
|
13
|
+
'read_file',
|
|
14
|
+
'write_file',
|
|
15
|
+
'edit_file',
|
|
16
|
+
'list_files',
|
|
17
|
+
'run_command',
|
|
18
|
+
];
|
|
19
|
+
export function createDeepAgent(options) {
|
|
20
|
+
const { instructions, sandbox, commands = [], planning = true, subagents = [], skills = [], include = ['AGENTS.md'], offload, contextEditing, toolSelection, permissions, middleware = [], tools: extraTools = [], ...agentOptions } = options;
|
|
21
|
+
const workspace = sandbox
|
|
22
|
+
? [...sandboxTools(sandbox, { shell: commands.length > 0, mount: MOUNT }), editFileTool(sandbox)]
|
|
23
|
+
: [];
|
|
24
|
+
const own = [
|
|
25
|
+
...(planning ? [writeTodosTool()] : []),
|
|
26
|
+
...(skills.length > 0 ? [loadSkillTool(skills, sandbox)] : []),
|
|
27
|
+
...workspace,
|
|
28
|
+
...extraTools,
|
|
29
|
+
];
|
|
30
|
+
const helpers = new Map();
|
|
31
|
+
const approved = new Set(Object.keys(agentOptions.interruptOn ?? {}));
|
|
32
|
+
const { interruptOn: _approvals, ...helperOptions } = agentOptions;
|
|
33
|
+
for (const helper of subagents) {
|
|
34
|
+
const allowed = helper.tools ? new Set(helper.tools) : undefined;
|
|
35
|
+
helpers.set(helper.name, createAgent({
|
|
36
|
+
...helperOptions,
|
|
37
|
+
name: `${options.name ?? 'deep-agent'}.${helper.name}`,
|
|
38
|
+
...(helper.model ? { model: helper.model } : {}),
|
|
39
|
+
maxIterations: helper.maxIterations ?? 20,
|
|
40
|
+
systemPrompt: `${helper.instructions}\n\n${WORKSPACE_NOTE}`,
|
|
41
|
+
tools: own.filter((candidate) => !approved.has(candidate.name) && (allowed ? allowed.has(candidate.name) : true)),
|
|
42
|
+
...(permissions === false ? {} : { permissions: permissions ?? defaultPolicy(commands) }),
|
|
43
|
+
checkpointer: false,
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
const tools = [...own, ...(helpers.size > 0 ? [delegateTool(subagents, helpers)] : [])];
|
|
47
|
+
const kit = [
|
|
48
|
+
...(sandbox && (include.length > 0 || offload !== false)
|
|
49
|
+
? [
|
|
50
|
+
filesystemContext({
|
|
51
|
+
source: sandbox,
|
|
52
|
+
include,
|
|
53
|
+
...(offload === false ? {} : { offload: { overChars: 20_000, ...offload } }),
|
|
54
|
+
}),
|
|
55
|
+
]
|
|
56
|
+
: []),
|
|
57
|
+
...(contextEditing === false
|
|
58
|
+
? []
|
|
59
|
+
: [
|
|
60
|
+
contextEditor({
|
|
61
|
+
triggerTokens: 60_000,
|
|
62
|
+
...contextEditing,
|
|
63
|
+
exclude: ['write_todos', ...(contextEditing?.exclude ?? [])],
|
|
64
|
+
}),
|
|
65
|
+
]),
|
|
66
|
+
...(toolSelection
|
|
67
|
+
? [toolSelector({ ...toolSelection, always: [...CORE_TOOLS, ...(toolSelection.always ?? [])] })]
|
|
68
|
+
: []),
|
|
69
|
+
];
|
|
70
|
+
return createAgent({
|
|
71
|
+
maxIterations: 40,
|
|
72
|
+
...agentOptions,
|
|
73
|
+
systemPrompt: systemPrompt(instructions, { planning, sandbox: Boolean(sandbox), commands, subagents, skills }),
|
|
74
|
+
tools,
|
|
75
|
+
middleware: [...kit, ...middleware],
|
|
76
|
+
...(permissions === false ? {} : { permissions: permissions ?? defaultPolicy(commands) }),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function defaultPolicy(commands) {
|
|
80
|
+
return permissionPolicy({
|
|
81
|
+
filesystem: { write: [`${MOUNT}/**`] },
|
|
82
|
+
shell: { allow: commands },
|
|
83
|
+
tools: { write_todos: 'allow', delegate: 'allow', load_skill: 'allow' },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const WORKSPACE_NOTE = `Files live in ${MOUNT}; paths are relative to it.`;
|
|
87
|
+
function systemPrompt(instructions, parts) {
|
|
88
|
+
const lines = [];
|
|
89
|
+
if (instructions)
|
|
90
|
+
lines.push(instructions, '');
|
|
91
|
+
lines.push('Work through the task step by step, checking each step before the next.');
|
|
92
|
+
if (parts.planning) {
|
|
93
|
+
lines.push('For any task of more than two steps, first record a plan with write_todos, then keep it current: mark a step in_progress when you start it and completed when it is done.');
|
|
94
|
+
}
|
|
95
|
+
if (parts.sandbox) {
|
|
96
|
+
lines.push(`${WORKSPACE_NOTE} Read before you change a file; prefer edit_file to rewriting a whole file. A large result may be saved to a file and shown as a preview: read the file when you need the rest.`);
|
|
97
|
+
if (parts.commands.length > 0)
|
|
98
|
+
lines.push(`You may run these commands: ${parts.commands.join(', ')}.`);
|
|
99
|
+
}
|
|
100
|
+
if (parts.subagents.length > 0) {
|
|
101
|
+
lines.push('', 'Delegate a self-contained piece of work with delegate, to one of:');
|
|
102
|
+
for (const helper of parts.subagents)
|
|
103
|
+
lines.push(`- ${helper.name}: ${helper.description}`);
|
|
104
|
+
}
|
|
105
|
+
if (parts.skills.length > 0) {
|
|
106
|
+
lines.push('', 'Skills you can load with load_skill when a task calls for one:');
|
|
107
|
+
for (const skill of parts.skills)
|
|
108
|
+
lines.push(`- ${skill.name}: ${skill.description}`);
|
|
109
|
+
}
|
|
110
|
+
return lines.join('\n');
|
|
111
|
+
}
|
|
112
|
+
function writeTodosTool() {
|
|
113
|
+
return tool({
|
|
114
|
+
name: 'write_todos',
|
|
115
|
+
description: 'Records the plan for this task as a list of steps, each pending, in_progress, or completed. Send the whole list each time.',
|
|
116
|
+
parameters: {
|
|
117
|
+
type: 'object',
|
|
118
|
+
properties: {
|
|
119
|
+
todos: {
|
|
120
|
+
type: 'array',
|
|
121
|
+
items: {
|
|
122
|
+
type: 'object',
|
|
123
|
+
properties: {
|
|
124
|
+
content: { type: 'string' },
|
|
125
|
+
status: { type: 'string', enum: ['pending', 'in_progress', 'completed'] },
|
|
126
|
+
},
|
|
127
|
+
required: ['content', 'status'],
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
required: ['todos'],
|
|
132
|
+
},
|
|
133
|
+
capabilities: [],
|
|
134
|
+
execute: ({ todos }) => {
|
|
135
|
+
const list = Array.isArray(todos) ? todos : [];
|
|
136
|
+
const mark = { pending: '[ ]', in_progress: '[~]', completed: '[x]' };
|
|
137
|
+
const done = list.filter((todo) => todo.status === 'completed').length;
|
|
138
|
+
return `Plan (${done}/${list.length} done):\n${list.map((todo) => `${mark[todo.status] ?? '[ ]'} ${todo.content}`).join('\n')}`;
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function editFileTool(sandbox) {
|
|
143
|
+
const local = (file) => String(file ?? '')
|
|
144
|
+
.replace(/\\/g, '/')
|
|
145
|
+
.replace(new RegExp(`^${MOUNT}/`), '')
|
|
146
|
+
.replace(/^\/+/, '');
|
|
147
|
+
return tool({
|
|
148
|
+
name: 'edit_file',
|
|
149
|
+
description: `Replaces one exact occurrence of old_text with new_text in a file. Paths are relative to ${MOUNT}.`,
|
|
150
|
+
parameters: {
|
|
151
|
+
type: 'object',
|
|
152
|
+
properties: { path: { type: 'string' }, old_text: { type: 'string' }, new_text: { type: 'string' } },
|
|
153
|
+
required: ['path', 'old_text', 'new_text'],
|
|
154
|
+
},
|
|
155
|
+
capabilities: (args) => [`filesystem:write:${MOUNT}/${local(args.path)}`],
|
|
156
|
+
execute: async ({ path, old_text: oldText, new_text: newText }) => {
|
|
157
|
+
const file = local(path);
|
|
158
|
+
const text = await sandbox.readFile(file);
|
|
159
|
+
const count = text.split(String(oldText)).length - 1;
|
|
160
|
+
if (count === 0)
|
|
161
|
+
throw new Error(`old_text was not found in ${file}`);
|
|
162
|
+
if (count > 1)
|
|
163
|
+
throw new Error(`old_text appears ${count} times in ${file}; include more context so it appears once`);
|
|
164
|
+
await sandbox.writeFile(file, text.replace(String(oldText), () => String(newText)));
|
|
165
|
+
return { edited: file };
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function loadSkillTool(skills, sandbox) {
|
|
170
|
+
const byName = new Map(skills.map((skill) => [skill.name, skill]));
|
|
171
|
+
return tool({
|
|
172
|
+
name: 'load_skill',
|
|
173
|
+
description: 'Loads the instructions of a skill by name.',
|
|
174
|
+
parameters: {
|
|
175
|
+
type: 'object',
|
|
176
|
+
properties: { name: { type: 'string', enum: skills.map((skill) => skill.name) } },
|
|
177
|
+
required: ['name'],
|
|
178
|
+
},
|
|
179
|
+
capabilities: [],
|
|
180
|
+
execute: async ({ name }) => {
|
|
181
|
+
const skill = byName.get(String(name));
|
|
182
|
+
if (!skill)
|
|
183
|
+
throw new Error(`No skill named "${String(name)}"`);
|
|
184
|
+
if (skill.instructions !== undefined)
|
|
185
|
+
return skill.instructions;
|
|
186
|
+
if (skill.file && sandbox)
|
|
187
|
+
return sandbox.readFile(skill.file);
|
|
188
|
+
throw new Error(`Skill "${skill.name}" has no instructions`);
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function delegateTool(subagents, helpers) {
|
|
193
|
+
return tool({
|
|
194
|
+
name: 'delegate',
|
|
195
|
+
description: 'Hands a self-contained task to a helper, which works in a fresh context and returns its answer.',
|
|
196
|
+
parameters: {
|
|
197
|
+
type: 'object',
|
|
198
|
+
properties: {
|
|
199
|
+
agent: { type: 'string', enum: subagents.map((helper) => helper.name) },
|
|
200
|
+
task: { type: 'string', description: 'Everything the helper needs to know: it sees nothing else.' },
|
|
201
|
+
},
|
|
202
|
+
required: ['agent', 'task'],
|
|
203
|
+
},
|
|
204
|
+
capabilities: [],
|
|
205
|
+
execute: async ({ agent, task }, context) => {
|
|
206
|
+
const helper = helpers.get(String(agent));
|
|
207
|
+
if (!helper)
|
|
208
|
+
throw new Error(`No helper named "${String(agent)}"`);
|
|
209
|
+
const result = await helper.invoke(agentInput(String(task)), {
|
|
210
|
+
...(context.principal ? { principal: context.principal } : {}),
|
|
211
|
+
...(context.signal ? { signal: context.signal } : {}),
|
|
212
|
+
});
|
|
213
|
+
return (result.state.answer || `The ${String(agent)} helper finished without an answer (${result.state.stopReason}).`);
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { SchemaMigration } from '../utils/schema-migrations.js';
|
|
2
|
+
/** How one check came out: `fail` breaks the deployment, `warn` needs a look, `skip` had nothing to check. */
|
|
3
|
+
export type DoctorStatus = 'ok' | 'warn' | 'fail' | 'skip';
|
|
4
|
+
/** One check's result. */
|
|
5
|
+
export interface DoctorCheck {
|
|
6
|
+
/** A stable id, such as `node` or `migrations`. */
|
|
7
|
+
id: string;
|
|
8
|
+
/** What was checked, in a few words. */
|
|
9
|
+
title: string;
|
|
10
|
+
/** How it came out. */
|
|
11
|
+
status: DoctorStatus;
|
|
12
|
+
/** What was found. Never a secret's value. */
|
|
13
|
+
detail: string;
|
|
14
|
+
/** What to do about a warning or a failure. */
|
|
15
|
+
hint?: string;
|
|
16
|
+
}
|
|
17
|
+
/** Every check's result, and whether the deployment can run. */
|
|
18
|
+
export interface DoctorReport {
|
|
19
|
+
/** The checks, in the order they ran. */
|
|
20
|
+
checks: DoctorCheck[];
|
|
21
|
+
/** True when nothing failed. */
|
|
22
|
+
ok: boolean;
|
|
23
|
+
/** Checks that failed. */
|
|
24
|
+
failures: number;
|
|
25
|
+
/** Checks that warned. */
|
|
26
|
+
warnings: number;
|
|
27
|
+
}
|
|
28
|
+
/** A database for the doctor to reach, as `nexus db` reaches one. */
|
|
29
|
+
export interface DoctorDatabase {
|
|
30
|
+
/** A Postgres client (`query`) or a SQLite handle or client. */
|
|
31
|
+
client: unknown;
|
|
32
|
+
/** Which kind it is. Detected from the client when omitted. */
|
|
33
|
+
dialect?: 'postgres' | 'sqlite';
|
|
34
|
+
/** The migrations it should have. Defaults to every bundled adapter's on its default tables. */
|
|
35
|
+
migrations?: readonly SchemaMigration[];
|
|
36
|
+
/** The table that records applied migrations. */
|
|
37
|
+
table?: string;
|
|
38
|
+
}
|
|
39
|
+
/** What the doctor knows about how the application is deployed. */
|
|
40
|
+
export interface DoctorDeployment {
|
|
41
|
+
/** How many replicas run it. Settings that keep state in one process are checked only above 1. */
|
|
42
|
+
replicas: number;
|
|
43
|
+
/**
|
|
44
|
+
* The stores and shared state it uses, by any name: a checkpointer, the operation store, the
|
|
45
|
+
* server's state, tenant usage, a rate-limit store, a circuit store, a cache. Each is reported when
|
|
46
|
+
* it keeps its state in process memory, where each replica would see only its own.
|
|
47
|
+
*/
|
|
48
|
+
stores?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
/** What `diagnose()` checks. Everything is optional; a check without its input is skipped. */
|
|
51
|
+
export interface DoctorOptions {
|
|
52
|
+
/** The Node.js version to check. Defaults to the running one. */
|
|
53
|
+
nodeVersion?: string;
|
|
54
|
+
/** Where credentials are read from. Defaults to `process.env`. Only names are reported. */
|
|
55
|
+
env?: Record<string, string | undefined>;
|
|
56
|
+
/** Whether a package is installed. Defaults to resolving it from the working directory. */
|
|
57
|
+
resolvePackage?: (name: string) => boolean;
|
|
58
|
+
/** A database: connectivity, and migrations pending or changed. */
|
|
59
|
+
database?: DoctorDatabase;
|
|
60
|
+
/** A Redis client to ping. */
|
|
61
|
+
redis?: {
|
|
62
|
+
ping(): unknown;
|
|
63
|
+
};
|
|
64
|
+
/** An operation store, read for leases that lapsed and a queue nobody drains. */
|
|
65
|
+
operations?: {
|
|
66
|
+
stats?(now: string): unknown;
|
|
67
|
+
list?(): unknown;
|
|
68
|
+
};
|
|
69
|
+
/** How long the oldest queued operation may wait before the queue counts as stalled. Defaults to 15 minutes. */
|
|
70
|
+
maxQueueAgeMs?: number;
|
|
71
|
+
/** A client, read for provider health. */
|
|
72
|
+
ai?: {
|
|
73
|
+
getProviderHealth(): ReadonlyArray<{
|
|
74
|
+
providerName: string;
|
|
75
|
+
healthy: boolean;
|
|
76
|
+
stale?: boolean;
|
|
77
|
+
}>;
|
|
78
|
+
};
|
|
79
|
+
/** How old the bundled model registry may be, in days. Defaults to the registry's own window. */
|
|
80
|
+
registryMaxAgeDays?: number;
|
|
81
|
+
/** How the application is deployed. */
|
|
82
|
+
deployment?: DoctorDeployment;
|
|
83
|
+
/** Replaces the clock, for tests. */
|
|
84
|
+
now?: () => Date;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Runs every check its options allow and reports each, never throwing: a check that cannot run is a
|
|
88
|
+
* failure with the reason.
|
|
89
|
+
*/
|
|
90
|
+
export declare function diagnose(options?: DoctorOptions): Promise<DoctorReport>;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
const PEERS = {
|
|
4
|
+
openai: 'the OpenAI SDK, used only where an application passes one in',
|
|
5
|
+
'@anthropic-ai/sdk': 'the Anthropic SDK, likewise',
|
|
6
|
+
ollama: 'the Ollama client',
|
|
7
|
+
zod: 'zod schemas for tools and structured output',
|
|
8
|
+
ajv: 'JSON Schema validation of structured output',
|
|
9
|
+
'ajv-formats': 'formats for ajv',
|
|
10
|
+
};
|
|
11
|
+
const CREDENTIALS = {
|
|
12
|
+
openai: ['OPENAI_API_KEY'],
|
|
13
|
+
anthropic: ['ANTHROPIC_API_KEY'],
|
|
14
|
+
google: ['GOOGLE_API_KEY', 'GEMINI_API_KEY'],
|
|
15
|
+
'azure-openai': ['AZURE_OPENAI_API_KEY'],
|
|
16
|
+
groq: ['GROQ_API_KEY'],
|
|
17
|
+
mistral: ['MISTRAL_API_KEY'],
|
|
18
|
+
cohere: ['COHERE_API_KEY', 'CO_API_KEY'],
|
|
19
|
+
deepseek: ['DEEPSEEK_API_KEY'],
|
|
20
|
+
openrouter: ['OPENROUTER_API_KEY'],
|
|
21
|
+
};
|
|
22
|
+
const PROCESS_LOCAL = {
|
|
23
|
+
MemoryGraphCheckpointer: { status: 'fail', why: 'a thread started on one replica cannot be resumed on another' },
|
|
24
|
+
MemoryOperationStore: { status: 'fail', why: 'operations and server runs are invisible to other replicas' },
|
|
25
|
+
MemoryServerStore: { status: 'fail', why: 'threads and runs exist only on the replica that created them' },
|
|
26
|
+
MemoryRunEventLog: { status: 'fail', why: 'a client reconnecting to another replica cannot follow its run' },
|
|
27
|
+
MemoryTenantUsage: { status: 'fail', why: 'each replica counts its own tenant limits, multiplying them' },
|
|
28
|
+
MemoryRateLimitStore: { status: 'warn', why: 'each replica enforces its own limit, multiplying it' },
|
|
29
|
+
MemoryCircuitStateStore: { status: 'warn', why: 'a circuit opened on one replica stays closed on the others' },
|
|
30
|
+
MemoryStore: { status: 'warn', why: 'long-term memory written on one replica is missing on the others' },
|
|
31
|
+
MemoryTraceStore: { status: 'warn', why: 'each replica keeps only its own traces' },
|
|
32
|
+
MemoryPromptStore: { status: 'warn', why: 'a prompt promoted on one replica is not served by the others' },
|
|
33
|
+
MemoryCacheAdapter: { status: 'warn', why: 'each replica caches separately' },
|
|
34
|
+
MemoryRollupStore: { status: 'warn', why: 'each replica sums only its own runs' },
|
|
35
|
+
};
|
|
36
|
+
export async function diagnose(options = {}) {
|
|
37
|
+
const now = options.now ?? (() => new Date());
|
|
38
|
+
const checks = [];
|
|
39
|
+
const run = async (id, title, check) => {
|
|
40
|
+
try {
|
|
41
|
+
checks.push({ id, title, ...(await check()) });
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
checks.push({ id, title, status: 'fail', detail: error instanceof Error ? error.message : String(error) });
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
await run('node', 'Node.js version', () => nodeCheck(options.nodeVersion ?? process.versions.node));
|
|
48
|
+
await run('peers', 'Optional packages', () => peersCheck(options.resolvePackage ?? defaultResolver()));
|
|
49
|
+
await run('credentials', 'Provider credentials', () => credentialsCheck(options.env ?? process.env));
|
|
50
|
+
await run('registry', 'Model registry age', () => registryCheck(now(), options.registryMaxAgeDays));
|
|
51
|
+
await run('database', 'Database connectivity', () => databaseCheck(options.database));
|
|
52
|
+
await run('migrations', 'Schema migrations', () => migrationsCheck(options.database));
|
|
53
|
+
await run('redis', 'Redis', () => redisCheck(options.redis));
|
|
54
|
+
await run('queue', 'Operation queue and leases', () => queueCheck(options.operations, now(), options.maxQueueAgeMs));
|
|
55
|
+
await run('health', 'Provider health', () => healthCheck(options.ai));
|
|
56
|
+
await run('topology', 'Settings that only work in one process', () => topologyCheck(options.deployment));
|
|
57
|
+
const failures = checks.filter((check) => check.status === 'fail').length;
|
|
58
|
+
const warnings = checks.filter((check) => check.status === 'warn').length;
|
|
59
|
+
return { checks, ok: failures === 0, failures, warnings };
|
|
60
|
+
}
|
|
61
|
+
function nodeCheck(version) {
|
|
62
|
+
const major = Number(version.replace(/^v/, '').split('.')[0]);
|
|
63
|
+
return major >= 22
|
|
64
|
+
? { status: 'ok', detail: `Node.js ${version}` }
|
|
65
|
+
: { status: 'fail', detail: `Node.js ${version}; this package needs 22 or newer`, hint: 'Upgrade Node.js.' };
|
|
66
|
+
}
|
|
67
|
+
function defaultResolver() {
|
|
68
|
+
const resolve = createRequire(path.join(process.cwd(), 'package.json')).resolve;
|
|
69
|
+
return (name) => {
|
|
70
|
+
try {
|
|
71
|
+
resolve(name);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function peersCheck(resolve) {
|
|
80
|
+
const installed = Object.keys(PEERS).filter((name) => resolve(name));
|
|
81
|
+
const missing = Object.keys(PEERS).filter((name) => !installed.includes(name));
|
|
82
|
+
return {
|
|
83
|
+
status: 'ok',
|
|
84
|
+
detail: `Installed: ${installed.length ? installed.join(', ') : 'none'}. Not installed: ${missing.length ? missing.join(', ') : 'none'}. Every one is optional; install only what you use.`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function credentialsCheck(env) {
|
|
88
|
+
const found = Object.entries(CREDENTIALS)
|
|
89
|
+
.filter(([, names]) => names.some((name) => Boolean(env[name]?.trim())))
|
|
90
|
+
.map(([provider]) => provider);
|
|
91
|
+
return found.length
|
|
92
|
+
? { status: 'ok', detail: `Credentials found for ${found.join(', ')}.` }
|
|
93
|
+
: {
|
|
94
|
+
status: 'warn',
|
|
95
|
+
detail: 'No provider credential found in the environment.',
|
|
96
|
+
hint: 'Set the API key of each hosted provider you use, such as OPENAI_API_KEY. Local providers need none.',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function registryCheck(now, maxAgeDays) {
|
|
100
|
+
const { checkRegistryFreshness } = await import('../models/registry.js');
|
|
101
|
+
const freshness = checkRegistryFreshness({ now, ...(maxAgeDays === undefined ? {} : { maxAgeDays }) });
|
|
102
|
+
return freshness.stale
|
|
103
|
+
? {
|
|
104
|
+
status: 'warn',
|
|
105
|
+
detail: `The bundled registry was verified ${freshness.ageDays} days ago (${freshness.verifiedAt}), past its ${freshness.maxAgeDays}-day window.`,
|
|
106
|
+
hint: 'Upgrade nexus-ai-pro, or override stale entries with models.registry.',
|
|
107
|
+
}
|
|
108
|
+
: { status: 'ok', detail: `Verified ${freshness.ageDays} days ago (${freshness.verifiedAt}).` };
|
|
109
|
+
}
|
|
110
|
+
function dialectOf(database) {
|
|
111
|
+
if (database.dialect)
|
|
112
|
+
return database.dialect;
|
|
113
|
+
const client = database.client;
|
|
114
|
+
if (client && typeof client.query === 'function')
|
|
115
|
+
return 'postgres';
|
|
116
|
+
return 'sqlite';
|
|
117
|
+
}
|
|
118
|
+
async function databaseCheck(database) {
|
|
119
|
+
if (!database)
|
|
120
|
+
return { status: 'skip', detail: 'No database given.' };
|
|
121
|
+
const dialect = dialectOf(database);
|
|
122
|
+
if (dialect === 'postgres') {
|
|
123
|
+
await database.client.query('SELECT 1');
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const { toSqliteClient } = await import('../sqlite/client.js');
|
|
127
|
+
await toSqliteClient(database.client).all('SELECT 1', []);
|
|
128
|
+
}
|
|
129
|
+
return { status: 'ok', detail: `Reached the ${dialect === 'postgres' ? 'Postgres' : 'SQLite'} database.` };
|
|
130
|
+
}
|
|
131
|
+
async function migrationsCheck(database) {
|
|
132
|
+
if (!database)
|
|
133
|
+
return { status: 'skip', detail: 'No database given.' };
|
|
134
|
+
const dialect = dialectOf(database);
|
|
135
|
+
const options = database.table ? { table: database.table } : {};
|
|
136
|
+
let status;
|
|
137
|
+
if (dialect === 'postgres') {
|
|
138
|
+
const [{ postgresMigrations }, { postgresMigrationStatus }] = await Promise.all([
|
|
139
|
+
import('../postgres/index.js'),
|
|
140
|
+
import('../postgres/migrations.js'),
|
|
141
|
+
]);
|
|
142
|
+
status = await postgresMigrationStatus(database.client, database.migrations ?? postgresMigrations(), options);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const [{ sqliteMigrations }, { sqliteMigrationStatus }] = await Promise.all([
|
|
146
|
+
import('../sqlite/index.js'),
|
|
147
|
+
import('../sqlite/migrations.js'),
|
|
148
|
+
]);
|
|
149
|
+
status = await sqliteMigrationStatus(database.client, database.migrations ?? sqliteMigrations(), options);
|
|
150
|
+
}
|
|
151
|
+
if (status.changed.length) {
|
|
152
|
+
return {
|
|
153
|
+
status: 'fail',
|
|
154
|
+
detail: `${status.changed.length} migration${status.changed.length === 1 ? '' : 's'} changed after running: ${status.changed.map(({ migration }) => `${migration.component} v${migration.version}`).join(', ')}.`,
|
|
155
|
+
hint: 'A migration never changes once applied. Check the adapter options match those it ran with.',
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (status.pending.length) {
|
|
159
|
+
return {
|
|
160
|
+
status: 'fail',
|
|
161
|
+
detail: `${status.pending.length} pending: ${status.pending.map((migration) => `${migration.component} v${migration.version}`).join(', ')}.`,
|
|
162
|
+
hint: 'Run: nexus db migrate --client db.mjs',
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const newer = status.unknown.length ? ` ${status.unknown.length} recorded by a newer release.` : '';
|
|
166
|
+
return { status: 'ok', detail: `${status.applied.length} applied, none pending.${newer}` };
|
|
167
|
+
}
|
|
168
|
+
async function redisCheck(redis) {
|
|
169
|
+
if (!redis)
|
|
170
|
+
return { status: 'skip', detail: 'No Redis client given.' };
|
|
171
|
+
const reply = await redis.ping();
|
|
172
|
+
return { status: 'ok', detail: `PING answered ${String(reply)}.` };
|
|
173
|
+
}
|
|
174
|
+
async function queueCheck(operations, now, maxQueueAgeMs = 15 * 60_000) {
|
|
175
|
+
if (!operations)
|
|
176
|
+
return { status: 'skip', detail: 'No operation store given.' };
|
|
177
|
+
const { operationStats } = await import('../operations/stats.js');
|
|
178
|
+
const stats = await operationStats(operations, { now });
|
|
179
|
+
const problems = [];
|
|
180
|
+
if (stats.lapsedLeases > 0) {
|
|
181
|
+
problems.push(`${stats.lapsedLeases} running operation${stats.lapsedLeases === 1 ? '' : 's'} hold a lease that lapsed`);
|
|
182
|
+
}
|
|
183
|
+
const age = stats.oldestQueuedAt ? now.getTime() - Date.parse(stats.oldestQueuedAt) : 0;
|
|
184
|
+
if (age > maxQueueAgeMs)
|
|
185
|
+
problems.push(`the oldest queued operation has waited ${Math.round(age / 60_000)} minutes`);
|
|
186
|
+
const counts = Object.entries(stats.byStatus)
|
|
187
|
+
.map(([status, count]) => `${count} ${status}`)
|
|
188
|
+
.join(', ');
|
|
189
|
+
return problems.length
|
|
190
|
+
? {
|
|
191
|
+
status: 'warn',
|
|
192
|
+
detail: `${problems.join('; ')}. (${counts || 'nothing unfinished'})`,
|
|
193
|
+
hint: 'Run recovery — OperationRunner.recover(), or the agent server with recoverEveryMs — and a worker that claims the queue.',
|
|
194
|
+
}
|
|
195
|
+
: { status: 'ok', detail: counts ? `Unfinished: ${counts}. No lapsed lease.` : 'Nothing unfinished.' };
|
|
196
|
+
}
|
|
197
|
+
function healthCheck(ai) {
|
|
198
|
+
if (!ai)
|
|
199
|
+
return { status: 'skip', detail: 'No client given.' };
|
|
200
|
+
const snapshots = ai.getProviderHealth();
|
|
201
|
+
if (snapshots.length === 0)
|
|
202
|
+
return { status: 'skip', detail: 'Health tracking is off, or no provider has been called.' };
|
|
203
|
+
const stale = snapshots.filter((item) => item.stale).map((item) => item.providerName);
|
|
204
|
+
const unhealthy = snapshots.filter((item) => !item.healthy && !item.stale).map((item) => item.providerName);
|
|
205
|
+
const issues = [
|
|
206
|
+
...(stale.length ? [`stale: ${stale.join(', ')}`] : []),
|
|
207
|
+
...(unhealthy.length ? [`unhealthy: ${unhealthy.join(', ')}`] : []),
|
|
208
|
+
];
|
|
209
|
+
return issues.length
|
|
210
|
+
? {
|
|
211
|
+
status: 'warn',
|
|
212
|
+
detail: `${issues.join('; ')}.`,
|
|
213
|
+
hint: 'Refresh stale entries with ai.checkProviders({ staleOnly: true }).',
|
|
214
|
+
}
|
|
215
|
+
: { status: 'ok', detail: `${snapshots.length} provider${snapshots.length === 1 ? '' : 's'} healthy.` };
|
|
216
|
+
}
|
|
217
|
+
function topologyCheck(deployment) {
|
|
218
|
+
if (!deployment)
|
|
219
|
+
return { status: 'skip', detail: 'No deployment described.' };
|
|
220
|
+
if (deployment.replicas <= 1)
|
|
221
|
+
return { status: 'ok', detail: 'One replica: in-process state is shared by everything.' };
|
|
222
|
+
const findings = [];
|
|
223
|
+
for (const [name, value] of Object.entries(deployment.stores ?? {})) {
|
|
224
|
+
const kind = value?.constructor?.name;
|
|
225
|
+
const known = kind ? PROCESS_LOCAL[kind] : undefined;
|
|
226
|
+
if (known)
|
|
227
|
+
findings.push({ status: known.status, text: `${name} is a ${kind}: ${known.why}` });
|
|
228
|
+
}
|
|
229
|
+
if (findings.length === 0) {
|
|
230
|
+
return { status: 'ok', detail: `${deployment.replicas} replicas, and nothing keeps its state in one process.` };
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
status: findings.some((finding) => finding.status === 'fail') ? 'fail' : 'warn',
|
|
234
|
+
detail: `${deployment.replicas} replicas: ${findings.map((finding) => finding.text).join('; ')}.`,
|
|
235
|
+
hint: 'Use the Redis, Postgres, or SQLite adapter for each, or run one replica.',
|
|
236
|
+
};
|
|
237
|
+
}
|