Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/desktop/electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ export default defineConfig({
resolve: {
alias: {
'@shared': resolve(__dirname, '../../packages/shared-domain/src'),
'@bridge-contract': resolve(__dirname, '../../packages/bridge-contract/src')
'@bridge-contract': resolve(__dirname, '../../packages/bridge-contract/src'),
// The MCP server's daily_note and note_sections read periodic-note
// settings and outlines the way the app does, from these store-free
// modules (lib/vault-layout, lib/outline, lib/template-render).
'@renderer': resolve(__dirname, '../../packages/app-core/src')
}
}
},
Expand Down
117 changes: 101 additions & 16 deletions apps/desktop/src/cli/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,43 @@ import {
createFolder,
createNote,
deleteFolder,
deleteAsset,
deleteNote,
duplicateAsset,
duplicateNote,
emptyTrash,
importAsset,
insertAtLine,
insertAtLineInBody,
listAssets,
listDatabaseDirs,
listDeletedAssets,
listFolders,
listNotes,
listTemplateFiles,
moveAsset,
moveNote,
moveToTrash,
parseTaskIndex,
prependToBody,
prependToNote,
readAsset,
readDatabaseVaultLayout,
readNote,
readNoteComments,
readPrimaryNotesLocation,
readTemplateFolder,
readVaultFileTextOrNull,
readVaultSettingsRaw,
renameAsset,
renameFolder,
renameNote,
replaceInBody,
replaceInNote,
restoreDeletedAsset,
restoreFromTrash,
scanAllTasks,
scanTasksForPath,
searchText,
splitTaskId,
toggleFileTaskInBody,
Expand All @@ -60,12 +71,16 @@ import {
writeNote,
writeNoteComments,
writeVaultFileText,
type CustomTemplateFile,
type DeletedVaultAsset,
type ImportedVaultAsset,
type NoteComment,
type NoteCommentInput,
type NoteContent,
type NoteFolder,
type NoteMeta,
type PrimaryNotesLocation,
type VaultAssetMeta,
type VaultTask,
type VaultTextSearchMatch
} from '../mcp/vault-ops.js'
Expand All @@ -92,15 +107,7 @@ import { RemoteRequestError } from '../main/remote/connection.js'
import { CliRemoteClient } from './remote/client.js'
import type { VaultTarget } from './vault-target.js'

export interface VaultAssetMeta {
path: string
name: string
size: number
updatedAt: number
/** Listed from a link rather than a file. Only a server's listing holds
* these, and marks them; see NoteMeta.symlink. */
symlink?: boolean
}
export type { VaultAssetMeta }

/** Where a vault lives and how it is laid out: what `vault_info` reports. */
export type VaultDescription =
Expand Down Expand Up @@ -148,6 +155,32 @@ export interface VaultBackend {
templateFolder(): Promise<string | null>
listNotes(): Promise<NoteMeta[]>
listAssets(): Promise<VaultAssetMeta[]>
/** An attachment's bytes. */
readAsset(rel: string): Promise<Uint8Array>
/** Add a file to the vault's `assets/` folder, never overwriting. */
importAsset(filename: string, bytes: Uint8Array): Promise<ImportedVaultAsset>
/** Rename or move an attachment and re-target the notes that reference it.
* `rewriteIn` limits which notes a vault on this machine rewrites; a server
* rewrites every note itself and takes no such limit. */
renameAsset(
rel: string,
name: string,
rewriteIn?: (notePath: string) => boolean
): Promise<VaultAssetMeta>
moveAsset(
rel: string,
targetDir: string,
rewriteIn?: (notePath: string) => boolean
): Promise<VaultAssetMeta>
duplicateAsset(rel: string): Promise<VaultAssetMeta>
/** A soft delete the app's Trash can undo. Purging is left to the app. */
deleteAsset(rel: string): Promise<DeletedVaultAsset>
listDeletedAssets(): Promise<DeletedVaultAsset[]>
restoreDeletedAsset(deleted: DeletedVaultAsset): Promise<VaultAssetMeta>
/** The vault's settings as stored, for periodic-note locations. */
vaultSettings(): Promise<Record<string, unknown>>
/** The vault's own templates, as raw files. */
listTemplates(): Promise<CustomTemplateFile[]>
listFolders(): Promise<{ folder: NoteFolder; subpath: string }[]>
readNote(rel: string): Promise<NoteContent>
writeNote(rel: string, body: string): Promise<NoteMeta>
Expand Down Expand Up @@ -181,6 +214,8 @@ export interface VaultBackend {
searchText(query: string, limit: number): Promise<VaultTextSearchMatch[]>
backlinks(rel: string): Promise<NoteMeta[]>
scanAllTasks(opts?: { includeExcluded?: boolean }): Promise<VaultTask[]>
/** One note's tasks, numbered exactly as scanAllTasks numbers them. */
scanTasksForPath(rel: string, opts?: { includeExcluded?: boolean }): Promise<VaultTask[]>
toggleTask(taskId: string): Promise<VaultTask | null>
/** A note's comments as stored (#738); `writeComments` replaces the list. */
listComments(rel: string): Promise<NoteComment[]>
Expand Down Expand Up @@ -251,6 +286,17 @@ async function followRecordPageRename(
await ops.writeDatabaseSchema(csvPath, sidecarOf(next), next.rows)
}

/** The fields both engines report for an attachment; a server sends more. */
function plainAssetMeta(asset: VaultAssetMeta): VaultAssetMeta {
return {
path: asset.path,
name: asset.name,
size: asset.size,
updatedAt: asset.updatedAt,
...(asset.symlink ? { symlink: true } : {})
}
}

/** A vault on this machine. Every method is vault-ops bound to one root. */
class LocalBackend implements VaultBackend {
readonly kind = 'local' as const
Expand Down Expand Up @@ -287,6 +333,26 @@ class LocalBackend implements VaultBackend {
return linked
}
listAssets = (): Promise<VaultAssetMeta[]> => listAssets(this.root)
readAsset = (rel: string): Promise<Uint8Array> => readAsset(this.root, rel)
importAsset = (filename: string, bytes: Uint8Array): Promise<ImportedVaultAsset> =>
importAsset(this.root, filename, bytes)
renameAsset = (
rel: string,
name: string,
rewriteIn?: (notePath: string) => boolean
): Promise<VaultAssetMeta> => renameAsset(this.root, rel, name, rewriteIn)
moveAsset = (
rel: string,
targetDir: string,
rewriteIn?: (notePath: string) => boolean
): Promise<VaultAssetMeta> => moveAsset(this.root, rel, targetDir, rewriteIn)
duplicateAsset = (rel: string): Promise<VaultAssetMeta> => duplicateAsset(this.root, rel)
deleteAsset = (rel: string): Promise<DeletedVaultAsset> => deleteAsset(this.root, rel)
listDeletedAssets = (): Promise<DeletedVaultAsset[]> => listDeletedAssets(this.root)
restoreDeletedAsset = (deleted: DeletedVaultAsset): Promise<VaultAssetMeta> =>
restoreDeletedAsset(this.root, deleted.undoToken)
vaultSettings = (): Promise<Record<string, unknown>> => readVaultSettingsRaw(this.root)
listTemplates = (): Promise<CustomTemplateFile[]> => listTemplateFiles(this.root)
listFolders = (): Promise<{ folder: NoteFolder; subpath: string }[]> => listFolders(this.root)
readNote = (rel: string): Promise<NoteContent> => readNote(this.root, rel)
writeNote = (rel: string, body: string): Promise<NoteMeta> => writeNote(this.root, rel, body)
Expand Down Expand Up @@ -334,6 +400,8 @@ class LocalBackend implements VaultBackend {
backlinks = (rel: string): Promise<NoteMeta[]> => backlinks(this.root, rel)
scanAllTasks = (opts?: { includeExcluded?: boolean }): Promise<VaultTask[]> =>
scanAllTasks(this.root, opts)
scanTasksForPath = (rel: string, opts?: { includeExcluded?: boolean }): Promise<VaultTask[]> =>
scanTasksForPath(this.root, rel, opts)
toggleTask = (taskId: string): Promise<VaultTask | null> => toggleTask(this.root, taskId)
listComments = (rel: string): Promise<NoteComment[]> => readNoteComments(this.root, rel)
writeComments = (rel: string, comments: NoteCommentInput[]): Promise<NoteComment[]> =>
Expand Down Expand Up @@ -411,13 +479,27 @@ class RemoteBackend implements VaultBackend {
}
listNotes = (): Promise<NoteMeta[]> => this.client.listNotes()
listAssets = async (): Promise<VaultAssetMeta[]> =>
(await this.client.listAssets()).map((asset) => ({
path: asset.path,
name: asset.name,
size: asset.size,
updatedAt: asset.updatedAt,
...(asset.symlink ? { symlink: true } : {})
}))
(await this.client.listAssets()).map(plainAssetMeta)
readAsset = (rel: string): Promise<Uint8Array> => this.client.readAsset(rel)
importAsset = (filename: string, bytes: Uint8Array): Promise<ImportedVaultAsset> =>
this.client.uploadAsset(filename, bytes)
/** The server rewrites links in every note itself, so `rewriteIn` has no
* meaning here; a client that sees part of the vault is refused before this
* (ScopedBackend). */
renameAsset = (rel: string, name: string): Promise<VaultAssetMeta> =>
this.client.renameAsset(rel, name).then(plainAssetMeta)
moveAsset = (rel: string, targetDir: string): Promise<VaultAssetMeta> =>
this.client.moveAsset(rel, targetDir).then(plainAssetMeta)
duplicateAsset = (rel: string): Promise<VaultAssetMeta> =>
this.client.duplicateAsset(rel).then(plainAssetMeta)
deleteAsset = (rel: string): Promise<DeletedVaultAsset> => this.client.deleteAsset(rel)
listDeletedAssets = async (): Promise<DeletedVaultAsset[]> =>
(await this.client.listDeletedAssets()) ?? []
restoreDeletedAsset = (deleted: DeletedVaultAsset): Promise<VaultAssetMeta> =>
this.client.restoreDeletedAsset(deleted).then(plainAssetMeta)
vaultSettings = (): Promise<Record<string, unknown>> => this.client.getVaultSettings()
listTemplates = async (): Promise<CustomTemplateFile[]> =>
(await this.client.listTemplates()) ?? []
listFolders = (): Promise<{ folder: NoteFolder; subpath: string }[]> =>
this.client.listFolders()
readNote = (rel: string): Promise<NoteContent> => this.client.readNote(rel)
Expand Down Expand Up @@ -507,6 +589,9 @@ class RemoteBackend implements VaultBackend {
scanAllTasks = (opts?: { includeExcluded?: boolean }): Promise<VaultTask[]> =>
this.client.scanTasks(opts)

scanTasksForPath = (rel: string, opts?: { includeExcluded?: boolean }): Promise<VaultTask[]> =>
this.client.scanTasksForPath(normalizeRelPath(rel), opts)

listComments = (rel: string): Promise<NoteComment[]> =>
this.client.readComments(normalizeRelPath(rel))
writeComments = (rel: string, comments: NoteCommentInput[]): Promise<NoteComment[]> =>
Expand Down
59 changes: 57 additions & 2 deletions apps/desktop/src/cli/remote/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The slice of the ZenNotes server API the `zn` CLI needs (#493).
*
* A deliberately smaller thing than the desktop app's RemoteServerClient:
* no WebSocket watcher, no assets, no comments, no settings. A CLI command
* no WebSocket watcher. A CLI command
* runs once and exits, so there is nothing to keep in sync — and staying
* fetch-only is what lets this ship inside the standalone `cli.js` bundle
* (see the note in main/remote/connection.ts).
Expand All @@ -12,8 +12,15 @@
* commands keep printing exactly the fields they print for a local vault.
*/

import { remoteJsonRequest } from '../../main/remote/connection.js'
import {
remoteBytesRequest,
remoteFormRequest,
remoteJsonRequest
} from '../../main/remote/connection.js'
import type {
CustomTemplateFile,
DeletedVaultAsset,
ImportedVaultAsset,
NoteComment,
NoteCommentInput,
NoteContent,
Expand Down Expand Up @@ -76,6 +83,7 @@ export class CliRemoteClient {
primaryNotesLocation?: string
systemFolderPaths?: Record<string, string> | null
templates?: unknown
[key: string]: unknown
}> {
return this.get('/api/vault/settings')
}
Expand Down Expand Up @@ -119,6 +127,21 @@ export class CliRemoteClient {
return this.get<RemoteAssetMeta[]>('/api/assets')
}

readAsset(relPath: string): Promise<Uint8Array> {
return remoteBytesRequest(`/api/assets/raw?path=${encodeURIComponent(relPath)}`, {
baseUrl: this.baseUrl,
authToken: this.authToken
})
}

listDeletedAssets(): Promise<DeletedVaultAsset[] | null> {
return this.get<DeletedVaultAsset[] | null>('/api/assets/deleted')
}

listTemplates(): Promise<CustomTemplateFile[] | null> {
return this.get<CustomTemplateFile[] | null>('/api/templates')
}

scanTasksForPath(
relPath: string,
opts?: { includeExcluded?: boolean }
Expand All @@ -131,6 +154,38 @@ export class CliRemoteClient {

/* --- writes --- */

/** Multipart, not JSON: the server reads a `file` form part plus the owning
* note's path, which an upload from a tool does not have. */
uploadAsset(filename: string, bytes: Uint8Array): Promise<ImportedVaultAsset> {
const form = new FormData()
form.append('notePath', '')
form.append('file', new Blob([bytes as Uint8Array<ArrayBuffer>]), filename)
return remoteFormRequest<ImportedVaultAsset>('/api/assets/upload', form, {
baseUrl: this.baseUrl,
authToken: this.authToken
})
}

renameAsset(relPath: string, name: string): Promise<RemoteAssetMeta> {
return this.post<RemoteAssetMeta>('/api/assets/rename', { path: relPath, name })
}

moveAsset(relPath: string, targetDir: string): Promise<RemoteAssetMeta> {
return this.post<RemoteAssetMeta>('/api/assets/move', { path: relPath, targetDir })
}

duplicateAsset(relPath: string): Promise<RemoteAssetMeta> {
return this.post<RemoteAssetMeta>('/api/assets/duplicate', { path: relPath })
}

deleteAsset(relPath: string): Promise<DeletedVaultAsset> {
return this.post<DeletedVaultAsset>('/api/assets/delete', { path: relPath })
}

restoreDeletedAsset(deleted: DeletedVaultAsset): Promise<RemoteAssetMeta> {
return this.post<RemoteAssetMeta>('/api/assets/restore', deleted)
}

writeNote(relPath: string, body: string): Promise<NoteMeta> {
return this.post<NoteMeta>('/api/notes/write', { path: relPath, body })
}
Expand Down
Loading
Loading