/** * Copyright (c) 2026, Salesforce, Inc., * All rights reserved. * For full license text, see the LICENSE.txt file */ /* eslint-disable @typescript-eslint/no-explicit-any -- graphiti traverses untyped schema/introspection JSON; see follow-up to replace with `unknown` + narrowing */ import { CommandError, getSessionSchema, resolveDirectoryPathInSession, resolveAliasSegments, parsePathInput, formatAliasContext, detectSObjectName, printDirectory, } from "./query-helpers.js"; import { getOrgAuth } from "../lib/auth.js"; import { formatNavigationPath, parseSearchTerms, parseSearchRegex } from "../lib/formatter.js"; import { getObjectInfo } from "../lib/object-info.js"; import { loadSession, saveSession, syncFocusFromNavigationPath, getNavigationContext, isInArgsContext, queryNavToSchemaPath, } from "../lib/session.js"; import { getRootFields, resolvePath, type WalkerResult } from "../lib/walker.js"; export function queryPwd(sessionId: string): void { const session = loadSession(sessionId); console.log(formatNavigationPath(session.navigationPath)); const aliasCtx = formatAliasContext(session); if (aliasCtx) console.log(aliasCtx); } export async function queryLs( sessionId: string, opts: { search?: string; regex?: string; long?: boolean; all?: boolean; dataCloud?: boolean }, directories?: string[], ): Promise { const session = loadSession(sessionId); if (opts.long) { const sObjectName = detectSObjectName(session); if (sObjectName) { try { const auth = await getOrgAuth(session.orgAlias); await getObjectInfo(auth, session.orgAlias, sObjectName); } catch { // Ignore — picklist values just won't be shown } } } if (directories && directories.length > 0) { const schema = getSessionSchema(session); for (const rawDir of directories) { const dir = rawDir.includes(".") && !rawDir.includes("/") ? rawDir.replace(/\./g, "/") : rawDir; let resolved = parsePathInput(session.navigationPath, dir); if (resolved.length > 0 && resolved[0] !== "query" && resolved[0] !== "variables") { resolved = ["query", ...resolved]; } resolved = resolveAliasSegments(session, resolved); const ctx = getNavigationContext(resolved); if (ctx === "variables" || isInArgsContext(resolved)) { const tempSession = { ...session, navigationPath: resolved }; printDirectory(tempSession, opts); continue; } const schemaPath = queryNavToSchemaPath(resolved); if (schemaPath.length === 0) { let rootFields = getRootFields(schema, session.operation); const rootMatcher = opts.regex ? parseSearchRegex(opts.regex) : opts.search ? parseSearchTerms(opts.search) : undefined; if (rootMatcher) { rootFields = rootFields.filter((f) => rootMatcher.test(f.name)); } console.log(`${rawDir}/`); for (const f of rootFields) { console.log(` ${f.name}/`); } console.log(""); continue; } let wr: WalkerResult; try { wr = resolvePath(schema, session.operation, schemaPath); } catch (e: any) { console.log(`${rawDir}/ — ${e.message}`); console.log(""); continue; } if (wr.isLeaf) { console.log(`${rawDir} (${wr.typeName}) — leaf field`); console.log(""); continue; } console.log(`${rawDir}/`); const tempSession = { ...session, navigationPath: resolved }; printDirectory(tempSession, { ...opts, all: opts.all || false }); console.log(""); } return; } printDirectory(session, opts); } export function queryCd(sessionId: string, rawPath: string): void { const session = loadSession(sessionId); let nextPath: string[]; try { nextPath = resolveDirectoryPathInSession(session, rawPath); } catch (error: any) { throw new CommandError(`Invalid path: ${error.message}`); } session.navigationPath = nextPath; syncFocusFromNavigationPath(session); saveSession(session); console.log(formatNavigationPath(session.navigationPath)); printDirectory(session, { showFields: false }); }