Compare commits
133 Commits
4859c9708a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 655069a28a | |||
| 593f1646a1 | |||
| c77450d497 | |||
| 23f05b72f7 | |||
| dd53ca3fbc | |||
| 6c9dd9605b | |||
| 46c239df56 | |||
| 2fd132e827 | |||
| 0f3f1efa08 | |||
| e4fa61ae07 | |||
| 8fe18d108f | |||
| b517672663 | |||
| b016f6d812 | |||
| 505527ae4f | |||
| b6b1d16e54 | |||
| 46fd6c1b85 | |||
| 5433cb59ac | |||
| e027364c0e | |||
| 3675a26407 | |||
| 9a1f301527 | |||
| 59333ac920 | |||
| 381deb417d | |||
| 5801e49dc1 | |||
| a8ca3b643b | |||
| 39bb232b57 | |||
| 824313f106 | |||
| 01b90e00a0 | |||
| c77686250e | |||
| 48e0903542 | |||
| df2bdc2041 | |||
| a3ca44640c | |||
| 3a8fafd2fd | |||
| 16f45d77b2 | |||
| 700ed79c43 | |||
| 6ec61610f1 | |||
| 89859684eb | |||
| dc35518bad | |||
| 4a03fc0ba2 | |||
| 10fa174388 | |||
| 8ee51c6626 | |||
| d31ceb72cb | |||
| c8d2c73a72 | |||
| a772e3abea | |||
| c735c1afa7 | |||
| d85654d4b2 | |||
| ae2e534fdb | |||
| ae1c8703e7 | |||
| d944c07838 | |||
| bb82f1d4a1 | |||
| 7dfcd78abd | |||
| 8e60fe31a5 | |||
| 28398b17f4 | |||
| cf1d4bd46f | |||
| 7fa6e6232c | |||
| fcc574428c | |||
| 1917043c10 | |||
| 4b52ac6156 | |||
| 1487c0337e | |||
| 43f04dcd86 | |||
| c07ba9a4d8 | |||
| ef6c970654 | |||
| e7d5cba01b | |||
| 82c69a2c35 | |||
| b11be9b521 | |||
| 1b0bae37bf | |||
| c2b2b1ff11 | |||
| 5f77641f3f | |||
| ba9634c478 | |||
| 49675a49d2 | |||
| d1acfd4d71 | |||
| 68de265137 | |||
| 57bdcef990 | |||
| 52809b39b1 | |||
| e3e94ac2d9 | |||
| d78cb5c07b | |||
| 211271efca | |||
| c2cecd9383 | |||
| d0e1ac5559 | |||
| cea47f10e2 | |||
| e1abe457e8 | |||
| 562eed2b98 | |||
| 7c38061c1c | |||
| 67a1f8a5f0 | |||
| d3c21247e0 | |||
| cd4a01a2c8 | |||
| b93b650689 | |||
| e284f030ae | |||
| 4c9c432d33 | |||
| 64ca5d4637 | |||
| a89175e230 | |||
| 26f06f3298 | |||
| 6a7a8c4288 | |||
| 62f87b8ab4 | |||
| ff777e590f | |||
| 7f1dbc390c | |||
| 59c236d871 | |||
| c3a8a8a755 | |||
| 2e61059568 | |||
| 936a20a03e | |||
| 0da149e105 | |||
| dc6fc4c631 | |||
| d3e2397217 | |||
| 3b14c1ecf3 | |||
| bb58f98afd | |||
| 66c24b565f | |||
| ccfaa4afe6 | |||
| f46239188a | |||
| 4a8a420c62 | |||
| b3d69f0291 | |||
| f8f6a242d6 | |||
| 341c1bf8ee | |||
| 4f6f7a6e1e | |||
| 72f6cc7d97 | |||
| f52e33760e | |||
| e87c35e256 | |||
| 2d98780007 | |||
| 26c481ea1f | |||
| 65d89a198e | |||
| 8f073bbebd | |||
| 4dd4781c5a | |||
| a00e4b85ac | |||
| caaec98225 | |||
| 4b1557cf6a | |||
| 985d8b53c2 | |||
| 941db4c6f4 | |||
| a73af6b44d | |||
| e2054c9c12 | |||
| 28e08451e4 | |||
| abbcef594a | |||
| 8224b3d59f | |||
| f7e1662bca | |||
| ae66775cb7 | |||
| 741979fc39 |
53
.credo.exs
Normal file
53
.credo.exs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
%{
|
||||||
|
configs: [
|
||||||
|
%{
|
||||||
|
name: "default",
|
||||||
|
files: %{
|
||||||
|
included: ["lib/", "test/", "config/", "mix.exs"],
|
||||||
|
excluded: [~r"/deps/", ~r"/_build/", ~r"/priv/static/"]
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
parse_timeout: 10_000,
|
||||||
|
color: true,
|
||||||
|
checks: [
|
||||||
|
{Credo.Check.Consistency.ExceptionNames},
|
||||||
|
{Credo.Check.Consistency.LineEndings},
|
||||||
|
{Credo.Check.Consistency.SpaceAroundOperators},
|
||||||
|
{Credo.Check.Consistency.SpaceInParentheses},
|
||||||
|
{Credo.Check.Consistency.TabsOrSpaces},
|
||||||
|
{Credo.Check.Design.AliasUsage, false},
|
||||||
|
{Credo.Check.Readability.BlockPipe, false},
|
||||||
|
{Credo.Check.Readability.AliasOrder, false},
|
||||||
|
{Credo.Check.Readability.LargeNumbers, false},
|
||||||
|
{Credo.Check.Readability.MaxLineLength, false},
|
||||||
|
{Credo.Check.Readability.ModuleDoc, false},
|
||||||
|
{Credo.Check.Readability.PreferImplicitTry, false},
|
||||||
|
{Credo.Check.Readability.Semicolons, false},
|
||||||
|
{Credo.Check.Readability.StringSigils, false},
|
||||||
|
{Credo.Check.Readability.TrailingBlankLine, false},
|
||||||
|
{Credo.Check.Readability.UnnecessaryAliasExpansion, false},
|
||||||
|
{Credo.Check.Readability.WithSingleClause, false},
|
||||||
|
{Credo.Check.Refactor.Apply, false},
|
||||||
|
{Credo.Check.Refactor.CondStatements, false},
|
||||||
|
{Credo.Check.Refactor.CyclomaticComplexity, false},
|
||||||
|
{Credo.Check.Refactor.FilterFilter, false},
|
||||||
|
{Credo.Check.Refactor.FilterReject, false},
|
||||||
|
{Credo.Check.Refactor.FunctionArity, false},
|
||||||
|
{Credo.Check.Refactor.MapJoin, false},
|
||||||
|
{Credo.Check.Refactor.Nesting, false},
|
||||||
|
{Credo.Check.Refactor.NegatedConditionsWithElse, false},
|
||||||
|
{Credo.Check.Refactor.RejectFilter, false},
|
||||||
|
{Credo.Check.Refactor.RejectReject, false},
|
||||||
|
{Credo.Check.Refactor.RedundantWithClauseResult, false},
|
||||||
|
{Credo.Check.Warning.ApplicationConfigInModuleAttribute},
|
||||||
|
{Credo.Check.Warning.BoolOperationOnSameValues},
|
||||||
|
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
|
||||||
|
{Credo.Check.Warning.IExPry},
|
||||||
|
{Credo.Check.Warning.LazyLogging},
|
||||||
|
{Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, false},
|
||||||
|
{Credo.Check.Warning.OperationOnSameValues},
|
||||||
|
{Credo.Check.Warning.RaiseInsideRescue}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Downloaded JS — exclude from language detection
|
||||||
|
priv/**/*.js linguist-vendored=true
|
||||||
4
.mix_audit.ignore
Normal file
4
.mix_audit.ignore
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# GHSA-rhv4-8758-jx7v is pinned transitively through bumblebee -> progress_bar.
|
||||||
|
# ecto_sqlite3 0.24.x can move to decimal 3.x, but that line is currently
|
||||||
|
# unsatisfiable alongside the app's Bumblebee dependency.
|
||||||
|
GHSA-rhv4-8758-jx7v
|
||||||
57
AGENTS.md
57
AGENTS.md
@@ -28,8 +28,9 @@ This document provides context and best practices for GitHub Copilot when workin
|
|||||||
- we have an allium spec in the specs/ folder. you must weed the specs against built code to make sure you follow the spec.
|
- we have an allium spec in the specs/ folder. you must weed the specs against built code to make sure you follow the spec.
|
||||||
- when changing the spec, validate the spec with the available command line tool.
|
- when changing the spec, validate the spec with the available command line tool.
|
||||||
- you MUST run tests with command line tools at least once to capture compile errors in tests, do not use the integrated testing of vscode, as that blocks on compile errors
|
- you MUST run tests with command line tools at least once to capture compile errors in tests, do not use the integrated testing of vscode, as that blocks on compile errors
|
||||||
- you MUST run build, test and check dialyzer messages and you MUST treet warnings as errors and fix them. we want clean builds, clean tests and clean dialyzer results
|
- you MUST run build, test, credo, deps.audit and check dialyzer messages and you MUST treet warnings as errors and fix them. we want clean builds, clean tests, clean credo, clean dependency audits and clean dialyzer results
|
||||||
- on a headless Linux machine, you have to run tests with this command (if mix test complains about DISPLAX): xvfb-run mix test
|
- on a headless Linux machine, you have to run tests with this command (if mix test complains about DISPLAX): xvfb-run mix test
|
||||||
|
- CSS precedence: Tailwind utilities and the ui-* kit (utilities.css) are in @layer and ALWAYS lose to the unlayered hand-written files (shell.css, panel.css, ...). Convention: hand-written CSS owns appearance, Tailwind utilities in templates own layout — never put display/flex-direction/size rules in hand-written CSS for elements whose layout the template controls, and define each selector in exactly one file.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -55,7 +56,16 @@ This document provides context and best practices for GitHub Copilot when workin
|
|||||||
|
|
||||||
- Never leave tests failing, even if they appear unrelated to your changes
|
- Never leave tests failing, even if they appear unrelated to your changes
|
||||||
- If a test failure is pre-existing, fix it as part of your current work
|
- If a test failure is pre-existing, fix it as part of your current work
|
||||||
- Run the full test suite (`npm test`) before considering any task complete
|
- **Always write the full test run to a log file first, then grep that file for failures.** Never run `mix test` in the terminal and try to grep live output — the output is interleaved, truncated, and nearly impossible to parse. Pattern:
|
||||||
|
```
|
||||||
|
mix test 2>&1 > /tmp/test_run.log
|
||||||
|
grep -E "FAILED|^[ ]*1\)" /tmp/test_run.log
|
||||||
|
grep -A 15 "^[ ]*1\)" /tmp/test_run.log # details for the first failure
|
||||||
|
```
|
||||||
|
Work from the log file, not from repeated terminal runs.
|
||||||
|
|
||||||
|
> **Zero failing tests. No exceptions.**
|
||||||
|
- Run the full test suite (`mix test`) before considering any task complete
|
||||||
- If you cannot fix a test, explain why and propose a solution
|
- If you cannot fix a test, explain why and propose a solution
|
||||||
|
|
||||||
> **Zero failing tests. No exceptions.**
|
> **Zero failing tests. No exceptions.**
|
||||||
@@ -102,11 +112,52 @@ This document provides context and best practices for GitHub Copilot when workin
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## ⚠️ MANDATORY: macOS Bundle Must Be Fully Standalone
|
||||||
|
|
||||||
|
**The built `BDS2.app` MUST run on a Mac without Homebrew.**
|
||||||
|
|
||||||
|
- No Mach-O inside the `.app` may reference a Homebrew/local path (`/opt/homebrew`, `/usr/local`)
|
||||||
|
- Every external dylib reachable from the release's NIFs must be copied into `Contents/Frameworks` and rewritten to `@loader_path`-relative install names (`BDS.MacBundle.Dylibs`)
|
||||||
|
- The same physical dylib referenced under multiple names must be deduped (one real file + symlinks) so dyld loads a single image — duplicate images cause duplicate Objective-C class / wx event-table registrations
|
||||||
|
- `BDS.MacBundle.verify_standalone/1` runs after relocation and fails the build if any external reference remains — do not weaken or bypass this gate
|
||||||
|
|
||||||
|
> **The bundle must be self-contained. No outside references. No exceptions.**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ⚠️ MANDATORY: macOS Bundle Build Order and Blogmark Verification
|
||||||
|
|
||||||
|
**`mix release` copies `priv/static` as-is — it never rebuilds assets.** Always build in this order, or the bundle ships stale JS while the server code is current (this silently broke blogmark deep links in July 2026: the server waited for the client's `shell_ready` event that the stale `app.js` never sent, so every deep link queued forever with no error, no toast, no log):
|
||||||
|
|
||||||
|
```
|
||||||
|
mix assets.deploy
|
||||||
|
MIX_ENV=prod mix release bds --overwrite
|
||||||
|
mix bds.bundle.macos
|
||||||
|
```
|
||||||
|
|
||||||
|
**After building a bundle, verify blogmark deep links end-to-end with:**
|
||||||
|
|
||||||
|
```
|
||||||
|
scripts/verify_blogmark_macos.sh [path/to/BDS2.app] # defaults to dist/macos/BDS2.app
|
||||||
|
```
|
||||||
|
|
||||||
|
The script launches the bundle via LaunchServices with an isolated database (`launchctl setenv BDS_DATABASE_PATH ...`), fires `bds2://new-post` links with `open -a` exactly like a browser bookmarklet, and asserts draft posts appear in the database — both for cold start (link launches the app, queued until the shell attaches) and for an already-running app. It refuses to run while any BDS2 instance is up; quit the app first.
|
||||||
|
|
||||||
|
Things learned the hard way, encoded in the script — do not "simplify" them away:
|
||||||
|
|
||||||
|
- Deep links only reach an app **launched through LaunchServices** (`open -a`). A process started by exec'ing `Contents/MacOS/bds2` from a shell never receives Apple Events, so deep links vanish without a trace. For the same reason `open` does not inherit the shell environment — inject env vars with `launchctl setenv` (and unset them afterwards).
|
||||||
|
- To introspect a running bundle, launch it with `RELEASE_DISTRIBUTION=name` and `RELEASE_NODE=bds@127.0.0.1` set (via `launchctl setenv`), then use `.../Contents/Resources/rel/bin/bds rpc '<elixir>'` with the same env. The default `-sname` node is unreachable when the hostname resolves to LAN addresses.
|
||||||
|
- The wx → `Desktop.Env` → `BDS.Desktop.DeepLink` → shell pipeline downstream of the OS can be exercised without LaunchServices via `send(Process.whereis(BDS.Desktop.DeepLink), {:open_url, ~c"bds2://new-post?..."})` over rpc.
|
||||||
|
|
||||||
|
> **Assets before release. Verify blogmarks after bundling. No exceptions.**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## ⚠️ MANDATORY: Proper I18N for UI and Rendering Text
|
## ⚠️ MANDATORY: Proper I18N for UI and Rendering Text
|
||||||
|
|
||||||
**All user-facing text MUST follow proper i18n patterns.**
|
**All user-facing text MUST follow proper i18n patterns.**
|
||||||
|
|
||||||
- Do not hardcode UI strings directly in React components, menu templates, dialogs, or toasts
|
- Do not hardcode UI strings directly in LiveView/HEEx components, menu templates, dialogs, or toasts
|
||||||
- Store UI copy in language resources and resolve text through i18n helpers/hooks
|
- Store UI copy in language resources and resolve text through i18n helpers/hooks
|
||||||
- UI language MUST come from the operating system locale
|
- UI language MUST come from the operating system locale
|
||||||
- Rendering/preview/generated-content language MUST come from project preferences (`mainLanguage`), not UI locale
|
- Rendering/preview/generated-content language MUST come from project preferences (`mainLanguage`), not UI locale
|
||||||
|
|||||||
29
API.md
29
API.md
@@ -40,6 +40,7 @@ local meta = bds.meta.get_project_metadata()
|
|||||||
- [app.get_default_project_path](#appget_default_project_path)
|
- [app.get_default_project_path](#appget_default_project_path)
|
||||||
- [app.get_system_language](#appget_system_language)
|
- [app.get_system_language](#appget_system_language)
|
||||||
- [app.get_title_bar_metrics](#appget_title_bar_metrics)
|
- [app.get_title_bar_metrics](#appget_title_bar_metrics)
|
||||||
|
- [app.log](#applog)
|
||||||
- [app.notify_renderer_ready](#appnotify_renderer_ready)
|
- [app.notify_renderer_ready](#appnotify_renderer_ready)
|
||||||
- [app.open_folder](#appopen_folder)
|
- [app.open_folder](#appopen_folder)
|
||||||
- [app.read_project_metadata](#appread_project_metadata)
|
- [app.read_project_metadata](#appread_project_metadata)
|
||||||
@@ -150,7 +151,7 @@ local result = bds.app.get_default_project_path()
|
|||||||
|
|
||||||
### app.get_system_language
|
### app.get_system_language
|
||||||
|
|
||||||
Return the current UI locale.
|
Return the current UI locale (the server-side UI language setting, falling back to the OS locale when unset).
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
@@ -202,6 +203,30 @@ nil -- or
|
|||||||
local result = bds.app.get_title_bar_metrics()
|
local result = bds.app.get_title_bar_metrics()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### app.log
|
||||||
|
|
||||||
|
Append a line to the script output stream. Multiple arguments are joined with spaces. Output appears in the desktop app's Output panel (and on stdout in the CLI); Lua's global `print` is routed the same way.
|
||||||
|
|
||||||
|
**Parameters**
|
||||||
|
|
||||||
|
- text (string, required)
|
||||||
|
|
||||||
|
**Response specification**
|
||||||
|
|
||||||
|
- Return type: `boolean`
|
||||||
|
|
||||||
|
**Example response**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
true
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example call**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local result = bds.app.log("value")
|
||||||
|
```
|
||||||
|
|
||||||
### app.notify_renderer_ready
|
### app.notify_renderer_ready
|
||||||
|
|
||||||
Notify the host application that the renderer is ready.
|
Notify the host application that the renderer is ready.
|
||||||
@@ -3443,7 +3468,7 @@ local result = bds.meta.sync_on_startup()
|
|||||||
|
|
||||||
### meta.update_project_metadata
|
### meta.update_project_metadata
|
||||||
|
|
||||||
Update metadata for the current project.
|
Update metadata for the current project. Keys omitted from updates keep their current values.
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
- [Generating and publishing](#generating-and-publishing)
|
- [Generating and publishing](#generating-and-publishing)
|
||||||
- [Typical editorial workflows](#typical-editorial-workflows)
|
- [Typical editorial workflows](#typical-editorial-workflows)
|
||||||
- [Working fully offline](#working-fully-offline)
|
- [Working fully offline](#working-fully-offline)
|
||||||
|
- [Running a headless server and the terminal UI](#running-a-headless-server-and-the-terminal-ui)
|
||||||
- [Troubleshooting and recovery](#troubleshooting-and-recovery)
|
- [Troubleshooting and recovery](#troubleshooting-and-recovery)
|
||||||
- [Team conventions](#team-conventions)
|
- [Team conventions](#team-conventions)
|
||||||
|
|
||||||
@@ -443,6 +444,29 @@ When AI is involved, airplane mode determines which automatic actions are allowe
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Running a headless server and the terminal UI
|
||||||
|
|
||||||
|
bDS2 can run without a window on a server (for example a Linux VPS) and be used from several places at once. Start it with `BDS_MODE=server`; the same release binary that runs the desktop app then runs headless. All clients connect through one SSH port (default 2222) — the web endpoint itself stays private on the server.
|
||||||
|
|
||||||
|
Access is controlled by SSH keys, not passwords. On first start the server creates an `ssh/` folder next to its database (for example `~/Library/Application Support/BDS2/ssh/` on macOS) containing the host key and an empty `authorized_keys` file. Paste the public keys of every machine that may connect into `authorized_keys`, one per line — the same file format OpenSSH uses.
|
||||||
|
|
||||||
|
To work in a terminal, connect with plain `ssh -p 2222 user@server`: the terminal UI opens directly. You can browse posts, media, templates, scripts, and tags, create and edit posts, publish, preview images, and run the one-shot AI actions. The status line at the bottom always shows the available keys.
|
||||||
|
|
||||||
|
To work in the desktop app against a remote server, use **File → Connect to Server…** and enter `user@host` (or `user@host:port`). The app connects with the SSH key from its own local `ssh/` folder and shows the remote workspace in the window; **File → Disconnect from Server** returns to the local workspace. A plain browser works too, through a manual SSH tunnel (`ssh -p 2222 -L 4010:127.0.0.1:4010 user@server`, then open `http://127.0.0.1:4010`).
|
||||||
|
|
||||||
|
Everything you see stays synchronized: when a script, another client, or a pipeline changes content on the server, every connected terminal and window updates. The interface language is a server-side setting — changing it in any client changes it for all of them.
|
||||||
|
|
||||||
|
### Key takeaways
|
||||||
|
|
||||||
|
- `BDS_MODE=server` runs the same app headless; only the SSH port is exposed.
|
||||||
|
- Access is public-key only: manage `authorized_keys` in the server's `ssh/` folder.
|
||||||
|
- `ssh` gives the terminal UI; **File → Connect to Server…** gives the desktop app; both use the same keys.
|
||||||
|
- All connected clients stay synchronized and share one interface language.
|
||||||
|
|
||||||
|
[↑ Back to In this article](#in-this-article)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Troubleshooting and recovery
|
## Troubleshooting and recovery
|
||||||
|
|
||||||
If content looks correct locally but is missing for collaborators, the usual cause is that changes were published but not committed and pushed. Check repository status, create a commit, then push to the expected remote.
|
If content looks correct locally but is missing for collaborators, the usual cause is that changes were published but not committed and pushed. Check repository status, create a commit, then push to the expected remote.
|
||||||
|
|||||||
81
README.md
81
README.md
@@ -12,6 +12,7 @@ The major architectural rework is in place.
|
|||||||
- Assets use Phoenix-default Tailwind and esbuild tooling from [assets/](/Users/gb/Projects/bDS2/assets) into [priv/static/](/Users/gb/Projects/bDS2/priv/static).
|
- Assets use Phoenix-default Tailwind and esbuild tooling from [assets/](/Users/gb/Projects/bDS2/assets) into [priv/static/](/Users/gb/Projects/bDS2/priv/static).
|
||||||
- Core editorial flows are implemented in the main application: posts, media, tags, templates, scripts, imports, preview, generation, publishing, maintenance, AI, and MCP.
|
- Core editorial flows are implemented in the main application: posts, media, tags, templates, scripts, imports, preview, generation, publishing, maintenance, AI, and MCP.
|
||||||
- Localization is now a first-class architectural concern rather than an afterthought: UI chrome and rendered site output have separate locale flows, and post/media translation workflows are built into the domain model.
|
- Localization is now a first-class architectural concern rather than an afterthought: UI chrome and rendered site output have separate locale flows, and post/media translation workflows are built into the domain model.
|
||||||
|
- The app boots in three modes: the desktop app, a headless server (SSH-served terminal UI + tunneled GUI), and a local TUI — see [Headless Server Mode And Terminal UI](#headless-server-mode-and-terminal-ui).
|
||||||
|
|
||||||
The rewrite still aims to preserve the product behavior of bDS while replacing the technical stack. The contract is product behavior, not the old implementation language or framework choices.
|
The rewrite still aims to preserve the product behavior of bDS while replacing the technical stack. The contract is product behavior, not the old implementation language or framework choices.
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ If you are tracing UI behavior, start there first:
|
|||||||
|
|
||||||
- LiveView event routing, workbench state, overlays, and menu handling live in the desktop shell modules.
|
- LiveView event routing, workbench state, overlays, and menu handling live in the desktop shell modules.
|
||||||
- HEEx templates under the same tree now own most common layout and state styling.
|
- HEEx templates under the same tree now own most common layout and state styling.
|
||||||
- Monaco remains a vendor drop under [priv/ui/monaco/](/Users/gb/Projects/bDS2/priv/ui/monaco).
|
- Monaco is bundled as an ESM bundle via esbuild into `priv/static/assets/monaco.js` and `monaco.css` (source entry at `assets/js/monaco_entry.js`).
|
||||||
|
|
||||||
### Domain Modules
|
### Domain Modules
|
||||||
|
|
||||||
@@ -156,6 +157,84 @@ mix dialyzer
|
|||||||
mix assets.build
|
mix assets.build
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Headless Server Mode And Terminal UI
|
||||||
|
|
||||||
|
Since issue #26 the app has three boot modes, selected with the `BDS_MODE`
|
||||||
|
environment variable (any release or dev boot; default is the desktop app):
|
||||||
|
|
||||||
|
- `desktop` — the native desktop app, unchanged.
|
||||||
|
- `server` — headless: no window, loopback-only HTTP endpoint, CLI sync
|
||||||
|
watcher, and an SSH daemon serving the terminal UI. Works on macOS,
|
||||||
|
Windows, and Linux (no display needed).
|
||||||
|
- `tui` — the headless server plus a TUI attached to the launching terminal.
|
||||||
|
|
||||||
|
### Build and run a headless server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
MIX_ENV=prod mix release bds --overwrite
|
||||||
|
BDS_MODE=server _build/prod/rel/bds/bin/bds start
|
||||||
|
```
|
||||||
|
|
||||||
|
The SSH daemon listens on port `2222` (`BDS_SSH_PORT` or
|
||||||
|
`config :bds, :server, ssh_port:` to change). Authentication is public-key
|
||||||
|
only: on first boot the server generates a host key and an empty
|
||||||
|
`authorized_keys` (mode 600) in `ssh/` next to the database (default
|
||||||
|
`~/Library/Application Support/BDS2/ssh/`, or under `BDS_DATABASE_PATH`).
|
||||||
|
Add your public key there, then connect from any terminal:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ssh -p 2222 <server-host> # opens the TUI
|
||||||
|
```
|
||||||
|
|
||||||
|
Each SSH connection gets its own TUI session on the server; only terminal
|
||||||
|
cells cross the wire. All clients stay synchronized through the domain
|
||||||
|
event bus (`BDS.Events`) — a post created by a pipeline or another client
|
||||||
|
appears in every open shell.
|
||||||
|
|
||||||
|
### TUI keys
|
||||||
|
|
||||||
|
`↑/↓` or `j/k` navigate · `enter` open · `n` new post · `1-5` switch view
|
||||||
|
(posts/media/templates/scripts/tags) · `:` command prompt (metadata
|
||||||
|
diff, validate site, force render, rebuilds, …; `:?` lists all) ·
|
||||||
|
metadata-diff/site-validation reports open as panels — `enter` applies
|
||||||
|
the whole report (repair from files / incremental apply), `esc` cancels ·
|
||||||
|
`r` refresh · in the editor:
|
||||||
|
`ctrl+s` save, `ctrl+p` publish, `ctrl+e` word-wrapped preview,
|
||||||
|
`ctrl+t` title, `ctrl+l` language, `ctrl+g` AI suggestions
|
||||||
|
(airplane-mode gated), `esc` back · `ctrl+q` quit.
|
||||||
|
Images preview inline via the terminal image protocol. The UI language is
|
||||||
|
the server-side setting shared by every client.
|
||||||
|
|
||||||
|
### Remote GUI
|
||||||
|
|
||||||
|
The HTTP endpoint stays bound to `127.0.0.1` and is never exposed; GUI
|
||||||
|
clients tunnel through the same SSH daemon and keys:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ssh -p 2222 -L 4010:127.0.0.1:4010 -N <server-host> &
|
||||||
|
open http://127.0.0.1:4010
|
||||||
|
```
|
||||||
|
|
||||||
|
Or use the desktop app directly: **File → Connect to Server…** takes
|
||||||
|
`user@host[:port]`, connects with the client key from the same local
|
||||||
|
`ssh/` directory (public-key auth, trust-on-first-use `known_hosts`),
|
||||||
|
tunnels to the server, and points the window at it. **File → Disconnect
|
||||||
|
from Server** returns to the local workspace.
|
||||||
|
|
||||||
|
## Monaco Editor
|
||||||
|
|
||||||
|
Monaco is bundled via esbuild using its ESM entry point (`monaco-editor/esm/vs/editor/editor.main.js`). The
|
||||||
|
main bundle is built as part of `mix assets.build` and lives at `priv/static/assets/monaco.js` and
|
||||||
|
`monaco.css`; ESM worker bundles live under `priv/static/assets/monaco/`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mix monaco.version # show current monaco-editor version
|
||||||
|
mix monaco.update 0.55.1 # update to a specific version and rebuild the bundle
|
||||||
|
```
|
||||||
|
|
||||||
|
The update task runs `npm install monaco-editor@<version>`, cleans the old bundle, and rebuilds via esbuild.
|
||||||
|
See https://www.npmjs.com/package/monaco-editor for available versions.
|
||||||
|
|
||||||
Notes for developers:
|
Notes for developers:
|
||||||
|
|
||||||
- Specs in [specs/](/Users/gb/Projects/bDS2/specs) define the intended product behavior.
|
- Specs in [specs/](/Users/gb/Projects/bDS2/specs) define the intended product behavior.
|
||||||
|
|||||||
818
TECHDEBTS.md
818
TECHDEBTS.md
@@ -1,818 +0,0 @@
|
|||||||
# Technical Debt Backlog
|
|
||||||
|
|
||||||
Findings from a full codebase + stack review (2026-06-11) covering BEAM/OTP
|
|
||||||
antipatterns, build-vs-buy decisions, and architecture. Tasks are ordered in
|
|
||||||
phases that make sense to execute sequentially; within a phase, tasks are
|
|
||||||
independent unless noted. Phase 1 contains the top five findings.
|
|
||||||
|
|
||||||
## How to work these tasks
|
|
||||||
|
|
||||||
Every task must follow the project rules in `AGENTS.md`:
|
|
||||||
|
|
||||||
- **Test-first**: write a failing test before the fix, against the real module.
|
|
||||||
- **Clean gates**: `mix compile --warnings-as-errors`, full `mix test`
|
|
||||||
(use `xvfb-run mix test` on headless Linux), and `mix dialyzer` must all pass.
|
|
||||||
- **Delete dead code completely** — no commented-out remnants.
|
|
||||||
- **Specs**: check `specs/` for an allium spec covering the touched area; if the
|
|
||||||
spec changes, validate it with `allium check <file>` (must exit 0).
|
|
||||||
- **No new CDN/external assets** in preview or generated HTML.
|
|
||||||
- New user-facing strings go through gettext with translations for de/fr/it/es.
|
|
||||||
- If a task touches an app API surface, update the script bridge and `API.md`
|
|
||||||
in the same change.
|
|
||||||
|
|
||||||
Each task lists: context (why it matters), affected files, suggested approach,
|
|
||||||
and acceptance criteria. Re-verify line numbers before editing — they reflect
|
|
||||||
the review snapshot (commit `bf93403`).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Phase 1 — Top five (security & correctness)
|
|
||||||
|
|
||||||
### TD-01: Move the AI secret encryption key out of the repo ✅ DONE (2026-06-11)
|
|
||||||
|
|
||||||
**Severity: High (security).**
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.AI.SecretKey` resolves the master key from the
|
|
||||||
macOS Keychain (`security` CLI) with a 0600 key-file fallback under the
|
|
||||||
private app dir; the deterministic node-name fallback is gone (operations
|
|
||||||
return `{:error, :secret_key_unavailable}`); `BDS.AI.SecretMigration`
|
|
||||||
re-encrypts legacy rows at every boot from `BDS.RepoBootstrap`; the endpoint
|
|
||||||
`secret_key_base` is generated per boot. The legacy repo key literal remains
|
|
||||||
in `SecretBackend`/tests **only** to decrypt and migrate existing user
|
|
||||||
databases — remove it together with `SecretMigration` in a future release.
|
|
||||||
The test env pins a deterministic `:ai_secret_key` in `config/test.exs` so
|
|
||||||
the suite never touches the keyring; that string protects nothing.
|
|
||||||
Rode along (mandated clean-gates): fixed all 10 pre-existing compiler type
|
|
||||||
warnings surfaced by the full recompile and the one dialyzer finding
|
|
||||||
(MapSet opacity in `mac_bundle/dylibs.ex`), so `mix compile
|
|
||||||
--warnings-as-errors --force` and `mix dialyzer` are now clean baselines.
|
|
||||||
|
|
||||||
**Context.** `BDS.AI.SecretBackend` encrypts AI provider API keys at rest with
|
|
||||||
AES-256-GCM, but the key is the hardcoded string in `config/config.exs`
|
|
||||||
(`config :bds, :ai_secret_key, "bds_desktop_shell_secret_key_base_..."`), which
|
|
||||||
is committed to the repository and **never overridden in
|
|
||||||
`config/runtime.exs`**, so production uses it too. Anyone with the user's
|
|
||||||
SQLite database file plus the public source can decrypt their API keys. The
|
|
||||||
fallback path is worse: `sha256(Atom.to_string(node()) <> ":bds:ai")`, and the
|
|
||||||
node name is effectively always `nonode@nohost`, making the fallback key a
|
|
||||||
constant. The same hardcoded string also serves as the prod `secret_key_base`
|
|
||||||
for `BDS.Desktop.Endpoint`.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/secret_backend.ex` (`secret_key/0`, lines ~35–41)
|
|
||||||
- `config/config.exs` (lines ~22–26: `:desktop` secret_key_base and `:ai_secret_key`)
|
|
||||||
- `config/runtime.exs` (no override exists today)
|
|
||||||
|
|
||||||
**Approach.**
|
|
||||||
1. On macOS, store the key in the Keychain via `System.cmd("security", ["add-generic-password", ...])` /
|
|
||||||
`find-generic-password`. On other platforms (and as a portable fallback),
|
|
||||||
generate a random 32-byte key on first launch and persist it with `0600`
|
|
||||||
permissions under the app's private data dir (`~/Library/Application
|
|
||||||
Support/bds` on macOS — see project conventions; never the repo or the
|
|
||||||
project.json folder).
|
|
||||||
2. Remove the deterministic node-name fallback entirely; if no key can be
|
|
||||||
obtained, fail loudly rather than silently degrade to obfuscation.
|
|
||||||
3. Migration: on startup, if secrets were encrypted with the legacy hardcoded
|
|
||||||
key, decrypt with the old key and re-encrypt with the new one (one-time,
|
|
||||||
then remove legacy key knowledge in a follow-up release).
|
|
||||||
4. Generate the endpoint `secret_key_base` at runtime (random per boot is fine
|
|
||||||
for a loopback-only desktop endpoint) instead of shipping it in config.
|
|
||||||
|
|
||||||
**Acceptance.** No secret material in the repo; `grep -r "secret_key_base_64\|ai_secret_key" config/` shows no literal keys; existing encrypted secrets still decrypt after upgrade (covered by a migration test); SecretBackend tests cover missing-key failure mode.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TD-02: Replace the hand-rolled `:httpc` client with Req ✅ DONE (2026-06-11)
|
|
||||||
|
|
||||||
**Severity: High (reliability), enables TD-06 (streaming).**
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.AI.HttpClient` is a Req wrapper with explicit
|
|
||||||
connect/receive timeouts and constant-delay transient retries for GETs only
|
|
||||||
(POST completions are never retried), configurable under
|
|
||||||
`config :bds, BDS.AI.HttpClient` (`:connect_timeout_ms`,
|
|
||||||
`:receive_timeout_ms`, `:get_max_retries`, `:retry_delay_ms`). The legacy
|
|
||||||
`{:ok, %{status, headers, body}}` contract is preserved (raw binary body,
|
|
||||||
downcased single-valued headers), so runtime/catalog callers are unchanged.
|
|
||||||
The automation driver's health check also moved to Req, `:inets` left
|
|
||||||
`extra_applications`, and no `:httpc` remains under `lib/`
|
|
||||||
(test files still use `:httpc` as a client against local servers — they start
|
|
||||||
`:inets` themselves). TD-06 (SSE streaming via Req `into:`) is now unblocked.
|
|
||||||
|
|
||||||
**Context.** `BDS.AI.HttpClient` wraps `:httpc` with empty http options — the
|
|
||||||
default timeout is **infinity**, so a hung LLM endpoint (Ollama, LM Studio,
|
|
||||||
any OpenAI-compatible server) blocks the chat task forever (see TD-03 for the
|
|
||||||
blocking chain above it). There is no retry, no connection pooling, no pinned
|
|
||||||
TLS verification (behavior depends on the OTP version's `:httpc` defaults),
|
|
||||||
and `:inets.start()`/`:ssl.start()` are called redundantly on every request
|
|
||||||
(both are already in `extra_applications`). Req is already an optional
|
|
||||||
dependency of the `image` package, so the dependency tree barely grows.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/http_client.ex` (delete or reduce to a thin Req wrapper)
|
|
||||||
- `lib/bds/ai/openai_compatible_runtime.ex` (calls `HttpClient.get/post`)
|
|
||||||
- `lib/bds/desktop/automation.ex` (also uses `:httpc`; test-automation server, lower priority but should follow)
|
|
||||||
- `mix.exs` (add `{:req, "~> 0.5"}`)
|
|
||||||
|
|
||||||
**Approach.** Add Req with explicit `connect_options` (timeout), `receive_timeout`
|
|
||||||
(generous but finite, e.g. 120s for LLM responses; make it config-driven under
|
|
||||||
`config :bds, :ai`), `retry: :transient` for idempotent GETs only (do NOT
|
|
||||||
auto-retry chat completions), and default TLS verification. Keep the existing
|
|
||||||
`{:ok, %{status, headers, body}} | {:error, reason}` contract so the runtime
|
|
||||||
and its tests change minimally, or inject Req as the `:http_client` the way
|
|
||||||
tests already do.
|
|
||||||
|
|
||||||
**Acceptance.** No `:httpc` calls remain under `lib/bds/ai/`; a test proves a
|
|
||||||
slow/hung endpoint produces `{:error, %{kind: :http_error, reason: :timeout}}`
|
|
||||||
within the configured budget instead of hanging; dialyzer clean.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TD-03: Fix `BDS.AI.InFlight` ETS ownership and creation race ✅ DONE (2026-06-11)
|
|
||||||
|
|
||||||
**Severity: High (correctness).**
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.AI.InFlight` is now a minimal GenServer whose
|
|
||||||
`init/1` creates the named table (`:named_table, :public, :set,
|
|
||||||
read_concurrency: true`); it is supervised in `BDS.Application` (before
|
|
||||||
anything that uses chat), so the table lives for the VM's lifetime and the
|
|
||||||
concurrent-first-use race is impossible by construction. The lazy `table/0`
|
|
||||||
creation path is deleted; `register/unregister/lookup` reference the named
|
|
||||||
table directly. `test/bds/ai/in_flight_test.exs` proves registrations survive
|
|
||||||
the death of the registering process and that the supervised process owns the
|
|
||||||
table.
|
|
||||||
|
|
||||||
**Context.** `lib/bds/ai/in_flight.ex` creates its named ETS table lazily in
|
|
||||||
whichever process first calls `table/0`. Two defects: (1) the table is owned
|
|
||||||
by that first caller — typically a transient LiveView or chat task — so when
|
|
||||||
that process exits, the table and all in-flight chat registrations vanish,
|
|
||||||
breaking `cancel_chat/1`; (2) two concurrent first-callers race on
|
|
||||||
`:ets.new/2` with `:named_table`, and the loser crashes with `badarg`.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/in_flight.ex`
|
|
||||||
- `lib/bds/application.ex` (supervision tree)
|
|
||||||
|
|
||||||
**Approach.** Create the table from a stable owner. Simplest options:
|
|
||||||
- create it directly in `BDS.Application.start/2` (the application process
|
|
||||||
lives for the VM's lifetime), or
|
|
||||||
- give InFlight a minimal GenServer whose `init/1` creates the table
|
|
||||||
(`:named_table, :public, :set, read_concurrency: true`) and add it to the
|
|
||||||
supervision tree before anything that uses chat.
|
|
||||||
|
|
||||||
Remove the lazy `table/0` creation path entirely; `register/unregister/lookup`
|
|
||||||
should assume the table exists.
|
|
||||||
|
|
||||||
**Acceptance.** A test demonstrates registrations survive the death of the
|
|
||||||
registering process; no code path calls `:ets.new` outside the owner's init;
|
|
||||||
concurrent-first-use race is impossible by construction.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TD-04: Flush embedding indexes on shutdown (or delete the dead `flush_all`) ✅ DONE (2026-06-11)
|
|
||||||
|
|
||||||
**Severity: Medium (perf/contract), High confidence.**
|
|
||||||
|
|
||||||
**Status: implemented.** `Shutdown.persist_safely/0` now calls
|
|
||||||
`BDS.Embeddings.Index.flush_all()` next to `MainWindow.persist_now()`; each
|
|
||||||
persist step is hardened individually (own rescue/catch) so one failure never
|
|
||||||
blocks quit or skips the other step. `terminate/2` stays as defense-in-depth
|
|
||||||
for supervised restarts. A test proves a debounced (unsaved) index reaches
|
|
||||||
disk through the real shutdown path before the hard quit fires. The
|
|
||||||
`terminate/2` audit found no other graceful-shutdown dependency:
|
|
||||||
`job_runner.ex` only detaches in-memory state (moot under SIGKILL),
|
|
||||||
`automation.ex` is the test-automation harness whose ports die with the VM,
|
|
||||||
and `main_window.ex` bounds persistence was already covered by
|
|
||||||
`MainWindow.persist_now()` in the shutdown path. The code now matches the
|
|
||||||
spec's DebouncedPersistence invariant (`specs/embedding.allium:216`).
|
|
||||||
|
|
||||||
**Context.** App shutdown SIGKILLs the BEAM (`BDS.Desktop.Shutdown.quit/0` —
|
|
||||||
a documented and legitimate workaround for a wxWidgets static-destructor
|
|
||||||
segfault on macOS). Consequence: **no `terminate/2` callback in the whole app
|
|
||||||
ever runs on quit.** `BDS.Embeddings.Index` traps exits and relies on
|
|
||||||
`terminate/2` to persist debounced HNSW index saves, and its moduledoc
|
|
||||||
promises "force-saved on project switch / shutdown". Meanwhile
|
|
||||||
`Embeddings.Index.flush_all/0` exists but has **zero production callers**
|
|
||||||
(dead code per the AGENTS.md mandate). The lazy-reload-from-DB fallback makes
|
|
||||||
this a startup-performance bug (index rebuilt unnecessarily) rather than data
|
|
||||||
loss, but the code contradicts its own contract.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/desktop/shutdown.ex` (`persist_safely/0`, lines ~92–99)
|
|
||||||
- `lib/bds/embeddings/index.ex` (`flush_all/0` line ~87, `terminate/2` line ~167, moduledoc)
|
|
||||||
|
|
||||||
**Approach.** Call `BDS.Embeddings.Index.flush_all()` inside
|
|
||||||
`Shutdown.persist_safely/0` next to `MainWindow.persist_now()` (same
|
|
||||||
rescue/catch hardening). Keep `terminate/2` as defense-in-depth for supervised
|
|
||||||
restarts. Audit for other state that assumed graceful shutdown (search the
|
|
||||||
tree for `terminate/2` implementations and check each one's expectations
|
|
||||||
against the SIGKILL path). If instead the decision is that lazy rebuild is the
|
|
||||||
intended behavior, delete `flush_all/0` and fix the moduledoc — pick one,
|
|
||||||
don't keep both.
|
|
||||||
|
|
||||||
**Acceptance.** Either `flush_all` is wired into the shutdown path with a test
|
|
||||||
(injectable shutdown module already exists: `:desktop_shutdown_module` env),
|
|
||||||
or it is deleted and the moduledoc no longer claims shutdown saving. No other
|
|
||||||
`terminate/2` in the codebase silently depends on graceful shutdown for
|
|
||||||
correctness.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TD-05: Replace xmerl with Saxy in the WXR importer; add import transactions ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Severity: Medium-High (DoS + integrity on user-supplied files).**
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.WxrParser` now parses WXR with `Saxy.parse_stream/3`
|
|
||||||
for files and `Saxy.parse_string/3` for in-memory XML, keeping element names as
|
|
||||||
binaries instead of interning atoms and preserving the existing result shape.
|
|
||||||
Both import write paths now batch work in `Repo.transaction` chunks of 500
|
|
||||||
(`BDS.ImportExecution` and `BDS.Posts.RebuildFromFiles`), so mid-batch failures
|
|
||||||
roll back cleanly instead of leaving partial imports behind. Acceptance proof now
|
|
||||||
includes a bounded atom-growth parser test with many unique element names,
|
|
||||||
existing import fixture tests, rollback tests for both import and rebuild, and a
|
|
||||||
local SQLite benchmark showing the batching win (`1000` inserts: `183ms`
|
|
||||||
per-row transactions vs `83ms` in `500`-row chunks, `2.2x` faster).
|
|
||||||
|
|
||||||
**Context.** `BDS.WxrParser.parse_xml/1` uses `:xmerl_scan.string/1`, which
|
|
||||||
**creates atoms from element and attribute names** in the parsed document. WXR
|
|
||||||
files are user-supplied imports, so a malicious or merely huge/weird file can
|
|
||||||
grow the atom table (atoms are never GC'd → eventual VM crash). It also reads
|
|
||||||
the entire file into memory (`File.read!` + full DOM); real WordPress exports
|
|
||||||
reach hundreds of MB. Separately, the import/rebuild write loops run one
|
|
||||||
autocommit transaction per row on SQLite — slow (one fsync per post) and a
|
|
||||||
failure mid-way leaves a half-imported database.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/wxr_parser.ex` (full rewrite of the parsing layer; keep the output map shape)
|
|
||||||
- `lib/bds/import_execution.ex` (line ~359: `Enum.each(post_ids, ...)` write loop)
|
|
||||||
- `lib/bds/posts/rebuild_from_files.ex` (lines ~37–56: per-file upsert loop)
|
|
||||||
- `mix.exs` (add `{:saxy, "~> 1.6"}`)
|
|
||||||
|
|
||||||
**Approach.**
|
|
||||||
1. Rewrite WxrParser on Saxy (SAX or its simple-form DOM builder). Saxy keeps
|
|
||||||
names as binaries (no atom creation) and supports streaming via
|
|
||||||
`Saxy.parse_stream/3` with `File.stream!`. Preserve the existing public
|
|
||||||
contract (`parse_file/1`, `parse_xml/1` returning
|
|
||||||
`%{site:, posts:, pages:, media:, categories:, tags:}`) so
|
|
||||||
`import_analysis.ex`/`import_execution.ex` are untouched; the existing
|
|
||||||
parser tests become the safety net.
|
|
||||||
Note: `sweet_xml` is already in the dep tree via `image`, but it is
|
|
||||||
xmerl-based and inherits the atom problem — don't use it for this.
|
|
||||||
2. Wrap import and rebuild write loops in `Repo.transaction` (chunked, e.g.
|
|
||||||
500 rows per transaction, to keep WAL size and progress reporting sane).
|
|
||||||
Consider `Repo.insert_all` in chunks for plain inserts — there is currently
|
|
||||||
not a single `insert_all` in the codebase.
|
|
||||||
|
|
||||||
**Acceptance.** A test feeds XML with many unique element names and asserts
|
|
||||||
`:erlang.system_info(:atom_count)` does not grow proportionally; existing
|
|
||||||
import fixture tests pass unchanged; a failure injected mid-import leaves the
|
|
||||||
DB unchanged (rolled back chunk); large-import benchmark shows the transaction
|
|
||||||
batching speedup.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Phase 2 — Unbounded blocking & cancellation
|
|
||||||
|
|
||||||
### TD-06: Real SSE streaming for chat ✅ DONE (2026-06-11)
|
|
||||||
|
|
||||||
**Depends on TD-02 (Req).**
|
|
||||||
|
|
||||||
**Status: implemented.** Chat requests now send `"stream": true` (+
|
|
||||||
`stream_options.include_usage`) and consume the SSE response incrementally
|
|
||||||
via `HttpClient.post_stream/5` (Req `into:`). `BDS.AI.SSE` assembles content
|
|
||||||
deltas, tool-call fragments, and usage, emitting **cumulative content
|
|
||||||
snapshots** throttled to `stream_emit_interval_ms` (default 100ms) — replace
|
|
||||||
semantics, so the chat editor needed no changes and tool rounds reset
|
|
||||||
naturally. Streaming applies only to `operation: :chat` with an `:on_stream`
|
|
||||||
callback, can be disabled via `config :bds, :chat, streaming: false`, and
|
|
||||||
providers that ignore the stream flag are auto-detected by content-type and
|
|
||||||
parsed as plain JSON. Cancellation kills the chat task, which aborts the
|
|
||||||
underlying connection (server-observed in tests). Persistence semantics are
|
|
||||||
unchanged (one assistant row per round, same usage normalization).
|
|
||||||
|
|
||||||
**Context.** `OpenAICompatibleRuntime.generate/3` never sets `"stream": true`;
|
|
||||||
the UI's `{:chat_streaming_content, ...}` event fires exactly once with the
|
|
||||||
complete response, i.e. streaming is fake. For local models this is the
|
|
||||||
single biggest perceived-latency win available.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/openai_compatible_runtime.ex`
|
|
||||||
- `lib/bds/ai/chat.ex` (`notify_chat_event` plumbing, `chat_round/9`)
|
|
||||||
- `lib/bds/desktop/shell_live/chat_editor.ex` (consume incremental events)
|
|
||||||
|
|
||||||
**Approach.** Use Req's `into:` option (fun or `:self`) to consume
|
|
||||||
`text/event-stream` chunks, parse `data:` lines incrementally, emit
|
|
||||||
`{:chat_streaming_content, conversation_id, delta}` per token-batch, and
|
|
||||||
accumulate the full message for persistence (keep persistence semantics
|
|
||||||
identical: one assistant row per round, tool_calls assembled from streamed
|
|
||||||
fragments). Gate on a config flag so non-streaming providers still work.
|
|
||||||
Remember the AGENTS.md AI rule: all automatic AI activity stays gated by
|
|
||||||
airplane mode / local model / toast.
|
|
||||||
|
|
||||||
**Acceptance.** A mock SSE server test asserts multiple incremental content
|
|
||||||
events arrive before the final `{:ok, reply}`; tool-call rounds still work;
|
|
||||||
cancellation mid-stream (TD-07) aborts the HTTP request.
|
|
||||||
|
|
||||||
### TD-07: Bound the chat await chain; end-to-end timeout & cancellation ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.AI.Chat.send_chat_message/3` no longer waits
|
|
||||||
unboundedly on the supervised chat task: `await_chat_task/2` now applies a
|
|
||||||
global deadline derived from the configured per-request HTTP budget and the
|
|
||||||
bounded tool loop (`BDS.AI.HttpClient.request_timeout_ms() *
|
|
||||||
(chat_max_tool_rounds + 1) + config :bds, :chat, :await_timeout_margin_ms`). On
|
|
||||||
deadline expiry it returns `{:error, :chat_timeout}` and shuts the task down via
|
|
||||||
`Task.shutdown/2`, so the caller is released even if the runtime wedges.
|
|
||||||
`config/config.exs` now exposes `:await_timeout_margin_ms` under `:chat`. The
|
|
||||||
acceptance proof is a shutdown-aware blocking runtime test that asserts the
|
|
||||||
timeout result, verifies the task receives shutdown, and confirms the
|
|
||||||
conversation persists only the user message on timeout; existing cancellation
|
|
||||||
and streaming tests remain green.
|
|
||||||
|
|
||||||
**Context.** `BDS.AI.Chat.send_chat_message/3` blocks the caller (a LiveView
|
|
||||||
process) on a hand-rolled `await_chat_task/1` — a raw `receive` with **no
|
|
||||||
`after` clause**. Combined with the infinite HTTP timeout (TD-02) the whole
|
|
||||||
chain is unbounded. Even after TD-02, a defense-in-depth deadline belongs
|
|
||||||
here.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/chat.ex` (`send_chat_message/3` lines ~185–219, `await_chat_task/1` lines ~855–882)
|
|
||||||
|
|
||||||
**Approach.** Give `await_chat_task` an `after` deadline derived from config
|
|
||||||
(request timeout × max_tool_rounds + margin), returning
|
|
||||||
`{:error, :chat_timeout}` and terminating the task via
|
|
||||||
`Task.Supervisor.terminate_child` (the path `cancel_chat/1` already uses).
|
|
||||||
Alternatively restructure to fully async: run the chat task fire-and-forget
|
|
||||||
and deliver results to the LiveView via the existing `event_target` /
|
|
||||||
PubSub mechanism, so no process ever blocks. The async restructure is the
|
|
||||||
better end state; the `after` clause is the cheap immediate fix.
|
|
||||||
|
|
||||||
**Acceptance.** A stalled runtime stub cannot hang the caller past the
|
|
||||||
deadline; cancel during a round kills the HTTP request and leaves the
|
|
||||||
conversation in a consistent persisted state.
|
|
||||||
|
|
||||||
### TD-08: Remove test-sandbox scaffolding from production chat code ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.AI.Chat.send_chat_message/3` now starts the
|
|
||||||
chat worker directly with `Task.Supervisor.async_nolink/2`; the temporary
|
|
||||||
`:sandbox_ready` receive/send barrier and `allow_repo_sandbox/1` are deleted
|
|
||||||
from production code. The repo is on `ecto_sql 3.13.5`, and the supervised
|
|
||||||
chat task works correctly through sandbox `$callers` propagation in the
|
|
||||||
project's existing test modes (manual checkout plus the suite's ownership /
|
|
||||||
shared-mode callers). `test/bds/ai/chat_sandbox_cleanup_test.exs` now proves
|
|
||||||
both acceptance points: the production module has zero sandbox references, and
|
|
||||||
`send_chat_message/3` still persists its user/assistant messages through the
|
|
||||||
supervised task without explicit sandbox allowance. Validation gates are clean:
|
|
||||||
`mix compile --warnings-as-errors --force`, full `mix test`, and
|
|
||||||
`mix dialyzer` all pass.
|
|
||||||
|
|
||||||
**Context.** `chat.ex` contains a `:sandbox_ready` send/receive handshake and
|
|
||||||
`allow_repo_sandbox/1` (with `Code.ensure_loaded?` + blanket `rescue`) purely
|
|
||||||
so the Ecto SQL sandbox works for the `async_nolink` chat task in tests. Since
|
|
||||||
ecto_sql 3.4 the sandbox automatically follows `$callers` for processes
|
|
||||||
spawned via `Task`/`Task.Supervisor`, making all of this unnecessary —
|
|
||||||
**verify this against the project's ecto_sql version and test modes first**
|
|
||||||
(it holds for the default `:shared`/ownership modes when the caller chain is
|
|
||||||
intact).
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/chat.ex` (lines ~197–214, ~938–950)
|
|
||||||
|
|
||||||
**Approach.** Delete the handshake (`receive :sandbox_ready`, the
|
|
||||||
`send(task.pid, :sandbox_ready)`, and `allow_repo_sandbox/1`); run the full
|
|
||||||
test suite to confirm `$callers` propagation covers it. If some test setup
|
|
||||||
genuinely needs explicit allowance, move that into the test helper, not
|
|
||||||
production code.
|
|
||||||
|
|
||||||
**Acceptance.** Production module has zero sandbox references; full suite
|
|
||||||
green.
|
|
||||||
|
|
||||||
### TD-09: Graceful task cancellation in `BDS.Tasks` ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.Tasks.cancel_task/1` and
|
|
||||||
`BDS.Scripting.JobRunner.handle_call(:cancel, ...)` now terminate supervised
|
|
||||||
workers through their owning `Task.Supervisor` (`terminate_child/2`), so the
|
|
||||||
worker receives `:shutdown` instead of an immediate `:kill`. Queue bookkeeping
|
|
||||||
and `:cancelled` status semantics are unchanged, but cooperative cleanup now
|
|
||||||
runs before the slot is freed. Coverage now includes a task worker that traps
|
|
||||||
exits, confirms cleanup executes on cancellation, and proves the queued task is
|
|
||||||
promoted afterward, plus a managed scripting job runtime test that traps exits
|
|
||||||
and observes the same shutdown-driven cleanup path.
|
|
||||||
|
|
||||||
**Context.** `Tasks.cancel_task/1` uses `Process.exit(pid, :kill)` — the
|
|
||||||
worker gets no chance to clean up mid-upload or mid-file-write
|
|
||||||
(`Persistence.atomic_write` mitigates file corruption but not e.g. remote
|
|
||||||
half-states). `cancel_chat` already models the right approach.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/tasks.ex` (line ~130)
|
|
||||||
- `lib/bds/scripting/job_runner.ex` (`handle_call(:cancel, ...)` line ~67 — same pattern)
|
|
||||||
|
|
||||||
**Approach.** Use `Task.Supervisor.terminate_child/2` (delivers `:shutdown`,
|
|
||||||
escalates to kill after the child's shutdown timeout). Workers that need
|
|
||||||
cleanup can trap exits locally. Keep the state bookkeeping identical
|
|
||||||
(`:cancelled` status, queue promotion).
|
|
||||||
|
|
||||||
**Acceptance.** A test worker with `trap_exit` observes `:shutdown` and runs
|
|
||||||
its cleanup before dying; cancelled tasks still free a concurrency slot.
|
|
||||||
|
|
||||||
### TD-10: Timeouts for external `git` commands ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.Git` now applies config-driven timeout budgets
|
|
||||||
to every shell-out (`config :bds, :git` with `:local_timeout_ms` defaulting to
|
|
||||||
15s and `:network_timeout_ms` defaulting to 120s for fetch/pull/push). The
|
|
||||||
default runner no longer uses unbounded `System.cmd/3`; it launches the child
|
|
||||||
through `Port.open/2`, collects stdout/stderr, and on timeout closes the port
|
|
||||||
and explicitly terminates the OS process if it is still alive. Timeout results
|
|
||||||
surface as structured `%{kind: :timeout, operation:, timeout_ms:, message:}`
|
|
||||||
errors, while existing structured auth guidance is preserved. Acceptance proof
|
|
||||||
now includes a bounded runner-stub timeout test and a real-process cleanup test
|
|
||||||
that launches `sleep`, times out, and asserts the spawned PID is gone.
|
|
||||||
|
|
||||||
**Context.** `BDS.Git` shells out via `System.cmd`, which has **no timeout**.
|
|
||||||
`GIT_TERMINAL_PROMPT=0` and SSH BatchMode prevent interactive hangs, but a
|
|
||||||
network-level stall on `fetch`/`pull`/`push`/`lfs` blocks the calling task
|
|
||||||
indefinitely. (Shelling out to git itself is the right call — current Elixir
|
|
||||||
libgit2 bindings are not mature; keep that decision.)
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/git.ex` (`system_runner/3` line ~343, `run_git/3`)
|
|
||||||
|
|
||||||
**Approach.** Wrap the command in `Task.async` + `Task.yield(task, timeout) || Task.shutdown(task, :brutal_kill)`
|
|
||||||
inside `system_runner` (note: the OS process may need explicit killing —
|
|
||||||
consider `MuonTrap` for proper external-process supervision, or spawn git via
|
|
||||||
a port with `:kill_group`). Default timeout config-driven: generous for
|
|
||||||
network ops (120s), short for local ops (15s). Map timeout to a structured
|
|
||||||
error like the existing `%{kind: :auth, ...}` shape so the UI can toast it.
|
|
||||||
|
|
||||||
**Acceptance.** A runner stub that sleeps past the deadline produces
|
|
||||||
`{:error, %{kind: :timeout, ...}}`; no orphaned OS processes after timeout
|
|
||||||
(test with a real `sleep` binary).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Phase 3 — Process architecture (de-bottleneck singletons)
|
|
||||||
|
|
||||||
### TD-11: Move preview rendering out of the `BDS.Preview` GenServer ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.Preview` now keeps only preview-server lifecycle and
|
|
||||||
drain coordination in the GenServer. The current server state lives in a public
|
|
||||||
ETS table, so both direct preview calls and socket-served HTTP requests resolve
|
|
||||||
against that fast state read and render outside the singleton owner process.
|
|
||||||
Direct `request/2` and `preview_draft/3` calls now run in short-lived tracked
|
|
||||||
tasks, while TCP request workers render inline after inheriting the test sandbox
|
|
||||||
allowance; graceful stop flips `is_running` false in ETS before draining and
|
|
||||||
waits for those tracked render workers to finish. Coverage now proves two real
|
|
||||||
HTTP preview renders can be in flight at once and that `stop_preview/1` waits
|
|
||||||
for a blocked render to complete before returning.
|
|
||||||
|
|
||||||
**Context.** Every preview HTTP request (`Preview.request/2`,
|
|
||||||
`preview_draft/3`) renders Markdown + Liquid **inside** the singleton
|
|
||||||
`BDS.Preview` GenServer via `handle_call`. A browser loading one page fires
|
|
||||||
parallel requests (page, CSS, images) that all serialize through this single
|
|
||||||
process — the textbook "process used for code organization" bottleneck.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/preview.ex` (`handle_call({:request, ...})` line ~93, `handle_call({:preview_draft, ...})` line ~102)
|
|
||||||
- `lib/bds/preview/router.ex` (the Plug calling into Preview)
|
|
||||||
|
|
||||||
**Approach.** Keep the GenServer for what actually needs serialization
|
|
||||||
(server lifecycle: start/stop/ensure, current-project state, graceful drain).
|
|
||||||
For requests, expose a fast state read (`:sys.get_state`-free — e.g. a public
|
|
||||||
ETS table or a lightweight `handle_call(:current_project)`) and perform
|
|
||||||
rendering in the Bandit request process itself. The drain logic
|
|
||||||
(`@drain_timeout`) already tracks inflight requests — adapt it to count
|
|
||||||
renderers via monitors or a counter.
|
|
||||||
|
|
||||||
**Acceptance.** A test issues N concurrent slow renders and asserts they
|
|
||||||
overlap (wall time « N × single render); stop_preview still drains correctly.
|
|
||||||
|
|
||||||
### TD-12: Move HNSW builds and duplicate scans out of `Embeddings.Index` handle_call ✅ DONE (2026-06-12)
|
|
||||||
|
|
||||||
**Status: implemented.** `BDS.Embeddings.Index` now runs duplicate scans and
|
|
||||||
HNSW rebuilds in supervised tasks instead of inside `handle_call`, while the
|
|
||||||
GenServer keeps only the small serialized state surface (current index, label
|
|
||||||
map, debounce timers, and flush coordination). Neighbor queries continue to hit
|
|
||||||
the current index while a scan or rebuild is in flight; rebuild requests for a
|
|
||||||
project coalesce onto the latest requested snapshot; `flush/1` and `flush_all/0`
|
|
||||||
wait for in-flight rebuilds before persisting; and `forget/1` cancels pending
|
|
||||||
index work cleanly. Acceptance proof now includes focused concurrency tests for
|
|
||||||
both slow duplicate scans and slow rebuilds, and the existing debounced
|
|
||||||
persistence coverage remains green.
|
|
||||||
|
|
||||||
**Context.** `Embeddings.Index` (singleton) builds HNSW graphs and runs full
|
|
||||||
duplicate scans inside `handle_call` with client timeout `:infinity`. A long
|
|
||||||
duplicate scan head-of-line-blocks every neighbor query the UI makes. The
|
|
||||||
state it guards (index refs, label maps, debounce timers) is small; the work
|
|
||||||
is what's big.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/embeddings/index.ex` (`handle_call({:put, ...})` ~105, `{:duplicate_pairs, ...}` ~128)
|
|
||||||
|
|
||||||
**Approach.** For `duplicate_pairs` (read-only over the index ref): capture
|
|
||||||
the entry in the GenServer, spawn the scan in a `Task` (HNSWLib index
|
|
||||||
resources are usable from other processes — verify; they are NIF resources,
|
|
||||||
confirm thread-safety of concurrent reads in hnswlib docs), and reply via
|
|
||||||
`GenServer.reply/2`. For `put` (build): construct the graph in the caller or
|
|
||||||
a Task and hand the finished index to the GenServer for state swap +
|
|
||||||
debounced save. Queries during a rebuild keep hitting the old index.
|
|
||||||
|
|
||||||
**Acceptance.** Neighbor queries return while a duplicate scan is running
|
|
||||||
(test with a large synthetic index); debounce/flush semantics unchanged.
|
|
||||||
|
|
||||||
### TD-13: Slim down the `Publishing` GenServer call surface
|
|
||||||
|
|
||||||
**Context.** `Publishing` does Repo writes inside `handle_call`
|
|
||||||
(`:upload_site`, `:update_job`) and the uploader makes per-file synchronous
|
|
||||||
calls (`:should_upload_scp_file` / `:mark_uploaded_scp_file`) — chatty
|
|
||||||
serialization through one process during uploads.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/publishing.ex` (lines ~45–119)
|
|
||||||
|
|
||||||
**Approach.** Job creation (Repo insert) can happen in the caller before the
|
|
||||||
call; the GenServer keeps only `scp_uploads` mtime state. Batch the mtime
|
|
||||||
check: one call returning the filtered upload list instead of two calls per
|
|
||||||
file. Consider whether `scp_uploads` state should be ETS
|
|
||||||
(`read_concurrency`) owned by the GenServer.
|
|
||||||
|
|
||||||
**Acceptance.** Upload of N files makes O(1) GenServer calls for mtime
|
|
||||||
bookkeeping, not O(N)·2; behavior identical for incremental uploads.
|
|
||||||
|
|
||||||
### TD-14: Replace polling with messaging (CliSync watcher + rebuild sequencing)
|
|
||||||
|
|
||||||
**Context.** Two polling loops:
|
|
||||||
1. `CliSync.Watcher` polls the SQLite notifications table every **100 ms
|
|
||||||
forever** — ~10 queries/sec at idle in a battery-powered desktop app.
|
|
||||||
2. `shell_commands.ex` `wait_for_group_phase/3` sleep-polls
|
|
||||||
`Tasks.list_tasks()` every 50 ms to sequence rebuild steps.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/cli_sync/watcher.ex` (`@default_poll_interval_ms 100`)
|
|
||||||
- `lib/bds/desktop/shell_commands.ex` (`wait_for_group_phase/3` lines ~563–585)
|
|
||||||
- `lib/bds/tasks.ex` (add completion broadcast)
|
|
||||||
|
|
||||||
**Approach.**
|
|
||||||
1. Watcher: cheapest meaningful fix is a `PRAGMA data_version` pre-check —
|
|
||||||
a single integer read that changes only when *another connection* commits;
|
|
||||||
only query the notifications table when it moves. (SQLite update hooks
|
|
||||||
don't fire for external connections, so the CLI-writes use case genuinely
|
|
||||||
needs polling — make it near-free instead of removing it.) Alternatively
|
|
||||||
watch the `-wal` file mtime with the `file_system` package. Also consider
|
|
||||||
lengthening the idle interval with backoff.
|
|
||||||
2. Rebuild sequencing: have `BDS.Tasks` broadcast task terminal states on
|
|
||||||
`BDS.PubSub` (it already sits next to PubSub in the supervision tree);
|
|
||||||
`wait_for_group_phase` subscribes and `receive`s with a deadline instead
|
|
||||||
of sleep-polling.
|
|
||||||
|
|
||||||
**Acceptance.** Idle app issues no table queries when `data_version` is
|
|
||||||
unchanged (or interval ≥ 1s with backoff); rebuild sequencing has no
|
|
||||||
`Process.sleep`; CLI-sync round-trip latency stays ≤ current behavior.
|
|
||||||
|
|
||||||
### TD-15: `BDS.Tasks` housekeeping (queue type, eviction timers)
|
|
||||||
|
|
||||||
**Context.** Minor inefficiencies in `tasks.ex`: the pending queue is a list
|
|
||||||
appended with `++` (O(n) per submit), and **every** finishing task schedules
|
|
||||||
a fresh 1-hour `send_after` eviction timer — timers accumulate without bound
|
|
||||||
(harmless, but sloppy).
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/tasks.ex` (`handle_call({:submit_task,...})` ~84, `schedule_finished_task_eviction/1` ~365)
|
|
||||||
|
|
||||||
**Approach.** Use `:queue` for the pending queue. Replace per-finish timers
|
|
||||||
with one periodic sweep (`send_after` rescheduled in `handle_info(:evict_finished_tasks, ...)`)
|
|
||||||
or track a single timer ref. Also consider downgrading `report_progress` from
|
|
||||||
`call` to `cast` (progress is fire-and-forget; the throttle already drops
|
|
||||||
updates).
|
|
||||||
|
|
||||||
**Acceptance.** One live eviction timer at most; queue operations O(1);
|
|
||||||
existing task lifecycle tests green.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Phase 4 — Build-vs-buy replacements
|
|
||||||
|
|
||||||
### TD-16: Frontmatter robustness — yaml_elixir/ymlr or harden the hand-rolled parser
|
|
||||||
|
|
||||||
**Context.** `BDS.Frontmatter` is a hand-rolled YAML subset with concrete
|
|
||||||
bugs for user-edited files:
|
|
||||||
- `parse_document` splits on `"\n---\n"` — **CRLF files never match** and are
|
|
||||||
rejected as `:invalid_frontmatter`. External editors on Windows will produce
|
|
||||||
these.
|
|
||||||
- `parse_string` handles quotes by trimming the trailing quote char — a quoted
|
|
||||||
string containing its own quote mid-string is corrupted.
|
|
||||||
- No nesting support (may be intentional).
|
|
||||||
|
|
||||||
Decision needed: adopt `yaml_elixir` (parse) + `ymlr` (emit), or keep the
|
|
||||||
subset deliberately (round-trip stability for file diffs is a legitimate
|
|
||||||
reason) and fix the bugs. Note the metadata sync rule: frontmatter is read by
|
|
||||||
publishing, metadata-diff, and rebuild-from-files — all three must stay in
|
|
||||||
agreement, which argues for keeping one serializer either way.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/frontmatter.ex`
|
|
||||||
- Callers: `lib/bds/posts/file_sync.ex`, `lib/bds/posts/rebuild_from_files.ex`, `lib/bds/document_fields.ex`
|
|
||||||
|
|
||||||
**Approach (minimum).** Normalize `\r\n` → `\n` before parsing; rewrite
|
|
||||||
`parse_string` to properly unescape (scan, don't trim); add property-style
|
|
||||||
round-trip tests (`parse(serialize(x)) == x`) including quotes, newlines,
|
|
||||||
unicode, CRLF input. If switching to yaml_elixir: keep `serialize_document`
|
|
||||||
output byte-stable against current golden files to avoid noisy git diffs in
|
|
||||||
user projects.
|
|
||||||
|
|
||||||
**Acceptance.** CRLF fixture parses; round-trip property tests pass; golden
|
|
||||||
serialization fixtures unchanged (if keeping custom serializer).
|
|
||||||
|
|
||||||
### TD-17: Language detection via `paasaa` (optional, low priority)
|
|
||||||
|
|
||||||
**Context.** `Search.detect_language/1` uses diacritic regexes + tiny word
|
|
||||||
lists; German text without umlauts (common in short posts) falls through to
|
|
||||||
English, picking the wrong stemmer. `paasaa` is a pure-Elixir trigram
|
|
||||||
detector.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/search.ex` (lines ~60–89, `detect_language/1`, `@language_hints`)
|
|
||||||
|
|
||||||
**Approach.** Add `{:paasaa, "~> 0.6"}`; use it for texts above a length
|
|
||||||
threshold, keep the cheap heuristics as a short-text fallback; map ISO codes
|
|
||||||
to `@stemmer_algorithms` keys; default `"en"` unchanged. Delete
|
|
||||||
`@language_hints` if fully superseded (dead-code rule).
|
|
||||||
|
|
||||||
**Acceptance.** Misclassification fixtures (umlaut-free German, accented-free
|
|
||||||
French) detect correctly; stemmer selection unchanged for English.
|
|
||||||
|
|
||||||
### TD-18: Evaluate Oban (Lite engine) for durable jobs
|
|
||||||
|
|
||||||
**Context.** Three overlapping job systems exist: in-memory UI tasks
|
|
||||||
(`BDS.Tasks`), DB-persisted publish jobs (`Publishing` + `PublishJob` rows),
|
|
||||||
and scripting jobs (`JobStore`/`JobRunner`/`JobSupervisor`). The durable ones
|
|
||||||
(publishing, possibly long script jobs) would get retries, uniqueness,
|
|
||||||
crash-recovery-on-restart, and telemetry for free from Oban's SQLite `Lite`
|
|
||||||
engine on the existing `ecto_sqlite3` setup. **This is an evaluation task,
|
|
||||||
not a mandate** — the in-memory `BDS.Tasks` UI progress system should stay.
|
|
||||||
|
|
||||||
**Files (for the evaluation).**
|
|
||||||
- `lib/bds/publishing.ex`, `lib/bds/publishing/publish_job.ex`
|
|
||||||
- `lib/bds/scripting/job_store.ex`, `job_runner.ex`, `job_supervisor.ex`
|
|
||||||
- `lib/bds/tasks.ex` (stays; would wrap Oban job progress for the UI)
|
|
||||||
|
|
||||||
**Approach.** Spike: model `upload_site` as an Oban worker with the Lite
|
|
||||||
engine; check binary-size and startup cost impact for the desktop release;
|
|
||||||
check interplay with the test sandbox and with the `bds_mcp` release. Write
|
|
||||||
up a go/no-go with the spike branch. If no-go, document why in this file and
|
|
||||||
close.
|
|
||||||
|
|
||||||
**Acceptance.** Decision documented; if go: publishing migrated first, with
|
|
||||||
crash-recovery test (kill app mid-upload, job resumes/retries on restart).
|
|
||||||
|
|
||||||
### TD-19: Add credo, mix_audit (and consider sobelow) to the quality gates
|
|
||||||
|
|
||||||
**Context.** The project enforces dialyzer + warnings-as-errors but has no
|
|
||||||
style/consistency linter and no dependency CVE audit. Cheap, high-leverage
|
|
||||||
additions.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `mix.exs` (deps + `validate` alias), new `.credo.exs`
|
|
||||||
|
|
||||||
**Approach.** Add `{:credo, "~> 1.7", only: [:dev, :test], runtime: false}`
|
|
||||||
and `{:mix_audit, "~> 2.1", only: [:dev, :test], runtime: false}`. Extend the
|
|
||||||
`validate` alias: `["test", "credo --strict", "deps.audit", "dialyzer"]`.
|
|
||||||
Start credo in non-strict mode if the initial violation count is large; fix
|
|
||||||
or explicitly disable rules rather than leaving noise. Sobelow is
|
|
||||||
Phoenix-web-oriented; evaluate whether its checks add signal for a
|
|
||||||
loopback-only desktop endpoint before adopting.
|
|
||||||
|
|
||||||
**Acceptance.** `mix validate` runs all four gates clean; CI/agent
|
|
||||||
instructions (AGENTS.md) updated to mention them.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Phase 5 — Consistency, config & hygiene
|
|
||||||
|
|
||||||
### TD-20: Align SQLite pool configuration between dev and prod
|
|
||||||
|
|
||||||
**Context.** Dev runs `pool_size: 5` (config.exs), prod runs `pool_size: 1`
|
|
||||||
(runtime.exs) — so dev and prod have **different concurrency semantics**: dev
|
|
||||||
can hit `SQLITE_BUSY`/interleavings prod never sees; prod serializes every
|
|
||||||
read behind every write. WAL mode + `busy_timeout: 15_000` are already set
|
|
||||||
globally.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `config/config.exs` (pool_size 5), `config/runtime.exs` (POOL_SIZE default "1")
|
|
||||||
|
|
||||||
**Approach.** Pick one deliberate model and apply it to both envs. Options:
|
|
||||||
(a) pool of 1 everywhere (simplest, fully serialized, fine for single-user);
|
|
||||||
(b) modest pool (3–5) everywhere relying on WAL + busy_timeout; (c)
|
|
||||||
ecto_sqlite3's documented separate read/write pool pattern. Document the
|
|
||||||
choice in a comment. Run the suite and a manual smoke under the chosen prod
|
|
||||||
setting.
|
|
||||||
|
|
||||||
**Acceptance.** Same pool model in dev and prod; rationale comment in config;
|
|
||||||
no busy-timeout regressions in tests.
|
|
||||||
|
|
||||||
### TD-21: Harden `Persistence.atomic_write`
|
|
||||||
|
|
||||||
**Context.** `atomic_write/2` uses a fixed `path <> ".tmp"` temp name — two
|
|
||||||
concurrent writers to the same path corrupt each other's temp file before
|
|
||||||
rename. No fsync before rename (crash can leave an empty/partial target on
|
|
||||||
some filesystems); for blog-post files that durability trade-off is
|
|
||||||
defensible, but the temp-name collision is not.
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/persistence.ex` (`atomic_write/2` lines ~70–82)
|
|
||||||
|
|
||||||
**Approach.** Unique temp suffix:
|
|
||||||
`path <> ".tmp." <> Integer.to_string(System.unique_integer([:positive]))`.
|
|
||||||
Optionally `:file.sync` the temp file before rename behind an opt-in flag for
|
|
||||||
critical writes. Clean up stray `*.tmp.*` files defensively where directories
|
|
||||||
are scanned (check rebuild-from-files glob patterns ignore them).
|
|
||||||
|
|
||||||
**Acceptance.** Concurrent-writer test produces two intact outcomes (last
|
|
||||||
write wins, no corruption); rebuild file globs ignore temp files.
|
|
||||||
|
|
||||||
### TD-22: Wrap `delete_chat_conversation` in a transaction
|
|
||||||
|
|
||||||
**Context.** `chat.ex` deletes all messages (`Repo.delete_all`) then the
|
|
||||||
conversation (`Repo.delete`) without a transaction — a failure in between
|
|
||||||
strands orphan message rows. The codebase uses `Repo.transaction`
|
|
||||||
conscientiously elsewhere; this is an outlier. (Check for siblings: any other
|
|
||||||
multi-statement write without a transaction found during work on this task
|
|
||||||
should be fixed in the same pass — e.g. audit `lib/bds/ai/catalog.ex`,
|
|
||||||
`lib/bds/scripts.ex`.)
|
|
||||||
|
|
||||||
**Files.**
|
|
||||||
- `lib/bds/ai/chat.ex` (`delete_chat_conversation/1` lines ~102–117)
|
|
||||||
|
|
||||||
**Approach.** `Repo.transaction(fn -> ... end)` or `Ecto.Multi`. Better:
|
|
||||||
add `ON DELETE CASCADE` via an Ecto migration on the
|
|
||||||
`chat_messages.conversation_id` FK and drop the manual `delete_all` (use
|
|
||||||
`mix ecto.gen.migration` per project rules).
|
|
||||||
|
|
||||||
**Acceptance.** Injected failure between the two deletes leaves both intact;
|
|
||||||
no orphaned messages possible.
|
|
||||||
|
|
||||||
### TD-23: Sweep blanket `rescue`/`catch` blocks for silent failure
|
|
||||||
|
|
||||||
**Context.** Several modules rescue *all* exceptions into `:ok`/fallbacks
|
|
||||||
(`shutdown.ex` defensibly — it must never block quit; but also
|
|
||||||
`overlay_components.ex` (11 rescues), `import_execution.ex`,
|
|
||||||
`import_analysis.ex`, `main_window.ex`, `Repo.ready?`). Blanket rescues hide
|
|
||||||
real bugs (typos become silent no-ops) and dialyzer can't see through them.
|
|
||||||
|
|
||||||
**Files.** Start with: `lib/bds/desktop/shell_live/overlay_components.ex`,
|
|
||||||
`lib/bds/import_execution.ex`, `lib/bds/import_analysis.ex`,
|
|
||||||
`lib/bds/desktop/main_window.ex`.
|
|
||||||
|
|
||||||
**Approach.** For each rescue: narrow to the specific exception(s) actually
|
|
||||||
expected, add a `Logger.warning` with context where swallowing is the right
|
|
||||||
call, and delete rescues that guard nothing. Shutdown.ex's rescues stay as-is
|
|
||||||
(documented intent: never block quit).
|
|
||||||
|
|
||||||
**Acceptance.** No bare `rescue _ ->` without either a narrow exception match
|
|
||||||
or a logged justification; suite green.
|
|
||||||
|
|
||||||
### TD-24: Fix stale `npm test` reference in AGENTS.md
|
|
||||||
|
|
||||||
**Context.** AGENTS.md "Fix All Test Failures" section says "Run the full
|
|
||||||
test suite (`npm test`)" — stale from the pre-Elixir rewrite. It's an
|
|
||||||
instruction file for agents; wrong commands actively mislead.
|
|
||||||
|
|
||||||
**Files.** `AGENTS.md`
|
|
||||||
|
|
||||||
**Approach.** Replace with `mix test` (and the `xvfb-run mix test` headless
|
|
||||||
note already present elsewhere in the file). While in there, scan for other
|
|
||||||
JS-era references (React components are mentioned in the i18n section —
|
|
||||||
verify whether that section should say LiveView/HEEx).
|
|
||||||
|
|
||||||
**Acceptance.** AGENTS.md commands all match the Elixir toolchain.
|
|
||||||
|
|
||||||
### TD-25: Generate the desktop endpoint secret at runtime ✅ DONE (2026-06-11, shipped with TD-01)
|
|
||||||
|
|
||||||
**Context.** The Phoenix endpoint `secret_key_base` (session/LiveView
|
|
||||||
signing) was a hardcoded repo string in all envs.
|
|
||||||
|
|
||||||
**Status: implemented as part of TD-01.** `BDS.Application.desktop_secret_key_base/0`
|
|
||||||
now generates `Base.encode64(:crypto.strong_rand_bytes(48))` per boot when no
|
|
||||||
explicit config is set; the static value was removed from `config/config.exs`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Explicit non-issues (decisions reviewed and endorsed)
|
|
||||||
|
|
||||||
Recorded so future reviews don't re-litigate them:
|
|
||||||
|
|
||||||
- **Shelling out to `git`** instead of libgit2 NIF bindings — correct;
|
|
||||||
bindings are immature. (Timeout gap tracked as TD-10.)
|
|
||||||
- **SIGKILL shutdown** for the wx static-destructor segfault — legitimate,
|
|
||||||
well-documented workaround. (Terminate-callback consequence tracked as TD-04.)
|
|
||||||
- **`BDS.BoundedAtoms`** allow-list atom conversion — good practice, keep.
|
|
||||||
- **Luerl with reduction/time caps** for user scripting — right tool, sound
|
|
||||||
sandbox config.
|
|
||||||
- **SQLite FTS5 + bm25** for search, **HNSWLib** for ANN, **Liquex/Earmark**
|
|
||||||
for rendering — all appropriate.
|
|
||||||
- **Integer-millisecond timestamps** (`Persistence.now_ms`) over
|
|
||||||
`:utc_datetime_usec` — committed design choice; consistent; keep
|
|
||||||
`parse_timestamp`'s seconds-vs-ms heuristic under test, don't migrate.
|
|
||||||
- **`elixir-desktop` dependency risk** (niche, lightly maintained) —
|
|
||||||
acknowledged; mitigation is the existing thin `BDS.Desktop.*` boundary, not
|
|
||||||
replacement.
|
|
||||||
- **No telemetry pipeline** — reasonable cut for a desktop app; revisit only
|
|
||||||
if support/debugging demands it.
|
|
||||||
223
TUI_EDITOR.md
Normal file
223
TUI_EDITOR.md
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
# TUI Editor — Implementation Plan (Issue #32)
|
||||||
|
|
||||||
|
Tracking issue: **#32** — "markdown, liquid and lua code editor with syntax
|
||||||
|
highlighting in the TUI".
|
||||||
|
Spec under change: `specs/tui.allium`.
|
||||||
|
|
||||||
|
Goal: a syntax-highlighted, word-wrapping TUI editor for posts (markdown
|
||||||
|
with `[[macro]]` syntax), templates (HTML + Liquid), and scripts (Lua),
|
||||||
|
replacing the current plain `ExRatatui.Widgets.Textarea` body.
|
||||||
|
|
||||||
|
## Decision: pure Elixir — no new Rust
|
||||||
|
|
||||||
|
`ExRatatui` already ships everything we need:
|
||||||
|
|
||||||
|
- `ExRatatui.CodeBlock.highlight/3` (`deps/ex_ratatui/lib/ex_ratatui/code_block.ex:103`)
|
||||||
|
returns `[%ExRatatui.Text.Line{}]` of styled spans via the bundled
|
||||||
|
syntect NIF. Supports Lua, Markdown, HTML, … out of the box.
|
||||||
|
- `ExRatatui.Widgets.CodeBlock` is read-only; we do **not** use it
|
||||||
|
directly for editing.
|
||||||
|
- `ExRatatui.Widget` protocol (`deps/ex_ratatui/lib/ex_ratatui/widget.ex:1`)
|
||||||
|
lets us implement a custom widget in pure Elixir that composes
|
||||||
|
primitives every frame. This is the seam we use.
|
||||||
|
- The editing engine stays Rust-owned
|
||||||
|
(`ExRatatui.textarea_new/0`, `textarea_handle_key/3`, `textarea_get_value/1`,
|
||||||
|
`textarea_cursor/1`, `textarea_line_count/1`). The custom widget only
|
||||||
|
re-renders the buffer through a styling + wrap pipeline.
|
||||||
|
- `ExRatatui.Widgets.Paragraph` accepts `[%Text.Line{}]` of styled spans
|
||||||
|
with `wrap: true` and a `scroll: {vertical, horizontal}` offset —
|
||||||
|
exactly the output shape we need.
|
||||||
|
|
||||||
|
The existing comment at `lib/bds/tui.ex:1451-1454` already pre-announces
|
||||||
|
this approach as "the planned MarkdownEditor custom widget".
|
||||||
|
|
||||||
|
Languages supported (matches GUI Monaco registrations in
|
||||||
|
`assets/js/monaco/languages.js`):
|
||||||
|
|
||||||
|
| Entity | Base lexer (NIF) | Elixir overlay |
|
||||||
|
|-----------------------|------------------|---------------------------------|
|
||||||
|
| post body | `markdown` | `[[macro …]]` recoloured |
|
||||||
|
| script body | `lua` | none |
|
||||||
|
| template body | `html` | `{{ … }}` and `{% … %}` recoloured |
|
||||||
|
|
||||||
|
Liquid is always HTML+Liquid in this codebase (no markdown+Liquid
|
||||||
|
templates exist). Plain text falls back to syntect's plain renderer.
|
||||||
|
|
||||||
|
## GUI styling parity (from `assets/js/monaco/theme.js` + `languages.js`)
|
||||||
|
|
||||||
|
- **macro** `[[ … ]]` → `keyword.macro` = `#C586C0`, **bold**.
|
||||||
|
- inside macro: `attribute.name` (e.g. `names`) → `#9CDCFE`,
|
||||||
|
`attribute.value` (e.g. `"x"`) → `#CE9178`.
|
||||||
|
- Liquid uses base vs-dark tokens; terminal approximation uses the same
|
||||||
|
base theme accent colours. No custom Liquid token colours overridden.
|
||||||
|
|
||||||
|
Use `{:rgb,197,134,192}` etc. via `ExRatatui.Style` (already supported
|
||||||
|
by `ExRatatui.CodeBlock.from_native/1`).
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
state.editor.textarea (unchanged, Rust-owned buffer/cursor/undo)
|
||||||
|
state.editor.language (:markdown_macros | :liquid | :lua | :text)
|
||||||
|
state.editor.preview? (unchanged ctrl+e toggle — default false for posts)
|
||||||
|
|
||||||
|
body_widget/3 edit branch: %BDS.UI.CodeEditor{textarea:, language:, theme:, wrap:, cursor_style:, line_highlight_style:, block:}
|
||||||
|
↳ impl ExRatatui.Widget.render:
|
||||||
|
1. content = ExRatatui.textarea_get_value(textarea)
|
||||||
|
2. {row, col} = ExRatatui.textarea_cursor(textarea)
|
||||||
|
3. lines = BDS.UI.CodeEditor.Highlight.highlight(content, language, theme)
|
||||||
|
4. apply current-line bg style into every span on lines[row]
|
||||||
|
5. cut the span at byte col on lines[row], splice cursor-style cell
|
||||||
|
6. top = vertical-scroll offset so cursor visual row stays in window (Latin-width count)
|
||||||
|
7. return [{%Paragraph{text: lines, wrap: true, scroll: {top,0}, block: block}, rect}]
|
||||||
|
```
|
||||||
|
|
||||||
|
`editor_key/1` in `lib/bds/tui.ex` keeps calling
|
||||||
|
`ExRatatui.textarea_handle_key/3` — no edit-model rewrite, the
|
||||||
|
textarea's own undo/redo/clipboard keeps working.
|
||||||
|
|
||||||
|
## Phases (test-first, per AGENTS.md)
|
||||||
|
|
||||||
|
### Phase 0 — Spec
|
||||||
|
|
||||||
|
Edit `specs/tui.allium`:
|
||||||
|
|
||||||
|
- `OpenEntry`: posts open in the **editor** (wrap on, highlighting on)
|
||||||
|
by default — not the rendered Markdown preview. New empty posts still
|
||||||
|
open in the editor (unchanged).
|
||||||
|
- Rename `WrappedPreview` → `EditorMode`: `ctrl+e` toggles editor ↔
|
||||||
|
read-only rendered-Markdown preview (via `ExRatatui.Widgets.Markdown`).
|
||||||
|
Drop the "textarea cannot wrap" caveat.
|
||||||
|
- Add `CodeEditorWrapping` rule: syntax highlighting + auto wrap +
|
||||||
|
highlighted current line. Explicitly **no line-number gutter**.
|
||||||
|
- Validate with `allium check specs/tui.allium`.
|
||||||
|
|
||||||
|
### Phase 1 — Red tests
|
||||||
|
|
||||||
|
New file `test/bds/ui/code_editor_test.exs`:
|
||||||
|
|
||||||
|
- `BDS.UI.CodeEditor.Highlight.highlight/3` for `:lua` colours a
|
||||||
|
`function … end` keyword with the theme's keyword colour.
|
||||||
|
- `:markdown_macros` recolours `[[gallery names="x"]]`:
|
||||||
|
- `[[`, `]]` and macro name → `{:rgb,197,134,192}` bold
|
||||||
|
- `names` → `{:rgb,156,220,254}`
|
||||||
|
- `"x"` → `{:rgb,206,145,120}`
|
||||||
|
- surrounding markdown still base-styled.
|
||||||
|
- `:liquid` recolours `{{ x }}` and `{% if x %}` over an HTML base;
|
||||||
|
`if` keyword distinct from identifiers.
|
||||||
|
- Widget emits a `%Paragraph{wrap: true, scroll:}`, never a bare `%Textarea`,
|
||||||
|
when language is one of the highlighted set.
|
||||||
|
|
||||||
|
Extended `test/bds/tui_test.exs`:
|
||||||
|
|
||||||
|
- Opening an existing post lands in the **editor** (`preview? == false`),
|
||||||
|
not the rendered preview. Replace the assertion in
|
||||||
|
`test "opening a post lands in the markdown preview, not the editor"`.
|
||||||
|
Test name/logic flips.
|
||||||
|
- Long line wraps across N visual rows in a 40-wide viewport
|
||||||
|
(assert via `CellSession.take_cells/1`).
|
||||||
|
- Cursor at the last visual row stays visible when scrolling a small
|
||||||
|
viewport (assert cursor-style cell appears in snapshot cells).
|
||||||
|
- Current line's cells have a distinct `bg` from non-current rows on
|
||||||
|
the same buffer.
|
||||||
|
- `ctrl+e` flips to preview and back; preview still rendered Markdown.
|
||||||
|
- `ctrl+s` persists textarea bytes unchanged regardless of highlight/wrap
|
||||||
|
(existing assertion still covers this — keep it green).
|
||||||
|
|
||||||
|
### Phase 2 — `lib/bds/ui/code_editor/highlight.ex` + `overlay.ex`
|
||||||
|
|
||||||
|
- `BDS.UI.CodeEditor.Highlight.highlight(content, language, theme)` →
|
||||||
|
`[%ExRatatui.Text.Line{}]`:
|
||||||
|
- `:lua`, `:text` → pass straight through to
|
||||||
|
`ExRatatui.CodeBlock.highlight/3`.
|
||||||
|
- `:markdown_macros` → base highlight as `"markdown"`, then apply
|
||||||
|
`Overlay.markdown_macros/1` per source line.
|
||||||
|
- `:liquid` → base highlight as `"html"`, then apply
|
||||||
|
`Overlay.liquid/1` per source line.
|
||||||
|
- `BDS.UI.CodeEditor.Overlay`:
|
||||||
|
- `apply_ranges(line, ranges_with_styles)` — re-splits a `Line`'s
|
||||||
|
spans into a new span list, preserving the base styling outside
|
||||||
|
each range.
|
||||||
|
- `markdown_macro_ranges/1` — `~r/\[\[\s*[a-zA-Z][\w-]*[^]]*\]\]/s`.
|
||||||
|
- `macro_inner_ranges/1` — break `attr=` (attr-name) and quoted value
|
||||||
|
into separate styled sub-ranges inside a macro.
|
||||||
|
- `liquid_ranges/1` — `~r/(\{\{.*?\}\}|\{%-?\s*(\w+).*?-?%\})/s`,
|
||||||
|
with a sub-range for the tag keyword (`if`, `for`, `assign`, …).
|
||||||
|
- Cache results by `{content_hash, language, theme}` if profiling shows
|
||||||
|
re-tokenisation overhead on keystroke (Phase 2 stretch goal —
|
||||||
|
skip until a test says otherwise).
|
||||||
|
|
||||||
|
### Phase 3 — `lib/bds/ui/code_editor/widget.ex`
|
||||||
|
|
||||||
|
- `%BDS.UI.CodeEditor{}` struct fields: `textarea`, `language`,
|
||||||
|
`theme` (default `:base16_ocean_dark`, readable in dark terminals),
|
||||||
|
`wrap: true`, `cursor_style`, `line_highlight_style`, `block`, `scroll`.
|
||||||
|
- `defimpl ExRatatui.Widget` per the pipeline above.
|
||||||
|
- Cursor cell: re-style the single codepoint at `col` on source line
|
||||||
|
`row` into `cursor_style`. Paragraph wraps the same line; the styled
|
||||||
|
cell rides the wrap.
|
||||||
|
- Scroll math: count display rows by wrapping each source line into
|
||||||
|
`width` columns (Latin-width assumption — 1 char = 1 cell). Note the
|
||||||
|
limitation explicitly; CJK width is a follow-up if/when needed.
|
||||||
|
- Line highlight: apply `line_highlight_style` (a `bg:` colour) to
|
||||||
|
every span on the cursor's source line before cursor cut-in.
|
||||||
|
|
||||||
|
### Phase 4 — Wire into `lib/bds/tui.ex`
|
||||||
|
|
||||||
|
- `build_editor/4` (around `lib/bds/tui.ex:1968`): default
|
||||||
|
`preview? = false` for existing posts. New posts already open in
|
||||||
|
editor — unchanged.
|
||||||
|
- `body_widget/3` edit branch (`lib/bds/tui.ex:1466`): emit
|
||||||
|
`%BDS.UI.CodeEditor{}` instead of `%ExRatatui.Widgets.Textarea{}` for
|
||||||
|
entities that have a syntax highlighter (`:lua`, `:markdown_macros`,
|
||||||
|
`:liquid`). Plain text can keep `%Textarea{}` or feed `:text` through
|
||||||
|
the new widget — pick whichever keeps the test surface smaller.
|
||||||
|
- Remove the "wrap limitation" comment block at
|
||||||
|
`lib/bds/tui.ex:1451-1454`.
|
||||||
|
- `cycle_language/1` continues per post; cycle through the active
|
||||||
|
set (`:markdown_macros`, `:liquid`, `:lua`, `:text`) matching the
|
||||||
|
existing GUI editor language options.
|
||||||
|
- Preview branch (`body_widget/3` preview arm at `lib/bds/tui.ex:1455`)
|
||||||
|
stays on `ExRatatui.Widgets.Markdown` — unchanged.
|
||||||
|
- No new user-facing strings: the existing body title already flows
|
||||||
|
through `dgettext("ui", …)`. If a new toolbar/hint is added later,
|
||||||
|
it MUST go through gettext and provide de/fr/it/es translations.
|
||||||
|
|
||||||
|
### Phase 5 — Verify
|
||||||
|
|
||||||
|
- `mix test` (write to `/tmp/test_run.log` first, grep for failures —
|
||||||
|
per AGENTS.md).
|
||||||
|
- `mix credo --strict`
|
||||||
|
- `mix deps.audit --ignore-file .mix_audit.ignore`
|
||||||
|
- `mix dialyzer` — treat warnings as errors.
|
||||||
|
|
||||||
|
No bundle rebuild, no asset change, no NIF rebuild. Monaco still drives
|
||||||
|
the GUI editor; this change is TUI-only.
|
||||||
|
|
||||||
|
## Risks / limits
|
||||||
|
|
||||||
|
- **Per-frame NIF cost:** re-tokenises the whole buffer on every
|
||||||
|
keystroke. For typical post sizes (<50 KB) syntect is sub-ms. Add the
|
||||||
|
`{content_hash, language, theme}` cache only if a test shows it.
|
||||||
|
- **CJK cell width:** wrap math assumes 1 char = 1 cell. Fine for
|
||||||
|
de/fr/it/es. Note as TODO; not in scope for this phase.
|
||||||
|
- **Liquid mis-lexing inside HTML** is cosmetically possible (an HTML
|
||||||
|
attribute containing `{{`). Overlay wins after the base lexer; the
|
||||||
|
result is visually tolerable. A custom `.sublime-syntax` in the
|
||||||
|
dep would be the real fix — out of scope here per "stay on Elixir".
|
||||||
|
- **Cursor in a wrapped paragraph** rides the styled cell. Equivalent
|
||||||
|
to a cell-caret; GUI uses a real caret — acceptable TUI parity.
|
||||||
|
|
||||||
|
## Files touched (expected)
|
||||||
|
|
||||||
|
- `specs/tui.allium` — edited.
|
||||||
|
- `lib/bds/ui/code_editor/highlight.ex` — new.
|
||||||
|
- `lib/bds/ui/code_editor/overlay.ex` — new.
|
||||||
|
- `lib/bds/ui/code_editor/widget.ex` — new.
|
||||||
|
- `lib/bds/tui.ex` — edited (`build_editor`, `body_widget`, comment
|
||||||
|
removal, default `preview?`).
|
||||||
|
- `test/bds/ui/code_editor_test.exs` — new.
|
||||||
|
- `test/bds/tui_test.exs` — edited (flip the "opens in preview"
|
||||||
|
assertion, add wrap/cursor/current-line tests).
|
||||||
|
|
||||||
|
No changes to `mix.exs`, no new deps, no Rust, no assets.
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
@source "../css";
|
@source "../css";
|
||||||
@source "../js";
|
@source "../js";
|
||||||
@source "../../lib/bds/desktop";
|
@source "../../lib/bds/desktop";
|
||||||
|
@source "../../deps/live_toast/lib";
|
||||||
|
|
||||||
@import "./tokens.css";
|
@import "./tokens.css";
|
||||||
@import "./shell.css";
|
@import "./shell.css";
|
||||||
|
|||||||
@@ -646,12 +646,6 @@
|
|||||||
max-width: 22px;
|
max-width: 22px;
|
||||||
max-height: 22px;
|
max-height: 22px;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
background: var(--vscode-button-background);
|
|
||||||
color: var(--vscode-button-foreground);
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-panel .chat-send-button:hover:not(:disabled) {
|
|
||||||
background: var(--vscode-button-hoverBackground);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.chat-panel .chat-send-button:disabled {
|
.chat-panel .chat-send-button:disabled {
|
||||||
|
|||||||
@@ -14,8 +14,7 @@
|
|||||||
|
|
||||||
.editor-main,
|
.editor-main,
|
||||||
.editor-meta,
|
.editor-meta,
|
||||||
.panel-shell,
|
.panel-shell {
|
||||||
.assistant-card {
|
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,8 +49,7 @@
|
|||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-toolbar-button:hover,
|
.editor-toolbar-button:hover {
|
||||||
.panel-tab:hover {
|
|
||||||
background: var(--vscode-toolbar-hoverBackground);
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -617,6 +615,14 @@
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.post-editor .tag-suggestion-section-label {
|
||||||
|
padding: 4px 12px;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
color: var(--vscode-descriptionForeground, #a6a6a6);
|
||||||
|
}
|
||||||
|
|
||||||
.post-editor .tag-suggestion.create-new {
|
.post-editor .tag-suggestion.create-new {
|
||||||
border-top: 1px solid var(--vscode-widget-border, #454545);
|
border-top: 1px solid var(--vscode-widget-border, #454545);
|
||||||
margin-top: 4px;
|
margin-top: 4px;
|
||||||
@@ -742,8 +748,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.post-editor .editor-toolbar {
|
.post-editor .editor-toolbar {
|
||||||
display: grid;
|
display: flex;
|
||||||
grid-template-columns: 1fr auto 1fr;
|
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
margin-bottom: 8px;
|
margin-bottom: 8px;
|
||||||
@@ -751,23 +756,27 @@
|
|||||||
|
|
||||||
.post-editor .editor-toolbar-left {
|
.post-editor .editor-toolbar-left {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-editor .editor-toolbar-center {
|
.post-editor .editor-toolbar-center {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex: none;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ponytail: right group keeps its content width so buttons never wrap or
|
||||||
|
overlap the mode toggle; the left label shrinks instead */
|
||||||
.post-editor .editor-toolbar-right {
|
.post-editor .editor-toolbar-right {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex: 1 0 auto;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
gap: 6px;
|
gap: 6px;
|
||||||
min-width: 0;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-editor .editor-mode-toggle {
|
.post-editor .editor-mode-toggle {
|
||||||
@@ -809,38 +818,6 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-editor .gallery-button {
|
|
||||||
padding: 4px 12px;
|
|
||||||
font-size: 12px;
|
|
||||||
border-radius: 4px;
|
|
||||||
background-color: var(--vscode-button-secondaryBackground);
|
|
||||||
color: var(--vscode-button-secondaryForeground);
|
|
||||||
border: none;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: background-color 0.15s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.post-editor .gallery-button:hover {
|
|
||||||
background-color: var(--vscode-button-secondaryHoverBackground);
|
|
||||||
}
|
|
||||||
|
|
||||||
.post-editor .insert-post-link-button,
|
|
||||||
.post-editor .insert-media-button {
|
|
||||||
padding: 4px 8px;
|
|
||||||
font-size: 14px;
|
|
||||||
border-radius: 4px;
|
|
||||||
background-color: var(--vscode-button-secondaryBackground);
|
|
||||||
color: var(--vscode-button-secondaryForeground);
|
|
||||||
border: none;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: background-color 0.15s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.post-editor .insert-post-link-button:hover,
|
|
||||||
.post-editor .insert-media-button:hover {
|
|
||||||
background-color: var(--vscode-button-secondaryHoverBackground);
|
|
||||||
}
|
|
||||||
|
|
||||||
.post-editor .editor-preview {
|
.post-editor .editor-preview {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
background-color: var(--vscode-input-background);
|
background-color: var(--vscode-input-background);
|
||||||
@@ -929,8 +906,7 @@
|
|||||||
.post-editor .editor-header,
|
.post-editor .editor-header,
|
||||||
.scripts-view-shell .ui-editor-header,
|
.scripts-view-shell .ui-editor-header,
|
||||||
.templates-view-shell .ui-editor-header,
|
.templates-view-shell .ui-editor-header,
|
||||||
.post-editor .metadata-toggle-header,
|
.post-editor .metadata-toggle-header {
|
||||||
.post-editor .editor-toolbar {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: flex-start;
|
align-items: flex-start;
|
||||||
@@ -946,7 +922,6 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-editor .editor-toolbar-right,
|
|
||||||
.post-editor .ui-editor-actions,
|
.post-editor .ui-editor-actions,
|
||||||
.scripts-view-shell .ui-editor-actions,
|
.scripts-view-shell .ui-editor-actions,
|
||||||
.templates-view-shell .ui-editor-actions {
|
.templates-view-shell .ui-editor-actions {
|
||||||
|
|||||||
@@ -67,30 +67,12 @@
|
|||||||
color: var(--vscode-descriptionForeground);
|
color: var(--vscode-descriptionForeground);
|
||||||
}
|
}
|
||||||
|
|
||||||
.import-analysis button,
|
|
||||||
.import-analysis select {
|
.import-analysis select {
|
||||||
border: 1px solid var(--vscode-button-border, transparent);
|
border: 1px solid var(--vscode-button-border, transparent);
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.import-analysis button {
|
|
||||||
background: var(--vscode-button-secondaryBackground, var(--vscode-button-background));
|
|
||||||
color: var(--vscode-button-secondaryForeground, var(--vscode-button-foreground));
|
|
||||||
padding: 8px 12px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.import-analysis button:hover:not(:disabled) {
|
|
||||||
background: var(--vscode-button-secondaryHoverBackground, var(--vscode-button-hoverBackground));
|
|
||||||
}
|
|
||||||
|
|
||||||
.import-analyze-btn,
|
|
||||||
.import-execute-btn {
|
|
||||||
background: var(--vscode-button-background) !important;
|
|
||||||
color: var(--vscode-button-foreground) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.import-analysis button:disabled {
|
.import-analysis button:disabled {
|
||||||
opacity: 0.65;
|
opacity: 0.65;
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
@@ -110,7 +92,7 @@
|
|||||||
width: 18px;
|
width: 18px;
|
||||||
height: 18px;
|
height: 18px;
|
||||||
border: 2px solid var(--vscode-descriptionForeground);
|
border: 2px solid var(--vscode-descriptionForeground);
|
||||||
border-top-color: var(--vscode-button-background);
|
border-top-color: var(--vscode-button-background, #0e639c);
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
animation: import-spinner-rotate 0.8s linear infinite;
|
animation: import-spinner-rotate 0.8s linear infinite;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
|
|||||||
@@ -106,8 +106,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
[data-testid="media-editor"] .media-preview-image img {
|
[data-testid="media-editor"] .media-preview-image img {
|
||||||
width: 100%;
|
max-width: 100%;
|
||||||
height: 100%;
|
max-height: 100%;
|
||||||
object-fit: contain;
|
object-fit: contain;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
@@ -128,20 +128,6 @@
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
[data-testid="media-editor"] .add-link-btn {
|
|
||||||
background: var(--vscode-button-secondaryBackground);
|
|
||||||
border: none;
|
|
||||||
color: var(--vscode-button-secondaryForeground);
|
|
||||||
padding: 2px 8px;
|
|
||||||
border-radius: 3px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 11px;
|
|
||||||
}
|
|
||||||
|
|
||||||
[data-testid="media-editor"] .add-link-btn:hover {
|
|
||||||
background: var(--vscode-button-secondaryHoverBackground);
|
|
||||||
}
|
|
||||||
|
|
||||||
[data-testid="media-editor"] .post-picker {
|
[data-testid="media-editor"] .post-picker {
|
||||||
background: var(--vscode-dropdown-background);
|
background: var(--vscode-dropdown-background);
|
||||||
border: 1px solid var(--vscode-dropdown-border);
|
border: 1px solid var(--vscode-dropdown-border);
|
||||||
|
|||||||
@@ -537,3 +537,8 @@
|
|||||||
border: 1px solid var(--vscode-panel-border);
|
border: 1px solid var(--vscode-panel-border);
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.monaco-diff-editor-instance {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
.language-picker-modal-backdrop,
|
.language-picker-modal-backdrop,
|
||||||
.confirm-delete-modal-backdrop,
|
.confirm-delete-modal-backdrop,
|
||||||
.confirm-dialog-overlay,
|
.confirm-dialog-overlay,
|
||||||
|
.git-run-backdrop,
|
||||||
.gallery-overlay,
|
.gallery-overlay,
|
||||||
.lightbox-overlay {
|
.lightbox-overlay {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
@@ -35,6 +36,7 @@
|
|||||||
.language-picker-modal,
|
.language-picker-modal,
|
||||||
.confirm-delete-modal,
|
.confirm-delete-modal,
|
||||||
.confirm-dialog,
|
.confirm-dialog,
|
||||||
|
.git-run-modal,
|
||||||
.gallery-overlay-content {
|
.gallery-overlay-content {
|
||||||
position: relative;
|
position: relative;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
@@ -44,6 +46,76 @@
|
|||||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.git-run-modal {
|
||||||
|
width: min(760px, calc(100vw - 32px));
|
||||||
|
max-height: calc(100vh - 48px);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 12px 16px;
|
||||||
|
border-bottom: 1px solid #3c3c3c;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-title {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 14px;
|
||||||
|
font-family: var(--font-mono, monospace);
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-status {
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-status.running {
|
||||||
|
color: #d7ba7d;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-status.ok {
|
||||||
|
color: #4ec9b0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-status.fail {
|
||||||
|
color: #f48771;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-output {
|
||||||
|
margin: 0;
|
||||||
|
padding: 12px 16px;
|
||||||
|
overflow: auto;
|
||||||
|
flex: 1;
|
||||||
|
min-height: 160px;
|
||||||
|
max-height: 60vh;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-word;
|
||||||
|
font-family: var(--font-mono, monospace);
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.5;
|
||||||
|
background: #141414;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-stdout {
|
||||||
|
color: #d4d4d4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-stderr {
|
||||||
|
color: #f48771;
|
||||||
|
}
|
||||||
|
|
||||||
|
.git-run-footer {
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
padding: 12px 16px;
|
||||||
|
border-top: 1px solid #3c3c3c;
|
||||||
|
}
|
||||||
|
|
||||||
.ai-suggestions-modal,
|
.ai-suggestions-modal,
|
||||||
.language-picker-modal,
|
.language-picker-modal,
|
||||||
.confirm-delete-modal,
|
.confirm-delete-modal,
|
||||||
@@ -257,8 +329,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.ai-suggestions-modal-footer,
|
.ai-suggestions-modal-footer,
|
||||||
.confirm-delete-modal-footer,
|
.confirm-delete-modal-footer {
|
||||||
.confirm-dialog-actions {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
gap: 10px;
|
gap: 10px;
|
||||||
@@ -266,42 +337,12 @@
|
|||||||
border-top: 1px solid #3c3c3c;
|
border-top: 1px solid #3c3c3c;
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-cancel,
|
|
||||||
.button-delete,
|
|
||||||
.button-apply,
|
|
||||||
.confirm-dialog-actions button,
|
|
||||||
.insert-modal-submit,
|
|
||||||
.language-picker-row,
|
.language-picker-row,
|
||||||
.shared-popover-entry,
|
.shared-popover-entry,
|
||||||
.colour-swatch {
|
.colour-swatch {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-cancel,
|
|
||||||
.confirm-dialog-actions button,
|
|
||||||
.insert-modal-submit {
|
|
||||||
border: 1px solid #4c4c4c;
|
|
||||||
border-radius: 4px;
|
|
||||||
padding: 8px 14px;
|
|
||||||
background: transparent;
|
|
||||||
color: #f0f0f0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.button-apply,
|
|
||||||
.confirm-dialog-actions .primary,
|
|
||||||
.insert-modal-submit {
|
|
||||||
background: #0e639c;
|
|
||||||
border-color: #0e639c;
|
|
||||||
}
|
|
||||||
|
|
||||||
.button-delete {
|
|
||||||
border: none;
|
|
||||||
border-radius: 4px;
|
|
||||||
padding: 8px 14px;
|
|
||||||
background: #c73c3c;
|
|
||||||
color: #ffffff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.insert-modal-tabs {
|
.insert-modal-tabs {
|
||||||
display: flex;
|
display: flex;
|
||||||
margin: 0 -20px;
|
margin: 0 -20px;
|
||||||
@@ -336,3 +377,78 @@
|
|||||||
font: inherit;
|
font: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.insert-modal-results {
|
||||||
|
overflow: auto;
|
||||||
|
padding: 8px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-result-item {
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
color: inherit;
|
||||||
|
padding: 10px 20px;
|
||||||
|
text-align: left;
|
||||||
|
cursor: pointer;
|
||||||
|
font: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-result-item:hover {
|
||||||
|
background: #252526;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-result-title {
|
||||||
|
color: #ffffff;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-result-meta {
|
||||||
|
color: #9d9d9d;
|
||||||
|
font-size: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-status {
|
||||||
|
padding: 16px 20px;
|
||||||
|
color: #9d9d9d;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-external {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 16px 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-external .insert-modal-input {
|
||||||
|
padding: 8px 10px;
|
||||||
|
border: 1px solid #3c3c3c;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: #252526;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-label {
|
||||||
|
color: #9d9d9d;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.insert-modal-submit {
|
||||||
|
align-self: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Alert modal (errors) — reuses .confirm-delete-modal with a tone accent. */
|
||||||
|
.alert-modal-error {
|
||||||
|
border-top: 3px solid #ff6b6b;
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,14 +8,11 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-tabs {
|
|
||||||
display: flex;
|
|
||||||
gap: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-tabs {
|
.panel-tabs {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 2px;
|
gap: 2px;
|
||||||
|
align-items: stretch;
|
||||||
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-tab {
|
.panel-tab {
|
||||||
@@ -116,7 +113,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.assistant-sidebar-prompt-form,
|
.assistant-sidebar-prompt-form,
|
||||||
.assistant-sidebar-welcome,
|
|
||||||
.assistant-sidebar-transcript {
|
.assistant-sidebar-transcript {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
@@ -142,12 +138,6 @@
|
|||||||
|
|
||||||
.assistant-sidebar-start-button {
|
.assistant-sidebar-start-button {
|
||||||
align-self: flex-start;
|
align-self: flex-start;
|
||||||
border: 1px solid var(--vscode-button-border, transparent);
|
|
||||||
border-radius: 999px;
|
|
||||||
background: var(--vscode-button-background);
|
|
||||||
color: var(--vscode-button-foreground);
|
|
||||||
padding: 7px 14px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.assistant-sidebar-start-button:disabled {
|
.assistant-sidebar-start-button:disabled {
|
||||||
@@ -155,7 +145,6 @@
|
|||||||
opacity: 0.55;
|
opacity: 0.55;
|
||||||
}
|
}
|
||||||
|
|
||||||
.assistant-card,
|
|
||||||
.assistant-sidebar-message {
|
.assistant-sidebar-message {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
@@ -254,6 +243,12 @@
|
|||||||
background-color: var(--vscode-sideBar-background);
|
background-color: var(--vscode-sideBar-background);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* .panel-entry (shell.css) forces flex-direction: column and, being unlayered,
|
||||||
|
beats the Tailwind row utilities on this button — force the row here. */
|
||||||
|
.task-group-toggle {
|
||||||
|
flex-direction: row;
|
||||||
|
}
|
||||||
|
|
||||||
.output-entry {
|
.output-entry {
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
border-bottom: none;
|
border-bottom: none;
|
||||||
|
|||||||
@@ -402,24 +402,6 @@
|
|||||||
border-bottom: 1px solid var(--vscode-panel-border);
|
border-bottom: 1px solid var(--vscode-panel-border);
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-tabs {
|
|
||||||
display: flex;
|
|
||||||
align-items: stretch;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-tab {
|
|
||||||
border: none;
|
|
||||||
background: transparent;
|
|
||||||
color: var(--vscode-descriptionForeground);
|
|
||||||
padding: 0 12px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-tab.active {
|
|
||||||
color: var(--vscode-tab-activeForeground);
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-close {
|
.panel-close {
|
||||||
background: transparent;
|
background: transparent;
|
||||||
border: none;
|
border: none;
|
||||||
@@ -446,8 +428,7 @@
|
|||||||
padding: 12px 14px;
|
padding: 12px 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-entry,
|
.panel-entry {
|
||||||
.assistant-card {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
@@ -779,19 +760,6 @@
|
|||||||
padding-left: 18px;
|
padding-left: 18px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-toolbar {
|
|
||||||
display: flex;
|
|
||||||
gap: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.editor-toolbar button {
|
|
||||||
padding: 9px 14px;
|
|
||||||
border: 1px solid var(--line);
|
|
||||||
border-radius: 999px;
|
|
||||||
background: var(--panel-3);
|
|
||||||
color: var(--ink);
|
|
||||||
}
|
|
||||||
|
|
||||||
.editor-meta {
|
.editor-meta {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
@@ -799,18 +767,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.editor-meta-card,
|
.editor-meta-card,
|
||||||
.assistant-card,
|
|
||||||
.panel-entry {
|
.panel-entry {
|
||||||
padding: 16px;
|
padding: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.sidebar-header,
|
|
||||||
.assistant-header,
|
|
||||||
.panel-header {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
gap: 12px;
|
|
||||||
padding: 16px 18px;
|
|
||||||
border-bottom: 1px solid var(--line);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -148,7 +148,6 @@
|
|||||||
gap: 2px;
|
gap: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.assistant-card span,
|
|
||||||
.panel-entry span,
|
.panel-entry span,
|
||||||
.editor-meta-row span,
|
.editor-meta-row span,
|
||||||
.editor-subtitle,
|
.editor-subtitle,
|
||||||
@@ -777,8 +776,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.filter-chip.active {
|
.filter-chip.active {
|
||||||
background-color: var(--vscode-button-background);
|
background-color: var(--vscode-button-background, #0e639c);
|
||||||
color: var(--vscode-button-foreground);
|
color: var(--vscode-button-foreground, #ffffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
.filter-chip.has-color {
|
.filter-chip.has-color {
|
||||||
|
|||||||
@@ -37,6 +37,9 @@
|
|||||||
--vscode-editorGroupHeader-tabsBorder: #1e1e1e;
|
--vscode-editorGroupHeader-tabsBorder: #1e1e1e;
|
||||||
--vscode-toolbar-hoverBackground: rgba(90, 93, 94, 0.31);
|
--vscode-toolbar-hoverBackground: rgba(90, 93, 94, 0.31);
|
||||||
--vscode-toolbar-activeBackground: rgba(99, 102, 103, 0.31);
|
--vscode-toolbar-activeBackground: rgba(99, 102, 103, 0.31);
|
||||||
|
--vscode-button-secondaryBackground: rgba(255, 255, 255, 0.08);
|
||||||
|
--vscode-button-secondaryForeground: #cccccc;
|
||||||
|
--vscode-button-secondaryHoverBackground: #4a4d51;
|
||||||
--vscode-foreground: #cccccc;
|
--vscode-foreground: #cccccc;
|
||||||
--vscode-descriptionForeground: #858585;
|
--vscode-descriptionForeground: #858585;
|
||||||
--vscode-panel-border: #80808059;
|
--vscode-panel-border: #80808059;
|
||||||
@@ -130,12 +133,13 @@ button.compact {
|
|||||||
}
|
}
|
||||||
|
|
||||||
button.primary {
|
button.primary {
|
||||||
background-color: var(--vscode-button-background);
|
color: var(--vscode-button-foreground, #ffffff);
|
||||||
|
background-color: var(--vscode-button-background, #0e639c);
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
button.primary:hover {
|
button.primary:hover {
|
||||||
background-color: var(--vscode-button-hoverBackground);
|
background-color: var(--vscode-button-hoverBackground, #1177bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
button.success {
|
button.success {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
} from "../utils/shortcuts.js";
|
} from "../utils/shortcuts.js";
|
||||||
import { syncTitlebarOverlayInsets } from "../bridges/titlebar_overlay.js";
|
import { syncTitlebarOverlayInsets } from "../bridges/titlebar_overlay.js";
|
||||||
import { runMenuRuntimeCommand } from "../bridges/menu_runtime.js";
|
import { runMenuRuntimeCommand } from "../bridges/menu_runtime.js";
|
||||||
|
import { copyTextToClipboard } from "../utils/clipboard.js";
|
||||||
|
|
||||||
export const AppShell = {
|
export const AppShell = {
|
||||||
mounted() {
|
mounted() {
|
||||||
@@ -179,6 +180,10 @@ export const AppShell = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.handleEvent("clipboard", ({ text }) => {
|
||||||
|
copyTextToClipboard(text || "");
|
||||||
|
});
|
||||||
|
|
||||||
window.addEventListener("bds:native-menu-action", this.handleNativeMenuAction);
|
window.addEventListener("bds:native-menu-action", this.handleNativeMenuAction);
|
||||||
window.addEventListener("keydown", this.handleShortcutKeyDown, true);
|
window.addEventListener("keydown", this.handleShortcutKeyDown, true);
|
||||||
this.el.addEventListener("load", this.handleThumbnailLoad, true);
|
this.el.addEventListener("load", this.handleThumbnailLoad, true);
|
||||||
@@ -186,6 +191,9 @@ export const AppShell = {
|
|||||||
this.el.addEventListener("change", this.handleChange);
|
this.el.addEventListener("change", this.handleChange);
|
||||||
syncMediaThumbnailState(this.el);
|
syncMediaThumbnailState(this.el);
|
||||||
this.restoreStoredWorkbenchSession();
|
this.restoreStoredWorkbenchSession();
|
||||||
|
// Rehydration is done (any stored-session push is ordered before this):
|
||||||
|
// tell the server it may now deliver queued deep links.
|
||||||
|
this.pushEvent("shell_ready", {});
|
||||||
},
|
},
|
||||||
|
|
||||||
updated() {
|
updated() {
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import { ColourPicker } from "./colour_picker.js";
|
|||||||
import { MenuEditorTree } from "./menu_editor_tree.js";
|
import { MenuEditorTree } from "./menu_editor_tree.js";
|
||||||
import { MonacoEditor } from "./monaco_editor.js";
|
import { MonacoEditor } from "./monaco_editor.js";
|
||||||
import { MonacoDiffEditor } from "./monaco_diff_editor.js";
|
import { MonacoDiffEditor } from "./monaco_diff_editor.js";
|
||||||
|
import { createLiveToastHook } from "live_toast";
|
||||||
|
|
||||||
export const Hooks = {
|
export const Hooks = {
|
||||||
|
LiveToast: createLiveToastHook(),
|
||||||
AppShell,
|
AppShell,
|
||||||
SidebarInteractions,
|
SidebarInteractions,
|
||||||
SettingsSectionScroll,
|
SettingsSectionScroll,
|
||||||
|
|||||||
@@ -5,6 +5,66 @@ import {
|
|||||||
unregisterMonacoEditor
|
unregisterMonacoEditor
|
||||||
} from "../monaco/services.js";
|
} from "../monaco/services.js";
|
||||||
|
|
||||||
|
const WEBKIT_TEXTAREA_ARROW_COMMANDS = {
|
||||||
|
ArrowLeft: "cursorLeft",
|
||||||
|
ArrowRight: "cursorRight",
|
||||||
|
ArrowUp: "cursorUp",
|
||||||
|
ArrowDown: "cursorDown"
|
||||||
|
};
|
||||||
|
|
||||||
|
const isWebKitEngine = () => {
|
||||||
|
const userAgent = globalThis.navigator?.userAgent || "";
|
||||||
|
return userAgent.includes("AppleWebKit") && !userAgent.includes("Chrome") && !userAgent.includes("Chromium");
|
||||||
|
};
|
||||||
|
|
||||||
|
const isMonacoInputArea = (target) =>
|
||||||
|
target instanceof HTMLTextAreaElement && target.classList.contains("inputarea");
|
||||||
|
|
||||||
|
export const webKitTextAreaArrowCommand = (event) => {
|
||||||
|
if (event?.altKey || event?.ctrlKey || event?.metaKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const command = WEBKIT_TEXTAREA_ARROW_COMMANDS[event?.key];
|
||||||
|
|
||||||
|
if (!command) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return event.shiftKey ? `${command}Select` : command;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Applying a remote value via setValue resets the cursor to the buffer start,
|
||||||
|
// which then makes follow-up actions (like link inserts targeting the current
|
||||||
|
// selection) land at position 1,1. Capture and restore the selection so the
|
||||||
|
// cursor survives server-driven reconciles; Monaco clamps out-of-range
|
||||||
|
// positions to the new content.
|
||||||
|
export const applyRemoteEditorValue = (editor, value) => {
|
||||||
|
const selections = editor.getSelections ? editor.getSelections() : null;
|
||||||
|
editor.setValue(value);
|
||||||
|
|
||||||
|
if (selections && selections.length > 0 && editor.setSelections) {
|
||||||
|
editor.setSelections(selections);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const bridgeWebKitTextAreaArrowKey = (editor, event) => {
|
||||||
|
if (!editor || !isMonacoInputArea(event.target)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const command = webKitTextAreaArrowCommand(event);
|
||||||
|
|
||||||
|
if (!command) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
editor.trigger("keyboard", command, { source: "keyboard" });
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
export const MonacoEditor = {
|
export const MonacoEditor = {
|
||||||
mounted() {
|
mounted() {
|
||||||
this.textarea = document.getElementById(this.el.dataset.monacoInputId) || this.el.querySelector("textarea");
|
this.textarea = document.getElementById(this.el.dataset.monacoInputId) || this.el.querySelector("textarea");
|
||||||
@@ -17,6 +77,10 @@ export const MonacoEditor = {
|
|||||||
this.isApplyingRemoteUpdate = false;
|
this.isApplyingRemoteUpdate = false;
|
||||||
this.lastKnownValue = this.textarea?.value || "";
|
this.lastKnownValue = this.textarea?.value || "";
|
||||||
|
|
||||||
|
this.handleWebKitTextAreaArrowKey = (event) => {
|
||||||
|
bridgeWebKitTextAreaArrowKey(this.editor, event);
|
||||||
|
};
|
||||||
|
|
||||||
this.syncEditorFromTextarea = () => {
|
this.syncEditorFromTextarea = () => {
|
||||||
this.textarea = document.getElementById(this.el.dataset.monacoInputId) || this.el.querySelector("textarea");
|
this.textarea = document.getElementById(this.el.dataset.monacoInputId) || this.el.querySelector("textarea");
|
||||||
|
|
||||||
@@ -26,10 +90,28 @@ export const MonacoEditor = {
|
|||||||
|
|
||||||
const value = this.textarea.value || "";
|
const value = this.textarea.value || "";
|
||||||
|
|
||||||
|
// ponytail: ignore the server echoing back our own last-emitted value;
|
||||||
|
// only a genuinely remote value (translation switch, etc.) should reset
|
||||||
|
// the editor. Otherwise mid-typing echoes clobber the cursor to offset 0.
|
||||||
|
if (value === this.lastKnownValue) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// While the user is actively typing, Monaco owns the content. Never let a
|
||||||
|
// server re-render (autosave, dirty toggle, metadata) yank it out from
|
||||||
|
// under the cursor — reconcile only once the editor is blurred.
|
||||||
|
if (this.editor.hasTextFocus()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.editor.getValue() !== value) {
|
if (this.editor.getValue() !== value) {
|
||||||
this.isApplyingRemoteUpdate = true;
|
this.isApplyingRemoteUpdate = true;
|
||||||
this.editor.setValue(value);
|
|
||||||
this.isApplyingRemoteUpdate = false;
|
try {
|
||||||
|
applyRemoteEditorValue(this.editor, value);
|
||||||
|
} finally {
|
||||||
|
this.isApplyingRemoteUpdate = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lastKnownValue = value;
|
this.lastKnownValue = value;
|
||||||
@@ -232,6 +314,10 @@ export const MonacoEditor = {
|
|||||||
this.el.addEventListener("dragover", this.handleDragOver);
|
this.el.addEventListener("dragover", this.handleDragOver);
|
||||||
this.el.addEventListener("drop", this.handleDrop);
|
this.el.addEventListener("drop", this.handleDrop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isWebKitEngine()) {
|
||||||
|
this.host.addEventListener("keydown", this.handleWebKitTextAreaArrowKey, true);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("Failed to load Monaco editor", error);
|
console.error("Failed to load Monaco editor", error);
|
||||||
@@ -273,6 +359,8 @@ export const MonacoEditor = {
|
|||||||
this.el.removeEventListener("drop", this.handleDrop);
|
this.el.removeEventListener("drop", this.handleDrop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.host?.removeEventListener("keydown", this.handleWebKitTextAreaArrowKey, true);
|
||||||
|
|
||||||
unregisterMonacoEditor(this.editorId || this.el.id);
|
unregisterMonacoEditor(this.editorId || this.el.id);
|
||||||
this.editor?.dispose();
|
this.editor?.dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,54 @@
|
|||||||
import { loadScript } from "../utils/script_loader.js";
|
|
||||||
import { ensureMonacoTheme } from "./theme.js";
|
import { ensureMonacoTheme } from "./theme.js";
|
||||||
import { registerLiquidLanguage, registerMarkdownWithMacrosLanguage } from "./languages.js";
|
import { registerLiquidLanguage, registerMarkdownWithMacrosLanguage } from "./languages.js";
|
||||||
|
|
||||||
let monacoLoaderPromise;
|
let monacoLoaderPromise;
|
||||||
const monacoEditors = new Map();
|
const monacoEditors = new Map();
|
||||||
|
|
||||||
|
const monacoWorkerUrls = {
|
||||||
|
editor: "/assets/monaco/editor.worker.js",
|
||||||
|
css: "/assets/monaco/css.worker.js",
|
||||||
|
html: "/assets/monaco/html.worker.js",
|
||||||
|
json: "/assets/monaco/json.worker.js",
|
||||||
|
ts: "/assets/monaco/ts.worker.js"
|
||||||
|
};
|
||||||
|
|
||||||
|
const workerNameForLanguage = (label) => {
|
||||||
|
switch (label) {
|
||||||
|
case "css":
|
||||||
|
case "scss":
|
||||||
|
case "less":
|
||||||
|
return "css";
|
||||||
|
case "html":
|
||||||
|
case "handlebars":
|
||||||
|
case "razor":
|
||||||
|
return "html";
|
||||||
|
case "json":
|
||||||
|
return "json";
|
||||||
|
case "typescript":
|
||||||
|
case "javascript":
|
||||||
|
return "ts";
|
||||||
|
default:
|
||||||
|
return "editor";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const ensureMonacoEnvironment = () => {
|
||||||
|
if (globalThis.MonacoEnvironment?.getWorker) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
globalThis.MonacoEnvironment = {
|
||||||
|
...globalThis.MonacoEnvironment,
|
||||||
|
getWorker(_workerId, label) {
|
||||||
|
const workerName = workerNameForLanguage(label);
|
||||||
|
return new Worker(monacoWorkerUrls[workerName], { name: label, type: "module" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const loadMonaco = () => {
|
export const loadMonaco = () => {
|
||||||
|
ensureMonacoEnvironment();
|
||||||
|
|
||||||
if (window.monaco?.editor) {
|
if (window.monaco?.editor) {
|
||||||
ensureMonacoTheme(window.monaco);
|
ensureMonacoTheme(window.monaco);
|
||||||
registerLiquidLanguage(window.monaco);
|
registerLiquidLanguage(window.monaco);
|
||||||
@@ -17,20 +60,17 @@ export const loadMonaco = () => {
|
|||||||
return monacoLoaderPromise;
|
return monacoLoaderPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
monacoLoaderPromise = loadScript("/monaco/vs/loader.js")
|
monacoLoaderPromise = import("/assets/monaco.js")
|
||||||
.then(
|
.then((monaco) => {
|
||||||
() =>
|
window.monaco = monaco;
|
||||||
new Promise((resolve, reject) => {
|
if (!monaco.editor) throw new Error("Monaco loaded but monaco.editor is missing");
|
||||||
window.require.config({ paths: { vs: "/monaco/vs" } });
|
ensureMonacoTheme(monaco);
|
||||||
window.require(["vs/editor/editor.main"], () => {
|
registerLiquidLanguage(monaco);
|
||||||
ensureMonacoTheme(window.monaco);
|
registerMarkdownWithMacrosLanguage(monaco);
|
||||||
registerLiquidLanguage(window.monaco);
|
return monaco;
|
||||||
registerMarkdownWithMacrosLanguage(window.monaco);
|
})
|
||||||
resolve(window.monaco);
|
|
||||||
}, reject);
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
console.error("Monaco load failed:", error);
|
||||||
monacoLoaderPromise = null;
|
monacoLoaderPromise = null;
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
|
|||||||
3
assets/js/monaco_entry.js
Normal file
3
assets/js/monaco_entry.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
// ESM entry point for Monaco. Bundled by esbuild into priv/static/assets/monaco.js
|
||||||
|
// editor.main.js loads the API AND registers all bundled languages (api.js only exposes the API)
|
||||||
|
export * from "monaco-editor/esm/vs/editor/editor.main.js";
|
||||||
1
assets/js/monaco_workers/css.worker.js
Normal file
1
assets/js/monaco_workers/css.worker.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import "monaco-editor/esm/vs/language/css/css.worker.js";
|
||||||
1
assets/js/monaco_workers/editor.worker.js
Normal file
1
assets/js/monaco_workers/editor.worker.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import "monaco-editor/esm/vs/editor/editor.worker.js";
|
||||||
1
assets/js/monaco_workers/html.worker.js
Normal file
1
assets/js/monaco_workers/html.worker.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import "monaco-editor/esm/vs/language/html/html.worker.js";
|
||||||
1
assets/js/monaco_workers/json.worker.js
Normal file
1
assets/js/monaco_workers/json.worker.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import "monaco-editor/esm/vs/language/json/json.worker.js";
|
||||||
1
assets/js/monaco_workers/ts.worker.js
Normal file
1
assets/js/monaco_workers/ts.worker.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import "monaco-editor/esm/vs/language/typescript/ts.worker.js";
|
||||||
27
assets/js/utils/clipboard.js
Normal file
27
assets/js/utils/clipboard.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Copies text to the system clipboard, mirroring the old app's behavior:
|
||||||
|
// navigator.clipboard when available, with a hidden-textarea fallback for
|
||||||
|
// webviews that lack the async clipboard API.
|
||||||
|
export const copyTextToClipboard = async (text) => {
|
||||||
|
if (typeof navigator !== "undefined" && navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof document === "undefined" || typeof document.createElement !== "function") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const textArea = document.createElement("textarea");
|
||||||
|
textArea.value = text;
|
||||||
|
textArea.setAttribute("readonly", "");
|
||||||
|
textArea.style.position = "absolute";
|
||||||
|
textArea.style.left = "-9999px";
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
|
||||||
|
if (typeof document.execCommand === "function") {
|
||||||
|
document.execCommand("copy");
|
||||||
|
}
|
||||||
|
|
||||||
|
document.body.removeChild(textArea);
|
||||||
|
};
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
export const loadScript = (src) =>
|
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
const existing = document.querySelector(`script[src="${src}"]`);
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
if (existing.dataset.loaded === "true") {
|
|
||||||
resolve();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
existing.addEventListener("load", () => resolve(), { once: true });
|
|
||||||
existing.addEventListener("error", () => reject(new Error(`Failed to load ${src}`)), {
|
|
||||||
once: true
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const script = document.createElement("script");
|
|
||||||
script.src = src;
|
|
||||||
script.async = true;
|
|
||||||
script.addEventListener(
|
|
||||||
"load",
|
|
||||||
() => {
|
|
||||||
script.dataset.loaded = "true";
|
|
||||||
resolve();
|
|
||||||
},
|
|
||||||
{ once: true }
|
|
||||||
);
|
|
||||||
script.addEventListener("error", () => reject(new Error(`Failed to load ${src}`)), {
|
|
||||||
once: true
|
|
||||||
});
|
|
||||||
document.head.appendChild(script);
|
|
||||||
});
|
|
||||||
@@ -25,8 +25,13 @@ config :bds, :desktop,
|
|||||||
window_min_size: {800, 600},
|
window_min_size: {800, 600},
|
||||||
title: "Blogging Desktop Server"
|
title: "Blogging Desktop Server"
|
||||||
|
|
||||||
|
config :bds, :server, ssh_port: 2222
|
||||||
|
|
||||||
config :bds, BDS.Desktop.Endpoint,
|
config :bds, BDS.Desktop.Endpoint,
|
||||||
url: [host: "127.0.0.1"],
|
url: [host: "127.0.0.1"],
|
||||||
|
# Any-port loopback origins: the remote-GUI SSH tunnel terminates on an
|
||||||
|
# ephemeral local port, so the websocket origin port varies per connect.
|
||||||
|
check_origin: ["//127.0.0.1", "//localhost"],
|
||||||
adapter: Bandit.PhoenixAdapter,
|
adapter: Bandit.PhoenixAdapter,
|
||||||
render_errors: [formats: [html: BDS.Desktop.ErrorHTML], layout: false],
|
render_errors: [formats: [html: BDS.Desktop.ErrorHTML], layout: false],
|
||||||
pubsub_server: BDS.PubSub,
|
pubsub_server: BDS.PubSub,
|
||||||
@@ -55,6 +60,34 @@ config :esbuild,
|
|||||||
--external:/images/*
|
--external:/images/*
|
||||||
),
|
),
|
||||||
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
|
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
|
||||||
|
],
|
||||||
|
monaco: [
|
||||||
|
cd: Path.expand("../assets", __DIR__),
|
||||||
|
args: ~w(
|
||||||
|
js/monaco_entry.js
|
||||||
|
--bundle
|
||||||
|
--target=es2022
|
||||||
|
--format=esm
|
||||||
|
--outfile=../priv/static/assets/monaco.js
|
||||||
|
--loader:.ttf=dataurl
|
||||||
|
),
|
||||||
|
env: %{"NODE_PATH" => Path.expand("../node_modules", __DIR__)}
|
||||||
|
],
|
||||||
|
monaco_workers: [
|
||||||
|
cd: Path.expand("../assets", __DIR__),
|
||||||
|
args: ~w(
|
||||||
|
js/monaco_workers/editor.worker.js
|
||||||
|
js/monaco_workers/css.worker.js
|
||||||
|
js/monaco_workers/html.worker.js
|
||||||
|
js/monaco_workers/json.worker.js
|
||||||
|
js/monaco_workers/ts.worker.js
|
||||||
|
--bundle
|
||||||
|
--target=es2022
|
||||||
|
--format=esm
|
||||||
|
--outdir=../priv/static/assets/monaco
|
||||||
|
--loader:.ttf=dataurl
|
||||||
|
),
|
||||||
|
env: %{"NODE_PATH" => Path.expand("../node_modules", __DIR__)}
|
||||||
]
|
]
|
||||||
|
|
||||||
config :bds, :scripting,
|
config :bds, :scripting,
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
import Config
|
import Config
|
||||||
|
|
||||||
|
# Headless boots (BDS_MODE=server/tui, or desktop mode without a graphical
|
||||||
|
# display — issue #33) must neuter wx before the :desktop dependency
|
||||||
|
# application starts it and crashes the VM. Local tui mode additionally
|
||||||
|
# silences all other terminal writers (console logging goes to a file,
|
||||||
|
# model-download progress bars are disabled) — the TUI owns the terminal
|
||||||
|
# and every stray line scrolls its rendering up one row. Runtime config is
|
||||||
|
# the only hook that runs before dependency applications start.
|
||||||
|
BDS.Server.prepare_boot_env()
|
||||||
|
|
||||||
if config_env() == :prod do
|
if config_env() == :prod do
|
||||||
database_path =
|
database_path =
|
||||||
System.get_env("BDS_DATABASE_PATH") ||
|
System.get_env("BDS_DATABASE_PATH") ||
|
||||||
@@ -7,9 +16,11 @@ if config_env() == :prod do
|
|||||||
|
|
||||||
File.mkdir_p!(Path.dirname(database_path))
|
File.mkdir_p!(Path.dirname(database_path))
|
||||||
|
|
||||||
|
# Keep prod on the same modest SQLite pool as dev so WAL + busy_timeout see
|
||||||
|
# the same concurrency behavior in both environments unless explicitly tuned.
|
||||||
config :bds, BDS.Repo,
|
config :bds, BDS.Repo,
|
||||||
database: database_path,
|
database: database_path,
|
||||||
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "1")
|
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5")
|
||||||
|
|
||||||
# Persist downloaded embedding model files alongside the database data dir.
|
# Persist downloaded embedding model files alongside the database data dir.
|
||||||
config :bumblebee,
|
config :bumblebee,
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ config :bds, BDS.Repo,
|
|||||||
pool: Ecto.Adapters.SQL.Sandbox,
|
pool: Ecto.Adapters.SQL.Sandbox,
|
||||||
pool_size: 5,
|
pool_size: 5,
|
||||||
journal_mode: :wal,
|
journal_mode: :wal,
|
||||||
busy_timeout: 15_000
|
# Desktop/LiveView tests can leave short-lived reader/writer processes
|
||||||
|
# draining after assertions. Give SQLite more time to wait out those locks
|
||||||
|
# during serialized suite runs instead of failing the next test setup.
|
||||||
|
busy_timeout: 60_000
|
||||||
|
|
||||||
config :logger, level: :warning
|
config :logger, level: :warning
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
defmodule BDS.AI do
|
defmodule BDS.AI do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Public interface for AI features — endpoint configuration, secret management,
|
Stable public interface for AI features.
|
||||||
model catalog access, and dispatching chat and one-shot inference requests.
|
|
||||||
|
This facade owns the app-level AI state that other modules are expected to
|
||||||
|
depend on directly: endpoint configuration, secret management, airplane-mode
|
||||||
|
gating, and per-feature model preferences, alongside model catalog access and
|
||||||
|
dispatching chat and one-shot inference requests.
|
||||||
|
|
||||||
|
`BDS.AI.Catalog`, `BDS.AI.OneShot`, and `BDS.AI.Chat` are internal
|
||||||
|
implementation modules behind this boundary. Callers should treat `BDS.AI` as
|
||||||
|
the durable surface for AI behavior and configuration.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias BDS.AI.Catalog
|
alias BDS.AI.Catalog
|
||||||
@@ -46,13 +54,21 @@ defmodule BDS.AI do
|
|||||||
model = MapUtils.attr(attrs, :model)
|
model = MapUtils.attr(attrs, :model)
|
||||||
api_key = MapUtils.attr(attrs, :api_key)
|
api_key = MapUtils.attr(attrs, :api_key)
|
||||||
|
|
||||||
with :ok <- put_setting("ai.#{kind_key}.url", url),
|
with :ok <- put_or_delete_setting("ai.#{kind_key}.url", url),
|
||||||
:ok <- put_setting("ai.#{kind_key}.model", model),
|
:ok <- put_or_delete_setting("ai.#{kind_key}.model", model),
|
||||||
:ok <- put_secret("ai.#{kind_key}.api_key", api_key, backend) do
|
:ok <- put_or_delete_secret("ai.#{kind_key}.api_key", api_key, backend) do
|
||||||
{:ok, %{kind: kind, url: url, api_key: api_key, model: model}}
|
{:ok, %{kind: kind, url: url, api_key: api_key, model: model}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A nil field means "clear it" — writing nil into put_setting/2 would raise a
|
||||||
|
# FunctionClauseError and crash the caller (the preferences panel).
|
||||||
|
defp put_or_delete_setting(key, nil), do: delete_setting(key)
|
||||||
|
defp put_or_delete_setting(key, value) when is_binary(value), do: put_setting(key, value)
|
||||||
|
|
||||||
|
defp put_or_delete_secret(key, nil, _backend), do: delete_setting(encrypted_key(key))
|
||||||
|
defp put_or_delete_secret(key, value, backend), do: put_secret(key, value, backend)
|
||||||
|
|
||||||
@spec get_endpoint(endpoint_kind(), keyword()) ::
|
@spec get_endpoint(endpoint_kind(), keyword()) ::
|
||||||
{:ok, endpoint() | nil} | {:error, term()}
|
{:ok, endpoint() | nil} | {:error, term()}
|
||||||
def get_endpoint(kind, opts \\ []) when is_atom(kind) and is_list(opts) do
|
def get_endpoint(kind, opts \\ []) when is_atom(kind) and is_list(opts) do
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
defmodule BDS.AI.Chat do
|
defmodule BDS.AI.Chat do
|
||||||
@moduledoc false
|
@moduledoc """
|
||||||
|
Context for AI chat conversations: start, list, fetch, and delete
|
||||||
|
conversations, persist surface state, and resolve the effective chat model.
|
||||||
|
|
||||||
|
Automatic AI activity is gated by the app's airplane (offline) mode; callers
|
||||||
|
must respect that gating before invoking model-backed operations.
|
||||||
|
"""
|
||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
require Logger
|
require Logger
|
||||||
@@ -9,6 +15,7 @@ defmodule BDS.AI.Chat do
|
|||||||
alias BDS.AI.CatalogProvider
|
alias BDS.AI.CatalogProvider
|
||||||
alias BDS.AI.ChatConversation
|
alias BDS.AI.ChatConversation
|
||||||
alias BDS.AI.ChatMessage
|
alias BDS.AI.ChatMessage
|
||||||
|
alias BDS.AI.ChatTitleGenerator
|
||||||
alias BDS.AI.ChatTools
|
alias BDS.AI.ChatTools
|
||||||
alias BDS.AI.InFlight
|
alias BDS.AI.InFlight
|
||||||
alias BDS.AI.OpenAICompatibleRuntime
|
alias BDS.AI.OpenAICompatibleRuntime
|
||||||
@@ -24,8 +31,6 @@ defmodule BDS.AI.Chat do
|
|||||||
|
|
||||||
@default_system_prompt "You are the bDS AI backend. Be precise, prefer structured JSON when asked, and avoid inventing blog facts."
|
@default_system_prompt "You are the bDS AI backend. Be precise, prefer structured JSON when asked, and avoid inventing blog facts."
|
||||||
@default_max_output_tokens 16_384
|
@default_max_output_tokens 16_384
|
||||||
@title_max_output_tokens 256
|
|
||||||
@chat_title_max_length 30
|
|
||||||
@chat_max_tool_rounds 10
|
@chat_max_tool_rounds 10
|
||||||
@chat_await_timeout_margin_ms 5_000
|
@chat_await_timeout_margin_ms 5_000
|
||||||
@default_context_window 128_000
|
@default_context_window 128_000
|
||||||
@@ -34,7 +39,7 @@ defmodule BDS.AI.Chat do
|
|||||||
def start_chat(attrs \\ %{}) when is_map(attrs) do
|
def start_chat(attrs \\ %{}) when is_map(attrs) do
|
||||||
now = Persistence.now_ms()
|
now = Persistence.now_ms()
|
||||||
model = MapUtils.attr(attrs, :model)
|
model = MapUtils.attr(attrs, :model)
|
||||||
title = MapUtils.attr(attrs, :title) || generated_chat_title(model)
|
title = MapUtils.attr(attrs, :title) || ChatTitleGenerator.generated_chat_title(model)
|
||||||
|
|
||||||
%ChatConversation{}
|
%ChatConversation{}
|
||||||
|> ChatConversation.changeset(%{
|
|> ChatConversation.changeset(%{
|
||||||
@@ -106,12 +111,24 @@ defmodule BDS.AI.Chat do
|
|||||||
{:error, :not_found}
|
{:error, :not_found}
|
||||||
|
|
||||||
%ChatConversation{} = conversation ->
|
%ChatConversation{} = conversation ->
|
||||||
Repo.delete_all(
|
Repo.transaction(fn ->
|
||||||
from message in ChatMessage, where: message.conversation_id == ^conversation_id
|
Repo.delete_all(
|
||||||
)
|
from message in ChatMessage, where: message.conversation_id == ^conversation_id
|
||||||
|
)
|
||||||
|
|
||||||
case Repo.delete(conversation) do
|
case delete_chat_conversation_test_hook(conversation_id) do
|
||||||
{:ok, _conversation} -> {:ok, :deleted}
|
:ok ->
|
||||||
|
case Repo.delete(conversation) do
|
||||||
|
{:ok, _conversation} -> :ok
|
||||||
|
{:error, reason} -> Repo.rollback(reason)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
Repo.rollback(reason)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> case do
|
||||||
|
{:ok, :ok} -> {:ok, :deleted}
|
||||||
{:error, reason} -> {:error, reason}
|
{:error, reason} -> {:error, reason}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -418,11 +435,11 @@ defmodule BDS.AI.Chat do
|
|||||||
conversation = Repo.get!(ChatConversation, conversation_id)
|
conversation = Repo.get!(ChatConversation, conversation_id)
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
chat_user_message_count(conversation_id) < 1 ->
|
ChatTitleGenerator.chat_user_message_count(conversation_id) < 1 ->
|
||||||
Logger.debug("Chat title generation skipped reason=:no_user_messages")
|
Logger.debug("Chat title generation skipped reason=:no_user_messages")
|
||||||
{:ok, reply}
|
{:ok, reply}
|
||||||
|
|
||||||
not generated_chat_title?(conversation.title, conversation.model) ->
|
not ChatTitleGenerator.generated_chat_title?(conversation.title, conversation.model) ->
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
"Chat title generation skipped reason=:conversation_already_titled title=#{inspect(conversation.title)}"
|
"Chat title generation skipped reason=:conversation_already_titled title=#{inspect(conversation.title)}"
|
||||||
)
|
)
|
||||||
@@ -432,7 +449,7 @@ defmodule BDS.AI.Chat do
|
|||||||
true ->
|
true ->
|
||||||
Logger.debug("Chat title generation requested conversation_id=#{conversation_id}")
|
Logger.debug("Chat title generation requested conversation_id=#{conversation_id}")
|
||||||
|
|
||||||
case generate_chat_title(user_content, opts) do
|
case ChatTitleGenerator.generate_chat_title(user_content, opts) do
|
||||||
{:ok, title} when is_binary(title) and title != "" ->
|
{:ok, title} when is_binary(title) and title != "" ->
|
||||||
now = Persistence.now_ms()
|
now = Persistence.now_ms()
|
||||||
|
|
||||||
@@ -453,87 +470,6 @@ defmodule BDS.AI.Chat do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp generate_chat_title(user_content, opts) when is_binary(user_content) do
|
|
||||||
runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime)
|
|
||||||
|
|
||||||
with {:ok, endpoint, model, mode} <- Runtime.resolve_target(:chat_title, opts),
|
|
||||||
:ok <- Runtime.validate_target(:chat_title, model, mode),
|
|
||||||
request <- build_chat_title_request(user_content, model),
|
|
||||||
{:ok, response} <-
|
|
||||||
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts) do
|
|
||||||
title = sanitize_chat_title(Map.get(response, :content))
|
|
||||||
|
|
||||||
if title == "" do
|
|
||||||
Logger.warning("Chat title generation returned an empty title",
|
|
||||||
model: model,
|
|
||||||
content: inspect(Map.get(response, :content)),
|
|
||||||
usage: inspect(Map.get(response, :usage))
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, title}
|
|
||||||
else
|
|
||||||
{:error, reason} = error ->
|
|
||||||
Logger.warning("Chat title generation failed", reason: inspect(reason))
|
|
||||||
error
|
|
||||||
|
|
||||||
other ->
|
|
||||||
Logger.warning("Chat title generation failed", reason: inspect(other))
|
|
||||||
other
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp build_chat_title_request(user_content, model) do
|
|
||||||
%{
|
|
||||||
operation: :chat_title,
|
|
||||||
model: model,
|
|
||||||
max_output_tokens: @title_max_output_tokens,
|
|
||||||
messages: [
|
|
||||||
%{
|
|
||||||
"role" => "system",
|
|
||||||
"content" =>
|
|
||||||
"Generate an ultra-short title (2-3 words, max 25 characters) for this conversation. Focus ONLY on the topic. Ignore any capability disclaimers. Do not include reasoning. Output ONLY the title text."
|
|
||||||
},
|
|
||||||
%{"role" => "user", "content" => "Topic: #{String.slice(user_content, 0, 100)}"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp sanitize_chat_title(title) when is_binary(title) do
|
|
||||||
title =
|
|
||||||
title
|
|
||||||
|> String.trim()
|
|
||||||
|> String.trim_leading("\"")
|
|
||||||
|> String.trim_leading("'")
|
|
||||||
|> String.trim_trailing("\"")
|
|
||||||
|> String.trim_trailing("'")
|
|
||||||
|> String.trim_trailing(".")
|
|
||||||
|> String.trim_trailing("!")
|
|
||||||
|> String.trim_trailing("?")
|
|
||||||
|
|
||||||
if String.length(title) > @chat_title_max_length do
|
|
||||||
String.slice(title, 0, @chat_title_max_length - 3) <> "..."
|
|
||||||
else
|
|
||||||
title
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp sanitize_chat_title(_title), do: ""
|
|
||||||
|
|
||||||
defp chat_user_message_count(conversation_id) do
|
|
||||||
Repo.aggregate(
|
|
||||||
from(message in ChatMessage,
|
|
||||||
where: message.conversation_id == ^conversation_id and message.role == :user
|
|
||||||
),
|
|
||||||
:count,
|
|
||||||
:id
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp generated_chat_title?(title, model) do
|
|
||||||
title in [generated_chat_title(nil), generated_chat_title(model)]
|
|
||||||
end
|
|
||||||
|
|
||||||
defp chat_round(
|
defp chat_round(
|
||||||
_conversation,
|
_conversation,
|
||||||
_messages,
|
_messages,
|
||||||
@@ -566,47 +502,29 @@ defmodule BDS.AI.Chat do
|
|||||||
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, generate_opts),
|
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, generate_opts),
|
||||||
{:ok, assistant_message} <- persist_assistant_response(conversation.id, response),
|
{:ok, assistant_message} <- persist_assistant_response(conversation.id, response),
|
||||||
:ok <- touch_conversation(conversation.id) do
|
:ok <- touch_conversation(conversation.id) do
|
||||||
if is_binary(Map.get(response, :content)) and String.trim(Map.get(response, :content)) != "" do
|
notify_assistant_content(response, conversation.id, opts)
|
||||||
notify_chat_event(
|
|
||||||
opts,
|
|
||||||
{:chat_streaming_content, conversation.id, Map.get(response, :content)}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
tool_calls = decode_tool_calls(Map.get(response, :tool_calls))
|
response
|
||||||
|
|> Map.get(:tool_calls)
|
||||||
|
|> decode_tool_calls()
|
||||||
|
|> continue_chat_round(
|
||||||
|
conversation.id,
|
||||||
|
assistant_message,
|
||||||
|
endpoint,
|
||||||
|
model,
|
||||||
|
project_id,
|
||||||
|
tools,
|
||||||
|
runtime,
|
||||||
|
opts,
|
||||||
|
rounds_left
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Enum.each(tool_calls, fn tool_call ->
|
defp delete_chat_conversation_test_hook(conversation_id) do
|
||||||
notify_chat_event(opts, {:chat_tool_call, conversation.id, tool_call})
|
case Application.get_env(:bds, :chat_delete_conversation_test_hook) do
|
||||||
end)
|
hook when is_function(hook, 1) -> hook.(conversation_id)
|
||||||
|
_other -> :ok
|
||||||
cond do
|
|
||||||
tool_calls != [] and tools != [] ->
|
|
||||||
with {:ok, tool_messages} <-
|
|
||||||
execute_tool_calls(conversation.id, tool_calls, project_id, opts),
|
|
||||||
updated_messages <- load_chat_messages(conversation.id),
|
|
||||||
{:ok, reply} <-
|
|
||||||
chat_round(
|
|
||||||
Repo.get!(ChatConversation, conversation.id),
|
|
||||||
updated_messages,
|
|
||||||
endpoint,
|
|
||||||
model,
|
|
||||||
project_id,
|
|
||||||
tools,
|
|
||||||
runtime,
|
|
||||||
opts,
|
|
||||||
rounds_left - 1
|
|
||||||
) do
|
|
||||||
{:ok, Map.put(reply, :tool_messages, tool_messages)}
|
|
||||||
end
|
|
||||||
|
|
||||||
true ->
|
|
||||||
{:ok,
|
|
||||||
%{
|
|
||||||
conversation: format_conversation(Repo.get!(ChatConversation, conversation.id)),
|
|
||||||
assistant_message: format_chat_message(assistant_message),
|
|
||||||
tool_messages: []
|
|
||||||
}}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -654,6 +572,63 @@ defmodule BDS.AI.Chat do
|
|||||||
{:ok, tool_messages}
|
{:ok, tool_messages}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp continue_chat_round(
|
||||||
|
tool_calls,
|
||||||
|
conversation_id,
|
||||||
|
assistant_message,
|
||||||
|
endpoint,
|
||||||
|
model,
|
||||||
|
project_id,
|
||||||
|
tools,
|
||||||
|
runtime,
|
||||||
|
opts,
|
||||||
|
rounds_left
|
||||||
|
) do
|
||||||
|
Enum.each(tool_calls, fn tool_call ->
|
||||||
|
notify_chat_event(opts, {:chat_tool_call, conversation_id, tool_call})
|
||||||
|
end)
|
||||||
|
|
||||||
|
if tool_calls != [] and tools != [] do
|
||||||
|
with {:ok, tool_messages} <- execute_tool_calls(conversation_id, tool_calls, project_id, opts),
|
||||||
|
updated_messages <- load_chat_messages(conversation_id),
|
||||||
|
{:ok, reply} <-
|
||||||
|
chat_round(
|
||||||
|
Repo.get!(ChatConversation, conversation_id),
|
||||||
|
updated_messages,
|
||||||
|
endpoint,
|
||||||
|
model,
|
||||||
|
project_id,
|
||||||
|
tools,
|
||||||
|
runtime,
|
||||||
|
opts,
|
||||||
|
rounds_left - 1
|
||||||
|
) do
|
||||||
|
{:ok, Map.put(reply, :tool_messages, tool_messages)}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
final_chat_reply(conversation_id, assistant_message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp final_chat_reply(conversation_id, assistant_message) do
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
conversation: format_conversation(Repo.get!(ChatConversation, conversation_id)),
|
||||||
|
assistant_message: format_chat_message(assistant_message),
|
||||||
|
tool_messages: []
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp notify_assistant_content(response, conversation_id, opts) do
|
||||||
|
content = Map.get(response, :content)
|
||||||
|
|
||||||
|
if is_binary(content) and String.trim(content) != "" do
|
||||||
|
notify_chat_event(opts, {:chat_streaming_content, conversation_id, content})
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
defp build_chat_request(conversation, messages, model, project_id, tools) do
|
defp build_chat_request(conversation, messages, model, project_id, tools) do
|
||||||
system_message = %{"role" => "system", "content" => chat_system_prompt(project_id, tools)}
|
system_message = %{"role" => "system", "content" => chat_system_prompt(project_id, tools)}
|
||||||
|
|
||||||
@@ -831,9 +806,6 @@ defmodule BDS.AI.Chat do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp generated_chat_title(nil), do: "New Chat"
|
|
||||||
defp generated_chat_title(model), do: "Chat with #{model}"
|
|
||||||
|
|
||||||
defp load_chat_messages(conversation_id) do
|
defp load_chat_messages(conversation_id) do
|
||||||
Repo.all(
|
Repo.all(
|
||||||
from message in ChatMessage,
|
from message in ChatMessage,
|
||||||
@@ -969,5 +941,5 @@ defmodule BDS.AI.Chat do
|
|||||||
defp encode_nullable(nil), do: nil
|
defp encode_nullable(nil), do: nil
|
||||||
defp encode_nullable(value), do: Jason.encode!(value)
|
defp encode_nullable(value), do: Jason.encode!(value)
|
||||||
|
|
||||||
defp blank?(value), do: value in [nil, ""]
|
defp blank?(value), do: BDS.Values.blank?(value)
|
||||||
end
|
end
|
||||||
|
|||||||
99
lib/bds/ai/chat_title_generator.ex
Normal file
99
lib/bds/ai/chat_title_generator.ex
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
defmodule BDS.AI.ChatTitleGenerator do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
alias BDS.AI.ChatMessage
|
||||||
|
alias BDS.AI.OpenAICompatibleRuntime
|
||||||
|
alias BDS.AI.Runtime
|
||||||
|
alias BDS.Repo
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
@title_max_output_tokens 256
|
||||||
|
@chat_title_max_length 30
|
||||||
|
|
||||||
|
def generate_chat_title(user_content, opts) when is_binary(user_content) do
|
||||||
|
runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime)
|
||||||
|
|
||||||
|
with {:ok, endpoint, model, mode} <- Runtime.resolve_target(:chat_title, opts),
|
||||||
|
:ok <- Runtime.validate_target(:chat_title, model, mode),
|
||||||
|
request <- build_chat_title_request(user_content, model),
|
||||||
|
{:ok, response} <-
|
||||||
|
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts) do
|
||||||
|
title = sanitize_chat_title(Map.get(response, :content))
|
||||||
|
|
||||||
|
if title == "" do
|
||||||
|
Logger.warning("Chat title generation returned an empty title",
|
||||||
|
model: model,
|
||||||
|
content: inspect(Map.get(response, :content)),
|
||||||
|
usage: inspect(Map.get(response, :usage))
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, title}
|
||||||
|
else
|
||||||
|
{:error, reason} = error ->
|
||||||
|
Logger.warning("Chat title generation failed", reason: inspect(reason))
|
||||||
|
error
|
||||||
|
|
||||||
|
other ->
|
||||||
|
Logger.warning("Chat title generation failed", reason: inspect(other))
|
||||||
|
other
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_chat_title_request(user_content, model) do
|
||||||
|
%{
|
||||||
|
operation: :chat_title,
|
||||||
|
model: model,
|
||||||
|
max_output_tokens: @title_max_output_tokens,
|
||||||
|
messages: [
|
||||||
|
%{
|
||||||
|
"role" => "system",
|
||||||
|
"content" =>
|
||||||
|
"Generate an ultra-short title (2-3 words, max 25 characters) for this conversation. Focus ONLY on the topic. Ignore any capability disclaimers. Do not include reasoning. Output ONLY the title text."
|
||||||
|
},
|
||||||
|
%{"role" => "user", "content" => "Topic: #{String.slice(user_content, 0, 100)}"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def sanitize_chat_title(title) when is_binary(title) do
|
||||||
|
title =
|
||||||
|
title
|
||||||
|
|> String.trim()
|
||||||
|
|> String.trim_leading("\"")
|
||||||
|
|> String.trim_leading("'")
|
||||||
|
|> String.trim_trailing("\"")
|
||||||
|
|> String.trim_trailing("'")
|
||||||
|
|> String.trim_trailing(".")
|
||||||
|
|> String.trim_trailing("!")
|
||||||
|
|> String.trim_trailing("?")
|
||||||
|
|
||||||
|
if String.length(title) > @chat_title_max_length do
|
||||||
|
String.slice(title, 0, @chat_title_max_length - 3) <> "..."
|
||||||
|
else
|
||||||
|
title
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sanitize_chat_title(_title), do: ""
|
||||||
|
|
||||||
|
def chat_user_message_count(conversation_id) do
|
||||||
|
Repo.aggregate(
|
||||||
|
from(message in ChatMessage,
|
||||||
|
where: message.conversation_id == ^conversation_id and message.role == :user
|
||||||
|
),
|
||||||
|
:count,
|
||||||
|
:id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def generated_chat_title?(title, model) do
|
||||||
|
title in [generated_chat_title(nil), generated_chat_title(model)]
|
||||||
|
end
|
||||||
|
|
||||||
|
def generated_chat_title(nil), do: "New Chat"
|
||||||
|
def generated_chat_title(model), do: "Chat with #{model}"
|
||||||
|
end
|
||||||
49
lib/bds/ai/chat_tool_query_helpers.ex
Normal file
49
lib/bds/ai/chat_tool_query_helpers.ex
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
defmodule BDS.AI.ChatToolQueryHelpers do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.Search
|
||||||
|
|
||||||
|
def normalize_limit(value) when is_integer(value) and value > 0 and value <= 50, do: value
|
||||||
|
def normalize_limit(_value), do: 10
|
||||||
|
|
||||||
|
def normalize_offset(value) when is_integer(value) and value >= 0, do: value
|
||||||
|
def normalize_offset(_value), do: 0
|
||||||
|
|
||||||
|
def search_filters(arguments) do
|
||||||
|
%{}
|
||||||
|
|> BDS.MapUtils.maybe_put(:category, BDS.MapUtils.blank_to_nil(arguments["category"]))
|
||||||
|
|> BDS.MapUtils.maybe_put(:tags, BDS.MapUtils.blank_to_nil(arguments["tags"]))
|
||||||
|
|> BDS.MapUtils.maybe_put(:language, BDS.MapUtils.blank_to_nil(arguments["language"]))
|
||||||
|
|> BDS.MapUtils.maybe_put(
|
||||||
|
:missing_translation_language,
|
||||||
|
BDS.MapUtils.blank_to_nil(arguments["missingTranslationLanguage"])
|
||||||
|
)
|
||||||
|
|> BDS.MapUtils.maybe_put(:year, arguments["year"])
|
||||||
|
|> BDS.MapUtils.maybe_put(:month, arguments["month"])
|
||||||
|
|> BDS.MapUtils.maybe_put(:status, BDS.BoundedAtoms.post_status(arguments["status"]))
|
||||||
|
|> Map.put(:offset, normalize_offset(arguments["offset"]))
|
||||||
|
|> Map.put(:limit, normalize_limit(arguments["limit"]))
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_all_counted_posts(project_id, arguments) do
|
||||||
|
filters = search_filters(arguments) |> Map.put(:offset, 0) |> Map.put(:limit, 1)
|
||||||
|
{:ok, %{total: total}} = Search.search_posts(project_id, "", filters)
|
||||||
|
|
||||||
|
filters = Map.put(filters, :limit, max(total, 1))
|
||||||
|
{:ok, result} = Search.search_posts(project_id, "", filters)
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_result(project_id, query, filters, mapper) do
|
||||||
|
{:ok, result} = Search.search_posts(project_id, query, filters)
|
||||||
|
|
||||||
|
%{
|
||||||
|
posts: Enum.map(result.posts, mapper),
|
||||||
|
total: result.total,
|
||||||
|
offset: result.offset,
|
||||||
|
limit: result.limit,
|
||||||
|
has_more: result.offset + result.limit < result.total
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
350
lib/bds/ai/chat_tool_schemas.ex
Normal file
350
lib/bds/ai/chat_tool_schemas.ex
Normal file
@@ -0,0 +1,350 @@
|
|||||||
|
defmodule BDS.AI.ChatToolSchemas do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
def tool_spec(name, description, parameters) do
|
||||||
|
%{
|
||||||
|
"type" => "function",
|
||||||
|
"function" => %{
|
||||||
|
"name" => name,
|
||||||
|
"description" => description,
|
||||||
|
"parameters" => parameters
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def limit_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"limit" => %{"type" => "integer", "minimum" => 1, "maximum" => 50}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def post_search_schema(require_query) do
|
||||||
|
schema = %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"query" => %{"type" => "string"},
|
||||||
|
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
|
||||||
|
"category" => %{"type" => "string"},
|
||||||
|
"tags" => %{"type" => "array", "items" => %{"type" => "string"}},
|
||||||
|
"language" => %{"type" => "string"},
|
||||||
|
"missingTranslationLanguage" => %{"type" => "string"},
|
||||||
|
"year" => %{"type" => "integer"},
|
||||||
|
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
|
||||||
|
"limit" => %{"type" => "integer", "minimum" => 1, "maximum" => 50},
|
||||||
|
"offset" => %{"type" => "integer", "minimum" => 0}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if require_query, do: Map.put(schema, "required", ["query"]), else: schema
|
||||||
|
end
|
||||||
|
|
||||||
|
def count_posts_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"groupBy" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "string",
|
||||||
|
"enum" => ["year", "month", "tag", "category", "status"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"year" => %{"type" => "integer"},
|
||||||
|
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
|
||||||
|
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
|
||||||
|
"category" => %{"type" => "string"},
|
||||||
|
"tags" => %{"type" => "array", "items" => %{"type" => "string"}}
|
||||||
|
},
|
||||||
|
"required" => ["groupBy"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def post_id_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{"postId" => %{"type" => "string"}},
|
||||||
|
"required" => ["postId"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def media_id_schema(extra_properties \\ %{}) do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => Map.merge(%{"mediaId" => %{"type" => "string"}}, extra_properties),
|
||||||
|
"required" => ["mediaId"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_post_metadata_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"postId" => %{"type" => "string"},
|
||||||
|
"title" => %{"type" => "string"},
|
||||||
|
"excerpt" => %{"type" => "string"},
|
||||||
|
"tags" => %{"type" => "array", "items" => %{"type" => "string"}},
|
||||||
|
"categories" => %{"type" => "array", "items" => %{"type" => "string"}}
|
||||||
|
},
|
||||||
|
"required" => ["postId"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_media_metadata_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"mediaId" => %{"type" => "string"},
|
||||||
|
"title" => %{"type" => "string"},
|
||||||
|
"alt" => %{"type" => "string"},
|
||||||
|
"caption" => %{"type" => "string"},
|
||||||
|
"tags" => %{"type" => "array", "items" => %{"type" => "string"}}
|
||||||
|
},
|
||||||
|
"required" => ["mediaId"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_table_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional table title"},
|
||||||
|
"columns" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "string"},
|
||||||
|
"description" => "Column header names"
|
||||||
|
},
|
||||||
|
"rows" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "array", "items" => %{"type" => "string"}},
|
||||||
|
"description" => "Table rows, each row is an array of cell values"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_chart_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"chartType" => %{
|
||||||
|
"type" => "string",
|
||||||
|
"enum" => ["bar", "stacked-bar", "line", "area", "pie", "donut", "heatmap"],
|
||||||
|
"description" =>
|
||||||
|
"The type of chart to render. Use stacked-bar for multi-segment bars. Use heatmap for grid/matrix visualizations."
|
||||||
|
},
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional chart title"},
|
||||||
|
"series" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Array of data points.",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string", "description" => "Data point label"},
|
||||||
|
"value" => %{"type" => "number", "description" => "Data point value"},
|
||||||
|
"segments" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" =>
|
||||||
|
"Segments within this data point. Required for stacked-bar and heatmap charts.",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string"},
|
||||||
|
"value" => %{"type" => "number"}
|
||||||
|
},
|
||||||
|
"required" => ["label", "value"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["label"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["chartType", "series"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_form_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional form title"},
|
||||||
|
"fields" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Form fields to display",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"key" => %{"type" => "string", "description" => "Field identifier"},
|
||||||
|
"label" => %{"type" => "string", "description" => "Field label shown to user"},
|
||||||
|
"inputType" => %{
|
||||||
|
"type" => "string",
|
||||||
|
"enum" => ["text", "textarea", "select", "checkbox", "date", "number"],
|
||||||
|
"description" => "Type of input control"
|
||||||
|
},
|
||||||
|
"placeholder" => %{"type" => "string", "description" => "Placeholder text"},
|
||||||
|
"defaultValue" => %{"type" => "string", "description" => "Default value"},
|
||||||
|
"options" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Options for select fields",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string"},
|
||||||
|
"value" => %{"type" => "string"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => %{"type" => "boolean", "description" => "Whether the field is required"}
|
||||||
|
},
|
||||||
|
"required" => ["key", "label", "inputType"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"submitLabel" => %{"type" => "string", "description" => "Label for the submit button"},
|
||||||
|
"submitAction" => %{"type" => "string", "description" => "Action to dispatch on submit"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_card_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Card title"},
|
||||||
|
"subtitle" => %{"type" => "string", "description" => "Optional subtitle"},
|
||||||
|
"body" => %{"type" => "string", "description" => "Card body text (supports markdown)"},
|
||||||
|
"actions" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Optional action buttons on the card",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string", "description" => "Button label"},
|
||||||
|
"action" => %{"type" => "string", "description" => "Action name to dispatch"},
|
||||||
|
"payload" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"description" => "Optional action payload"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["label", "action"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_metric_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string", "description" => "Metric label"},
|
||||||
|
"value" => %{"type" => "string", "description" => "Metric value (displayed prominently)"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_list_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional list title"},
|
||||||
|
"items" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "string"},
|
||||||
|
"description" => "List items"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_tabs_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional tabs title"},
|
||||||
|
"tabs" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Array of tabs",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"label" => %{"type" => "string", "description" => "Tab label"},
|
||||||
|
"content" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Content items within the tab",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"type" => %{
|
||||||
|
"type" => "string",
|
||||||
|
"enum" => ["text", "metric", "list", "chart", "table"],
|
||||||
|
"description" => "Content type"
|
||||||
|
},
|
||||||
|
"text" => %{"type" => "string", "description" => "Text content (for type text)"},
|
||||||
|
"label" => %{"type" => "string", "description" => "Label (for type metric)"},
|
||||||
|
"value" => %{"type" => "string", "description" => "Display value (for type metric)"},
|
||||||
|
"title" => %{"type" => "string", "description" => "Title (for type list, chart, or table)"},
|
||||||
|
"items" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "string"},
|
||||||
|
"description" => "Items (for type list)"
|
||||||
|
},
|
||||||
|
"chartType" => %{
|
||||||
|
"type" => "string",
|
||||||
|
"enum" => ["bar", "stacked-bar", "line", "area", "pie", "donut", "heatmap"],
|
||||||
|
"description" => "Chart type (for type chart)"
|
||||||
|
},
|
||||||
|
"series" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Data series (for type chart)"
|
||||||
|
},
|
||||||
|
"columns" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "string"},
|
||||||
|
"description" => "Column headers (for type table)"
|
||||||
|
},
|
||||||
|
"rows" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "array", "items" => %{"type" => "string"}},
|
||||||
|
"description" => "Table rows (for type table)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["type"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["label", "content"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_mindmap_schema do
|
||||||
|
%{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"title" => %{"type" => "string", "description" => "Optional mind map title"},
|
||||||
|
"nodes" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"description" => "Flat array of nodes. The first node is the root. Each node references children by ID.",
|
||||||
|
"items" => %{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => %{
|
||||||
|
"id" => %{"type" => "string", "description" => "Unique node identifier"},
|
||||||
|
"label" => %{"type" => "string", "description" => "Node label text"},
|
||||||
|
"children" => %{
|
||||||
|
"type" => "array",
|
||||||
|
"items" => %{"type" => "string"},
|
||||||
|
"description" => "IDs of child nodes"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["id", "label"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -4,6 +4,8 @@ defmodule BDS.AI.ChatTools do
|
|||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
alias BDS.AI.Chat
|
alias BDS.AI.Chat
|
||||||
|
alias BDS.AI.ChatToolQueryHelpers, as: QueryHelpers
|
||||||
|
alias BDS.AI.ChatToolSchemas, as: Schemas
|
||||||
alias BDS.Media, as: MediaContext
|
alias BDS.Media, as: MediaContext
|
||||||
alias BDS.Media.Media
|
alias BDS.Media.Media
|
||||||
alias BDS.MCP.Queries
|
alias BDS.MCP.Queries
|
||||||
@@ -12,7 +14,6 @@ defmodule BDS.AI.ChatTools do
|
|||||||
alias BDS.Posts.PostMedia
|
alias BDS.Posts.PostMedia
|
||||||
alias BDS.Projects.Project
|
alias BDS.Projects.Project
|
||||||
alias BDS.Repo
|
alias BDS.Repo
|
||||||
alias BDS.Search
|
|
||||||
|
|
||||||
@spec execute(String.t(), map(), String.t() | nil) :: map()
|
@spec execute(String.t(), map(), String.t() | nil) :: map()
|
||||||
def execute("blog_stats", _arguments, project_id) do
|
def execute("blog_stats", _arguments, project_id) do
|
||||||
@@ -58,17 +59,14 @@ defmodule BDS.AI.ChatTools do
|
|||||||
|
|
||||||
def execute("search_posts", arguments, project_id) do
|
def execute("search_posts", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
filters = search_filters(arguments)
|
filters = QueryHelpers.search_filters(arguments)
|
||||||
|
|
||||||
{:ok, result} = Search.search_posts(project_id, arguments["query"] || "", filters)
|
QueryHelpers.search_result(
|
||||||
|
project_id,
|
||||||
%{
|
arguments["query"] || "",
|
||||||
posts: Enum.map(result.posts, &Queries.post_summary/1),
|
filters,
|
||||||
total: result.total,
|
&Queries.post_summary/1
|
||||||
offset: result.offset,
|
)
|
||||||
limit: result.limit,
|
|
||||||
has_more: result.offset + result.limit < result.total
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("read_post_by_slug", arguments, project_id) do
|
def execute("read_post_by_slug", arguments, project_id) do
|
||||||
@@ -83,41 +81,28 @@ defmodule BDS.AI.ChatTools do
|
|||||||
def execute("read_post", arguments, project_id) do
|
def execute("read_post", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
|
|
||||||
case Repo.get_by(Post,
|
with_post(arguments, project_id, %{success: false, error: "not_found"}, fn post ->
|
||||||
id: arguments["postId"] || arguments["post_id"],
|
%{post: Queries.post_detail(post)}
|
||||||
project_id: project_id
|
end)
|
||||||
) do
|
|
||||||
%Post{} = post -> %{post: Queries.post_detail(post)}
|
|
||||||
nil -> %{success: false, error: "not_found"}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("list_posts", arguments, project_id) do
|
def execute("list_posts", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
limit = normalize_limit(arguments["limit"])
|
limit = QueryHelpers.normalize_limit(arguments["limit"])
|
||||||
offset = normalize_offset(arguments["offset"])
|
offset = QueryHelpers.normalize_offset(arguments["offset"])
|
||||||
filters = search_filters(arguments) |> Map.merge(%{limit: limit, offset: offset})
|
filters = QueryHelpers.search_filters(arguments) |> Map.merge(%{limit: limit, offset: offset})
|
||||||
|
|
||||||
{:ok, result} = Search.search_posts(project_id, "", filters)
|
QueryHelpers.search_result(project_id, "", filters, fn post ->
|
||||||
|
post
|
||||||
%{
|
|> Queries.post_summary()
|
||||||
posts:
|
|> Map.put("url", "/posts/#{post.slug}")
|
||||||
Enum.map(result.posts, fn post ->
|
|> Map.put("updated_at", post.updated_at)
|
||||||
post
|
end)
|
||||||
|> Queries.post_summary()
|
|
||||||
|> Map.put("url", "/posts/#{post.slug}")
|
|
||||||
|> Map.put("updated_at", post.updated_at)
|
|
||||||
end),
|
|
||||||
total: result.total,
|
|
||||||
offset: result.offset,
|
|
||||||
limit: result.limit,
|
|
||||||
has_more: result.offset + result.limit < result.total
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("list_media", arguments, project_id) do
|
def execute("list_media", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
limit = normalize_limit(arguments["limit"])
|
limit = QueryHelpers.normalize_limit(arguments["limit"])
|
||||||
|
|
||||||
Repo.all(
|
Repo.all(
|
||||||
from(media in Media,
|
from(media in Media,
|
||||||
@@ -138,13 +123,9 @@ defmodule BDS.AI.ChatTools do
|
|||||||
def execute("get_media", arguments, project_id) do
|
def execute("get_media", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
|
|
||||||
case Repo.get_by(Media,
|
with_media(arguments, project_id, %{success: false, error: "not_found"}, fn media ->
|
||||||
id: arguments["mediaId"] || arguments["media_id"],
|
%{media: media_summary(media)}
|
||||||
project_id: project_id
|
end)
|
||||||
) do
|
|
||||||
%Media{} = media -> %{media: media_summary(media)}
|
|
||||||
nil -> %{success: false, error: "not_found"}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("view_image", arguments, project_id) do
|
def execute("view_image", arguments, project_id) do
|
||||||
@@ -220,7 +201,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
def execute("count_posts", arguments, project_id) do
|
def execute("count_posts", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
group_by = List.wrap(arguments["groupBy"] || arguments["group_by"]) |> Enum.map(&to_string/1)
|
group_by = List.wrap(arguments["groupBy"] || arguments["group_by"]) |> Enum.map(&to_string/1)
|
||||||
result = search_all_counted_posts(project_id, arguments)
|
result = QueryHelpers.search_all_counted_posts(project_id, arguments)
|
||||||
|
|
||||||
groups =
|
groups =
|
||||||
result.posts
|
result.posts
|
||||||
@@ -235,51 +216,33 @@ defmodule BDS.AI.ChatTools do
|
|||||||
def execute("get_post_backlinks", arguments, project_id) do
|
def execute("get_post_backlinks", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
|
|
||||||
case Repo.get_by(Post,
|
with_post(arguments, project_id, %{success: false, error: "not_found"}, fn post ->
|
||||||
id: arguments["postId"] || arguments["post_id"],
|
%{success: true, post_id: post.id, linked_by: Queries.linked_posts(post.id, :incoming)}
|
||||||
project_id: project_id
|
end)
|
||||||
) do
|
|
||||||
%Post{} = post ->
|
|
||||||
%{success: true, post_id: post.id, linked_by: Queries.linked_posts(post.id, :incoming)}
|
|
||||||
|
|
||||||
nil ->
|
|
||||||
%{success: false, error: "not_found"}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("get_post_outlinks", arguments, project_id) do
|
def execute("get_post_outlinks", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
|
|
||||||
case Repo.get_by(Post,
|
with_post(arguments, project_id, %{success: false, error: "not_found"}, fn post ->
|
||||||
id: arguments["postId"] || arguments["post_id"],
|
%{success: true, post_id: post.id, links_to: Queries.linked_posts(post.id, :outgoing)}
|
||||||
project_id: project_id
|
end)
|
||||||
) do
|
|
||||||
%Post{} = post ->
|
|
||||||
%{success: true, post_id: post.id, links_to: Queries.linked_posts(post.id, :outgoing)}
|
|
||||||
|
|
||||||
nil ->
|
|
||||||
%{success: false, error: "not_found"}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("get_post_media", arguments, project_id) do
|
def execute("get_post_media", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
post_id = arguments["postId"] || arguments["post_id"]
|
|
||||||
|
|
||||||
case Repo.get_by(Post, id: post_id, project_id: project_id) do
|
with_post(arguments, project_id, %{success: false, error: "not_found"}, fn post ->
|
||||||
%Post{} = post -> %{success: true, post_id: post.id, media: post_media(project_id, post.id)}
|
%{success: true, post_id: post.id, media: post_media(project_id, post.id)}
|
||||||
nil -> %{success: false, error: "not_found"}
|
end)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("get_media_posts", arguments, project_id) do
|
def execute("get_media_posts", arguments, project_id) do
|
||||||
project_id = project_id || active_project_id()
|
project_id = project_id || active_project_id()
|
||||||
media_id = arguments["mediaId"] || arguments["media_id"]
|
|
||||||
|
|
||||||
case Repo.get_by(Media, id: media_id, project_id: project_id) do
|
with_media(arguments, project_id, %{success: false, error: "not_found"}, fn media ->
|
||||||
%Media{} = media -> %{success: true, media_id: media.id, posts: media_posts(media.id)}
|
%{success: true, media_id: media.id, posts: media_posts(media.id)}
|
||||||
nil -> %{success: false, error: "not_found"}
|
end)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute("render_table", arguments, _project_id) do
|
def execute("render_table", arguments, _project_id) do
|
||||||
@@ -365,7 +328,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "blog_stats",
|
name: "blog_stats",
|
||||||
spec:
|
spec:
|
||||||
tool_spec("blog_stats", "Return aggregate blog statistics", %{
|
Schemas.tool_spec("blog_stats", "Return aggregate blog statistics", %{
|
||||||
"type" => "object",
|
"type" => "object",
|
||||||
"properties" => %{}
|
"properties" => %{}
|
||||||
})
|
})
|
||||||
@@ -373,7 +336,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "get_blog_stats",
|
name: "get_blog_stats",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_blog_stats",
|
"get_blog_stats",
|
||||||
"Get comprehensive blog statistics: total posts, media count, unique tag count, and unique category count. Use this first when you need to understand the scope of the data.",
|
"Get comprehensive blog statistics: total posts, media count, unique tag count, and unique category count. Use this first when you need to understand the scope of the data.",
|
||||||
%{"type" => "object", "properties" => %{}}
|
%{"type" => "object", "properties" => %{}}
|
||||||
@@ -382,7 +345,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "check_term",
|
name: "check_term",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"check_term",
|
"check_term",
|
||||||
"Check whether a term exists as a category, tag, or both. Returns post counts for each. Use before search_posts or list_posts when unsure whether a term is a category or tag.",
|
"Check whether a term exists as a category, tag, or both. Returns post counts for each. Use before search_posts or list_posts when unsure whether a term is a category or tag.",
|
||||||
%{
|
%{
|
||||||
@@ -395,16 +358,16 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "search_posts",
|
name: "search_posts",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"search_posts",
|
"search_posts",
|
||||||
"Search blog posts using full-text search. Can filter by category, tags, language, missing translation language, year, month, or status. Returns paginated concrete post data with titles, slugs, tags, categories, backlinks, and links_to.",
|
"Search blog posts using full-text search. Can filter by category, tags, language, missing translation language, year, month, or status. Returns paginated concrete post data with titles, slugs, tags, categories, backlinks, and links_to.",
|
||||||
post_search_schema(true)
|
Schemas.post_search_schema(true)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "read_post",
|
name: "read_post",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"read_post",
|
"read_post",
|
||||||
"Read full content and metadata of a specific blog post by ID. Includes backlinks, links_to, tags, categories, excerpt, status, language, and available languages.",
|
"Read full content and metadata of a specific blog post by ID. Includes backlinks, links_to, tags, categories, excerpt, status, language, and available languages.",
|
||||||
%{
|
%{
|
||||||
@@ -417,7 +380,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "read_post_by_slug",
|
name: "read_post_by_slug",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"read_post_by_slug",
|
"read_post_by_slug",
|
||||||
"Read full content and metadata of a specific blog post by slug. Includes backlinks, links_to, tags, categories, excerpt, status, language, and available languages.",
|
"Read full content and metadata of a specific blog post by slug. Includes backlinks, links_to, tags, categories, excerpt, status, language, and available languages.",
|
||||||
%{
|
%{
|
||||||
@@ -430,37 +393,37 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "list_posts",
|
name: "list_posts",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"list_posts",
|
"list_posts",
|
||||||
"List blog posts with optional filtering by status, category, tags, language, year, or month. Returns paginated concrete post data with titles, slugs, URLs, statuses, tags, categories, backlinks, and links_to. Use for recent, latest, top, or title-list requests.",
|
"List blog posts with optional filtering by status, category, tags, language, year, or month. Returns paginated concrete post data with titles, slugs, URLs, statuses, tags, categories, backlinks, and links_to. Use for recent, latest, top, or title-list requests.",
|
||||||
post_search_schema(false)
|
Schemas.post_search_schema(false)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "get_media",
|
name: "get_media",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_media",
|
"get_media",
|
||||||
"Get information about a specific media file by ID, including title, alt text, caption, tags, filename, MIME type, dimensions, and update time.",
|
"Get information about a specific media file by ID, including title, alt text, caption, tags, filename, MIME type, dimensions, and update time.",
|
||||||
media_id_schema()
|
Schemas.media_id_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "list_media",
|
name: "list_media",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"list_media",
|
"list_media",
|
||||||
"List concrete media data in the active project, including titles, filenames, MIME types, and update times.",
|
"List concrete media data in the active project, including titles, filenames, MIME types, and update times.",
|
||||||
limit_schema()
|
Schemas.limit_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "view_image",
|
name: "view_image",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"view_image",
|
"view_image",
|
||||||
"View an image thumbnail as a local data URL for visual inspection. Only works with image media files.",
|
"View an image thumbnail as a local data URL for visual inspection. Only works with image media files.",
|
||||||
media_id_schema(%{
|
Schemas.media_id_schema(%{
|
||||||
"size" => %{"type" => "string", "enum" => ["small", "medium", "large"]}
|
"size" => %{"type" => "string", "enum" => ["small", "medium", "large"]}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -468,25 +431,25 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "update_post_metadata",
|
name: "update_post_metadata",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"update_post_metadata",
|
"update_post_metadata",
|
||||||
"Update metadata for a blog post: title, excerpt, tags, or categories. Does not update post body content.",
|
"Update metadata for a blog post: title, excerpt, tags, or categories. Does not update post body content.",
|
||||||
update_post_metadata_schema()
|
Schemas.update_post_metadata_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "update_media_metadata",
|
name: "update_media_metadata",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"update_media_metadata",
|
"update_media_metadata",
|
||||||
"Update metadata for a media file: title, alt text, caption, or tags.",
|
"Update metadata for a media file: title, alt text, caption, or tags.",
|
||||||
update_media_metadata_schema()
|
Schemas.update_media_metadata_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "list_tags",
|
name: "list_tags",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"list_tags",
|
"list_tags",
|
||||||
"List all tags used across blog posts with post counts.",
|
"List all tags used across blog posts with post counts.",
|
||||||
%{
|
%{
|
||||||
@@ -498,7 +461,7 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "list_categories",
|
name: "list_categories",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"list_categories",
|
"list_categories",
|
||||||
"List all categories used across blog posts with post counts.",
|
"List all categories used across blog posts with post counts.",
|
||||||
%{"type" => "object", "properties" => %{}}
|
%{"type" => "object", "properties" => %{}}
|
||||||
@@ -507,46 +470,46 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "count_posts",
|
name: "count_posts",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"count_posts",
|
"count_posts",
|
||||||
"Count posts grouped by dimensions such as year, month, tag, category, or status. Use for analytics, distributions, and heat maps without transferring full post content.",
|
"Count posts grouped by dimensions such as year, month, tag, category, or status. Use for analytics, distributions, and heat maps without transferring full post content.",
|
||||||
count_posts_schema()
|
Schemas.count_posts_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "get_post_backlinks",
|
name: "get_post_backlinks",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_post_backlinks",
|
"get_post_backlinks",
|
||||||
"Get all posts that link to a specific post.",
|
"Get all posts that link to a specific post.",
|
||||||
post_id_schema()
|
Schemas.post_id_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "get_post_outlinks",
|
name: "get_post_outlinks",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_post_outlinks",
|
"get_post_outlinks",
|
||||||
"Get all posts that a specific post links to.",
|
"Get all posts that a specific post links to.",
|
||||||
post_id_schema()
|
Schemas.post_id_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "get_post_media",
|
name: "get_post_media",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_post_media",
|
"get_post_media",
|
||||||
"Get media files linked to a specific post.",
|
"Get media files linked to a specific post.",
|
||||||
post_id_schema()
|
Schemas.post_id_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "get_media_posts",
|
name: "get_media_posts",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"get_media_posts",
|
"get_media_posts",
|
||||||
"Get posts that use a specific media file.",
|
"Get posts that use a specific media file.",
|
||||||
media_id_schema()
|
Schemas.media_id_schema()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -559,73 +522,73 @@ defmodule BDS.AI.ChatTools do
|
|||||||
%{
|
%{
|
||||||
name: "render_card",
|
name: "render_card",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_card",
|
"render_card",
|
||||||
"Render an information card in the chat UI. Use this for displaying a summary, highlight, or actionable item.",
|
"Render an information card in the chat UI. Use this for displaying a summary, highlight, or actionable item.",
|
||||||
render_card_schema()
|
Schemas.render_card_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_table",
|
name: "render_table",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_table",
|
"render_table",
|
||||||
"Render a data table in the chat UI. Use this when the user asks for tabular data, comparisons, or structured information.",
|
"Render a data table in the chat UI. Use this when the user asks for tabular data, comparisons, or structured information.",
|
||||||
render_table_schema()
|
Schemas.render_table_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_chart",
|
name: "render_chart",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_chart",
|
"render_chart",
|
||||||
"Render an interactive chart in the chat UI. Use this when the user asks for a chart, graph, or data visualization. Supports bar, stacked-bar, line, area, pie, donut, and heatmap charts. Use stacked-bar for multi-segment bars and heatmap for grid/matrix visualizations.",
|
"Render an interactive chart in the chat UI. Use this when the user asks for a chart, graph, or data visualization. Supports bar, stacked-bar, line, area, pie, donut, and heatmap charts. Use stacked-bar for multi-segment bars and heatmap for grid/matrix visualizations.",
|
||||||
render_chart_schema()
|
Schemas.render_chart_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_form",
|
name: "render_form",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_form",
|
"render_form",
|
||||||
"Render an interactive form in the chat UI. Use this when you need to collect structured input from the user.",
|
"Render an interactive form in the chat UI. Use this when you need to collect structured input from the user.",
|
||||||
render_form_schema()
|
Schemas.render_form_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_metric",
|
name: "render_metric",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_metric",
|
"render_metric",
|
||||||
"Render a single metric/KPI display in the chat UI. Use this for showing a single important value with a label.",
|
"Render a single metric/KPI display in the chat UI. Use this for showing a single important value with a label.",
|
||||||
render_metric_schema()
|
Schemas.render_metric_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_list",
|
name: "render_list",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_list",
|
"render_list",
|
||||||
"Render a list of items in the chat UI. Use this for displaying bullet-point style lists, checklists, or simple enumerations.",
|
"Render a list of items in the chat UI. Use this for displaying bullet-point style lists, checklists, or simple enumerations.",
|
||||||
render_list_schema()
|
Schemas.render_list_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_tabs",
|
name: "render_tabs",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_tabs",
|
"render_tabs",
|
||||||
"Render a tabbed interface in the chat UI. Use this to organize information into multiple tabs that the user can switch between.",
|
"Render a tabbed interface in the chat UI. Use this to organize information into multiple tabs that the user can switch between.",
|
||||||
render_tabs_schema()
|
Schemas.render_tabs_schema()
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
name: "render_mindmap",
|
name: "render_mindmap",
|
||||||
spec:
|
spec:
|
||||||
tool_spec(
|
Schemas.tool_spec(
|
||||||
"render_mindmap",
|
"render_mindmap",
|
||||||
"Render a mind map diagram in the chat UI. Use this when the user asks for a mind map, concept map, topic tree, brainstorming diagram, or hierarchical overview of ideas.",
|
"Render a mind map diagram in the chat UI. Use this when the user asks for a mind map, concept map, topic tree, brainstorming diagram, or hierarchical overview of ideas.",
|
||||||
render_mindmap_schema()
|
Schemas.render_mindmap_schema()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -634,383 +597,26 @@ defmodule BDS.AI.ChatTools do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tool_spec(name, description, parameters) do
|
defp with_post(arguments, project_id, not_found_result, success_fun) do
|
||||||
%{
|
case Repo.get_by(Post,
|
||||||
"type" => "function",
|
id: arguments["postId"] || arguments["post_id"],
|
||||||
"function" => %{
|
project_id: project_id
|
||||||
"name" => name,
|
) do
|
||||||
"description" => description,
|
%Post{} = post -> success_fun.(post)
|
||||||
"parameters" => parameters
|
nil -> not_found_result
|
||||||
}
|
end
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp limit_schema do
|
defp with_media(arguments, project_id, not_found_result, success_fun) do
|
||||||
%{
|
case Repo.get_by(Media,
|
||||||
"type" => "object",
|
id: arguments["mediaId"] || arguments["media_id"],
|
||||||
"properties" => %{
|
project_id: project_id
|
||||||
"limit" => %{"type" => "integer", "minimum" => 1, "maximum" => 50}
|
) do
|
||||||
}
|
%Media{} = media -> success_fun.(media)
|
||||||
}
|
nil -> not_found_result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp post_search_schema(require_query) do
|
|
||||||
schema = %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"query" => %{"type" => "string"},
|
|
||||||
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
|
|
||||||
"category" => %{"type" => "string"},
|
|
||||||
"tags" => %{"type" => "array", "items" => %{"type" => "string"}},
|
|
||||||
"language" => %{"type" => "string"},
|
|
||||||
"missingTranslationLanguage" => %{"type" => "string"},
|
|
||||||
"year" => %{"type" => "integer"},
|
|
||||||
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
|
|
||||||
"limit" => %{"type" => "integer", "minimum" => 1, "maximum" => 50},
|
|
||||||
"offset" => %{"type" => "integer", "minimum" => 0}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if require_query, do: Map.put(schema, "required", ["query"]), else: schema
|
|
||||||
end
|
|
||||||
|
|
||||||
defp count_posts_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"groupBy" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "string",
|
|
||||||
"enum" => ["year", "month", "tag", "category", "status"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"year" => %{"type" => "integer"},
|
|
||||||
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
|
|
||||||
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
|
|
||||||
"category" => %{"type" => "string"},
|
|
||||||
"tags" => %{"type" => "array", "items" => %{"type" => "string"}}
|
|
||||||
},
|
|
||||||
"required" => ["groupBy"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp post_id_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{"postId" => %{"type" => "string"}},
|
|
||||||
"required" => ["postId"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp media_id_schema(extra_properties \\ %{}) do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => Map.merge(%{"mediaId" => %{"type" => "string"}}, extra_properties),
|
|
||||||
"required" => ["mediaId"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp update_post_metadata_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"postId" => %{"type" => "string"},
|
|
||||||
"title" => %{"type" => "string"},
|
|
||||||
"excerpt" => %{"type" => "string"},
|
|
||||||
"tags" => %{"type" => "array", "items" => %{"type" => "string"}},
|
|
||||||
"categories" => %{"type" => "array", "items" => %{"type" => "string"}}
|
|
||||||
},
|
|
||||||
"required" => ["postId"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp update_media_metadata_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"mediaId" => %{"type" => "string"},
|
|
||||||
"title" => %{"type" => "string"},
|
|
||||||
"alt" => %{"type" => "string"},
|
|
||||||
"caption" => %{"type" => "string"},
|
|
||||||
"tags" => %{"type" => "array", "items" => %{"type" => "string"}}
|
|
||||||
},
|
|
||||||
"required" => ["mediaId"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_table_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional table title"},
|
|
||||||
"columns" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"items" => %{"type" => "string"},
|
|
||||||
"description" => "Column header names"
|
|
||||||
},
|
|
||||||
"rows" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"items" => %{"type" => "array", "items" => %{"type" => "string"}},
|
|
||||||
"description" => "Table rows, each row is an array of cell values"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_chart_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"chartType" => %{
|
|
||||||
"type" => "string",
|
|
||||||
"enum" => ["bar", "stacked-bar", "line", "area", "pie", "donut", "heatmap"],
|
|
||||||
"description" =>
|
|
||||||
"The type of chart to render. Use stacked-bar for multi-segment bars. Use heatmap for grid/matrix visualizations."
|
|
||||||
},
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional chart title"},
|
|
||||||
"series" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Array of data points.",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string", "description" => "Data point label"},
|
|
||||||
"value" => %{"type" => "number", "description" => "Data point value"},
|
|
||||||
"segments" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" =>
|
|
||||||
"Segments within this data point. Required for stacked-bar and heatmap charts.",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string"},
|
|
||||||
"value" => %{"type" => "number"}
|
|
||||||
},
|
|
||||||
"required" => ["label", "value"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["label"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["chartType", "series"]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_form_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional form title"},
|
|
||||||
"fields" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Form fields to display",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"key" => %{"type" => "string", "description" => "Field identifier"},
|
|
||||||
"label" => %{"type" => "string", "description" => "Field label shown to user"},
|
|
||||||
"inputType" => %{
|
|
||||||
"type" => "string",
|
|
||||||
"enum" => ["text", "textarea", "select", "checkbox", "date", "number"],
|
|
||||||
"description" => "Type of input control"
|
|
||||||
},
|
|
||||||
"placeholder" => %{"type" => "string", "description" => "Placeholder text"},
|
|
||||||
"defaultValue" => %{"type" => "string", "description" => "Default value"},
|
|
||||||
"options" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Options for select fields",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string"},
|
|
||||||
"value" => %{"type" => "string"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => %{"type" => "boolean", "description" => "Whether the field is required"}
|
|
||||||
},
|
|
||||||
"required" => ["key", "label", "inputType"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"submitLabel" => %{"type" => "string", "description" => "Label for the submit button"},
|
|
||||||
"submitAction" => %{"type" => "string", "description" => "Action to dispatch on submit"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_card_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Card title"},
|
|
||||||
"subtitle" => %{"type" => "string", "description" => "Optional subtitle"},
|
|
||||||
"body" => %{"type" => "string", "description" => "Card body text (supports markdown)"},
|
|
||||||
"actions" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Optional action buttons on the card",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string", "description" => "Button label"},
|
|
||||||
"action" => %{"type" => "string", "description" => "Action name to dispatch"},
|
|
||||||
"payload" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"description" => "Optional action payload"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["label", "action"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_metric_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string", "description" => "Metric label"},
|
|
||||||
"value" => %{"type" => "string", "description" => "Metric value (displayed prominently)"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_list_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional list title"},
|
|
||||||
"items" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"items" => %{"type" => "string"},
|
|
||||||
"description" => "List items"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_tabs_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional tabs title"},
|
|
||||||
"tabs" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Array of tabs",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"label" => %{"type" => "string", "description" => "Tab label"},
|
|
||||||
"content" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Content items within the tab",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"type" => %{
|
|
||||||
"type" => "string",
|
|
||||||
"enum" => ["text", "metric", "list", "chart", "table"],
|
|
||||||
"description" => "Content type"
|
|
||||||
},
|
|
||||||
"text" => %{"type" => "string", "description" => "Text content (for type text)"},
|
|
||||||
"label" => %{"type" => "string", "description" => "Label (for type metric)"},
|
|
||||||
"value" => %{"type" => "string", "description" => "Display value (for type metric)"},
|
|
||||||
"title" => %{"type" => "string", "description" => "Title (for type list, chart, or table)"},
|
|
||||||
"items" => %{
|
|
||||||
"type" => "array", "items" => %{"type" => "string"},
|
|
||||||
"description" => "Items (for type list)"
|
|
||||||
},
|
|
||||||
"chartType" => %{
|
|
||||||
"type" => "string",
|
|
||||||
"enum" => ["bar", "stacked-bar", "line", "area", "pie", "donut", "heatmap"],
|
|
||||||
"description" => "Chart type (for type chart)"
|
|
||||||
},
|
|
||||||
"series" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Data series (for type chart)"
|
|
||||||
},
|
|
||||||
"columns" => %{
|
|
||||||
"type" => "array", "items" => %{"type" => "string"},
|
|
||||||
"description" => "Column headers (for type table)"
|
|
||||||
},
|
|
||||||
"rows" => %{
|
|
||||||
"type" => "array", "items" => %{"type" => "array", "items" => %{"type" => "string"}},
|
|
||||||
"description" => "Table rows (for type table)"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["type"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["label", "content"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_mindmap_schema do
|
|
||||||
%{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"title" => %{"type" => "string", "description" => "Optional mind map title"},
|
|
||||||
"nodes" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"description" => "Flat array of nodes. The first node is the root. Each node references children by ID.",
|
|
||||||
"items" => %{
|
|
||||||
"type" => "object",
|
|
||||||
"properties" => %{
|
|
||||||
"id" => %{"type" => "string", "description" => "Unique node identifier"},
|
|
||||||
"label" => %{"type" => "string", "description" => "Node label text"},
|
|
||||||
"children" => %{
|
|
||||||
"type" => "array",
|
|
||||||
"items" => %{"type" => "string"},
|
|
||||||
"description" => "IDs of child nodes"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required" => ["id", "label"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp normalize_limit(value) when is_integer(value) and value > 0 and value <= 50, do: value
|
|
||||||
defp normalize_limit(_value), do: 10
|
|
||||||
|
|
||||||
defp normalize_offset(value) when is_integer(value) and value >= 0, do: value
|
|
||||||
defp normalize_offset(_value), do: 0
|
|
||||||
|
|
||||||
defp search_filters(arguments) do
|
|
||||||
%{}
|
|
||||||
|> maybe_put(:category, arguments["category"])
|
|
||||||
|> maybe_put(:tags, arguments["tags"])
|
|
||||||
|> maybe_put(:language, arguments["language"])
|
|
||||||
|> maybe_put(:missing_translation_language, arguments["missingTranslationLanguage"])
|
|
||||||
|> maybe_put(:year, arguments["year"])
|
|
||||||
|> maybe_put(:month, arguments["month"])
|
|
||||||
|> maybe_put(:status, BDS.BoundedAtoms.post_status(arguments["status"]))
|
|
||||||
|> Map.put(:offset, normalize_offset(arguments["offset"]))
|
|
||||||
|> Map.put(:limit, normalize_limit(arguments["limit"]))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp search_all_counted_posts(project_id, arguments) do
|
|
||||||
filters = search_filters(arguments) |> Map.put(:offset, 0) |> Map.put(:limit, 1)
|
|
||||||
{:ok, %{total: total}} = Search.search_posts(project_id, "", filters)
|
|
||||||
|
|
||||||
filters = Map.put(filters, :limit, max(total, 1))
|
|
||||||
{:ok, result} = Search.search_posts(project_id, "", filters)
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
|
||||||
|
|
||||||
defp maybe_put(map, _key, nil), do: map
|
|
||||||
defp maybe_put(map, _key, ""), do: map
|
|
||||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
|
||||||
|
|
||||||
defp counted_terms(project_id, field) do
|
defp counted_terms(project_id, field) do
|
||||||
Repo.all(
|
Repo.all(
|
||||||
from post in Post, where: post.project_id == ^project_id, select: field(post, ^field)
|
from post in Post, where: post.project_id == ^project_id, select: field(post, ^field)
|
||||||
@@ -1024,7 +630,11 @@ defmodule BDS.AI.ChatTools do
|
|||||||
|
|
||||||
defp metadata_attrs(arguments, keys) do
|
defp metadata_attrs(arguments, keys) do
|
||||||
Enum.reduce(keys, %{}, fn key, acc ->
|
Enum.reduce(keys, %{}, fn key, acc ->
|
||||||
maybe_put(acc, BDS.MapUtils.safe_atomize_key(key), arguments[key])
|
BDS.MapUtils.maybe_put(
|
||||||
|
acc,
|
||||||
|
BDS.MapUtils.safe_atomize_key(key),
|
||||||
|
BDS.MapUtils.blank_to_nil(arguments[key])
|
||||||
|
)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -167,28 +167,13 @@ defmodule BDS.AI.OneShot do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp run_one_shot(:analyze_image = operation, payload, opts, formatter) do
|
|
||||||
runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime)
|
|
||||||
|
|
||||||
with {:ok, endpoint, model, mode} <- Runtime.resolve_target(operation, opts),
|
|
||||||
:ok <- Runtime.validate_target(operation, model, mode),
|
|
||||||
{:ok, payload} <- resolve_image_data_url(payload),
|
|
||||||
request <- build_one_shot_request(operation, payload, model, opts),
|
|
||||||
{:ok, response} <-
|
|
||||||
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts),
|
|
||||||
{:ok, json} <- extract_json_response(response),
|
|
||||||
usage <- Chat.normalize_usage(response.usage),
|
|
||||||
{:ok, result} <- formatter.(json, usage) do
|
|
||||||
{:ok, result}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp run_one_shot(operation, payload, opts, formatter) do
|
defp run_one_shot(operation, payload, opts, formatter) do
|
||||||
runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime)
|
runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime)
|
||||||
|
|
||||||
with {:ok, endpoint, model, mode} <- Runtime.resolve_target(operation, opts),
|
with {:ok, endpoint, model, mode} <- Runtime.resolve_target(operation, opts),
|
||||||
:ok <- Runtime.validate_target(operation, model, mode),
|
:ok <- Runtime.validate_target(operation, model, mode),
|
||||||
request <- build_one_shot_request(operation, payload, model, opts),
|
{:ok, prepared_payload} <- prepare_one_shot_payload(operation, payload),
|
||||||
|
request <- build_one_shot_request(operation, prepared_payload, model, opts),
|
||||||
{:ok, response} <-
|
{:ok, response} <-
|
||||||
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts),
|
runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts),
|
||||||
{:ok, json} <- extract_json_response(response),
|
{:ok, json} <- extract_json_response(response),
|
||||||
@@ -198,6 +183,9 @@ defmodule BDS.AI.OneShot do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp prepare_one_shot_payload(:analyze_image, payload), do: resolve_image_data_url(payload)
|
||||||
|
defp prepare_one_shot_payload(_operation, payload), do: {:ok, payload}
|
||||||
|
|
||||||
defp build_one_shot_request(operation, payload, model, opts) do
|
defp build_one_shot_request(operation, payload, model, opts) do
|
||||||
language = Keyword.get(opts, :language)
|
language = Keyword.get(opts, :language)
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,7 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
http_client = Keyword.get(opts, :http_client, HttpClient)
|
http_client = Keyword.get(opts, :http_client, HttpClient)
|
||||||
url = models_url(endpoint.url)
|
url = models_url(endpoint.url)
|
||||||
|
|
||||||
headers =
|
headers = request_headers(endpoint.api_key, false)
|
||||||
%{"accept" => "application/json"}
|
|
||||||
|> maybe_put_auth(endpoint.api_key)
|
|
||||||
|
|
||||||
with {:ok, response} <- http_client.get(url, headers),
|
with {:ok, response} <- http_client.get(url, headers),
|
||||||
200 <- response.status do
|
200 <- response.status do
|
||||||
@@ -26,21 +24,8 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
def generate(endpoint, request, opts) when is_map(endpoint) and is_map(request) do
|
def generate(endpoint, request, opts) when is_map(endpoint) and is_map(request) do
|
||||||
url = completions_url(endpoint.url)
|
url = completions_url(endpoint.url)
|
||||||
|
|
||||||
headers =
|
headers = request_headers(endpoint.api_key, true)
|
||||||
%{
|
payload = base_payload(request)
|
||||||
"content-type" => "application/json",
|
|
||||||
"accept" => "application/json"
|
|
||||||
}
|
|
||||||
|> maybe_put_auth(endpoint.api_key)
|
|
||||||
|
|
||||||
payload =
|
|
||||||
%{
|
|
||||||
"model" => request.model,
|
|
||||||
"messages" => request.messages,
|
|
||||||
"max_tokens" => request.max_output_tokens
|
|
||||||
}
|
|
||||||
|> maybe_disable_thinking(request.model)
|
|
||||||
|> maybe_put_tools(Map.get(request, :tools, []))
|
|
||||||
|
|
||||||
if stream?(request, opts) do
|
if stream?(request, opts) do
|
||||||
generate_streaming(url, headers, payload, request, Keyword.fetch!(opts, :on_stream))
|
generate_streaming(url, headers, payload, request, Keyword.fetch!(opts, :on_stream))
|
||||||
@@ -78,15 +63,12 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
result
|
result
|
||||||
|
|
||||||
{:ok, %{status: status, body: body}} ->
|
{:ok, %{status: status, body: body}} ->
|
||||||
Logger.error(
|
log_http_status_error("AI OpenAI-compatible", status, body)
|
||||||
"AI OpenAI-compatible HTTP error status=#{status} body=#{String.slice(body, 0, 2000)}"
|
http_status_error(status, body)
|
||||||
)
|
|
||||||
|
|
||||||
{:error, %{kind: :http_error, status: status, body: body}}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.error("AI OpenAI-compatible HTTP request failed: #{inspect(reason)}")
|
log_http_request_error("AI OpenAI-compatible", reason)
|
||||||
{:error, %{kind: :http_error, reason: reason}}
|
http_request_error(reason)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -95,11 +77,7 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
# snapshots to `on_stream` as they arrive. The assembled message goes
|
# snapshots to `on_stream` as they arrive. The assembled message goes
|
||||||
# through the same normalization as the blocking path.
|
# through the same normalization as the blocking path.
|
||||||
defp generate_streaming(url, headers, payload, request, on_stream) do
|
defp generate_streaming(url, headers, payload, request, on_stream) do
|
||||||
payload_json =
|
payload_json = payload |> streaming_payload() |> Jason.encode!()
|
||||||
payload
|
|
||||||
|> Map.put("stream", true)
|
|
||||||
|> Map.put("stream_options", %{"include_usage" => true})
|
|
||||||
|> Jason.encode!()
|
|
||||||
|
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
"AI OpenAI-compatible streaming request operation=#{inspect(Map.get(request, :operation))} model=#{inspect(request.model)} url=#{url} payload_size=#{byte_size(payload_json)}"
|
"AI OpenAI-compatible streaming request operation=#{inspect(Map.get(request, :operation))} model=#{inspect(request.model)} url=#{url} payload_size=#{byte_size(payload_json)}"
|
||||||
@@ -127,15 +105,12 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
end
|
end
|
||||||
|
|
||||||
{:ok, %{status: status, body: body}, _sse} ->
|
{:ok, %{status: status, body: body}, _sse} ->
|
||||||
Logger.error(
|
log_http_status_error("AI OpenAI-compatible streaming", status, body)
|
||||||
"AI OpenAI-compatible streaming HTTP error status=#{status} body=#{String.slice(body, 0, 2000)}"
|
http_status_error(status, body)
|
||||||
)
|
|
||||||
|
|
||||||
{:error, %{kind: :http_error, status: status, body: body}}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.error("AI OpenAI-compatible streaming request failed: #{inspect(reason)}")
|
log_http_request_error("AI OpenAI-compatible streaming", reason)
|
||||||
{:error, %{kind: :http_error, reason: reason}}
|
http_request_error(reason)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -236,6 +211,44 @@ defmodule BDS.AI.OpenAICompatibleRuntime do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp request_headers(api_key, include_content_type?) do
|
||||||
|
%{"accept" => "application/json"}
|
||||||
|
|> maybe_put_content_type(include_content_type?)
|
||||||
|
|> maybe_put_auth(api_key)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_content_type(headers, true),
|
||||||
|
do: Map.put(headers, "content-type", "application/json")
|
||||||
|
|
||||||
|
defp maybe_put_content_type(headers, false), do: headers
|
||||||
|
|
||||||
|
defp base_payload(request) do
|
||||||
|
%{
|
||||||
|
"model" => request.model,
|
||||||
|
"messages" => request.messages,
|
||||||
|
"max_tokens" => request.max_output_tokens
|
||||||
|
}
|
||||||
|
|> maybe_disable_thinking(request.model)
|
||||||
|
|> maybe_put_tools(Map.get(request, :tools, []))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp streaming_payload(payload) do
|
||||||
|
payload
|
||||||
|
|> Map.put("stream", true)
|
||||||
|
|> Map.put("stream_options", %{"include_usage" => true})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp log_http_status_error(prefix, status, body) do
|
||||||
|
Logger.error("#{prefix} HTTP error status=#{status} body=#{String.slice(body, 0, 2000)}")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp log_http_request_error(prefix, reason) do
|
||||||
|
Logger.error("#{prefix} request failed: #{inspect(reason)}")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp http_status_error(status, body), do: {:error, %{kind: :http_error, status: status, body: body}}
|
||||||
|
defp http_request_error(reason), do: {:error, %{kind: :http_error, reason: reason}}
|
||||||
|
|
||||||
defp maybe_put_auth(headers, nil), do: headers
|
defp maybe_put_auth(headers, nil), do: headers
|
||||||
defp maybe_put_auth(headers, ""), do: headers
|
defp maybe_put_auth(headers, ""), do: headers
|
||||||
|
|
||||||
|
|||||||
@@ -107,5 +107,5 @@ defmodule BDS.AI.Runtime do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp blank?(value), do: value in [nil, ""]
|
defp blank?(value), do: BDS.Values.blank?(value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -41,10 +41,7 @@ defmodule BDS.AI.SecretBackend do
|
|||||||
@spec decrypt(String.t()) :: {:ok, String.t()} | {:error, term()}
|
@spec decrypt(String.t()) :: {:ok, String.t()} | {:error, term()}
|
||||||
def decrypt(encoded) when is_binary(encoded) do
|
def decrypt(encoded) when is_binary(encoded) do
|
||||||
with {:ok, key} <- secret_key() do
|
with {:ok, key} <- secret_key() do
|
||||||
case decrypt_with(encoded, key) do
|
decrypt_with_fallback(encoded, key)
|
||||||
{:ok, plaintext} -> {:ok, plaintext}
|
|
||||||
{:error, :invalid_ciphertext} -> decrypt_legacy(encoded)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -79,6 +76,13 @@ defmodule BDS.AI.SecretBackend do
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp decrypt_with_fallback(encoded, key) do
|
||||||
|
case decrypt_with(encoded, key) do
|
||||||
|
{:ok, _plaintext} = ok -> ok
|
||||||
|
{:error, :invalid_ciphertext} -> decrypt_legacy(encoded)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp decrypt_with(encoded, key) do
|
defp decrypt_with(encoded, key) do
|
||||||
with {:ok, binary} <- Base.decode64(encoded),
|
with {:ok, binary} <- Base.decode64(encoded),
|
||||||
<<iv::binary-size(12), tag::binary-size(16), ciphertext::binary>> <- binary,
|
<<iv::binary-size(12), tag::binary-size(16), ciphertext::binary>> <- binary,
|
||||||
|
|||||||
@@ -105,11 +105,7 @@ defmodule BDS.AI.SecretKey do
|
|||||||
keychain_create()
|
keychain_create()
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.warning(
|
fallback_to_key_file("AI secret key: macOS Keychain unavailable", inspect(reason))
|
||||||
"AI secret key: macOS Keychain unavailable (#{inspect(reason)}); falling back to the key file"
|
|
||||||
)
|
|
||||||
|
|
||||||
resolve_file()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -157,12 +153,10 @@ defmodule BDS.AI.SecretKey do
|
|||||||
{:ok, key}
|
{:ok, key}
|
||||||
|
|
||||||
{output, status} ->
|
{output, status} ->
|
||||||
Logger.warning(
|
fallback_to_key_file(
|
||||||
"AI secret key: could not store the key in the Keychain " <>
|
"AI secret key: could not store the key in the Keychain",
|
||||||
"(status #{status}: #{String.trim(output)}); falling back to the key file"
|
"status #{status}: #{String.trim(output)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
resolve_file()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -230,4 +224,9 @@ defmodule BDS.AI.SecretKey do
|
|||||||
defp config(key, default) do
|
defp config(key, default) do
|
||||||
Application.get_env(:bds, __MODULE__, []) |> Keyword.get(key, default)
|
Application.get_env(:bds, __MODULE__, []) |> Keyword.get(key, default)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp fallback_to_key_file(prefix, detail) do
|
||||||
|
Logger.warning(prefix <> " (" <> detail <> "); falling back to the key file")
|
||||||
|
resolve_file()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,6 +5,27 @@ defmodule BDS.Application do
|
|||||||
|
|
||||||
@compiled_env Application.compile_env(:bds, :current_env, Mix.env())
|
@compiled_env Application.compile_env(:bds, :current_env, Mix.env())
|
||||||
|
|
||||||
|
# Children that depend on the boot mode (issue #26): desktop mode runs the
|
||||||
|
# wx window shell, server mode runs headless behind the SSH daemon. Both
|
||||||
|
# start the loopback HTTP endpoint and the CLI sync watcher.
|
||||||
|
def mode_children(:server, _env) do
|
||||||
|
[{BDS.Desktop.Server, []}, BDS.CliSync.Watcher, BDS.Server]
|
||||||
|
end
|
||||||
|
|
||||||
|
# CLI mode (issue #25): a one-shot command run against the shared database.
|
||||||
|
# No HTTP listener, SSH daemon, window, or sync watcher — the CLI is the
|
||||||
|
# external writer the watcher in a concurrently running app listens for.
|
||||||
|
def mode_children(:cli, _env), do: []
|
||||||
|
|
||||||
|
# Local TUI: the headless server plus a TUI on the launching terminal, so
|
||||||
|
# a GUI (via tunnel) and the local terminal can work in parallel. Quitting
|
||||||
|
# this TUI stops the whole VM — the terminal is the app in this mode.
|
||||||
|
# SSH-served TUI sessions never get this flag and stay session-local.
|
||||||
|
def mode_children(:tui, env),
|
||||||
|
do: mode_children(:server, env) ++ [{BDS.TUI, [stop_vm_on_exit: true]}]
|
||||||
|
|
||||||
|
def mode_children(:desktop, env), do: desktop_children(env)
|
||||||
|
|
||||||
def desktop_children(env \\ nil)
|
def desktop_children(env \\ nil)
|
||||||
|
|
||||||
def desktop_children(:test) do
|
def desktop_children(:test) do
|
||||||
@@ -41,7 +62,7 @@ defmodule BDS.Application do
|
|||||||
{Task.Supervisor, name: BDS.Scripting.TaskSupervisor},
|
{Task.Supervisor, name: BDS.Scripting.TaskSupervisor},
|
||||||
BDS.Scripting.JobSupervisor,
|
BDS.Scripting.JobSupervisor,
|
||||||
BDS.Embeddings.Index
|
BDS.Embeddings.Index
|
||||||
] ++ embedding_children() ++ desktop_children(current_env())
|
] ++ embedding_children() ++ mode_children(boot_mode(), current_env())
|
||||||
|
|
||||||
opts = [strategy: :one_for_one, name: BDS.Supervisor]
|
opts = [strategy: :one_for_one, name: BDS.Supervisor]
|
||||||
Supervisor.start_link(children, opts)
|
Supervisor.start_link(children, opts)
|
||||||
@@ -61,10 +82,26 @@ defmodule BDS.Application do
|
|||||||
Application.get_env(:bds, :current_env_override) || @compiled_env
|
Application.get_env(:bds, :current_env_override) || @compiled_env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Desktop mode without a graphical display boots the TUI instead
|
||||||
|
# (issue #33); the log line explains the surprise mode switch.
|
||||||
|
defp boot_mode do
|
||||||
|
resolved = BDS.Server.mode()
|
||||||
|
effective = BDS.Server.effective_mode(resolved)
|
||||||
|
|
||||||
|
if effective != resolved do
|
||||||
|
require Logger
|
||||||
|
Logger.info("No graphical display available, starting the terminal UI instead")
|
||||||
|
end
|
||||||
|
|
||||||
|
effective
|
||||||
|
end
|
||||||
|
|
||||||
defp desktop_window_children do
|
defp desktop_window_children do
|
||||||
if desktop_automation?() do
|
if desktop_automation?() do
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
|
disable_auto_window_menu()
|
||||||
|
|
||||||
window_opts =
|
window_opts =
|
||||||
BDS.Desktop.MainWindow.window_options(
|
BDS.Desktop.MainWindow.window_options(
|
||||||
menubar: BDS.Desktop.MenuBar,
|
menubar: BDS.Desktop.MenuBar,
|
||||||
@@ -75,11 +112,23 @@ defmodule BDS.Application do
|
|||||||
[
|
[
|
||||||
{Desktop.Window, window_opts},
|
{Desktop.Window, window_opts},
|
||||||
Supervisor.child_spec({BDS.Desktop.MainWindow, []}, id: BDS.Desktop.MainWindow.Watcher),
|
Supervisor.child_spec({BDS.Desktop.MainWindow, []}, id: BDS.Desktop.MainWindow.Watcher),
|
||||||
{BDS.Desktop.DeepLink, []}
|
{BDS.Desktop.DeepLink, []},
|
||||||
|
{BDS.Desktop.RemoteConnection, []}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# wxOSX injects an untracked "Window" menu into the menubar at install time,
|
||||||
|
# which races elixir-desktop's async menu populate and ends up at an
|
||||||
|
# unstable position. We disable it and ship our own :window menu group
|
||||||
|
# (BDS.UI.MenuBar) placed between Blog and Help.
|
||||||
|
defp disable_auto_window_menu do
|
||||||
|
if :os.type() == {:unix, :darwin} do
|
||||||
|
:wx.set_env(Desktop.Env.wx_env())
|
||||||
|
:wxMenuBar.setAutoWindowMenu(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp desktop_automation? do
|
defp desktop_automation? do
|
||||||
System.get_env("BDS_DESKTOP_AUTOMATION") in ["1", "true", "TRUE"]
|
System.get_env("BDS_DESKTOP_AUTOMATION") in ["1", "true", "TRUE"]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ defmodule BDS.Blogmark do
|
|||||||
@scheme "bds2"
|
@scheme "bds2"
|
||||||
@new_post_action "new-post"
|
@new_post_action "new-post"
|
||||||
|
|
||||||
|
# Input-hardening limits mirrored from the legacy bDS app (shared/blogmark.ts).
|
||||||
|
@max_title_length 200
|
||||||
|
@max_url_length 2048
|
||||||
|
@control_chars ~r/[\x00-\x1F\x7F]/
|
||||||
|
|
||||||
@type candidate :: %{required(String.t()) => term()}
|
@type candidate :: %{required(String.t()) => term()}
|
||||||
|
|
||||||
@type receive_result :: %{
|
@type receive_result :: %{
|
||||||
@@ -34,7 +39,8 @@ defmodule BDS.Blogmark do
|
|||||||
Parses a `bds2://new-post` deep link into a post candidate map.
|
Parses a `bds2://new-post` deep link into a post candidate map.
|
||||||
|
|
||||||
Returns `{:ok, candidate}` with string-keyed `title`, `url`, `content`,
|
Returns `{:ok, candidate}` with string-keyed `title`, `url`, `content`,
|
||||||
`tags` and `categories`, or an error when the scheme or action is unsupported.
|
`tags`, `categories` and optional `project_id`, or an error when the scheme
|
||||||
|
or action is unsupported.
|
||||||
"""
|
"""
|
||||||
@spec parse_deep_link(String.t()) ::
|
@spec parse_deep_link(String.t()) ::
|
||||||
{:ok, candidate()} | {:error, :unsupported_scheme | :unsupported_action | :invalid_url}
|
{:ok, candidate()} | {:error, :unsupported_scheme | :unsupported_action | :invalid_url}
|
||||||
@@ -54,10 +60,27 @@ defmodule BDS.Blogmark do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the `project_id` a deep link targets, or `nil` when the link carries
|
||||||
|
none or cannot be parsed. Used by the shell to switch to the link's project
|
||||||
|
before importing.
|
||||||
|
"""
|
||||||
|
@spec deep_link_project_id(String.t()) :: String.t() | nil
|
||||||
|
def deep_link_project_id(url) when is_binary(url) do
|
||||||
|
case parse_deep_link(url) do
|
||||||
|
{:ok, %{"project_id" => project_id}} -> project_id
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Receives a blogmark deep link for `project_id`: parses it, runs the transform
|
Receives a blogmark deep link for `project_id`: parses it, runs the transform
|
||||||
pipeline, and creates a draft post from the resulting candidate.
|
pipeline, and creates a draft post from the resulting candidate.
|
||||||
|
|
||||||
|
When the deep link URL carries a `project_id` query parameter, it takes
|
||||||
|
precedence over the passed `project_id` so the bookmarklet can target a
|
||||||
|
specific project regardless of which project is currently active.
|
||||||
|
|
||||||
Returns `{:ok, %{post:, toasts:, errors:}}` where `toasts` are the
|
Returns `{:ok, %{post:, toasts:, errors:}}` where `toasts` are the
|
||||||
budget-enforced transform messages and `errors` records any failed transforms.
|
budget-enforced transform messages and `errors` records any failed transforms.
|
||||||
"""
|
"""
|
||||||
@@ -66,10 +89,12 @@ defmodule BDS.Blogmark do
|
|||||||
def receive_deep_link(project_id, url, opts \\ [])
|
def receive_deep_link(project_id, url, opts \\ [])
|
||||||
when is_binary(project_id) and is_binary(url) and is_list(opts) do
|
when is_binary(project_id) and is_binary(url) and is_list(opts) do
|
||||||
with {:ok, candidate} <- parse_deep_link(url),
|
with {:ok, candidate} <- parse_deep_link(url),
|
||||||
|
target_project <- resolve_target_project(project_id, candidate),
|
||||||
|
candidate <- apply_default_content(candidate),
|
||||||
{:ok, %{data: data, toasts: toasts, errors: errors}} <-
|
{:ok, %{data: data, toasts: toasts, errors: errors}} <-
|
||||||
Transforms.run(project_id, candidate, opts),
|
Transforms.run(target_project, candidate, opts),
|
||||||
data <- apply_default_category(project_id, data),
|
data <- apply_default_category(target_project, data),
|
||||||
{:ok, post} <- create_draft(project_id, data) do
|
{:ok, post} <- create_draft(target_project, data) do
|
||||||
{:ok, %{post: post, toasts: toasts, errors: errors}}
|
{:ok, %{post: post, toasts: toasts, errors: errors}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -77,15 +102,102 @@ defmodule BDS.Blogmark do
|
|||||||
defp candidate_from_query(query) do
|
defp candidate_from_query(query) do
|
||||||
params = URI.decode_query(query || "")
|
params = URI.decode_query(query || "")
|
||||||
|
|
||||||
|
project_id =
|
||||||
|
case Map.get(params, "project_id") do
|
||||||
|
nil -> nil
|
||||||
|
"" -> nil
|
||||||
|
id -> id
|
||||||
|
end
|
||||||
|
|
||||||
|
url = sanitize_http_url(Map.get(params, "url"))
|
||||||
|
|
||||||
%{
|
%{
|
||||||
"title" => Map.get(params, "title", "") |> to_string(),
|
"title" => sanitize_title(Map.get(params, "title"), url_host(url)),
|
||||||
"url" => optional(params, "url"),
|
"url" => url,
|
||||||
"content" => optional(params, "content"),
|
"content" => optional(params, "content"),
|
||||||
"tags" => list_param(params, "tags"),
|
"tags" => list_param(params, "tags"),
|
||||||
"categories" => list_param(params, "categories")
|
"categories" => list_param(params, "categories"),
|
||||||
|
"project_id" => project_id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Strip control characters, trim, cap length; fall back to the URL host when
|
||||||
|
# blank — mirrors bDS sanitizeTitle. Title/url are single-line fields, so
|
||||||
|
# control-character stripping is safe here (unlike the multi-line body).
|
||||||
|
defp sanitize_title(raw, fallback_host) do
|
||||||
|
title =
|
||||||
|
raw
|
||||||
|
|> to_string()
|
||||||
|
|> strip_control()
|
||||||
|
|> String.trim()
|
||||||
|
|> String.slice(0, @max_title_length)
|
||||||
|
|
||||||
|
if title == "", do: fallback_host || "", else: title
|
||||||
|
end
|
||||||
|
|
||||||
|
# Accept only http(s) URLs, strip credentials and fragment, enforce a length
|
||||||
|
# cap; anything else collapses to nil so it can never reach the post body.
|
||||||
|
defp sanitize_http_url(raw) when is_binary(raw) do
|
||||||
|
trimmed = raw |> strip_control() |> String.trim()
|
||||||
|
|
||||||
|
case URI.parse(trimmed) do
|
||||||
|
%URI{scheme: scheme, host: host} = uri
|
||||||
|
when scheme in ["http", "https"] and is_binary(host) and host != "" ->
|
||||||
|
normalized = URI.to_string(%URI{uri | userinfo: nil, fragment: nil})
|
||||||
|
if String.length(normalized) <= @max_url_length, do: normalized, else: nil
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp sanitize_http_url(_other), do: nil
|
||||||
|
|
||||||
|
defp url_host(url) when is_binary(url), do: URI.parse(url).host
|
||||||
|
defp url_host(_other), do: nil
|
||||||
|
|
||||||
|
defp strip_control(value), do: String.replace(value, @control_chars, "")
|
||||||
|
|
||||||
|
# If the deep link carries a project_id, route to that project; otherwise
|
||||||
|
# use the currently active project passed by the caller. This lets the
|
||||||
|
# bookmarklet target a specific project even when a different one is open.
|
||||||
|
defp resolve_target_project(_caller_project_id, %{"project_id" => pid}) when is_binary(pid) do
|
||||||
|
pid
|
||||||
|
end
|
||||||
|
|
||||||
|
defp resolve_target_project(project_id, _candidate), do: project_id
|
||||||
|
|
||||||
|
# Match the legacy bDS app: when the bookmarklet supplies no body, seed the
|
||||||
|
# post content with a markdown link to the bookmarked page so transforms run
|
||||||
|
# against the same input. Explicit content always wins.
|
||||||
|
defp apply_default_content(candidate) do
|
||||||
|
with nil <- optional_string(Map.get(candidate, "content")),
|
||||||
|
url when is_binary(url) <- optional_string(Map.get(candidate, "url")) do
|
||||||
|
title = Map.get(candidate, "title", "")
|
||||||
|
Map.put(candidate, "content", blogmark_markdown_link(title, url))
|
||||||
|
else
|
||||||
|
_ -> candidate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp blogmark_markdown_link(title, url) do
|
||||||
|
"[" <> escape_markdown_link_text(title) <> "](" <> url <> ")"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Mirrors bDS escapeMarkdownLinkText: backslash first, then brackets/parens,
|
||||||
|
# then collapse newlines to spaces.
|
||||||
|
defp escape_markdown_link_text(value) do
|
||||||
|
value
|
||||||
|
|> to_string()
|
||||||
|
|> String.trim()
|
||||||
|
|> String.replace("\\", "\\\\")
|
||||||
|
|> String.replace("[", "\\[")
|
||||||
|
|> String.replace("]", "\\]")
|
||||||
|
|> String.replace("(", "\\(")
|
||||||
|
|> String.replace(")", "\\)")
|
||||||
|
|> String.replace(~r/\r?\n/, " ")
|
||||||
|
end
|
||||||
|
|
||||||
defp optional(params, key) do
|
defp optional(params, key) do
|
||||||
case Map.get(params, key) do
|
case Map.get(params, key) do
|
||||||
nil -> nil
|
nil -> nil
|
||||||
|
|||||||
@@ -54,19 +54,22 @@ defmodule BDS.BoundedAtoms do
|
|||||||
:fill_missing_translations,
|
:fill_missing_translations,
|
||||||
:find_duplicates,
|
:find_duplicates,
|
||||||
:generate_sitemap,
|
:generate_sitemap,
|
||||||
|
:force_render_site,
|
||||||
:validate_site,
|
:validate_site,
|
||||||
:upload_site,
|
:upload_site,
|
||||||
:documentation,
|
:documentation,
|
||||||
:api_documentation,
|
:api_documentation,
|
||||||
:close_tab
|
:close_tab
|
||||||
]
|
]
|
||||||
@menu_actions MenuBar.default_groups(dev_mode?: true)
|
@menu_actions MenuBar.default_groups(dev_mode?: true, window_menu?: true)
|
||||||
|> Enum.flat_map(fn group ->
|
|> Enum.flat_map(fn group ->
|
||||||
Enum.flat_map(group.items, fn
|
Enum.flat_map(group.items, fn
|
||||||
%{separator: true} -> []
|
%{separator: true} -> []
|
||||||
%{id: id} -> [id]
|
%{id: id} -> [id]
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@sidebar_view_ids Enum.map(Registry.sidebar_views(), & &1.id)
|
||||||
|
@editor_route_ids Enum.map(Registry.editor_routes(), & &1.id)
|
||||||
|
|
||||||
def atom(value, allowed, fallback \\ nil)
|
def atom(value, allowed, fallback \\ nil)
|
||||||
|
|
||||||
@@ -101,13 +104,9 @@ defmodule BDS.BoundedAtoms do
|
|||||||
Enum.find(allowed, fallback, &(Atom.to_string(&1) == value))
|
Enum.find(allowed, fallback, &(Atom.to_string(&1) == value))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp sidebar_views do
|
defp sidebar_views, do: @sidebar_view_ids
|
||||||
Enum.map(Registry.sidebar_views(), & &1.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp editor_routes do
|
defp editor_routes, do: @editor_route_ids
|
||||||
Enum.map(Registry.editor_routes(), & &1.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp normalize_menu_kind("category-archive"), do: "category_archive"
|
defp normalize_menu_kind("category-archive"), do: "category_archive"
|
||||||
defp normalize_menu_kind(value), do: value
|
defp normalize_menu_kind(value), do: value
|
||||||
|
|||||||
335
lib/bds/cli.ex
Normal file
335
lib/bds/cli.ex
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
defmodule BDS.CLI do
|
||||||
|
@moduledoc """
|
||||||
|
Command-line interface for managing content in a bDS2 workspace (issue #25).
|
||||||
|
|
||||||
|
Runs inside the release VM (`BDS_MODE=cli`) against the same settings and
|
||||||
|
cache database as the GUI/TUI application; mutations write
|
||||||
|
`BDS.CliSync.Notification` rows so a concurrently running app picks them
|
||||||
|
up through `BDS.CliSync.Watcher`.
|
||||||
|
|
||||||
|
The launcher script (`cli/bin/bds-cli` in the release, installable to
|
||||||
|
`~/.local/bin` via `BDS.CLI.Install`) passes the argv via `BDS_CLI_ARGV`
|
||||||
|
(unit-separator joined) because `bin/bds eval` cannot forward arguments.
|
||||||
|
"""
|
||||||
|
|
||||||
|
alias BDS.CLI.Commands
|
||||||
|
|
||||||
|
@argv_env "BDS_CLI_ARGV"
|
||||||
|
@argv_separator <<0x1F>>
|
||||||
|
|
||||||
|
@doc "Release entry point: run the command from `BDS_CLI_ARGV` and halt."
|
||||||
|
@spec main() :: no_return()
|
||||||
|
def main do
|
||||||
|
System.halt(run(env_argv()))
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Decodes the unit-separator-joined argv from the launcher script."
|
||||||
|
@spec env_argv(String.t() | nil) :: [String.t()]
|
||||||
|
def env_argv(value \\ System.get_env(@argv_env))
|
||||||
|
def env_argv(nil), do: []
|
||||||
|
def env_argv(value), do: String.split(value, @argv_separator, trim: true)
|
||||||
|
|
||||||
|
@doc "Parses and executes an argv, returning the process exit code."
|
||||||
|
@spec run([String.t()]) :: non_neg_integer()
|
||||||
|
def run(argv) when is_list(argv) do
|
||||||
|
optimus = optimus()
|
||||||
|
|
||||||
|
case Optimus.parse(optimus, argv) do
|
||||||
|
{:ok, %Optimus.ParseResult{}} ->
|
||||||
|
print_help(optimus, [])
|
||||||
|
0
|
||||||
|
|
||||||
|
{:ok, subcommand_path, parse_result} ->
|
||||||
|
execute(subcommand_path, parse_result)
|
||||||
|
|
||||||
|
:help ->
|
||||||
|
print_help(optimus, [])
|
||||||
|
0
|
||||||
|
|
||||||
|
{:help, subcommand_path} ->
|
||||||
|
print_help(optimus, subcommand_path)
|
||||||
|
0
|
||||||
|
|
||||||
|
:version ->
|
||||||
|
IO.puts(version())
|
||||||
|
0
|
||||||
|
|
||||||
|
{:error, errors} ->
|
||||||
|
print_errors(optimus, [], errors)
|
||||||
|
1
|
||||||
|
|
||||||
|
{:error, subcommand_path, errors} ->
|
||||||
|
print_errors(optimus, subcommand_path, errors)
|
||||||
|
1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Executes a parsed subcommand, returning the process exit code."
|
||||||
|
@spec execute([atom()], Optimus.ParseResult.t()) :: non_neg_integer()
|
||||||
|
def execute(subcommand_path, parse_result) do
|
||||||
|
{:ok, _apps} = Application.ensure_all_started(:bds)
|
||||||
|
|
||||||
|
subcommand_path
|
||||||
|
|> Commands.dispatch(parse_result)
|
||||||
|
|> report()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp report(:ok), do: 0
|
||||||
|
|
||||||
|
defp report({:ok, message}) when is_binary(message) do
|
||||||
|
IO.puts(message)
|
||||||
|
0
|
||||||
|
end
|
||||||
|
|
||||||
|
defp report({:error, message}) do
|
||||||
|
IO.puts(:stderr, "Error: " <> format_error(message))
|
||||||
|
1
|
||||||
|
end
|
||||||
|
|
||||||
|
defp format_error(message) when is_binary(message), do: message
|
||||||
|
defp format_error(%{message: message}) when is_binary(message), do: message
|
||||||
|
defp format_error(other), do: inspect(other)
|
||||||
|
|
||||||
|
@repair_parts ~w(post-links media-links thumbnails embeddings search)
|
||||||
|
|
||||||
|
@doc "The Optimus command definition (public for tests and help rendering)."
|
||||||
|
def optimus do
|
||||||
|
Optimus.new!(
|
||||||
|
name: "bds-cli",
|
||||||
|
description: "bDS2 workspace CLI",
|
||||||
|
about:
|
||||||
|
"Manages content in a bDS2 workspace using the same settings and " <>
|
||||||
|
"cache database as the desktop application.",
|
||||||
|
version: version(),
|
||||||
|
allow_unknown_args: false,
|
||||||
|
parse_double_dash: true,
|
||||||
|
subcommands: [
|
||||||
|
rebuild: [
|
||||||
|
name: "rebuild",
|
||||||
|
about: "Rebuild the caching database from the workspace files",
|
||||||
|
flags: [
|
||||||
|
incremental: [
|
||||||
|
long: "--incremental",
|
||||||
|
help:
|
||||||
|
"Run a metadata diff and auto-apply every difference from " <>
|
||||||
|
"file to database instead of a full rebuild"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
repair: [
|
||||||
|
name: "repair",
|
||||||
|
about: "Run one of the standard repair tasks outside the full rebuild",
|
||||||
|
args: [
|
||||||
|
part: [
|
||||||
|
value_name: "PART",
|
||||||
|
help: "One of: " <> Enum.join(@repair_parts, ", "),
|
||||||
|
required: true,
|
||||||
|
parser: &parse_repair_part/1
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
render: [
|
||||||
|
name: "render",
|
||||||
|
about: "Render the blog from the current content",
|
||||||
|
flags: [
|
||||||
|
incremental: [
|
||||||
|
long: "--incremental",
|
||||||
|
help: "Validate the generated output and apply only the differences"
|
||||||
|
],
|
||||||
|
force: [
|
||||||
|
long: "--force",
|
||||||
|
help: "Full re-render ignoring (but updating) stored content hashes"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
upload: [
|
||||||
|
name: "upload",
|
||||||
|
about: "Upload the rendered site to the configured host (rsync/scp)"
|
||||||
|
],
|
||||||
|
push: [
|
||||||
|
name: "push",
|
||||||
|
about: "Push the project repository to its origin"
|
||||||
|
],
|
||||||
|
pull: [
|
||||||
|
name: "pull",
|
||||||
|
about: "Pull the project repository and update the cache database"
|
||||||
|
],
|
||||||
|
post: [
|
||||||
|
name: "post",
|
||||||
|
about:
|
||||||
|
"Create a post from parameters or from JSON on stdin " <>
|
||||||
|
"(keys: title, content, excerpt, author, language, tags, categories, template)",
|
||||||
|
flags: [
|
||||||
|
stdin: [long: "--stdin", help: "Read post data as JSON from stdin"],
|
||||||
|
no_translate: [
|
||||||
|
long: "--no-translate",
|
||||||
|
help: "Skip automatic translation of the created post"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
options: post_content_options()
|
||||||
|
],
|
||||||
|
media: [
|
||||||
|
name: "media",
|
||||||
|
about:
|
||||||
|
"Import an image with automatically generated title, alt text, " <>
|
||||||
|
"caption and translations",
|
||||||
|
args: [
|
||||||
|
file: [
|
||||||
|
value_name: "FILE",
|
||||||
|
help: "Path of the image file to import",
|
||||||
|
required: true
|
||||||
|
]
|
||||||
|
],
|
||||||
|
options: [
|
||||||
|
language: [
|
||||||
|
long: "--language",
|
||||||
|
value_name: "LANG",
|
||||||
|
help: "Language for the generated texts (defaults to the project main language)"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
gallery: [
|
||||||
|
name: "gallery",
|
||||||
|
about:
|
||||||
|
"Create a gallery post and import all referenced images as new " <>
|
||||||
|
"media with generated texts and translations",
|
||||||
|
allow_unknown_args: true,
|
||||||
|
flags: [
|
||||||
|
stdin: [
|
||||||
|
long: "--stdin",
|
||||||
|
help: "Read gallery data as JSON from stdin (adds an \"images\" key to post keys)"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
options: post_content_options()
|
||||||
|
],
|
||||||
|
config: [
|
||||||
|
name: "config",
|
||||||
|
about: "Read and write global preference values",
|
||||||
|
subcommands: [
|
||||||
|
get: [
|
||||||
|
name: "get",
|
||||||
|
about: "Print a preference value",
|
||||||
|
args: [key: [value_name: "KEY", help: "Preference key", required: true]]
|
||||||
|
],
|
||||||
|
set: [
|
||||||
|
name: "set",
|
||||||
|
about: "Set a preference value",
|
||||||
|
args: [
|
||||||
|
key: [value_name: "KEY", help: "Preference key", required: true],
|
||||||
|
value: [value_name: "VALUE", help: "New value", required: true]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
list: [
|
||||||
|
name: "list",
|
||||||
|
about: "List all preference keys and values"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
project: [
|
||||||
|
name: "project",
|
||||||
|
about: "Manage the projects registered in the cache database",
|
||||||
|
subcommands: [
|
||||||
|
list: [
|
||||||
|
name: "list",
|
||||||
|
about: "List all projects"
|
||||||
|
],
|
||||||
|
add: [
|
||||||
|
name: "add",
|
||||||
|
about: "Open a project folder and add it to the cache database",
|
||||||
|
args: [
|
||||||
|
path: [value_name: "PATH", help: "Project data folder", required: true]
|
||||||
|
],
|
||||||
|
options: [
|
||||||
|
name: [
|
||||||
|
long: "--name",
|
||||||
|
value_name: "NAME",
|
||||||
|
help: "Project name (defaults to the folder name)"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
switch: [
|
||||||
|
name: "switch",
|
||||||
|
about: "Switch the active project",
|
||||||
|
args: [
|
||||||
|
project: [
|
||||||
|
value_name: "PROJECT",
|
||||||
|
help: "Project id, slug, or name",
|
||||||
|
required: true
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
tui: [
|
||||||
|
name: "tui",
|
||||||
|
about: "Open the app in TUI mode for interactive use"
|
||||||
|
],
|
||||||
|
lua: [
|
||||||
|
name: "lua",
|
||||||
|
about: "Run a utility (long-running task) Lua script from the database",
|
||||||
|
allow_unknown_args: true,
|
||||||
|
args: [
|
||||||
|
script: [
|
||||||
|
value_name: "SCRIPT",
|
||||||
|
help: "Script slug in the active project",
|
||||||
|
required: true
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp post_content_options do
|
||||||
|
[
|
||||||
|
title: [long: "--title", value_name: "TITLE", help: "Post title"],
|
||||||
|
content: [long: "--content", value_name: "MARKDOWN", help: "Post body (markdown)"],
|
||||||
|
excerpt: [long: "--excerpt", value_name: "TEXT", help: "Post excerpt"],
|
||||||
|
author: [long: "--author", value_name: "NAME", help: "Post author"],
|
||||||
|
language: [
|
||||||
|
long: "--language",
|
||||||
|
value_name: "LANG",
|
||||||
|
help: "Post language (auto-detected from the content when omitted)"
|
||||||
|
],
|
||||||
|
template: [long: "--template", value_name: "SLUG", help: "Template slug"],
|
||||||
|
tags: [long: "--tags", value_name: "TAGS", help: "Comma-separated tags"],
|
||||||
|
categories: [
|
||||||
|
long: "--categories",
|
||||||
|
value_name: "CATEGORIES",
|
||||||
|
help: "Comma-separated categories"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_repair_part(value) when value in @repair_parts, do: {:ok, value}
|
||||||
|
|
||||||
|
defp parse_repair_part(value),
|
||||||
|
do: {:error, "unknown repair part #{inspect(value)}; expected one of: #{Enum.join(@repair_parts, ", ")}"}
|
||||||
|
|
||||||
|
defp print_help(optimus, subcommand_path) do
|
||||||
|
optimus
|
||||||
|
|> Optimus.Help.help(subcommand_path, terminal_width())
|
||||||
|
|> Enum.each(&IO.puts/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_errors(optimus, [], errors) do
|
||||||
|
optimus |> Optimus.Errors.format(errors) |> Enum.each(&IO.puts(:stderr, &1))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_errors(optimus, subcommand_path, errors) do
|
||||||
|
optimus
|
||||||
|
|> Optimus.Errors.format(subcommand_path, errors)
|
||||||
|
|> Enum.each(&IO.puts(:stderr, &1))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp terminal_width do
|
||||||
|
case Optimus.Term.width() do
|
||||||
|
{:ok, width} -> width
|
||||||
|
_other -> 80
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp version do
|
||||||
|
to_string(Application.spec(:bds, :vsn) || "dev")
|
||||||
|
end
|
||||||
|
end
|
||||||
696
lib/bds/cli/commands.ex
Normal file
696
lib/bds/cli/commands.ex
Normal file
@@ -0,0 +1,696 @@
|
|||||||
|
defmodule BDS.CLI.Commands do
|
||||||
|
@moduledoc """
|
||||||
|
Implementations of the `bds-cli` subcommands (issue #25).
|
||||||
|
|
||||||
|
Every command runs synchronously against the engine modules the GUI uses
|
||||||
|
and returns `:ok`, `{:ok, message}`, or `{:error, message}`. Mutations
|
||||||
|
write `BDS.CliSync` notification rows so a concurrently running app
|
||||||
|
refreshes its state (see `specs/cli_sync.allium`).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.AI
|
||||||
|
alias BDS.CliSync
|
||||||
|
alias BDS.Desktop.ShellLive.GalleryImport
|
||||||
|
alias BDS.Generation
|
||||||
|
alias BDS.Git
|
||||||
|
alias BDS.Maintenance
|
||||||
|
alias BDS.Media
|
||||||
|
alias BDS.Metadata
|
||||||
|
alias BDS.Posts
|
||||||
|
alias BDS.Posts.AutoTranslation
|
||||||
|
alias BDS.Projects
|
||||||
|
alias BDS.Publishing
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Scripting
|
||||||
|
alias BDS.Scripts
|
||||||
|
alias BDS.Scripts.Script
|
||||||
|
alias BDS.Search
|
||||||
|
alias BDS.Settings
|
||||||
|
alias BDS.Tasks
|
||||||
|
|
||||||
|
@site_sections [:core, :single, :category, :tag, :date]
|
||||||
|
@bulk_entity_types ~w(post media script template)
|
||||||
|
@gallery_concurrency 2
|
||||||
|
@poll_interval_ms 250
|
||||||
|
|
||||||
|
@doc "Dispatches a parsed subcommand path to its implementation."
|
||||||
|
@spec dispatch([atom()], Optimus.ParseResult.t()) :: :ok | {:ok, String.t()} | {:error, term()}
|
||||||
|
def dispatch([:rebuild], %{flags: %{incremental: true}}),
|
||||||
|
do: with_project(&incremental_rebuild/1)
|
||||||
|
|
||||||
|
def dispatch([:rebuild], _parse_result), do: with_project(&full_rebuild/1)
|
||||||
|
|
||||||
|
def dispatch([:repair], %{args: %{part: part}}),
|
||||||
|
do: with_project(&repair(part, &1))
|
||||||
|
|
||||||
|
def dispatch([:render], %{flags: %{incremental: true, force: true}}),
|
||||||
|
do: {:error, "--incremental and --force are mutually exclusive"}
|
||||||
|
|
||||||
|
def dispatch([:render], %{flags: %{incremental: true}}),
|
||||||
|
do: with_project(&incremental_render/1)
|
||||||
|
|
||||||
|
def dispatch([:render], %{flags: flags}),
|
||||||
|
do: with_project(&full_render(&1, flags[:force] == true))
|
||||||
|
|
||||||
|
def dispatch([:upload], _parse_result), do: with_project(&upload/1)
|
||||||
|
|
||||||
|
def dispatch([:push], _parse_result) do
|
||||||
|
with_project(fn project ->
|
||||||
|
with {:ok, result} <- Git.push(project.id) do
|
||||||
|
print_git_output(result)
|
||||||
|
{:ok, "Pushed"}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:pull], _parse_result) do
|
||||||
|
with_project(fn project ->
|
||||||
|
with {:ok, result} <- Git.pull(project.id) do
|
||||||
|
print_git_output(result)
|
||||||
|
# Update the cache database from the pulled files, like the app's
|
||||||
|
# incremental rebuild: file→db for every difference plus orphans.
|
||||||
|
incremental_rebuild(project)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:post], %{flags: flags, options: options}) do
|
||||||
|
with_project(fn project ->
|
||||||
|
with {:ok, attrs} <- post_attrs(flags, options),
|
||||||
|
{:ok, attrs} <- ensure_language(attrs),
|
||||||
|
{:ok, post} <- create_post(project, attrs) do
|
||||||
|
unless flags[:no_translate], do: translate_post(post)
|
||||||
|
{:ok, "Created post #{post.id} (#{post.slug}, #{post.language})"}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:media], %{args: %{file: file}, options: options}) do
|
||||||
|
with_project(fn project ->
|
||||||
|
with {:ok, metadata} <- Metadata.get_project_metadata(project.id),
|
||||||
|
:ok <- ensure_files_exist([file]) do
|
||||||
|
language = present(options[:language]) || metadata.main_language
|
||||||
|
targets = GalleryImport.translate_targets(metadata, language)
|
||||||
|
|
||||||
|
case GalleryImport.import_and_enrich(file, project.id, language, targets) do
|
||||||
|
{:ok, media, title} ->
|
||||||
|
:ok = notify("media", media.id, :created)
|
||||||
|
{:ok, "Imported media #{media.id} (#{title})"}
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
{:error, format_reason(reason)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:gallery], %{flags: flags, options: options, unknown: unknown}) do
|
||||||
|
with_project(fn project ->
|
||||||
|
with {:ok, attrs, images} <- gallery_attrs(flags, options, unknown),
|
||||||
|
:ok <- ensure_files_exist(images),
|
||||||
|
{:ok, attrs} <- ensure_language(attrs),
|
||||||
|
{:ok, post} <- create_post(project, attrs) do
|
||||||
|
import_gallery_images(project, post, images)
|
||||||
|
:ok = notify("media", "*", :created)
|
||||||
|
:ok = notify("post", post.id, :updated)
|
||||||
|
unless flags[:no_translate], do: translate_post(post)
|
||||||
|
{:ok, "Created gallery post #{post.id} (#{post.slug}) with #{length(images)} images"}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:config, :get], %{args: %{key: key}}) do
|
||||||
|
case Settings.get_global_setting(key) do
|
||||||
|
nil -> {:error, "#{key} is not set"}
|
||||||
|
value -> {:ok, value}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:config, :set], %{args: %{key: key, value: value}}) do
|
||||||
|
with :ok <- Settings.put_global_setting(key, value) do
|
||||||
|
:ok = notify("setting", key, :updated)
|
||||||
|
{:ok, "#{key} = #{value}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:config, :list], _parse_result) do
|
||||||
|
Settings.list_global_settings()
|
||||||
|
|> Enum.each(fn {key, value} -> IO.puts("#{key}=#{value}") end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:project, :list], _parse_result) do
|
||||||
|
Enum.each(Projects.list_projects(), fn project ->
|
||||||
|
marker = if project.is_active, do: "* ", else: " "
|
||||||
|
IO.puts("#{marker}#{project.id} #{project.slug} #{project.name}")
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:project, :add], %{args: %{path: path}, options: options}) do
|
||||||
|
expanded = Path.expand(path)
|
||||||
|
|
||||||
|
if File.dir?(expanded) do
|
||||||
|
name = present(options[:name]) || Path.basename(expanded)
|
||||||
|
|
||||||
|
with {:ok, project} <- Projects.create_project(%{name: name, data_path: expanded}) do
|
||||||
|
:ok = notify("project", project.id, :created)
|
||||||
|
{:ok, "Added project #{project.id} (#{project.slug}); switch with: project switch #{project.slug}"}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
{:error, "#{expanded} is not a directory"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:project, :switch], %{args: %{project: reference}}) do
|
||||||
|
with {:ok, project} <- resolve_project(reference),
|
||||||
|
{:ok, project} <- Projects.set_active_project(project.id) do
|
||||||
|
:ok = notify("project", project.id, :updated)
|
||||||
|
{:ok, "Active project: #{project.name}"}
|
||||||
|
else
|
||||||
|
{:error, :not_found} -> {:error, "No project matches #{inspect(reference)}"}
|
||||||
|
{:error, reason} -> {:error, format_reason(reason)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:tui], _parse_result) do
|
||||||
|
{:error,
|
||||||
|
"The TUI is started by the bds-cli launcher script (BDS_MODE=tui); " <>
|
||||||
|
"it is not available through a direct BDS.CLI invocation"}
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch([:lua], %{args: %{script: slug}, unknown: script_args}) do
|
||||||
|
with_project(fn project ->
|
||||||
|
case Repo.one(
|
||||||
|
from script in Script,
|
||||||
|
where: script.project_id == ^project.id and script.slug == ^slug
|
||||||
|
) do
|
||||||
|
nil ->
|
||||||
|
{:error, "No script with slug #{inspect(slug)} in the active project"}
|
||||||
|
|
||||||
|
%Script{kind: kind} when kind != :utility ->
|
||||||
|
{:error, "Script #{inspect(slug)} is a #{kind} script; only utility (task) scripts can be run"}
|
||||||
|
|
||||||
|
%Script{enabled: false} ->
|
||||||
|
{:error, "Script #{inspect(slug)} is disabled"}
|
||||||
|
|
||||||
|
script ->
|
||||||
|
run_lua_job(project, script, script_args)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── rebuild ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp full_rebuild(project) do
|
||||||
|
project.id
|
||||||
|
|> Maintenance.full_rebuild_steps()
|
||||||
|
|> Enum.reduce_while(:ok, fn step, :ok ->
|
||||||
|
IO.puts("==> #{step.name}")
|
||||||
|
|
||||||
|
case step.work.(progress_printer()) do
|
||||||
|
{:error, message} -> {:halt, {:error, message}}
|
||||||
|
_result -> {:cont, :ok}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> case do
|
||||||
|
:ok ->
|
||||||
|
notify_bulk_change()
|
||||||
|
{:ok, "Rebuild complete"}
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp incremental_rebuild(project) do
|
||||||
|
reporter = progress_printer()
|
||||||
|
{:ok, diff} = Maintenance.metadata_diff(project.id, on_progress: reporter)
|
||||||
|
|
||||||
|
items =
|
||||||
|
Enum.map(diff.diff_reports, &%{entity_type: &1.entity_type, entity_id: &1.entity_id})
|
||||||
|
|
||||||
|
orphans = Enum.map(diff.orphan_reports, &%{file_path: &1.file_path})
|
||||||
|
|
||||||
|
if items != [] do
|
||||||
|
{:ok, _repair} =
|
||||||
|
Maintenance.repair_metadata_diff(project.id, :file_to_db, items, on_progress: reporter)
|
||||||
|
end
|
||||||
|
|
||||||
|
if orphans != [] do
|
||||||
|
{:ok, _import} =
|
||||||
|
Maintenance.import_metadata_diff_orphans(project.id, orphans, on_progress: reporter)
|
||||||
|
end
|
||||||
|
|
||||||
|
if items != [] or orphans != [], do: notify_bulk_change()
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
"Applied #{length(items)} differences and imported #{length(orphans)} orphan files from the filesystem"}
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── repair ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp repair("post-links", project) do
|
||||||
|
:ok = Posts.rebuild_post_links(project.id, on_progress: progress_printer())
|
||||||
|
:ok = notify("post", "*", :updated)
|
||||||
|
{:ok, "Post links rebuilt"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp repair("media-links", project) do
|
||||||
|
{:ok, result} = Media.rebuild_media_links(project.id, on_progress: progress_printer())
|
||||||
|
:ok = notify("media", "*", :updated)
|
||||||
|
{:ok, "Media links rebuilt (#{result.links} links)"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp repair("thumbnails", project) do
|
||||||
|
result = Media.regenerate_missing_thumbnails(project.id, on_progress: progress_printer())
|
||||||
|
:ok = notify("media", "*", :updated)
|
||||||
|
{:ok, "Missing thumbnails regenerated (#{inspect(Map.get(result, :generated, 0))} generated)"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp repair("embeddings", project) do
|
||||||
|
case Maintenance.rebuild_embedding_index(project.id, on_progress: progress_printer()) do
|
||||||
|
{:error, message} -> {:error, message}
|
||||||
|
result -> {:ok, "Embedding index rebuilt (#{result.rebuilt_count} posts)"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp repair("search", project) do
|
||||||
|
:ok = Search.reindex_posts(project.id, on_progress: progress_printer())
|
||||||
|
:ok = Search.reindex_media(project.id, on_progress: progress_printer())
|
||||||
|
{:ok, "Search text reindexed"}
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── render ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp full_render(project, force?) do
|
||||||
|
render_opts = if force?, do: [force: true], else: []
|
||||||
|
|
||||||
|
Enum.each(@site_sections, fn section ->
|
||||||
|
IO.puts("==> Rendering #{section}")
|
||||||
|
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.render_site_section(
|
||||||
|
project.id,
|
||||||
|
section,
|
||||||
|
[on_progress: progress_printer()] ++ render_opts
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
IO.puts("==> Building search index")
|
||||||
|
|
||||||
|
{:ok, _index} =
|
||||||
|
Generation.build_site_search_index(
|
||||||
|
project.id,
|
||||||
|
[on_progress: progress_printer()] ++ render_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
{:ok, "Site rendered"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp incremental_render(project) do
|
||||||
|
{:ok, report} =
|
||||||
|
Generation.validate_site(project.id, @site_sections, on_progress: progress_printer())
|
||||||
|
|
||||||
|
{:ok, applied} =
|
||||||
|
Generation.apply_validation(project.id, report, on_progress: progress_printer())
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
"Validation applied (#{applied.rendered_url_count} rendered, " <>
|
||||||
|
"#{applied.deleted_url_count} deleted)"}
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── upload ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp upload(project) do
|
||||||
|
with {:ok, metadata} <- Metadata.get_project_metadata(project.id),
|
||||||
|
{:ok, credentials} <- upload_credentials(metadata.publishing_preferences),
|
||||||
|
{:ok, job} <- Publishing.upload_site(project.id, credentials) do
|
||||||
|
await_task(job.task_id, "Upload complete")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp upload_credentials(prefs) when is_map(prefs) do
|
||||||
|
credentials = %{
|
||||||
|
ssh_host: Map.get(prefs, "ssh_host"),
|
||||||
|
ssh_user: Map.get(prefs, "ssh_user"),
|
||||||
|
ssh_remote_path: Map.get(prefs, "ssh_remote_path"),
|
||||||
|
ssh_mode: Map.get(prefs, "ssh_mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
if Enum.all?(
|
||||||
|
[credentials.ssh_host, credentials.ssh_user, credentials.ssh_remote_path],
|
||||||
|
&is_binary/1
|
||||||
|
) do
|
||||||
|
{:ok, credentials}
|
||||||
|
else
|
||||||
|
{:error, "Publishing preferences are incomplete; configure the upload host in Settings"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp upload_credentials(_prefs),
|
||||||
|
do: {:error, "Publishing preferences are incomplete; configure the upload host in Settings"}
|
||||||
|
|
||||||
|
# ── post / gallery ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp post_attrs(%{stdin: true}, _options) do
|
||||||
|
with {:ok, data} <- read_stdin_json() do
|
||||||
|
json_post_attrs(data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp post_attrs(_flags, options) do
|
||||||
|
case present(options[:title]) do
|
||||||
|
nil ->
|
||||||
|
{:error, "--title is required (or pass --stdin with JSON post data)"}
|
||||||
|
|
||||||
|
title ->
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
title: title,
|
||||||
|
content: options[:content] || "",
|
||||||
|
excerpt: present(options[:excerpt]),
|
||||||
|
author: present(options[:author]),
|
||||||
|
language: present(options[:language]),
|
||||||
|
template_slug: present(options[:template]),
|
||||||
|
tags: split_list(options[:tags]),
|
||||||
|
categories: split_list(options[:categories])
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp gallery_attrs(%{stdin: true}, _options, _unknown) do
|
||||||
|
with {:ok, data} <- read_stdin_json(),
|
||||||
|
{:ok, attrs} <- json_post_attrs(data) do
|
||||||
|
case Map.get(data, "images") do
|
||||||
|
images when is_list(images) and images != [] -> {:ok, attrs, images}
|
||||||
|
_other -> {:error, "JSON gallery data needs a non-empty \"images\" array"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp gallery_attrs(flags, options, unknown) do
|
||||||
|
case unknown do
|
||||||
|
[] ->
|
||||||
|
{:error, "Pass the image files as arguments (or use --stdin with JSON gallery data)"}
|
||||||
|
|
||||||
|
images ->
|
||||||
|
with {:ok, attrs} <- post_attrs(flags, options) do
|
||||||
|
{:ok, attrs, images}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp json_post_attrs(data) when is_map(data) do
|
||||||
|
case present(Map.get(data, "title")) do
|
||||||
|
nil ->
|
||||||
|
{:error, "JSON post data needs a \"title\""}
|
||||||
|
|
||||||
|
title ->
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
title: title,
|
||||||
|
content: Map.get(data, "content") || "",
|
||||||
|
excerpt: present(Map.get(data, "excerpt")),
|
||||||
|
author: present(Map.get(data, "author")),
|
||||||
|
language: present(Map.get(data, "language")),
|
||||||
|
template_slug: present(Map.get(data, "template")),
|
||||||
|
tags: json_list(Map.get(data, "tags")),
|
||||||
|
categories: json_list(Map.get(data, "categories"))
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp json_post_attrs(_data), do: {:error, "JSON post data must be an object"}
|
||||||
|
|
||||||
|
defp read_stdin_json do
|
||||||
|
case IO.read(:stdio, :eof) do
|
||||||
|
:eof ->
|
||||||
|
{:error, "No JSON data on stdin"}
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
{:error, "Could not read stdin: #{inspect(reason)}"}
|
||||||
|
|
||||||
|
data ->
|
||||||
|
case Jason.decode(data) do
|
||||||
|
{:ok, decoded} -> {:ok, decoded}
|
||||||
|
{:error, error} -> {:error, "Invalid JSON on stdin: #{Exception.message(error)}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Language auto-detection (issue #25): ask the configured AI endpoint —
|
||||||
|
# BDS.AI internally routes to the local model in airplane mode — and fall
|
||||||
|
# back to the offline search heuristic, informing the user (the CLI
|
||||||
|
# equivalent of the airplane-mode toast).
|
||||||
|
defp ensure_language(%{language: language} = attrs) when is_binary(language), do: {:ok, attrs}
|
||||||
|
|
||||||
|
defp ensure_language(attrs) do
|
||||||
|
text = String.trim("#{attrs.title}\n#{attrs.content}")
|
||||||
|
|
||||||
|
language =
|
||||||
|
case AI.detect_language(text) do
|
||||||
|
{:ok, %{language_code: language}} when is_binary(language) ->
|
||||||
|
language
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
IO.puts("AI language detection unavailable; using the offline heuristic")
|
||||||
|
Search.detect_language(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, %{attrs | language: language}}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp create_post(project, attrs) do
|
||||||
|
case Posts.create_post(Map.put(attrs, :project_id, project.id)) do
|
||||||
|
{:ok, post} ->
|
||||||
|
:ok = notify("post", post.id, :created)
|
||||||
|
{:ok, post}
|
||||||
|
|
||||||
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
|
{:error, changeset_message(changeset)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Automatic translation mirrors the GUI: schedule the background tasks and
|
||||||
|
# wait for them, so the CLI exits only when the work is done. When nothing
|
||||||
|
# gets scheduled (airplane mode without a local model, or a single-language
|
||||||
|
# blog) the user is told instead of a silent no-op.
|
||||||
|
defp translate_post(post) do
|
||||||
|
before_count = length(active_tasks())
|
||||||
|
:ok = AutoTranslation.maybe_schedule(post)
|
||||||
|
|
||||||
|
case length(active_tasks()) - before_count do
|
||||||
|
queued when queued > 0 ->
|
||||||
|
IO.puts("Waiting for #{queued} translation task(s)...")
|
||||||
|
await_active_tasks()
|
||||||
|
:ok = notify("post", post.id, :updated)
|
||||||
|
|
||||||
|
_none ->
|
||||||
|
IO.puts("Automatic translation was not scheduled (offline, unconfigured AI, or nothing to translate)")
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp import_gallery_images(project, post, images) do
|
||||||
|
{:ok, metadata} = Metadata.get_project_metadata(project.id)
|
||||||
|
language = post.language || metadata.main_language
|
||||||
|
parent = self()
|
||||||
|
|
||||||
|
runner =
|
||||||
|
Task.async(fn ->
|
||||||
|
GalleryImport.start(
|
||||||
|
images,
|
||||||
|
project.id,
|
||||||
|
post.id,
|
||||||
|
language,
|
||||||
|
@gallery_concurrency,
|
||||||
|
parent
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
print_gallery_progress()
|
||||||
|
Task.await(runner, :infinity)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_gallery_progress do
|
||||||
|
receive do
|
||||||
|
{:add_image_processed, title} ->
|
||||||
|
IO.puts("Imported #{title}")
|
||||||
|
print_gallery_progress()
|
||||||
|
|
||||||
|
{:add_image_error, path, reason} ->
|
||||||
|
IO.puts(:stderr, "Failed to import #{path}: #{format_reason(reason)}")
|
||||||
|
print_gallery_progress()
|
||||||
|
|
||||||
|
{:add_images_complete, count} ->
|
||||||
|
IO.puts("Processed #{count} image(s)")
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp ensure_files_exist(paths) do
|
||||||
|
case Enum.reject(paths, &File.regular?/1) do
|
||||||
|
[] -> :ok
|
||||||
|
missing -> {:error, "No such file: #{Enum.join(missing, ", ")}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── lua ───────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
# Utility ("task") scripts run with the managed-job execution budget
|
||||||
|
# (unlimited time/reductions) but synchronously — the CLI is the one
|
||||||
|
# waiting for the long-running activity.
|
||||||
|
defp run_lua_job(project, script, args) do
|
||||||
|
scripting_config = Application.fetch_env!(:bds, :scripting)
|
||||||
|
|
||||||
|
case Scripting.execute_project_script(
|
||||||
|
project.id,
|
||||||
|
Scripts.resolved_content(script),
|
||||||
|
script.entrypoint || "main",
|
||||||
|
args,
|
||||||
|
timeout: Keyword.get(scripting_config, :job_timeout, :infinity),
|
||||||
|
max_reductions: Keyword.get(scripting_config, :job_max_reductions, :none),
|
||||||
|
on_output: fn text -> IO.puts(text) end
|
||||||
|
) do
|
||||||
|
{:ok, nil} -> {:ok, "Script finished"}
|
||||||
|
{:ok, result} -> {:ok, "Script finished: #{format_reason(result)}"}
|
||||||
|
{:error, reason} -> {:error, "Script failed: #{format_reason(reason)}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ── shared helpers ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
defp with_project(fun) do
|
||||||
|
case Projects.get_active_project() do
|
||||||
|
nil -> {:error, "No active project selected; use: project switch <project>"}
|
||||||
|
project -> fun.(project)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp resolve_project(reference) do
|
||||||
|
projects = Projects.list_projects()
|
||||||
|
|
||||||
|
found =
|
||||||
|
Enum.find(projects, fn project ->
|
||||||
|
reference in [project.id, project.slug, project.name]
|
||||||
|
end)
|
||||||
|
|
||||||
|
if found, do: {:ok, found}, else: {:error, :not_found}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp await_task(task_id, success_message) do
|
||||||
|
case Tasks.get_task(task_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, "Task disappeared before completion"}
|
||||||
|
|
||||||
|
%{status: :completed} ->
|
||||||
|
{:ok, success_message}
|
||||||
|
|
||||||
|
%{status: :failed} = task ->
|
||||||
|
{:error, format_reason(task.error || "task failed")}
|
||||||
|
|
||||||
|
%{message: message} = task ->
|
||||||
|
print_progress(task.progress || 0.0, message)
|
||||||
|
Process.sleep(@poll_interval_ms)
|
||||||
|
await_task(task_id, success_message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp active_tasks do
|
||||||
|
Enum.filter(Tasks.list_tasks(), &(&1.status in [:pending, :running]))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp await_active_tasks do
|
||||||
|
case active_tasks() do
|
||||||
|
[] ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
_tasks ->
|
||||||
|
Process.sleep(@poll_interval_ms)
|
||||||
|
await_active_tasks()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
A 2-arity progress callback that prints deduplicated `[ 42%] message`
|
||||||
|
lines to stdout.
|
||||||
|
"""
|
||||||
|
def progress_printer do
|
||||||
|
&print_progress/2
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_progress(value, message) do
|
||||||
|
percent = value |> Kernel.*(100) |> round() |> min(100) |> max(0)
|
||||||
|
line = {percent, message}
|
||||||
|
|
||||||
|
if Process.get({__MODULE__, :last_progress}) != line do
|
||||||
|
Process.put({__MODULE__, :last_progress}, line)
|
||||||
|
|
||||||
|
if is_binary(message) and message != "" do
|
||||||
|
IO.puts("[#{String.pad_leading(Integer.to_string(percent), 3)}%] #{message}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_git_output(%{output: output}) when is_binary(output) and output != "" do
|
||||||
|
IO.puts(String.trim_trailing(output))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp print_git_output(_result), do: :ok
|
||||||
|
|
||||||
|
defp notify(entity_type, entity_id, action) do
|
||||||
|
{:ok, _notification} = CliSync.cli_mutation_performed(entity_type, entity_id, action)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
# Bulk maintenance touched an unknown set of rows; a wildcard notification
|
||||||
|
# per entity type makes a running app reload its lists.
|
||||||
|
defp notify_bulk_change do
|
||||||
|
Enum.each(@bulk_entity_types, ¬ify(&1, "*", :updated))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp split_list(nil), do: []
|
||||||
|
|
||||||
|
defp split_list(value) when is_binary(value) do
|
||||||
|
value
|
||||||
|
|> String.split(",")
|
||||||
|
|> Enum.map(&String.trim/1)
|
||||||
|
|> Enum.reject(&(&1 == ""))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp json_list(value) when is_list(value), do: Enum.map(value, &to_string/1)
|
||||||
|
defp json_list(value) when is_binary(value), do: split_list(value)
|
||||||
|
defp json_list(_value), do: []
|
||||||
|
|
||||||
|
defp present(nil), do: nil
|
||||||
|
|
||||||
|
defp present(value) when is_binary(value) do
|
||||||
|
case String.trim(value) do
|
||||||
|
"" -> nil
|
||||||
|
trimmed -> trimmed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp changeset_message(%Ecto.Changeset{} = changeset) do
|
||||||
|
changeset
|
||||||
|
|> Ecto.Changeset.traverse_errors(fn {message, opts} ->
|
||||||
|
Enum.reduce(opts, message, fn {key, value}, acc ->
|
||||||
|
String.replace(acc, "%{#{key}}", to_string(value))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|> Enum.map_join("; ", fn {field, messages} -> "#{field}: #{Enum.join(messages, ", ")}" end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp format_reason(reason) when is_binary(reason), do: reason
|
||||||
|
defp format_reason(%{message: message}) when is_binary(message), do: message
|
||||||
|
defp format_reason(%{guidance: guidance}) when is_binary(guidance), do: guidance
|
||||||
|
defp format_reason(reason), do: inspect(reason)
|
||||||
|
end
|
||||||
57
lib/bds/cli/install.ex
Normal file
57
lib/bds/cli/install.ex
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
defmodule BDS.CLI.Install do
|
||||||
|
@moduledoc """
|
||||||
|
Installs the `bds-cli` launcher into `~/.local/bin` (issue #25), used by
|
||||||
|
the install buttons in the GUI and TUI settings.
|
||||||
|
|
||||||
|
The installed file is a two-line shim exec'ing the release's
|
||||||
|
`cli/bin/bds-cli` launcher (shipped via `rel/overlays`), so the CLI always
|
||||||
|
runs the same release — and therefore the same settings and cache
|
||||||
|
database — as the installed application.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@launcher_name "bds-cli"
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Writes the `~/.local/bin/bds-cli` shim. Returns `{:ok, installed_path}`,
|
||||||
|
or `{:error, :no_release}` when not running from a packaged release (dev
|
||||||
|
runs have no launcher to point at).
|
||||||
|
"""
|
||||||
|
@spec install(keyword()) :: {:ok, Path.t()} | {:error, :no_release | File.posix()}
|
||||||
|
def install(opts \\ []) do
|
||||||
|
bin_dir = Keyword.get(opts, :bin_dir, Path.expand("~/.local/bin"))
|
||||||
|
|
||||||
|
with {:ok, launcher} <- launcher_path(Keyword.get(opts, :release_root)) do
|
||||||
|
installed = Path.join(bin_dir, @launcher_name)
|
||||||
|
|
||||||
|
with :ok <- File.mkdir_p(bin_dir),
|
||||||
|
:ok <- File.write(installed, shim_script(launcher)),
|
||||||
|
:ok <- File.chmod(installed, 0o755) do
|
||||||
|
{:ok, installed}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
The release's CLI launcher script, resolved from `RELEASE_ROOT` (set by
|
||||||
|
release scripts) or the code root dir (the release root when running a
|
||||||
|
release). `{:error, :no_release}` when neither contains the launcher.
|
||||||
|
"""
|
||||||
|
@spec launcher_path(Path.t() | nil) :: {:ok, Path.t()} | {:error, :no_release}
|
||||||
|
def launcher_path(release_root \\ nil) do
|
||||||
|
[release_root, System.get_env("RELEASE_ROOT"), to_string(:code.root_dir())]
|
||||||
|
|> Enum.reject(&(&1 in [nil, ""]))
|
||||||
|
|> Enum.map(&Path.join([&1, "cli", "bin", @launcher_name]))
|
||||||
|
|> Enum.find(&File.regular?/1)
|
||||||
|
|> case do
|
||||||
|
nil -> {:error, :no_release}
|
||||||
|
path -> {:ok, path}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp shim_script(launcher) do
|
||||||
|
"""
|
||||||
|
#!/bin/sh
|
||||||
|
exec "#{launcher}" "$@"
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -54,6 +54,11 @@ defmodule BDS.CliSync do
|
|||||||
end)}
|
end)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def data_version do
|
||||||
|
%{rows: [[version]]} = Repo.query!("PRAGMA data_version", [])
|
||||||
|
version
|
||||||
|
end
|
||||||
|
|
||||||
def prune_notifications(now \\ Persistence.now_ms()) when is_integer(now) do
|
def prune_notifications(now \\ Persistence.now_ms()) when is_integer(now) do
|
||||||
{processed_count, _} =
|
{processed_count, _} =
|
||||||
Repo.delete_all(
|
Repo.delete_all(
|
||||||
|
|||||||
@@ -29,7 +29,12 @@ defmodule BDS.CliSync.Watcher do
|
|||||||
Keyword.get(opts, :poll_interval_ms),
|
Keyword.get(opts, :poll_interval_ms),
|
||||||
@default_poll_interval_ms
|
@default_poll_interval_ms
|
||||||
),
|
),
|
||||||
pubsub: Keyword.get(opts, :pubsub, BDS.PubSub)
|
pubsub: Keyword.get(opts, :pubsub, BDS.PubSub),
|
||||||
|
data_version_reader: Keyword.get(opts, :data_version_reader, &CliSync.data_version/0),
|
||||||
|
notification_fetcher:
|
||||||
|
Keyword.get(opts, :notification_fetcher, &CliSync.db_file_change_detected/0),
|
||||||
|
pruner: Keyword.get(opts, :pruner, &CliSync.prune_notifications/0),
|
||||||
|
last_data_version: nil
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, schedule_poll(state)}
|
{:ok, schedule_poll(state)}
|
||||||
@@ -49,18 +54,24 @@ defmodule BDS.CliSync.Watcher do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp process_notifications(state) do
|
defp process_notifications(state) do
|
||||||
{:ok, notifications} = CliSync.db_file_change_detected()
|
current_data_version = state.data_version_reader.()
|
||||||
{:ok, _pruned} = CliSync.prune_notifications()
|
|
||||||
|
|
||||||
Enum.each(notifications, fn notification ->
|
if state.last_data_version == current_data_version do
|
||||||
Phoenix.PubSub.broadcast(
|
%{state | last_data_version: current_data_version}
|
||||||
state.pubsub,
|
else
|
||||||
topic(),
|
{:ok, notifications} = state.notification_fetcher.()
|
||||||
{:entity_changed, notification_payload(notification)}
|
{:ok, _pruned} = state.pruner.()
|
||||||
)
|
|
||||||
end)
|
|
||||||
|
|
||||||
state
|
Enum.each(notifications, fn notification ->
|
||||||
|
Phoenix.PubSub.broadcast(
|
||||||
|
state.pubsub,
|
||||||
|
topic(),
|
||||||
|
{:entity_changed, notification_payload(notification)}
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
%{state | last_data_version: current_data_version}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp notification_payload(notification) do
|
defp notification_payload(notification) do
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ defmodule BDS.Desktop.Automation do
|
|||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
use GenServer
|
use GenServer
|
||||||
|
require Logger
|
||||||
|
|
||||||
@ready_timeout 60_000
|
@ready_timeout 60_000
|
||||||
@request_timeout 30_000
|
@request_timeout 30_000
|
||||||
@@ -13,7 +14,9 @@ defmodule BDS.Desktop.Automation do
|
|||||||
def stop_session(session) do
|
def stop_session(session) do
|
||||||
GenServer.stop(session, :normal, @request_timeout)
|
GenServer.stop(session, :normal, @request_timeout)
|
||||||
catch
|
catch
|
||||||
:exit, _reason -> :ok
|
:exit, reason ->
|
||||||
|
Logger.debug("swallowed desktop automation stop_session exit: #{inspect(reason)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def snapshot(session) do
|
def snapshot(session) do
|
||||||
@@ -220,7 +223,12 @@ defmodule BDS.Desktop.Automation do
|
|||||||
defp safe_driver_request(state, payload) do
|
defp safe_driver_request(state, payload) do
|
||||||
driver_request(state, payload)
|
driver_request(state, payload)
|
||||||
rescue
|
rescue
|
||||||
_error -> :ok
|
error ->
|
||||||
|
Logger.debug(
|
||||||
|
"swallowed desktop automation safe_driver_request error: #{inspect(error)} payload=#{inspect(payload)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
defp shutdown_driver(state) do
|
defp shutdown_driver(state) do
|
||||||
@@ -368,7 +376,9 @@ defmodule BDS.Desktop.Automation do
|
|||||||
defp safe_close_port(port) do
|
defp safe_close_port(port) do
|
||||||
Port.close(port)
|
Port.close(port)
|
||||||
catch
|
catch
|
||||||
:error, _reason -> :ok
|
:error, reason ->
|
||||||
|
Logger.debug("swallowed desktop automation safe_close_port error: #{inspect(reason)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
defp normalize_simple_reply("ok"), do: :ok
|
defp normalize_simple_reply("ok"), do: :ok
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
defmodule BDS.Desktop.DeepLink do
|
defmodule BDS.Desktop.DeepLink do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Receives OS URL-scheme events for the `bds2://` scheme and routes them to the
|
Receives OS URL-scheme events for the `bds2://` scheme and routes them to the
|
||||||
shell (spec: script.allium `BlogmarkReceived`).
|
live shell (spec: script.allium `BlogmarkReceived`).
|
||||||
|
|
||||||
On macOS the `BDS2.app` bundle registers `bds2://` as a custom URL scheme via
|
On macOS the `BDS2.app` bundle registers `bds2://` as a custom URL scheme via
|
||||||
the `CFBundleURLTypes` entry in its `Info.plist` (built by `BDS.MacBundle` /
|
the `CFBundleURLTypes` entry in its `Info.plist` (built by `BDS.MacBundle` /
|
||||||
`mix bds.bundle.macos`). When the browser bookmarklet navigates to
|
`mix bds.bundle.macos`). When the browser bookmarklet navigates to
|
||||||
`bds2://new-post?title=&url=`, the OS launches/raises the app and `Desktop.Env`
|
`bds2://new-post?title=&url=`, the OS launches/raises the app and `Desktop.Env`
|
||||||
delivers an `{:open_url, [url]}` event. This GenServer subscribes to those
|
relays erlang `:wx`'s `{:open_url, Url}` app event (where `Url` is the URL as a
|
||||||
events and forwards recognised `bds2://` links to the live shell over PubSub,
|
charlist). This GenServer subscribes to those events and forwards recognised
|
||||||
where `BDS.Blogmark` turns them into draft posts.
|
`bds2://` links to the live shell.
|
||||||
|
|
||||||
|
Cold start matters here: when the bookmarklet *launches* the app, the deep
|
||||||
|
link arrives during startup, before the LiveView shell's websocket has
|
||||||
|
connected. Such links are queued and replayed the moment a shell registers via
|
||||||
|
`attach/2`; while a shell is connected, links are delivered to it directly.
|
||||||
|
|
||||||
The `bds2://` scheme is distinct from the legacy app's `bds://` so the two
|
The `bds2://` scheme is distinct from the legacy app's `bds://` so the two
|
||||||
installs do not contend for the same registration.
|
installs do not contend for the same registration.
|
||||||
@@ -19,8 +24,6 @@ defmodule BDS.Desktop.DeepLink do
|
|||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
alias BDS.CliSync.Watcher
|
|
||||||
|
|
||||||
@scheme "bds2://"
|
@scheme "bds2://"
|
||||||
|
|
||||||
def child_spec(opts) do
|
def child_spec(opts) do
|
||||||
@@ -31,32 +34,69 @@ defmodule BDS.Desktop.DeepLink do
|
|||||||
GenServer.start_link(__MODULE__, opts, name: Keyword.get(opts, :name, __MODULE__))
|
GenServer.start_link(__MODULE__, opts, name: Keyword.get(opts, :name, __MODULE__))
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@doc """
|
||||||
def init(opts) do
|
Registers the live shell `pid` as the deep-link sink and immediately replays
|
||||||
pubsub = Keyword.get(opts, :pubsub, BDS.PubSub)
|
any links that arrived before a shell was connected. Future links are sent to
|
||||||
topic = Keyword.get(opts, :topic, Watcher.topic())
|
this pid until it dies, after which links queue again.
|
||||||
|
"""
|
||||||
subscribe_to_env()
|
@spec attach(pid(), GenServer.server()) :: :ok
|
||||||
|
def attach(shell_pid, server \\ __MODULE__) when is_pid(shell_pid) do
|
||||||
{:ok, %{pubsub: pubsub, topic: topic}}
|
GenServer.call(server, {:attach, shell_pid})
|
||||||
end
|
end
|
||||||
|
|
||||||
# Desktop.Env delivers OS events as {event_name, args} tuples.
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info({:open_url, [url | _rest]}, state) when is_binary(url) do
|
def init(_opts) do
|
||||||
{:noreply, route(url, state)}
|
subscribe_to_env()
|
||||||
|
{:ok, %{shell: nil, pending: []}}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call({:attach, pid}, _from, state) do
|
||||||
|
Process.monitor(pid)
|
||||||
|
|
||||||
|
state.pending
|
||||||
|
|> Enum.reverse()
|
||||||
|
|> Enum.each(&send(pid, {:blogmark_deep_link, &1}))
|
||||||
|
|
||||||
|
{:reply, :ok, %{state | shell: pid, pending: []}}
|
||||||
|
end
|
||||||
|
|
||||||
|
# erlang's :wx delivers macOS app events as `{:open_url, Url}` where `Url` is
|
||||||
|
# the URL *string itself* — a charlist on current OTP, not a list of URLs
|
||||||
|
# (see `wx:subscribe_events/0`). Desktop.Env forwards it unchanged, so the
|
||||||
|
# payload must be normalised to a binary before routing.
|
||||||
|
@impl true
|
||||||
|
def handle_info({:open_url, payload}, state) do
|
||||||
|
{:noreply, route(to_url(payload), state)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_info({:DOWN, _ref, :process, pid, _reason}, %{shell: pid} = state) do
|
||||||
|
{:noreply, %{state | shell: nil}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info(_message, state), do: {:noreply, state}
|
def handle_info(_message, state), do: {:noreply, state}
|
||||||
|
|
||||||
defp route(url, state) do
|
# Accept the charlist wx actually sends, a bare binary, or a [binary | _] list
|
||||||
if String.starts_with?(url, @scheme) do
|
# (the shape Desktop.Env's docs imply), normalising all to a binary URL.
|
||||||
Phoenix.PubSub.broadcast(state.pubsub, state.topic, {:blogmark_deep_link, url})
|
defp to_url(value) when is_binary(value), do: value
|
||||||
else
|
defp to_url([first | _]) when is_binary(first), do: first
|
||||||
Logger.debug("ignoring non-bds2 deep link: #{inspect(url)}")
|
defp to_url(value) when is_list(value), do: List.to_string(value)
|
||||||
end
|
defp to_url(_other), do: ""
|
||||||
|
|
||||||
state
|
defp route(url, state) do
|
||||||
|
cond do
|
||||||
|
not String.starts_with?(url, @scheme) ->
|
||||||
|
Logger.debug("ignoring non-bds2 deep link: #{inspect(url)}")
|
||||||
|
state
|
||||||
|
|
||||||
|
is_pid(state.shell) and Process.alive?(state.shell) ->
|
||||||
|
send(state.shell, {:blogmark_deep_link, url})
|
||||||
|
state
|
||||||
|
|
||||||
|
true ->
|
||||||
|
# No shell connected yet (cold start) — hold the link until one attaches.
|
||||||
|
%{state | pending: [url | state.pending]}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Desktop.Env is only present when the wx desktop adapter is running. Guard the
|
# Desktop.Env is only present when the wx desktop adapter is running. Guard the
|
||||||
@@ -66,7 +106,9 @@ defmodule BDS.Desktop.DeepLink do
|
|||||||
try do
|
try do
|
||||||
Desktop.Env.subscribe()
|
Desktop.Env.subscribe()
|
||||||
catch
|
catch
|
||||||
:exit, _reason -> :ok
|
:exit, reason ->
|
||||||
|
Logger.debug("swallowed deep_link Desktop.Env.subscribe exit: #{inspect(reason)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
52
lib/bds/desktop/dialogs.ex
Normal file
52
lib/bds/desktop/dialogs.ex
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
defmodule BDS.Desktop.Dialogs do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
# Native single-line text prompt, same osascript approach as
|
||||||
|
# BDS.Desktop.FilePicker. Returns {:ok, text} | :cancel | {:error, map}.
|
||||||
|
def prompt_text(message, default \\ "") when is_binary(message) do
|
||||||
|
if System.get_env("BDS_DESKTOP_AUTOMATION") == "1" do
|
||||||
|
:cancel
|
||||||
|
else
|
||||||
|
case :os.type() do
|
||||||
|
{:unix, :darwin} -> prompt_text_macos(message, default)
|
||||||
|
_other -> {:error, %{message: "Dialogs are only supported on macOS desktop"}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def alert(title, message) when is_binary(title) and is_binary(message) do
|
||||||
|
if System.get_env("BDS_DESKTOP_AUTOMATION") == "1" or :os.type() != {:unix, :darwin} do
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
script = "display alert \"#{escape(title)}\" message \"#{escape(message)}\""
|
||||||
|
_ = System.cmd("osascript", ["-e", script], stderr_to_stdout: true)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp prompt_text_macos(message, default) do
|
||||||
|
script =
|
||||||
|
"text returned of (display dialog \"#{escape(message)}\" default answer \"#{escape(default)}\")"
|
||||||
|
|
||||||
|
case System.cmd("osascript", ["-e", script], stderr_to_stdout: true) do
|
||||||
|
{output, 0} -> {:ok, String.trim(output)}
|
||||||
|
{output, _status} -> normalize_failure(output)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_failure(output) do
|
||||||
|
message = String.trim(output)
|
||||||
|
|
||||||
|
if message == "" or String.contains?(String.downcase(message), "canceled") do
|
||||||
|
:cancel
|
||||||
|
else
|
||||||
|
{:error, %{message: message}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp escape(value) do
|
||||||
|
value
|
||||||
|
|> String.replace("\\", "\\\\")
|
||||||
|
|> String.replace("\"", "\\\"")
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -17,19 +17,14 @@ defmodule BDS.Desktop.Endpoint do
|
|||||||
plug(Plug.Static,
|
plug(Plug.Static,
|
||||||
at: "/assets",
|
at: "/assets",
|
||||||
from: {:bds, "priv/static/assets"},
|
from: {:bds, "priv/static/assets"},
|
||||||
only: ["app.css", "app.js"]
|
only: ["app.css", "app.js", "monaco.css", "monaco.js", "monaco"]
|
||||||
)
|
|
||||||
|
|
||||||
plug(Plug.Static,
|
|
||||||
at: "/monaco",
|
|
||||||
from: {:bds, "priv/ui/monaco"},
|
|
||||||
only: ["vs"]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
plug(BDS.Desktop.Router)
|
plug(BDS.Desktop.Router)
|
||||||
|
|
||||||
defp maybe_require_desktop_auth(conn, _opts) do
|
defp maybe_require_desktop_auth(conn, _opts) do
|
||||||
if System.get_env("BDS_DESKTOP_AUTOMATION") in ["1", "true", "TRUE"] do
|
if System.get_env("BDS_DESKTOP_AUTOMATION") in ["1", "true", "TRUE"] or
|
||||||
|
not BDS.Server.desktop_auth_required?() do
|
||||||
conn
|
conn
|
||||||
else
|
else
|
||||||
Desktop.Auth.call(conn, [])
|
Desktop.Auth.call(conn, [])
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
defmodule BDS.Desktop.ExternalLinks do
|
|
||||||
@moduledoc false
|
|
||||||
|
|
||||||
@github_url "https://github.com/rfc1437/bDS2"
|
|
||||||
@github_issues_url "#{@github_url}/issues"
|
|
||||||
|
|
||||||
@spec github_url() :: String.t()
|
|
||||||
def github_url, do: @github_url
|
|
||||||
|
|
||||||
@spec github_issues_url() :: String.t()
|
|
||||||
def github_issues_url, do: @github_issues_url
|
|
||||||
end
|
|
||||||
@@ -13,6 +13,7 @@ defmodule BDS.Desktop.Layouts do
|
|||||||
<title><%= @page_title || "Blogging Desktop Server" %></title>
|
<title><%= @page_title || "Blogging Desktop Server" %></title>
|
||||||
<meta name="csrf-token" content={Phoenix.Controller.get_csrf_token()} />
|
<meta name="csrf-token" content={Phoenix.Controller.get_csrf_token()} />
|
||||||
<link phx-track-static rel="stylesheet" href="/assets/app.css" />
|
<link phx-track-static rel="stylesheet" href="/assets/app.css" />
|
||||||
|
<link phx-track-static rel="stylesheet" href="/assets/monaco.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<%= @inner_content %>
|
<%= @inner_content %>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ defmodule BDS.Desktop.MainWindow do
|
|||||||
use GenServer
|
use GenServer
|
||||||
|
|
||||||
alias Desktop.Window
|
alias Desktop.Window
|
||||||
|
require Logger
|
||||||
|
|
||||||
@window_id __MODULE__
|
@window_id __MODULE__
|
||||||
@server_name BDS.Desktop.MainWindow.Watcher
|
@server_name BDS.Desktop.MainWindow.Watcher
|
||||||
@@ -22,7 +23,9 @@ defmodule BDS.Desktop.MainWindow do
|
|||||||
def persist_now(timeout \\ 100) do
|
def persist_now(timeout \\ 100) do
|
||||||
GenServer.call(@server_name, :persist_bounds_now, timeout)
|
GenServer.call(@server_name, :persist_bounds_now, timeout)
|
||||||
catch
|
catch
|
||||||
:exit, _reason -> :ok
|
:exit, reason ->
|
||||||
|
Logger.debug("swallowed main_window persist_now exit: #{inspect(reason)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def window_options(extra_opts \\ []) do
|
def window_options(extra_opts \\ []) do
|
||||||
@@ -169,10 +172,17 @@ defmodule BDS.Desktop.MainWindow do
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
rescue
|
rescue
|
||||||
ErlangError -> nil
|
error in ErlangError ->
|
||||||
FunctionClauseError -> nil
|
Logger.debug("swallowed main_window restore_bounds error: #{inspect(error)}")
|
||||||
|
nil
|
||||||
|
|
||||||
|
error in FunctionClauseError ->
|
||||||
|
Logger.debug("swallowed main_window restore_bounds error: #{inspect(error)}")
|
||||||
|
nil
|
||||||
catch
|
catch
|
||||||
:exit, _reason -> nil
|
:exit, reason ->
|
||||||
|
Logger.debug("swallowed main_window restore_bounds exit: #{inspect(reason)}")
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -8,16 +8,45 @@ defmodule BDS.Desktop.MediaController do
|
|||||||
alias BDS.Projects
|
alias BDS.Projects
|
||||||
alias BDS.Repo
|
alias BDS.Repo
|
||||||
|
|
||||||
def thumbnail(conn, %{"media_id" => media_id} = params) do
|
@web_image_types ~w(image/jpeg image/png image/webp image/gif image/svg+xml image/avif)
|
||||||
case active_media_thumbnail(media_id, Map.get(params, "size")) do
|
|
||||||
{:ok, content_type, path} ->
|
|
||||||
conn
|
|
||||||
|> Plug.Conn.put_resp_content_type(content_type)
|
|
||||||
|> Plug.Conn.send_file(200, path)
|
|
||||||
|
|
||||||
:error ->
|
def thumbnail(conn, %{"media_id" => media_id} = params) do
|
||||||
send_resp(conn, 404, "not found")
|
send_media(conn, active_media_thumbnail(media_id, Map.get(params, "size")))
|
||||||
|
end
|
||||||
|
|
||||||
|
def file(conn, %{"media_id" => media_id}) do
|
||||||
|
send_media(conn, active_media_file(media_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp send_media(conn, {:ok, content_type, path}) do
|
||||||
|
conn
|
||||||
|
|> Plug.Conn.put_resp_content_type(content_type)
|
||||||
|
|> Plug.Conn.send_file(200, path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp send_media(conn, :error), do: send_resp(conn, 404, "not found")
|
||||||
|
|
||||||
|
# Serves the original image at full resolution; formats browsers cannot
|
||||||
|
# render inline fall back to the large thumbnail.
|
||||||
|
defp active_media_file(media_id) do
|
||||||
|
with %{} = project <- Projects.get_active_project(),
|
||||||
|
%MediaRecord{} = media <- Repo.get(MediaRecord, media_id),
|
||||||
|
true <- media.project_id == project.id,
|
||||||
|
true <- media.mime_type in @web_image_types,
|
||||||
|
absolute_path = Path.join(Projects.project_data_dir(project), media.file_path),
|
||||||
|
true <- File.exists?(absolute_path) do
|
||||||
|
{:ok, media.mime_type, absolute_path}
|
||||||
|
else
|
||||||
|
_other -> active_media_thumbnail(media_id, "large")
|
||||||
end
|
end
|
||||||
|
rescue
|
||||||
|
error in [Exqlite.Error, DBConnection.OwnershipError] ->
|
||||||
|
if match?(%Exqlite.Error{}, error) and
|
||||||
|
not String.contains?(Exception.message(error), "no such table") do
|
||||||
|
reraise error, __STACKTRACE__
|
||||||
|
end
|
||||||
|
|
||||||
|
:error
|
||||||
end
|
end
|
||||||
|
|
||||||
defp active_media_thumbnail(media_id, size) do
|
defp active_media_thumbnail(media_id, size) do
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
use BDS.Desktop.MenuCompat
|
use BDS.Desktop.MenuCompat
|
||||||
alias BDS.Desktop.{ExternalLinks, ShellData, Shutdown, UILocale}
|
alias BDS.Desktop.{ShellData, Shutdown, UILocale}
|
||||||
alias BDS.UI.Commands
|
alias BDS.UI.Commands
|
||||||
alias BDS.UI.MenuBar, as: ShellMenuBar
|
alias BDS.UI.MenuBar, as: ShellMenuBar
|
||||||
alias Desktop.OS
|
alias Desktop.OS
|
||||||
@@ -25,12 +25,7 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
def mount(menu) do
|
def mount(menu) do
|
||||||
UILocale.put(ShellData.ui_language())
|
UILocale.put(ShellData.ui_language())
|
||||||
|
|
||||||
{:ok,
|
{:ok, Desktop.Menu.assign(menu, :groups, groups(native_menu_opts()))}
|
||||||
Desktop.Menu.assign(
|
|
||||||
menu,
|
|
||||||
:groups,
|
|
||||||
groups(dev_mode?: Application.get_env(:bds, :dev_routes, false))
|
|
||||||
)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@@ -59,7 +54,7 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("view_on_github", menu) do
|
def handle_event("view_on_github", menu) do
|
||||||
OS.launch_default_browser(ExternalLinks.github_url())
|
OS.launch_default_browser("https://github.com/rfc1437/bDS2")
|
||||||
{:noreply, menu}
|
{:noreply, menu}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -72,13 +67,60 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
{:noreply, menu}
|
{:noreply, menu}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_event("connect_server", menu) do
|
||||||
|
prompt =
|
||||||
|
dgettext("ui", "Server address (user@host or user@host:port), public-key auth:")
|
||||||
|
|
||||||
|
with {:ok, target} <- BDS.Desktop.Dialogs.prompt_text(prompt, "user@host"),
|
||||||
|
{:ok, url} <- BDS.Desktop.RemoteConnection.connect(target) do
|
||||||
|
Window.show(BDS.Desktop.MainWindow.window_id(), url)
|
||||||
|
else
|
||||||
|
:cancel ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
BDS.Desktop.Dialogs.alert(
|
||||||
|
dgettext("ui", "Connect to Server"),
|
||||||
|
dgettext("ui", "Connection failed: %{reason}",
|
||||||
|
reason: BDS.Desktop.RemoteConnection.format_reason(reason)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:noreply, menu}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("disconnect_server", menu) do
|
||||||
|
:ok = BDS.Desktop.RemoteConnection.disconnect()
|
||||||
|
Window.show(BDS.Desktop.MainWindow.window_id(), BDS.Desktop.url())
|
||||||
|
{:noreply, menu}
|
||||||
|
end
|
||||||
|
|
||||||
def handle_event("open_data_folder", menu) do
|
def handle_event("open_data_folder", menu) do
|
||||||
_ = BDS.Desktop.ShellCommands.execute("open_data_folder")
|
_ = BDS.Desktop.ShellCommands.execute("open_data_folder")
|
||||||
{:noreply, menu}
|
{:noreply, menu}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("report_issue", menu) do
|
def handle_event("report_issue", menu) do
|
||||||
OS.launch_default_browser(ExternalLinks.github_issues_url())
|
OS.launch_default_browser("https://github.com/rfc1437/bDS2/issues")
|
||||||
|
{:noreply, menu}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("minimize", menu) do
|
||||||
|
with_frame(&:wxTopLevelWindow.iconize(&1, iconize: true))
|
||||||
|
{:noreply, menu}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("zoom", menu) do
|
||||||
|
with_frame(fn frame ->
|
||||||
|
:wxTopLevelWindow.maximize(frame, maximize: not :wxTopLevelWindow.isMaximized(frame))
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:noreply, menu}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("bring_all_to_front", menu) do
|
||||||
|
with_frame(&:wxWindow.raise/1)
|
||||||
{:noreply, menu}
|
{:noreply, menu}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -91,12 +133,7 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
def handle_info({:set_ui_locale, locale}, menu) do
|
def handle_info({:set_ui_locale, locale}, menu) do
|
||||||
UILocale.put(locale)
|
UILocale.put(locale)
|
||||||
|
|
||||||
{:noreply,
|
{:noreply, Desktop.Menu.assign(menu, :groups, groups(native_menu_opts()))}
|
||||||
Desktop.Menu.assign(
|
|
||||||
menu,
|
|
||||||
:groups,
|
|
||||||
groups(dev_mode?: Application.get_env(:bds, :dev_routes, false))
|
|
||||||
)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info(_, menu) do
|
def handle_info(_, menu) do
|
||||||
@@ -123,6 +160,32 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp native_menu_opts do
|
||||||
|
[
|
||||||
|
dev_mode?: Application.get_env(:bds, :dev_routes, false),
|
||||||
|
window_menu?: OS.type() == MacOS
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp with_frame(fun) do
|
||||||
|
case frame() do
|
||||||
|
nil ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
frame ->
|
||||||
|
:wx.set_env(Desktop.Env.wx_env())
|
||||||
|
fun.(frame)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp frame do
|
||||||
|
try do
|
||||||
|
Window.frame(BDS.Desktop.MainWindow.window_id())
|
||||||
|
catch
|
||||||
|
:exit, _ -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp normalize_item(%{separator: true}), do: %{separator: true}
|
defp normalize_item(%{separator: true}), do: %{separator: true}
|
||||||
|
|
||||||
defp normalize_item(item) do
|
defp normalize_item(item) do
|
||||||
@@ -145,12 +208,15 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
defp group_label(:edit), do: dgettext("ui", "Edit")
|
defp group_label(:edit), do: dgettext("ui", "Edit")
|
||||||
defp group_label(:view), do: dgettext("ui", "View")
|
defp group_label(:view), do: dgettext("ui", "View")
|
||||||
defp group_label(:blog), do: dgettext("ui", "Blog")
|
defp group_label(:blog), do: dgettext("ui", "Blog")
|
||||||
|
defp group_label(:window), do: dgettext("ui", "Window")
|
||||||
defp group_label(:help), do: dgettext("ui", "Help")
|
defp group_label(:help), do: dgettext("ui", "Help")
|
||||||
|
|
||||||
defp item_label(:new_post), do: dgettext("ui", "New Post")
|
defp item_label(:new_post), do: dgettext("ui", "New Post")
|
||||||
defp item_label(:import_media), do: dgettext("ui", "Import Media")
|
defp item_label(:import_media), do: dgettext("ui", "Import Media")
|
||||||
defp item_label(:save), do: dgettext("ui", "Save")
|
defp item_label(:save), do: dgettext("ui", "Save")
|
||||||
defp item_label(:open_in_browser), do: dgettext("ui", "Open in Browser")
|
defp item_label(:open_in_browser), do: dgettext("ui", "Open in Browser")
|
||||||
|
defp item_label(:connect_server), do: dgettext("ui", "Connect to Server…")
|
||||||
|
defp item_label(:disconnect_server), do: dgettext("ui", "Disconnect from Server")
|
||||||
defp item_label(:open_data_folder), do: dgettext("ui", "Open Data Folder")
|
defp item_label(:open_data_folder), do: dgettext("ui", "Open Data Folder")
|
||||||
defp item_label(:close_tab), do: dgettext("ui", "Close Tab")
|
defp item_label(:close_tab), do: dgettext("ui", "Close Tab")
|
||||||
defp item_label(:quit), do: dgettext("ui", "Quit")
|
defp item_label(:quit), do: dgettext("ui", "Quit")
|
||||||
@@ -188,8 +254,12 @@ defmodule BDS.Desktop.MenuBar do
|
|||||||
defp item_label(:fill_missing_translations), do: dgettext("ui", "Fill Missing Translations")
|
defp item_label(:fill_missing_translations), do: dgettext("ui", "Fill Missing Translations")
|
||||||
defp item_label(:find_duplicates), do: dgettext("ui", "Find Duplicate Posts")
|
defp item_label(:find_duplicates), do: dgettext("ui", "Find Duplicate Posts")
|
||||||
defp item_label(:generate_sitemap), do: dgettext("ui", "Generate Site")
|
defp item_label(:generate_sitemap), do: dgettext("ui", "Generate Site")
|
||||||
|
defp item_label(:force_render_site), do: dgettext("ui", "Force Render Site")
|
||||||
defp item_label(:validate_site), do: dgettext("ui", "Validate Site")
|
defp item_label(:validate_site), do: dgettext("ui", "Validate Site")
|
||||||
defp item_label(:upload_site), do: dgettext("ui", "Upload Site")
|
defp item_label(:upload_site), do: dgettext("ui", "Upload Site")
|
||||||
|
defp item_label(:minimize), do: dgettext("ui", "Minimize")
|
||||||
|
defp item_label(:zoom), do: dgettext("ui", "Zoom")
|
||||||
|
defp item_label(:bring_all_to_front), do: dgettext("ui", "Bring All to Front")
|
||||||
defp item_label(:about), do: dgettext("ui", "About")
|
defp item_label(:about), do: dgettext("ui", "About")
|
||||||
defp item_label(:documentation), do: dgettext("ui", "Documentation")
|
defp item_label(:documentation), do: dgettext("ui", "Documentation")
|
||||||
defp item_label(:api_documentation), do: dgettext("ui", "API Documentation")
|
defp item_label(:api_documentation), do: dgettext("ui", "API Documentation")
|
||||||
|
|||||||
180
lib/bds/desktop/remote_connection.ex
Normal file
180
lib/bds/desktop/remote_connection.ex
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
defmodule BDS.Desktop.RemoteConnection do
|
||||||
|
@moduledoc """
|
||||||
|
GUI remote mode (issue #26, phase 5).
|
||||||
|
|
||||||
|
Connects the desktop app to a headless bDS2 server over the same SSH
|
||||||
|
channel (and the same public keys) the TUI uses: `:ssh.connect` with
|
||||||
|
public-key auth, then an OTP TCP/IP tunnel to the server's loopback HTTP
|
||||||
|
endpoint. The webview then loads the local tunnel end — the server never
|
||||||
|
exposes HTTP.
|
||||||
|
|
||||||
|
Client key material lives in the same private `ssh/` directory next to
|
||||||
|
the database (`BDS.Server.ssh_dir/0`): an `id_ed25519`/`id_rsa` identity
|
||||||
|
and a `known_hosts` written on first connect.
|
||||||
|
"""
|
||||||
|
|
||||||
|
use GenServer
|
||||||
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
@default_ssh_port 2222
|
||||||
|
@tunnel_timeout_ms 10_000
|
||||||
|
|
||||||
|
def start_link(opts \\ []) do
|
||||||
|
case Keyword.pop(opts, :name, __MODULE__) do
|
||||||
|
{nil, init_opts} -> GenServer.start_link(__MODULE__, init_opts)
|
||||||
|
{name, init_opts} -> GenServer.start_link(__MODULE__, init_opts, name: name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Parses `user@host[:port]` into connection parameters. The port is the
|
||||||
|
server's SSH port (default #{@default_ssh_port}).
|
||||||
|
"""
|
||||||
|
@spec parse_target(String.t()) ::
|
||||||
|
{:ok, %{user: String.t(), host: String.t(), port: pos_integer()}}
|
||||||
|
| {:error, :invalid_target}
|
||||||
|
def parse_target(target) when is_binary(target) do
|
||||||
|
with [user, rest] when user != "" <- String.split(String.trim(target), "@", parts: 2),
|
||||||
|
{:ok, host, port} <- parse_host_port(rest) do
|
||||||
|
{:ok, %{user: user, host: host, port: port}}
|
||||||
|
else
|
||||||
|
_other -> {:error, :invalid_target}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_host_port(rest) do
|
||||||
|
case String.split(rest, ":", parts: 2) do
|
||||||
|
[host] when host != "" ->
|
||||||
|
{:ok, host, @default_ssh_port}
|
||||||
|
|
||||||
|
[host, port] when host != "" ->
|
||||||
|
case Integer.parse(port) do
|
||||||
|
{number, ""} when number > 0 -> {:ok, host, number}
|
||||||
|
_other -> :error
|
||||||
|
end
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
:error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Connects and tunnels; returns the local URL the webview should load."
|
||||||
|
@spec connect(GenServer.server(), String.t()) :: {:ok, String.t()} | {:error, term()}
|
||||||
|
def connect(server \\ __MODULE__, target) do
|
||||||
|
GenServer.call(server, {:connect, target}, @tunnel_timeout_ms + 5_000)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec disconnect(GenServer.server()) :: :ok
|
||||||
|
def disconnect(server \\ __MODULE__) do
|
||||||
|
GenServer.call(server, :disconnect)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec status(GenServer.server()) :: :disconnected | {:connected, String.t(), String.t()}
|
||||||
|
def status(server \\ __MODULE__) do
|
||||||
|
GenServer.call(server, :status)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Human-readable connection error for menu/shell error reporting."
|
||||||
|
@spec format_reason(term()) :: String.t()
|
||||||
|
def format_reason(:invalid_target) do
|
||||||
|
dgettext("ui", "Use the form user@host or user@host:port.")
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_reason(%{message: message}) when is_binary(message), do: message
|
||||||
|
def format_reason(reason), do: inspect(reason)
|
||||||
|
|
||||||
|
## GenServer callbacks
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def init(opts) do
|
||||||
|
overrides = Keyword.get(opts, :test_overrides, [])
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
conn: nil,
|
||||||
|
monitor: nil,
|
||||||
|
target: nil,
|
||||||
|
url: nil,
|
||||||
|
connect_fun: Keyword.get(overrides, :connect_fun, &:ssh.connect/3),
|
||||||
|
tunnel_fun: Keyword.get(overrides, :tunnel_fun, &:ssh.tcpip_tunnel_to_server/6),
|
||||||
|
close_fun: Keyword.get(overrides, :close_fun, &:ssh.close/1),
|
||||||
|
ssh_dir_fun: Keyword.get(overrides, :ssh_dir_fun, &BDS.Server.ssh_dir/0)
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call({:connect, target}, _from, state) do
|
||||||
|
state = close_current(state)
|
||||||
|
|
||||||
|
with {:ok, parsed} <- parse_target(target),
|
||||||
|
{:ok, conn} <- do_connect(state, parsed),
|
||||||
|
{:ok, local_port} <- do_tunnel(state, conn) do
|
||||||
|
url = "http://127.0.0.1:#{local_port}/"
|
||||||
|
|
||||||
|
{:reply, {:ok, url},
|
||||||
|
%{
|
||||||
|
state
|
||||||
|
| conn: conn,
|
||||||
|
monitor: Process.monitor(conn),
|
||||||
|
target: String.trim(target),
|
||||||
|
url: url
|
||||||
|
}}
|
||||||
|
else
|
||||||
|
{:error, reason} = error ->
|
||||||
|
Logger.warning("Remote connection to #{inspect(target)} failed: #{inspect(reason)}")
|
||||||
|
{:reply, error, state}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_call(:disconnect, _from, state) do
|
||||||
|
{:reply, :ok, close_current(state)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_call(:status, _from, %{conn: nil} = state) do
|
||||||
|
{:reply, :disconnected, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_call(:status, _from, state) do
|
||||||
|
{:reply, {:connected, state.target, state.url}, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info({:DOWN, monitor, :process, _pid, reason}, %{monitor: monitor} = state) do
|
||||||
|
Logger.warning("Remote SSH connection lost: #{inspect(reason)}")
|
||||||
|
{:noreply, %{state | conn: nil, monitor: nil, target: nil, url: nil}}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_info(_message, state), do: {:noreply, state}
|
||||||
|
|
||||||
|
## Internal helpers
|
||||||
|
|
||||||
|
defp do_connect(state, %{user: user, host: host, port: port}) do
|
||||||
|
ssh_dir = BDS.Server.ensure_ssh_dir!(state.ssh_dir_fun.())
|
||||||
|
|
||||||
|
state.connect_fun.(String.to_charlist(host), port,
|
||||||
|
user: String.to_charlist(user),
|
||||||
|
user_dir: String.to_charlist(ssh_dir),
|
||||||
|
auth_methods: ~c"publickey",
|
||||||
|
# First connect records the host key in <ssh_dir>/known_hosts;
|
||||||
|
# later connects verify against it (trust-on-first-use).
|
||||||
|
silently_accept_hosts: true,
|
||||||
|
connect_timeout: @tunnel_timeout_ms
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_tunnel(state, conn) do
|
||||||
|
remote_port = Application.get_env(:bds, :desktop)[:port] || 4010
|
||||||
|
|
||||||
|
state.tunnel_fun.(conn, ~c"127.0.0.1", 0, ~c"127.0.0.1", remote_port, @tunnel_timeout_ms)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp close_current(%{conn: nil} = state), do: state
|
||||||
|
|
||||||
|
defp close_current(state) do
|
||||||
|
if state.monitor, do: Process.demonitor(state.monitor, [:flush])
|
||||||
|
_ = state.close_fun.(state.conn)
|
||||||
|
%{state | conn: nil, monitor: nil, target: nil, url: nil}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -19,6 +19,7 @@ defmodule BDS.Desktop.Router do
|
|||||||
|
|
||||||
get("/health", HealthController, :show)
|
get("/health", HealthController, :show)
|
||||||
get("/media-thumbnail/:media_id", MediaController, :thumbnail)
|
get("/media-thumbnail/:media_id", MediaController, :thumbnail)
|
||||||
|
get("/media-file/:media_id", MediaController, :file)
|
||||||
|
|
||||||
live_session :desktop_shell,
|
live_session :desktop_shell,
|
||||||
root_layout: {BDS.Desktop.Layouts, :root} do
|
root_layout: {BDS.Desktop.Layouts, :root} do
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
defmodule BDS.Desktop.Server do
|
defmodule BDS.Desktop.Server do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
use GenServer
|
|
||||||
|
|
||||||
def child_spec(opts) do
|
def child_spec(opts) do
|
||||||
%{
|
Supervisor.child_spec(
|
||||||
id: __MODULE__,
|
{Bandit,
|
||||||
start: {__MODULE__, :start_link, [opts]}
|
Keyword.merge(opts,
|
||||||
}
|
plug: BDS.Desktop.Endpoint,
|
||||||
end
|
scheme: :http,
|
||||||
|
ip: {127, 0, 0, 1},
|
||||||
def start_link(opts) do
|
port: port(),
|
||||||
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
startup_log: false
|
||||||
|
)},
|
||||||
|
id: __MODULE__
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def url do
|
def url do
|
||||||
@@ -24,18 +25,4 @@ defmodule BDS.Desktop.Server do
|
|||||||
_other -> Application.get_env(:bds, :desktop)[:port] || 4010
|
_other -> Application.get_env(:bds, :desktop)[:port] || 4010
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
|
||||||
def init(_opts) do
|
|
||||||
{:ok, bandit_pid} =
|
|
||||||
Bandit.start_link(
|
|
||||||
plug: BDS.Desktop.Endpoint,
|
|
||||||
scheme: :http,
|
|
||||||
ip: {127, 0, 0, 1},
|
|
||||||
port: port(),
|
|
||||||
startup_log: false
|
|
||||||
)
|
|
||||||
|
|
||||||
{:ok, %{bandit_pid: bandit_pid}}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
"rebuild_embedding_index",
|
"rebuild_embedding_index",
|
||||||
"Rebuild Embedding Index",
|
"Rebuild Embedding Index",
|
||||||
"Embeddings",
|
"Embeddings",
|
||||||
fn report -> rebuild_embedding_index_work(project, report) end
|
fn report -> Maintenance.rebuild_embedding_index(project.id, on_progress: report) end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -196,6 +196,14 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp dispatch("rebuild_media_links", project, _params) do
|
||||||
|
queue_task(project, "rebuild_media_links", "Rebuild Media Links", "Maintenance", fn report ->
|
||||||
|
{:ok, result} = BDS.Media.rebuild_media_links(project.id, on_progress: report)
|
||||||
|
report.(1.0, "Media links rebuilt")
|
||||||
|
%{project_id: project.id, counts: %{media_links: result.links}}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
defp dispatch("regenerate_missing_thumbnails", project, _params) do
|
defp dispatch("regenerate_missing_thumbnails", project, _params) do
|
||||||
queue_task(
|
queue_task(
|
||||||
project,
|
project,
|
||||||
@@ -213,7 +221,7 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
defp dispatch("rebuild_database", project, _params) do
|
defp dispatch("rebuild_database", project, _params) do
|
||||||
group_id = task_group_id("rebuild_database")
|
group_id = task_group_id("rebuild_database")
|
||||||
attrs = %{group_id: group_id, group_name: "Maintenance"}
|
attrs = %{group_id: group_id, group_name: "Maintenance"}
|
||||||
[first_step | remaining_steps] = rebuild_database_steps(project)
|
[first_step | remaining_steps] = Maintenance.full_rebuild_steps(project.id)
|
||||||
|
|
||||||
{:ok, posts_task} =
|
{:ok, posts_task} =
|
||||||
Tasks.submit_task(first_step.name, first_step.work, attrs)
|
Tasks.submit_task(first_step.name, first_step.work, attrs)
|
||||||
@@ -239,18 +247,13 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp dispatch("generate_sitemap", project, _params) do
|
defp dispatch("generate_sitemap", project, _params) do
|
||||||
queue_task(project, "generate_sitemap", "Generate Site", "Generation", fn report ->
|
queue_render_site(project, "generate_sitemap", "Render Site", [])
|
||||||
{:ok, generation} =
|
end
|
||||||
Generation.generate_site(project.id, @site_sections, on_progress: report)
|
|
||||||
|
|
||||||
report.(1.0, "Generated site output")
|
# Forced variant of the site render: ignores the stored content hashes and
|
||||||
|
# rewrites every output file, repairing any drift on disk.
|
||||||
%{
|
defp dispatch("force_render_site", project, _params) do
|
||||||
project_id: project.id,
|
queue_render_site(project, "force_render_site", "Force Render Site", force: true)
|
||||||
sections: generation.sections,
|
|
||||||
generated_count: length(generation.generated_files)
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp dispatch("validate_site", project, _params) do
|
defp dispatch("validate_site", project, _params) do
|
||||||
@@ -262,6 +265,42 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp dispatch("apply_site_validation", project, params) do
|
||||||
|
validation_report = normalize_apply_validation_report(BDS.MapUtils.attr(params, :report, %{}))
|
||||||
|
group_id = task_group_id("apply_site_validation")
|
||||||
|
attrs = %{group_id: group_id, group_name: "Apply Site Validation"}
|
||||||
|
|
||||||
|
{:ok, prepare_task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
"Prepare Validation Apply",
|
||||||
|
fn report ->
|
||||||
|
{:ok, preparation} =
|
||||||
|
Generation.prepare_validation_apply(project.id, validation_report,
|
||||||
|
on_progress: report
|
||||||
|
)
|
||||||
|
|
||||||
|
Map.put(preparation, :project_id, project.id)
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
Task.start(fn ->
|
||||||
|
run_apply_validation_sequence(project, validation_report, group_id, attrs, prepare_task.id)
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
kind: "task_queued",
|
||||||
|
action: "apply_site_validation",
|
||||||
|
title: "Apply Site Validation",
|
||||||
|
message: "Apply Site Validation tasks queued",
|
||||||
|
project_id: project.id,
|
||||||
|
task_id: prepare_task.id,
|
||||||
|
task_group_id: group_id,
|
||||||
|
panel_tab: "tasks"
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
defp dispatch("metadata_diff", project, _params) do
|
defp dispatch("metadata_diff", project, _params) do
|
||||||
queue_task(project, "metadata_diff", "Metadata Diff", "Maintenance", fn report ->
|
queue_task(project, "metadata_diff", "Metadata Diff", "Maintenance", fn report ->
|
||||||
{:ok, metadata_diff} = Maintenance.metadata_diff(project.id, on_progress: report)
|
{:ok, metadata_diff} = Maintenance.metadata_diff(project.id, on_progress: report)
|
||||||
@@ -439,6 +478,207 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp queue_render_site(project, action, title, render_opts) do
|
||||||
|
group_id = task_group_id(action)
|
||||||
|
attrs = %{group_id: group_id, group_name: title}
|
||||||
|
|
||||||
|
{:ok, first_task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
section_task_name(:core),
|
||||||
|
fn report ->
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.render_site_section(
|
||||||
|
project.id,
|
||||||
|
:core,
|
||||||
|
[on_progress: report] ++ render_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
report.(1.0, section_complete_message(:core))
|
||||||
|
:ok
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
Task.start(fn -> run_render_site_sequence(project, group_id, attrs, render_opts) end)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
kind: "task_queued",
|
||||||
|
action: action,
|
||||||
|
title: title,
|
||||||
|
message: title <> " tasks queued",
|
||||||
|
project_id: project.id,
|
||||||
|
task_id: first_task.id,
|
||||||
|
task_group_id: group_id,
|
||||||
|
panel_tab: "tasks"
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp run_render_site_sequence(project, group_id, attrs, render_opts) do
|
||||||
|
remaining_sections = [:single, :category, :tag, :date]
|
||||||
|
|
||||||
|
Enum.each(remaining_sections, fn section ->
|
||||||
|
{:ok, _task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
section_task_name(section),
|
||||||
|
fn report ->
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.render_site_section(
|
||||||
|
project.id,
|
||||||
|
section,
|
||||||
|
[on_progress: report] ++ render_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
report.(1.0, section_complete_message(section))
|
||||||
|
:ok
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
section_names = Enum.map([:core | remaining_sections], §ion_task_name/1)
|
||||||
|
|
||||||
|
with :ok <- wait_for_group_phase(group_id, section_names, @rebuild_phase_timeout) do
|
||||||
|
{:ok, _task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
"Build Search Index",
|
||||||
|
fn report ->
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.build_site_search_index(
|
||||||
|
project.id,
|
||||||
|
[on_progress: report] ++ render_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
report.(1.0, "Build Search Index complete")
|
||||||
|
:ok
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
wait_for_group_phase(group_id, ["Build Search Index"], @rebuild_phase_timeout)
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp run_apply_validation_sequence(project, validation_report, group_id, attrs, prepare_task_id) do
|
||||||
|
with :ok <-
|
||||||
|
wait_for_group_phase(group_id, ["Prepare Validation Apply"], @rebuild_phase_timeout),
|
||||||
|
{:ok, preparation} <- completed_task_result(prepare_task_id),
|
||||||
|
:ok <-
|
||||||
|
run_apply_validation_render_tasks(
|
||||||
|
project,
|
||||||
|
validation_report,
|
||||||
|
group_id,
|
||||||
|
attrs,
|
||||||
|
preparation
|
||||||
|
) do
|
||||||
|
if validation_apply_changed?(preparation) do
|
||||||
|
:ok = run_apply_validation_refresh_task(project, group_id, attrs, :calendar)
|
||||||
|
end
|
||||||
|
|
||||||
|
submit_apply_validation_check_task(project, attrs)
|
||||||
|
else
|
||||||
|
_other -> :ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp run_apply_validation_render_tasks(project, validation_report, group_id, attrs, preparation) do
|
||||||
|
sections = Map.get(preparation, :sections_to_render, [])
|
||||||
|
task_names = Enum.map(sections, §ion_task_name/1)
|
||||||
|
|
||||||
|
Enum.each(sections, fn section ->
|
||||||
|
task_name = section_task_name(section)
|
||||||
|
|
||||||
|
{:ok, _task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
task_name,
|
||||||
|
fn report ->
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.apply_validation_section(project.id, validation_report, section,
|
||||||
|
on_progress: report
|
||||||
|
)
|
||||||
|
|
||||||
|
report.(1.0, section_complete_message(section))
|
||||||
|
:ok
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
case task_names do
|
||||||
|
[] -> :ok
|
||||||
|
names -> wait_for_group_phase(group_id, names, @rebuild_phase_timeout)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp run_apply_validation_refresh_task(project, group_id, attrs, kind) do
|
||||||
|
task_name = apply_validation_refresh_task_name(kind)
|
||||||
|
|
||||||
|
{:ok, _task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
task_name,
|
||||||
|
fn report ->
|
||||||
|
{:ok, _result} =
|
||||||
|
Generation.refresh_validation_ancillary_outputs(project.id, kind, on_progress: report)
|
||||||
|
|
||||||
|
report.(1.0, apply_validation_refresh_complete_message(kind))
|
||||||
|
:ok
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
wait_for_group_phase(group_id, [task_name], @rebuild_phase_timeout)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp submit_apply_validation_check_task(project, attrs) do
|
||||||
|
{:ok, _task} =
|
||||||
|
Tasks.submit_task(
|
||||||
|
"Validate Site",
|
||||||
|
fn report ->
|
||||||
|
{:ok, validation} =
|
||||||
|
Generation.validate_site(project.id, @site_sections, on_progress: report)
|
||||||
|
|
||||||
|
site_validation_result(project.id, validation)
|
||||||
|
end,
|
||||||
|
attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp completed_task_result(task_id) do
|
||||||
|
case Tasks.get_task(task_id) do
|
||||||
|
%{status: :completed, result: result} when is_map(result) -> {:ok, result}
|
||||||
|
%{status: :failed, error: error} -> {:error, error}
|
||||||
|
_other -> {:error, :task_result_unavailable}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp validation_apply_changed?(preparation) do
|
||||||
|
Map.get(preparation, :sections_to_render, []) != [] or
|
||||||
|
Map.get(preparation, :deleted_url_count, 0) > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
defp section_task_name(:core), do: "Render Site Core"
|
||||||
|
defp section_task_name(:single), do: "Render Single Posts"
|
||||||
|
defp section_task_name(:category), do: "Render Category Archives"
|
||||||
|
defp section_task_name(:tag), do: "Render Tag Archives"
|
||||||
|
defp section_task_name(:date), do: "Render Date Archives"
|
||||||
|
|
||||||
|
defp section_complete_message(:core), do: "Render Site Core complete"
|
||||||
|
defp section_complete_message(:single), do: "Render Single Posts complete"
|
||||||
|
|
||||||
|
defp section_complete_message(:category),
|
||||||
|
do: "Render Category Archives complete"
|
||||||
|
|
||||||
|
defp section_complete_message(:tag), do: "Render Tag Archives complete"
|
||||||
|
defp section_complete_message(:date), do: "Render Date Archives complete"
|
||||||
|
|
||||||
|
defp apply_validation_refresh_task_name(:calendar), do: "Regenerate Calendar"
|
||||||
|
|
||||||
|
defp apply_validation_refresh_complete_message(:calendar), do: "Regenerate Calendar complete"
|
||||||
|
|
||||||
defp translation_fill_enabled?(metadata) do
|
defp translation_fill_enabled?(metadata) do
|
||||||
([metadata.main_language] ++ metadata.blog_languages)
|
([metadata.main_language] ++ metadata.blog_languages)
|
||||||
|> Enum.map(fn language ->
|
|> Enum.map(fn language ->
|
||||||
@@ -452,102 +692,6 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
|> length() > 1
|
|> length() > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
defp rebuild_database_steps(project) do
|
|
||||||
[
|
|
||||||
%{
|
|
||||||
name: "Rebuild Posts From Files",
|
|
||||||
work: fn report ->
|
|
||||||
{:ok, posts} =
|
|
||||||
Maintenance.rebuild_from_filesystem(project.id, "post",
|
|
||||||
on_progress: report,
|
|
||||||
rebuild_embeddings: false
|
|
||||||
)
|
|
||||||
|
|
||||||
report.(1.0, "Post rebuild complete")
|
|
||||||
%{project_id: project.id, counts: %{posts: length(posts)}}
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Rebuild Media From Files",
|
|
||||||
work: fn report ->
|
|
||||||
{:ok, media} =
|
|
||||||
Maintenance.rebuild_from_filesystem(project.id, "media", on_progress: report)
|
|
||||||
|
|
||||||
report.(1.0, "Media rebuild complete")
|
|
||||||
%{project_id: project.id, counts: %{media: length(media)}}
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Rebuild Scripts From Files",
|
|
||||||
work: fn report ->
|
|
||||||
{:ok, scripts} =
|
|
||||||
Maintenance.rebuild_from_filesystem(project.id, "script", on_progress: report)
|
|
||||||
|
|
||||||
report.(1.0, "Script rebuild complete")
|
|
||||||
%{project_id: project.id, counts: %{scripts: length(scripts)}}
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Rebuild Templates From Files",
|
|
||||||
work: fn report ->
|
|
||||||
{:ok, templates} =
|
|
||||||
Maintenance.rebuild_from_filesystem(project.id, "template", on_progress: report)
|
|
||||||
|
|
||||||
report.(1.0, "Template rebuild complete")
|
|
||||||
%{project_id: project.id, counts: %{templates: length(templates)}}
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Rebuild Post Links",
|
|
||||||
work: fn report ->
|
|
||||||
:ok = Posts.rebuild_post_links(project.id, on_progress: report)
|
|
||||||
report.(1.0, "Post links rebuilt")
|
|
||||||
%{project_id: project.id}
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Regenerate Missing Thumbnails",
|
|
||||||
work: fn report ->
|
|
||||||
result = BDS.Media.regenerate_missing_thumbnails(project.id, on_progress: report)
|
|
||||||
report.(1.0, "Missing thumbnails regenerated")
|
|
||||||
Map.put(result, :project_id, project.id)
|
|
||||||
end
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
name: "Rebuild Embedding Index",
|
|
||||||
work: fn report -> rebuild_embedding_index_work(project, report) end
|
|
||||||
}
|
|
||||||
]
|
|
||||||
end
|
|
||||||
|
|
||||||
defp rebuild_embedding_index_work(project, report) do
|
|
||||||
case Embeddings.rebuild_project(project.id, on_progress: report) do
|
|
||||||
{:ok, rebuilt_post_ids} ->
|
|
||||||
report.(1.0, "Embedding index rebuilt")
|
|
||||||
|
|
||||||
%{
|
|
||||||
project_id: project.id,
|
|
||||||
rebuilt_post_ids: rebuilt_post_ids,
|
|
||||||
rebuilt_count: length(rebuilt_post_ids)
|
|
||||||
}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
|
||||||
{:error, embedding_error_message(reason)}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp embedding_error_message(reason) do
|
|
||||||
detail =
|
|
||||||
case reason do
|
|
||||||
message when is_binary(message) -> message
|
|
||||||
{:embedding_backend_unavailable, _inner} -> "the embedding service did not start"
|
|
||||||
other -> inspect(other)
|
|
||||||
end
|
|
||||||
|
|
||||||
"Could not build the embedding index: #{detail}. The model is downloaded on first use, " <>
|
|
||||||
"so check your internet connection — or turn off semantic similarity in Settings."
|
|
||||||
end
|
|
||||||
|
|
||||||
defp run_rebuild_sequence(_group_id, _attrs, []), do: :ok
|
defp run_rebuild_sequence(_group_id, _attrs, []), do: :ok
|
||||||
|
|
||||||
defp run_rebuild_sequence(group_id, attrs, [step | remaining_steps]) do
|
defp run_rebuild_sequence(group_id, attrs, [step | remaining_steps]) do
|
||||||
@@ -559,27 +703,62 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp wait_for_group_phase(_group_id, _names, timeout) when timeout <= 0, do: :timeout
|
|
||||||
|
|
||||||
defp wait_for_group_phase(group_id, names, timeout) do
|
defp wait_for_group_phase(group_id, names, timeout) do
|
||||||
|
if timeout <= 0 do
|
||||||
|
:timeout
|
||||||
|
else
|
||||||
|
Phoenix.PubSub.subscribe(BDS.PubSub, Tasks.topic())
|
||||||
|
|
||||||
|
try do
|
||||||
|
case group_phase_status(group_id, names) do
|
||||||
|
:waiting -> wait_for_group_phase_message(group_id, names, timeout)
|
||||||
|
status -> status
|
||||||
|
end
|
||||||
|
after
|
||||||
|
Phoenix.PubSub.unsubscribe(BDS.PubSub, Tasks.topic())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp wait_for_group_phase_message(group_id, names, timeout) do
|
||||||
|
started_at = System.monotonic_time(:millisecond)
|
||||||
|
|
||||||
|
receive do
|
||||||
|
{:task_terminal, task} ->
|
||||||
|
elapsed = System.monotonic_time(:millisecond) - started_at
|
||||||
|
|
||||||
|
cond do
|
||||||
|
task.group_id == group_id and task.name in names and task.status == :failed ->
|
||||||
|
:failed
|
||||||
|
|
||||||
|
task.group_id == group_id and task.name in names ->
|
||||||
|
case group_phase_status(group_id, names) do
|
||||||
|
:waiting ->
|
||||||
|
wait_for_group_phase_message(group_id, names, timeout - elapsed)
|
||||||
|
|
||||||
|
status ->
|
||||||
|
status
|
||||||
|
end
|
||||||
|
|
||||||
|
true ->
|
||||||
|
wait_for_group_phase_message(group_id, names, timeout - elapsed)
|
||||||
|
end
|
||||||
|
after
|
||||||
|
timeout ->
|
||||||
|
:timeout
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp group_phase_status(group_id, names) do
|
||||||
tasks =
|
tasks =
|
||||||
BDS.Tasks.list_tasks()
|
BDS.Tasks.list_tasks()
|
||||||
|> Enum.filter(&(&1.group_id == group_id and &1.name in names))
|
|> Enum.filter(&(&1.group_id == group_id and &1.name in names))
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
length(tasks) < length(names) ->
|
length(tasks) < length(names) -> :waiting
|
||||||
Process.sleep(50)
|
Enum.any?(tasks, &(&1.status == :failed)) -> :failed
|
||||||
wait_for_group_phase(group_id, names, timeout - 50)
|
Enum.all?(tasks, &(&1.status == :completed)) -> :ok
|
||||||
|
true -> :waiting
|
||||||
Enum.any?(tasks, &(&1.status == :failed)) ->
|
|
||||||
:failed
|
|
||||||
|
|
||||||
Enum.all?(tasks, &(&1.status == :completed)) ->
|
|
||||||
:ok
|
|
||||||
|
|
||||||
true ->
|
|
||||||
Process.sleep(50)
|
|
||||||
wait_for_group_phase(group_id, names, timeout - 50)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -605,6 +784,20 @@ defmodule BDS.Desktop.ShellCommands do
|
|||||||
"http://#{server.host}:#{server.port}/"
|
"http://#{server.host}:#{server.port}/"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp normalize_apply_validation_report(report) when is_map(report) do
|
||||||
|
%{
|
||||||
|
sitemap_path: BDS.MapUtils.attr(report, :sitemap_path),
|
||||||
|
sitemap_changed: BDS.MapUtils.attr(report, :sitemap_changed, false),
|
||||||
|
missing_url_paths: BDS.MapUtils.attr(report, :missing_url_paths, []),
|
||||||
|
extra_url_paths: BDS.MapUtils.attr(report, :extra_url_paths, []),
|
||||||
|
updated_post_url_paths: BDS.MapUtils.attr(report, :updated_post_url_paths, []),
|
||||||
|
expected_url_count: BDS.MapUtils.attr(report, :expected_url_count, 0),
|
||||||
|
existing_html_url_count: BDS.MapUtils.attr(report, :existing_html_url_count, 0)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_apply_validation_report(_report), do: %{}
|
||||||
|
|
||||||
defp normalize_site_validation(report) do
|
defp normalize_site_validation(report) do
|
||||||
%{
|
%{
|
||||||
sitemap_path: report.sitemap_path,
|
sitemap_path: report.sitemap_path,
|
||||||
|
|||||||
@@ -91,32 +91,16 @@ defmodule BDS.Desktop.ShellData do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def assistant_cards do
|
# Accepts the caller's already-resolved locale so hot render paths never
|
||||||
[
|
# re-read the ui.language setting from the database.
|
||||||
%{
|
def editor_meta(task_status, ui_language \\ nil) do
|
||||||
label: dgettext("ui", "Offline Gate"),
|
|
||||||
text: dgettext("ui", "Automatic AI actions stay gated by airplane mode.")
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
label: dgettext("ui", "Filesystem Sync"),
|
|
||||||
text:
|
|
||||||
dgettext("ui", "Metadata flush, diffing, and rebuild hooks still need editor wiring.")
|
|
||||||
},
|
|
||||||
%{
|
|
||||||
label: dgettext("ui", "Desktop Runtime"),
|
|
||||||
text: dgettext("ui", "The app window is now served from LiveView state.")
|
|
||||||
}
|
|
||||||
]
|
|
||||||
end
|
|
||||||
|
|
||||||
def editor_meta(task_status) do
|
|
||||||
[
|
[
|
||||||
%{
|
%{
|
||||||
label: dgettext("ui", "Status"),
|
label: dgettext("ui", "Status"),
|
||||||
value: task_status.running_task_message || dgettext("ui", "Idle")
|
value: task_status.running_task_message || dgettext("ui", "Idle")
|
||||||
},
|
},
|
||||||
%{label: dgettext("ui", "Mode"), value: dgettext("ui", "Offline")},
|
%{label: dgettext("ui", "Mode"), value: dgettext("ui", "Offline")},
|
||||||
%{label: dgettext("ui", "Main Language"), value: ui_language()}
|
%{label: dgettext("ui", "Main Language"), value: ui_language || ui_language()}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -125,7 +109,7 @@ defmodule BDS.Desktop.ShellData do
|
|||||||
post_count: dashboard.post_stats.total_posts,
|
post_count: dashboard.post_stats.total_posts,
|
||||||
media_count: dashboard.media_stats.media_count,
|
media_count: dashboard.media_stats.media_count,
|
||||||
theme_badge: "desktop-shell",
|
theme_badge: "desktop-shell",
|
||||||
ui_language: Keyword.get(opts, :ui_language, ui_language()),
|
ui_language: Keyword.get_lazy(opts, :ui_language, &ui_language/0),
|
||||||
offline_mode: Keyword.get(opts, :offline_mode, true),
|
offline_mode: Keyword.get(opts, :offline_mode, true),
|
||||||
running_task_message: task_status.running_task_message,
|
running_task_message: task_status.running_task_message,
|
||||||
running_task_overflow: task_status.running_task_overflow,
|
running_task_overflow: task_status.running_task_overflow,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,11 +5,11 @@ defmodule BDS.Desktop.ShellLive.Bridges do
|
|||||||
import Phoenix.LiveView, only: [connected?: 1, send_update: 2]
|
import Phoenix.LiveView, only: [connected?: 1, send_update: 2]
|
||||||
|
|
||||||
alias BDS.Desktop.ShellData
|
alias BDS.Desktop.ShellData
|
||||||
alias BDS.Desktop.ShellLive.{ChatEditor, PostEditor}
|
alias BDS.Desktop.ShellLive.{ChatEditor, MediaEditor, PostEditor}
|
||||||
alias BDS.Desktop.ShellLive.{CliSync, SessionUtil}
|
alias BDS.Desktop.ShellLive.{CliSync, SessionUtil}
|
||||||
alias BDS.UI.Workbench
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
@refreshable_tab_meta_types [:import, :chat]
|
@refreshable_tab_meta_types [:import, :chat, :post, :media]
|
||||||
|
|
||||||
@spec handle_info(tuple() | atom(), Phoenix.LiveView.Socket.t(), map()) ::
|
@spec handle_info(tuple() | atom(), Phoenix.LiveView.Socket.t(), map()) ::
|
||||||
{:noreply, Phoenix.LiveView.Socket.t()}
|
{:noreply, Phoenix.LiveView.Socket.t()}
|
||||||
@@ -216,6 +216,26 @@ defmodule BDS.Desktop.ShellLive.Bridges do
|
|||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_info({:editor_translation_completed, :post, post_id, language}, socket, _callbacks) do
|
||||||
|
send_update(PostEditor,
|
||||||
|
id: "post-editor-#{post_id}",
|
||||||
|
action: :translation_completed,
|
||||||
|
language: language
|
||||||
|
)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_info({:editor_translation_completed, :media, media_id, language}, socket, _callbacks) do
|
||||||
|
send_update(MediaEditor,
|
||||||
|
id: "media-editor-#{media_id}",
|
||||||
|
action: :translation_completed,
|
||||||
|
language: language
|
||||||
|
)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def handle_info({:post_editor_apply_ai_suggestions, post_id, fields}, socket, _callbacks) do
|
def handle_info({:post_editor_apply_ai_suggestions, post_id, fields}, socket, _callbacks) do
|
||||||
send_update(PostEditor,
|
send_update(PostEditor,
|
||||||
id: "post-editor-#{post_id}",
|
id: "post-editor-#{post_id}",
|
||||||
@@ -246,7 +266,7 @@ defmodule BDS.Desktop.ShellLive.Bridges do
|
|||||||
|
|
||||||
socket
|
socket
|
||||||
|> assign(:task_status, task_status)
|
|> assign(:task_status, task_status)
|
||||||
|> assign(:editor_meta, ShellData.editor_meta(task_status))
|
|> assign(:editor_meta, ShellData.editor_meta(task_status, socket.assigns.page_language))
|
||||||
|> assign(
|
|> assign(
|
||||||
:status,
|
:status,
|
||||||
ShellData.status_bar(
|
ShellData.status_bar(
|
||||||
|
|||||||
@@ -90,12 +90,10 @@ defmodule BDS.Desktop.ShellLive.ChatEditor do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("send_chat_editor_message", _params, socket) do
|
def handle_event("send_chat_editor_message", _params, socket) do
|
||||||
Logger.info("CHAT send_chat_editor_message called, input=#{inspect(socket.assigns.input)}")
|
|
||||||
{:noreply, do_send_message(socket)}
|
{:noreply, do_send_message(socket)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("abort_chat_editor_message", _params, socket) do
|
def handle_event("abort_chat_editor_message", _params, socket) do
|
||||||
Logger.info("CHAT abort_chat_editor_message called")
|
|
||||||
{:noreply, do_abort_message(socket)}
|
{:noreply, do_abort_message(socket)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -545,10 +543,15 @@ defmodule BDS.Desktop.ShellLive.ChatEditor do
|
|||||||
|
|
||||||
@spec markdown_html(binary()) :: Phoenix.HTML.Safe.t()
|
@spec markdown_html(binary()) :: Phoenix.HTML.Safe.t()
|
||||||
def markdown_html(content) when is_binary(content) do
|
def markdown_html(content) when is_binary(content) do
|
||||||
|
# Match Earmark's defaults (GFM tables, strikethrough, autolinks); escape
|
||||||
|
# raw HTML to text rather than rendering it, as the old escape: true did.
|
||||||
html =
|
html =
|
||||||
case Earmark.as_html(content, escape: true) do
|
case MDEx.to_html(content,
|
||||||
{:ok, rendered, _messages} -> rendered
|
extension: [table: true, strikethrough: true, autolink: true],
|
||||||
{:error, rendered, _messages} -> rendered
|
render: [escape: true]
|
||||||
|
) do
|
||||||
|
{:ok, rendered} -> rendered
|
||||||
|
{:error, _reason} -> ""
|
||||||
end
|
end
|
||||||
|> rewrite_external_images()
|
|> rewrite_external_images()
|
||||||
|
|
||||||
@@ -576,8 +579,7 @@ defmodule BDS.Desktop.ShellLive.ChatEditor do
|
|||||||
def chart_width(_max_value, _value), do: 0
|
def chart_width(_max_value, _value), do: 0
|
||||||
|
|
||||||
@spec truthy?(term()) :: boolean()
|
@spec truthy?(term()) :: boolean()
|
||||||
def truthy?(value) when value in [true, "true", 1, "1", "on"], do: true
|
def truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
def truthy?(_value), do: false
|
|
||||||
|
|
||||||
# ── HEEx components ───────────────────────────────────────────────────────
|
# ── HEEx components ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -991,7 +993,9 @@ defmodule BDS.Desktop.ShellLive.ChatEditor do
|
|||||||
try do
|
try do
|
||||||
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
||||||
rescue
|
rescue
|
||||||
_error -> :ok
|
error ->
|
||||||
|
Logger.debug("swallowed chat_editor allow_repo_sandbox error: #{inspect(error)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
:ok
|
:ok
|
||||||
|
|||||||
@@ -303,6 +303,5 @@ defmodule BDS.Desktop.ShellLive.ChatEditor.ToolSurfaces do
|
|||||||
|
|
||||||
defp map_value(_map, _key, default), do: default
|
defp map_value(_map, _key, default), do: default
|
||||||
|
|
||||||
defp truthy?(value) when value in [true, "true", 1, "1", "on"], do: true
|
defp truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
defp truthy?(_value), do: false
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -62,7 +62,7 @@
|
|||||||
<h2><%= dgettext("ui", "API Key Required") %></h2>
|
<h2><%= dgettext("ui", "API Key Required") %></h2>
|
||||||
<p><%= dgettext("ui", "Configure an API key in Settings to enable AI chat.") %></p>
|
<p><%= dgettext("ui", "Configure an API key in Settings to enable AI chat.") %></p>
|
||||||
<div class="api-key-form">
|
<div class="api-key-form">
|
||||||
<button class="api-key-submit ui-button ui-button-primary" type="button" phx-click="open_chat_settings" phx-target={@myself}><%= dgettext("ui", "Open Settings") %></button>
|
<button class="api-key-submit primary ui-button ui-button-primary" type="button" phx-click="open_chat_settings" phx-target={@myself}><%= dgettext("ui", "Open Settings") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<% else %>
|
<% else %>
|
||||||
@@ -154,7 +154,7 @@
|
|||||||
|
|
||||||
<form class="chat-input-wrapper flex items-end gap-2" phx-change="change_chat_editor_input" phx-submit="send_chat_editor_message" phx-target={@myself}>
|
<form class="chat-input-wrapper flex items-end gap-2" phx-change="change_chat_editor_input" phx-submit="send_chat_editor_message" phx-target={@myself}>
|
||||||
<textarea class="chat-input chat-surface-input ui-textarea" name="message" rows="1" placeholder={dgettext("ui", "Type a message...")} disabled={@chat_editor.is_streaming}><%= @chat_editor.input %></textarea>
|
<textarea class="chat-input chat-surface-input ui-textarea" name="message" rows="1" placeholder={dgettext("ui", "Type a message...")} disabled={@chat_editor.is_streaming}><%= @chat_editor.input %></textarea>
|
||||||
<button class="chat-send-button ui-button ui-button-primary" data-testid="chat-send-button" type="button" phx-click="send_chat_editor_message" phx-target={@myself}>↑</button>
|
<button class="chat-send-button primary ui-button ui-button-primary" data-testid="chat-send-button" type="button" phx-click="send_chat_editor_message" phx-target={@myself}>↑</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<%= if @chat_editor.action_error do %>
|
<%= if @chat_editor.action_error do %>
|
||||||
|
|||||||
82
lib/bds/desktop/shell_live/content_state.ex
Normal file
82
lib/bds/desktop/shell_live/content_state.ex
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.ContentState do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.AI
|
||||||
|
alias BDS.Desktop.ShellData
|
||||||
|
alias BDS.Desktop.ShellLive.{SidebarState, SocketState, TabHelpers, TaskLocalization, TitlebarMenu}
|
||||||
|
|
||||||
|
def refresh_content(socket, workbench) do
|
||||||
|
projects =
|
||||||
|
case ShellData.project_snapshot() do
|
||||||
|
{:ok, data} -> data
|
||||||
|
{:error, :not_ready} -> ShellData.default_project_snapshot()
|
||||||
|
end
|
||||||
|
|
||||||
|
dashboard =
|
||||||
|
case ShellData.dashboard(projects.active_project_id) do
|
||||||
|
{:ok, data} -> data
|
||||||
|
{:error, :not_ready} -> BDS.UI.Dashboard.empty_snapshot()
|
||||||
|
end
|
||||||
|
|
||||||
|
git_badge_count =
|
||||||
|
case ShellData.git_badge_count(projects.active_project_id) do
|
||||||
|
{:ok, count} -> count
|
||||||
|
{:error, :not_ready} -> 0
|
||||||
|
end
|
||||||
|
|
||||||
|
active_view_id = Atom.to_string(workbench.active_view)
|
||||||
|
|
||||||
|
sidebar_data =
|
||||||
|
case ShellData.sidebar_view(
|
||||||
|
projects.active_project_id,
|
||||||
|
active_view_id,
|
||||||
|
SidebarState.current_filters(socket, active_view_id)
|
||||||
|
) do
|
||||||
|
{:ok, data} -> data
|
||||||
|
{:error, :not_ready} -> BDS.UI.Sidebar.view(nil, active_view_id, %{})
|
||||||
|
end
|
||||||
|
|
||||||
|
sidebar_data = SidebarState.merge_ui_state(socket, active_view_id, sidebar_data)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:projects, projects)
|
||||||
|
|> assign(:current_project, ShellData.current_project(projects))
|
||||||
|
|> assign(:dashboard, dashboard)
|
||||||
|
|> assign(:dashboard_timeline_entries, Map.get(dashboard, :timeline_entries, []))
|
||||||
|
|> assign(:dashboard_category_counts, Map.get(dashboard, :category_counts, []))
|
||||||
|
|> assign(:dashboard_recent_posts, Map.get(dashboard, :recent_posts, []))
|
||||||
|
|> assign(
|
||||||
|
:dashboard_tag_cloud_items,
|
||||||
|
ShellData.dashboard_tag_cloud_items(Map.get(dashboard, :tag_cloud_items, []))
|
||||||
|
)
|
||||||
|
|> assign(:git_badge_count, git_badge_count)
|
||||||
|
|> assign(:sidebar_data, sidebar_data)
|
||||||
|
|> SocketState.refresh_layout(workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reload_shell(socket, workbench) do
|
||||||
|
tab_meta = TabHelpers.sync_tab_meta(workbench, socket.assigns[:tab_meta] || %{})
|
||||||
|
raw_task_status = BDS.Tasks.status_snapshot()
|
||||||
|
page_language = socket.assigns[:page_language] || ShellData.ui_language()
|
||||||
|
|
||||||
|
offline_mode =
|
||||||
|
if Phoenix.LiveView.connected?(socket) do
|
||||||
|
Map.get(socket.assigns, :offline_mode, AI.airplane_mode?(true))
|
||||||
|
else
|
||||||
|
Map.get(socket.assigns, :offline_mode, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
task_status = TaskLocalization.localize_task_status(raw_task_status, page_language)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:tab_meta, tab_meta)
|
||||||
|
|> assign(:task_status, task_status)
|
||||||
|
|> assign(:offline_mode, offline_mode)
|
||||||
|
|> assign(:supported_ui_languages, ShellData.supported_ui_languages())
|
||||||
|
|> assign(:menu_groups, socket.assigns[:menu_groups] || TitlebarMenu.groups())
|
||||||
|
|> assign(:titlebar_menu_item_index, socket.assigns[:titlebar_menu_item_index])
|
||||||
|
|> refresh_content(workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -15,14 +15,7 @@ defmodule BDS.Desktop.ShellLive.GalleryImport do
|
|||||||
@spec start(list(String.t()), String.t(), String.t(), String.t(), integer(), pid()) :: :ok
|
@spec start(list(String.t()), String.t(), String.t(), String.t(), integer(), pid()) :: :ok
|
||||||
def start(paths, project_id, post_id, language, concurrency_limit, parent) do
|
def start(paths, project_id, post_id, language, concurrency_limit, parent) do
|
||||||
{:ok, metadata} = Metadata.get_project_metadata(project_id)
|
{:ok, metadata} = Metadata.get_project_metadata(project_id)
|
||||||
main_language = metadata.main_language || language
|
translate_targets = translate_targets(metadata, language)
|
||||||
blog_languages = metadata.blog_languages || []
|
|
||||||
|
|
||||||
translate_targets =
|
|
||||||
[main_language | blog_languages]
|
|
||||||
|> Enum.reject(&(&1 == language or is_nil(&1)))
|
|
||||||
|> Enum.uniq()
|
|
||||||
|
|
||||||
{in_flight, remaining} = Enum.split(paths, concurrency_limit)
|
{in_flight, remaining} = Enum.split(paths, concurrency_limit)
|
||||||
|
|
||||||
tasks =
|
tasks =
|
||||||
@@ -48,6 +41,36 @@ defmodule BDS.Desktop.ShellLive.GalleryImport do
|
|||||||
send(parent, {:add_images_complete, length(paths)})
|
send(parent, {:add_images_complete, length(paths)})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
The translation targets for AI-generated media texts: the project main
|
||||||
|
language plus all blog languages, minus the source `language`.
|
||||||
|
"""
|
||||||
|
def translate_targets(metadata, language) do
|
||||||
|
main_language = metadata.main_language || language
|
||||||
|
blog_languages = metadata.blog_languages || []
|
||||||
|
|
||||||
|
[main_language | blog_languages]
|
||||||
|
|> Enum.reject(&(&1 == language or is_nil(&1)))
|
||||||
|
|> Enum.uniq()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Imports a single file and runs the same best-effort AI enrichment
|
||||||
|
(title/alt/caption plus translations) as the gallery pipeline, without
|
||||||
|
linking the media to a post. Used by the CLI `media` command (issue #25).
|
||||||
|
|
||||||
|
Returns `{:ok, media, display_title}` or `{:error, reason}`.
|
||||||
|
"""
|
||||||
|
def import_and_enrich(path, project_id, language, translate_targets) do
|
||||||
|
case Media.import_media(%{project_id: project_id, source_path: path}) do
|
||||||
|
{:ok, media} ->
|
||||||
|
{:ok, media, enrich_media(media, language, translate_targets)}
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
{:error, reason}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp drain_tasks(
|
defp drain_tasks(
|
||||||
[],
|
[],
|
||||||
tasks,
|
tasks,
|
||||||
@@ -167,18 +190,14 @@ defmodule BDS.Desktop.ShellLive.GalleryImport do
|
|||||||
translate_targets,
|
translate_targets,
|
||||||
parent
|
parent
|
||||||
) do
|
) do
|
||||||
|
# Linking is mandatory and MUST happen regardless of AI availability
|
||||||
|
# (airplane mode / no local model), mirroring the drag-drop chain and the
|
||||||
|
# old app: import -> link -> (optional) AI enrichment. Gating the link
|
||||||
|
# behind AI is what left galleries empty.
|
||||||
with {:ok, media} <- Media.import_media(%{project_id: project_id, source_path: path}),
|
with {:ok, media} <- Media.import_media(%{project_id: project_id, source_path: path}),
|
||||||
true <- String.starts_with?(media.mime_type || "", "image/"),
|
true <- String.starts_with?(media.mime_type || "", "image/"),
|
||||||
{:ok, result} <- AI.analyze_image(media.id, language: language),
|
|
||||||
{:ok, _updated} <-
|
|
||||||
Media.update_media(media.id, %{
|
|
||||||
title: result.title,
|
|
||||||
alt: result.alt,
|
|
||||||
caption: result.caption
|
|
||||||
}),
|
|
||||||
{:ok, _link} <- Media.link_media_to_post(media.id, post_id) do
|
{:ok, _link} <- Media.link_media_to_post(media.id, post_id) do
|
||||||
translate_media_translations(media.id, translate_targets)
|
title = enrich_media(media, language, translate_targets)
|
||||||
title = result.title || media.original_name
|
|
||||||
send(parent, {:add_image_processed, title})
|
send(parent, {:add_image_processed, title})
|
||||||
else
|
else
|
||||||
false ->
|
false ->
|
||||||
@@ -190,6 +209,32 @@ defmodule BDS.Desktop.ShellLive.GalleryImport do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Best-effort AI enrichment: analysis + metadata update + translations. Any
|
||||||
|
# failure (offline, no model) is logged and never unlinks the image. Returns
|
||||||
|
# the display title used for progress reporting.
|
||||||
|
defp enrich_media(media, language, translate_targets) do
|
||||||
|
case AI.analyze_image(media.id, language: language) do
|
||||||
|
{:ok, result} ->
|
||||||
|
case Media.update_media(media.id, %{
|
||||||
|
title: result.title,
|
||||||
|
alt: result.alt,
|
||||||
|
caption: result.caption
|
||||||
|
}) do
|
||||||
|
{:ok, _updated} ->
|
||||||
|
translate_media_translations(media.id, translate_targets)
|
||||||
|
result.title || media.original_name
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
Logger.warning("Media metadata update failed for #{media.id}: #{inspect(reason)}")
|
||||||
|
media.original_name
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
Logger.warning("AI image analysis skipped for #{media.id}: #{inspect(reason)}")
|
||||||
|
media.original_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp translate_media_translations(_media_id, []), do: :ok
|
defp translate_media_translations(_media_id, []), do: :ok
|
||||||
|
|
||||||
defp translate_media_translations(media_id, [target | rest]) do
|
defp translate_media_translations(media_id, [target | rest]) do
|
||||||
|
|||||||
126
lib/bds/desktop/shell_live/git_handler.ex
Normal file
126
lib/bds/desktop/shell_live/git_handler.ex
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.GitHandler do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellLive.{SocketState, TabHelpers, UrlState}
|
||||||
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
|
def run_action(socket, event) do
|
||||||
|
project_id = current_project_id(socket)
|
||||||
|
|
||||||
|
{label, result} =
|
||||||
|
case event do
|
||||||
|
"git_fetch" -> {dgettext("ui", "Fetch"), git_call(project_id, &BDS.Git.fetch/1)}
|
||||||
|
"git_pull" -> {dgettext("ui", "Pull"), git_call(project_id, &BDS.Git.pull/1)}
|
||||||
|
"git_push" -> {dgettext("ui", "Push"), git_call(project_id, &BDS.Git.push/1)}
|
||||||
|
"git_prune_lfs" -> {dgettext("ui", "Prune LFS"), prune_lfs(project_id)}
|
||||||
|
end
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> append_git_result(label, result)
|
||||||
|
|> SocketState.refresh_sidebar(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
def commit(socket, "") do
|
||||||
|
socket
|
||||||
|
|> SocketState.append_output_entry(
|
||||||
|
dgettext("ui", "Commit"),
|
||||||
|
dgettext("ui", "Commit message is required"),
|
||||||
|
nil,
|
||||||
|
"error"
|
||||||
|
)
|
||||||
|
|> SocketState.refresh_sidebar(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
def commit(socket, message) do
|
||||||
|
case git_call(current_project_id(socket), &BDS.Git.commit_all(&1, message)) do
|
||||||
|
{:ok, _result} ->
|
||||||
|
workbench = close_git_diff_tabs(socket.assigns.workbench)
|
||||||
|
tab_meta = TabHelpers.sync_tab_meta(workbench, socket.assigns[:tab_meta] || %{})
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:tab_meta, tab_meta)
|
||||||
|
|> SocketState.append_output_entry(dgettext("ui", "Commit"), message, nil, "info")
|
||||||
|
|> SocketState.refresh_sidebar(workbench)
|
||||||
|
|> UrlState.push()
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> SocketState.append_output_entry(dgettext("ui", "Commit"), format_git_error(reason), nil, "error")
|
||||||
|
|> SocketState.refresh_sidebar(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(socket, remote_url) do
|
||||||
|
project_id = current_project_id(socket)
|
||||||
|
|
||||||
|
case git_call(project_id, &BDS.Git.initialize_repo/1) do
|
||||||
|
{:ok, _repo} ->
|
||||||
|
_ = maybe_set_git_remote(project_id, remote_url)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> SocketState.append_output_entry(
|
||||||
|
dgettext("ui", "Initialize Git"),
|
||||||
|
dgettext("ui", "Repository initialized"),
|
||||||
|
nil,
|
||||||
|
"info"
|
||||||
|
)
|
||||||
|
|> SocketState.refresh_sidebar(socket.assigns.workbench)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> SocketState.append_output_entry(
|
||||||
|
dgettext("ui", "Initialize Git"),
|
||||||
|
format_git_error(reason),
|
||||||
|
nil,
|
||||||
|
"error"
|
||||||
|
)
|
||||||
|
|> SocketState.refresh_sidebar(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_remote_url(value) do
|
||||||
|
case value |> to_string() |> String.trim() do
|
||||||
|
"" -> nil
|
||||||
|
url -> url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp git_call(nil, _fun), do: {:error, :no_project}
|
||||||
|
defp git_call("default", _fun), do: {:error, :no_project}
|
||||||
|
defp git_call(project_id, fun) when is_binary(project_id), do: fun.(project_id)
|
||||||
|
|
||||||
|
defp prune_lfs(nil), do: {:error, :no_project}
|
||||||
|
defp prune_lfs("default"), do: {:error, :no_project}
|
||||||
|
|
||||||
|
defp prune_lfs(project_id) when is_binary(project_id),
|
||||||
|
do: BDS.Git.prune_lfs_cache(project_id, 10)
|
||||||
|
|
||||||
|
defp maybe_set_git_remote(_project_id, nil), do: :ok
|
||||||
|
defp maybe_set_git_remote(project_id, remote_url), do: BDS.Git.set_remote(project_id, remote_url)
|
||||||
|
|
||||||
|
defp append_git_result(socket, label, {:ok, _result}) do
|
||||||
|
SocketState.append_output_entry(socket, label, dgettext("ui", "Done"), nil, "info")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp append_git_result(socket, label, {:error, reason}) do
|
||||||
|
SocketState.append_output_entry(socket, label, format_git_error(reason), nil, "error")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp format_git_error(:no_project), do: dgettext("ui", "No active project")
|
||||||
|
defp format_git_error(%{message: message}) when is_binary(message), do: message
|
||||||
|
defp format_git_error(%{guidance: guidance}) when is_binary(guidance), do: guidance
|
||||||
|
defp format_git_error({:git_failed, message}) when is_binary(message), do: message
|
||||||
|
defp format_git_error(reason), do: inspect(reason)
|
||||||
|
|
||||||
|
defp close_git_diff_tabs(workbench) do
|
||||||
|
workbench.tabs
|
||||||
|
|> Enum.filter(&(&1.type == :git_diff))
|
||||||
|
|> Enum.reduce(workbench, fn tab, wb -> Workbench.close_tab(wb, :git_diff, tab.id) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp current_project_id(socket), do: (socket.assigns[:projects] || %{})[:active_project_id]
|
||||||
|
end
|
||||||
130
lib/bds/desktop/shell_live/git_run.ex
Normal file
130
lib/bds/desktop/shell_live/git_run.ex
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.GitRun do
|
||||||
|
@moduledoc """
|
||||||
|
Live modal for streaming network git commands (fetch/pull/push).
|
||||||
|
|
||||||
|
Starts a `BDS.Git.Stream`, appends its stdout/stderr chunks as they arrive
|
||||||
|
(stderr rendered in red), shows a spinner while running and a pass/fail banner
|
||||||
|
from the exit status. Closing the modal cancels a still-running command, which
|
||||||
|
kills the OS process.
|
||||||
|
"""
|
||||||
|
|
||||||
|
use Phoenix.Component
|
||||||
|
|
||||||
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
|
alias BDS.Git
|
||||||
|
|
||||||
|
@operations %{"git_fetch" => :fetch, "git_pull" => :pull, "git_push" => :push}
|
||||||
|
|
||||||
|
@doc "True when the shell event is a streaming network git action."
|
||||||
|
def network_event?(event), do: Map.has_key?(@operations, event)
|
||||||
|
|
||||||
|
@doc "Open the modal and start streaming the operation."
|
||||||
|
def start(socket, event, project_id) do
|
||||||
|
operation = Map.fetch!(@operations, event)
|
||||||
|
|
||||||
|
run =
|
||||||
|
case start_stream(project_id, operation) do
|
||||||
|
{:ok, pid, ref} ->
|
||||||
|
%{operation: operation, pid: pid, ref: ref, lines: [], status: :running}
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
%{
|
||||||
|
operation: operation,
|
||||||
|
pid: nil,
|
||||||
|
ref: nil,
|
||||||
|
lines: [{:stderr, error_text(reason)}],
|
||||||
|
status: {:done, 1}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
assign(socket, :git_run, run)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Append a streamed chunk to the modal (newest first internally)."
|
||||||
|
def append(socket, ref, stream, chunk) do
|
||||||
|
case socket.assigns[:git_run] do
|
||||||
|
%{ref: ^ref, lines: lines} = run ->
|
||||||
|
assign(socket, :git_run, %{run | lines: [{stream, chunk} | lines]})
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Mark the run finished with its exit status."
|
||||||
|
def done(socket, ref, status) do
|
||||||
|
case socket.assigns[:git_run] do
|
||||||
|
%{ref: ^ref} = run ->
|
||||||
|
assign(socket, :git_run, %{run | status: {:done, status}, pid: nil})
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Close the modal, cancelling (killing) a still-running command."
|
||||||
|
def close(socket) do
|
||||||
|
case socket.assigns[:git_run] do
|
||||||
|
%{status: :running, pid: pid} when is_pid(pid) -> Git.Stream.cancel(pid)
|
||||||
|
_other -> :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
assign(socket, :git_run, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp start_stream(nil, _operation), do: {:error, :no_project}
|
||||||
|
defp start_stream("default", _operation), do: {:error, :no_project}
|
||||||
|
|
||||||
|
defp start_stream(project_id, operation) when is_binary(project_id),
|
||||||
|
do: Git.stream(project_id, operation, self())
|
||||||
|
|
||||||
|
defp error_text(reason) when reason in [:no_project, :not_found],
|
||||||
|
do: dgettext("ui", "No active project") <> "\n"
|
||||||
|
|
||||||
|
defp error_text(reason), do: inspect(reason) <> "\n"
|
||||||
|
|
||||||
|
def git_run_modal(assigns) do
|
||||||
|
~H"""
|
||||||
|
<%= if @git_run do %>
|
||||||
|
<div class="shell-overlay-backdrop git-run-backdrop" data-testid="git-run-backdrop" phx-window-keydown="git_run_keydown">
|
||||||
|
<button class="shell-overlay-dismiss" type="button" phx-click="git_run_close" aria-label={dgettext("ui", "Close")}></button>
|
||||||
|
<div class="git-run-modal" role="dialog" aria-modal="true">
|
||||||
|
<div class="git-run-header">
|
||||||
|
<h2 class="git-run-title">{title(@git_run.operation)}</h2>
|
||||||
|
<span class={["git-run-status", status_class(@git_run.status)]} data-testid="git-run-status">
|
||||||
|
{status_label(@git_run.status)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<pre class="git-run-output" data-testid="git-run-output"><%= for {stream, text} <- Enum.reverse(@git_run.lines) do %><span class={line_class(stream)}>{text}</span><% end %></pre>
|
||||||
|
<div class="git-run-footer">
|
||||||
|
<button class="git-action-button" type="button" phx-click="git_run_close" data-testid="git-run-close">
|
||||||
|
{close_label(@git_run.status)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp title(:fetch), do: dgettext("ui", "git fetch")
|
||||||
|
defp title(:pull), do: dgettext("ui", "git pull")
|
||||||
|
defp title(:push), do: dgettext("ui", "git push")
|
||||||
|
|
||||||
|
defp line_class(:stderr), do: "git-run-line git-run-stderr"
|
||||||
|
defp line_class(:stdout), do: "git-run-line git-run-stdout"
|
||||||
|
|
||||||
|
defp status_class(:running), do: "running"
|
||||||
|
defp status_class({:done, 0}), do: "ok"
|
||||||
|
defp status_class({:done, _status}), do: "fail"
|
||||||
|
|
||||||
|
defp status_label(:running), do: dgettext("ui", "Running…")
|
||||||
|
defp status_label({:done, 0}), do: dgettext("ui", "Done")
|
||||||
|
|
||||||
|
defp status_label({:done, status}),
|
||||||
|
do: dgettext("ui", "Failed (exit %{status})", status: status)
|
||||||
|
|
||||||
|
defp close_label(:running), do: dgettext("ui", "Cancel")
|
||||||
|
defp close_label({:done, _status}), do: dgettext("ui", "Close")
|
||||||
|
end
|
||||||
@@ -6,6 +6,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
alias BDS.{AI, ImportAnalysis, ImportDefinitions, ImportExecution}
|
alias BDS.{AI, ImportAnalysis, ImportDefinitions, ImportExecution}
|
||||||
alias BDS.Desktop.{FilePicker, FolderPicker, ShellData}
|
alias BDS.Desktop.{FilePicker, FolderPicker, ShellData}
|
||||||
alias BDS.Desktop.ShellLive.Notify
|
alias BDS.Desktop.ShellLive.Notify
|
||||||
|
require Logger
|
||||||
|
|
||||||
alias BDS.Desktop.ShellLive.ImportEditor.{
|
alias BDS.Desktop.ShellLive.ImportEditor.{
|
||||||
AnalysisState,
|
AnalysisState,
|
||||||
@@ -327,7 +328,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
|> assign(:taxonomy_edit, %{
|
|> assign(:taxonomy_edit, %{
|
||||||
type: type,
|
type: type,
|
||||||
name: name,
|
name: name,
|
||||||
value: mapped_to |> to_string() |> blank_to_nil()
|
value: mapped_to |> to_string() |> BDS.MapUtils.blank_to_nil()
|
||||||
})
|
})
|
||||||
|> build_data()
|
|> build_data()
|
||||||
|
|
||||||
@@ -693,7 +694,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
wxr_file_path: analysis_state.file_path,
|
wxr_file_path: analysis_state.file_path,
|
||||||
last_analysis_result: report
|
last_analysis_result: report
|
||||||
}
|
}
|
||||||
|> maybe_put(:name, AnalysisState.suggested_definition_name(report))
|
|> BDS.MapUtils.maybe_put(:name, AnalysisState.suggested_definition_name(report))
|
||||||
|
|
||||||
case ImportDefinitions.update_definition(definition_id, attrs) do
|
case ImportDefinitions.update_definition(definition_id, attrs) do
|
||||||
{:ok, _definition} ->
|
{:ok, _definition} ->
|
||||||
@@ -839,7 +840,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
<div class={["import-file-path", if(blank?(@import_editor.uploads_folder_path), do: "placeholder")]}>
|
<div class={["import-file-path", if(blank?(@import_editor.uploads_folder_path), do: "placeholder")]}>
|
||||||
<%= @import_editor.uploads_folder_path || dgettext("ui", "No folder selected") %>
|
<%= @import_editor.uploads_folder_path || dgettext("ui", "No folder selected") %>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" phx-click="select_import_uploads_folder" phx-target={@myself}><%= dgettext("ui", "Open") %></button>
|
<button class="secondary ui-button ui-button-secondary" type="button" phx-click="select_import_uploads_folder" phx-target={@myself}><%= dgettext("ui", "Open") %></button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="import-file-row">
|
<div class="import-file-row">
|
||||||
@@ -847,7 +848,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
<div class={["import-file-path", if(blank?(@import_editor.wxr_file_path), do: "placeholder")]}>
|
<div class={["import-file-path", if(blank?(@import_editor.wxr_file_path), do: "placeholder")]}>
|
||||||
<%= @import_editor.wxr_file_path || dgettext("ui", "Select a file to analyze") %>
|
<%= @import_editor.wxr_file_path || dgettext("ui", "Select a file to analyze") %>
|
||||||
</div>
|
</div>
|
||||||
<button class="import-analyze-btn" type="button" phx-click="select_import_wxr_file" phx-target={@myself}><%= dgettext("ui", "Select & Analyze") %></button>
|
<button class="import-analyze-btn primary ui-button ui-button-primary" type="button" phx-click="select_import_wxr_file" phx-target={@myself}><%= dgettext("ui", "Select & Analyze") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -943,7 +944,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
<%= if @counts.pages > 0 do %><span class="import-count-tag"><%= @counts.pages %> <%= dgettext("ui", "pages") %></span><% end %>
|
<%= if @counts.pages > 0 do %><span class="import-count-tag"><%= @counts.pages %> <%= dgettext("ui", "pages") %></span><% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="import-execute-btn" type="button" phx-click="execute_import_editor" phx-target={@myself} disabled={@counts.total == 0}>
|
<button class="import-execute-btn primary ui-button ui-button-primary" type="button" phx-click="execute_import_editor" phx-target={@myself} disabled={@counts.total == 0}>
|
||||||
<%= if @counts.total == 0 do %>
|
<%= if @counts.total == 0 do %>
|
||||||
<%= dgettext("ui", "Nothing to Import") %>
|
<%= dgettext("ui", "Nothing to Import") %>
|
||||||
<% else %>
|
<% else %>
|
||||||
@@ -999,7 +1000,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
<%= if @sections.taxonomy do %>
|
<%= if @sections.taxonomy do %>
|
||||||
<div class="taxonomy-analyze-row">
|
<div class="taxonomy-analyze-row">
|
||||||
<div class="taxonomy-analyze-dropdown">
|
<div class="taxonomy-analyze-dropdown">
|
||||||
<button class="taxonomy-analyze-btn" type="button" phx-click="toggle_import_ai_model_selector" phx-target={@myself}><%= dgettext("ui", "Analyze with...") %></button>
|
<button class="taxonomy-analyze-btn secondary ui-button ui-button-secondary" type="button" phx-click="toggle_import_ai_model_selector" phx-target={@myself}><%= dgettext("ui", "Analyze with...") %></button>
|
||||||
<%= if @import_editor.model_selector_open? do %>
|
<%= if @import_editor.model_selector_open? do %>
|
||||||
<div class="taxonomy-model-dropdown">
|
<div class="taxonomy-model-dropdown">
|
||||||
<%= for model <- @import_editor.available_models do %>
|
<%= for model <- @import_editor.available_models do %>
|
||||||
@@ -1011,7 +1012,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="taxonomy-analyze-btn" type="button" phx-click="analyze_import_taxonomy_ai" phx-target={@myself} disabled={Enum.empty?(@import_editor.available_models) and not @import_editor.offline?}>
|
<button class="taxonomy-analyze-btn secondary ui-button ui-button-secondary" type="button" phx-click="analyze_import_taxonomy_ai" phx-target={@myself} disabled={Enum.empty?(@import_editor.available_models) and not @import_editor.offline?}>
|
||||||
<%= @import_editor.selected_model_label %>
|
<%= @import_editor.selected_model_label %>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
@@ -1361,7 +1362,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
list={"taxonomy-suggestions-#{@type}"}
|
list={"taxonomy-suggestions-#{@type}"}
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
/>
|
/>
|
||||||
<button class="taxonomy-edit-btn" type="submit" title={dgettext("ui", "Map to...")}>✓</button>
|
<button class="taxonomy-edit-btn secondary" type="submit" title={dgettext("ui", "Map to...")}>✓</button>
|
||||||
<button class="taxonomy-edit-btn ghost" type="button" phx-click="cancel_import_taxonomy_edit" phx-target={@myself} title={dgettext("ui", "Cancel")}>×</button>
|
<button class="taxonomy-edit-btn ghost" type="button" phx-click="cancel_import_taxonomy_edit" phx-target={@myself} title={dgettext("ui", "Cancel")}>×</button>
|
||||||
<%= if present?(item.mapped_to) do %>
|
<%= if present?(item.mapped_to) do %>
|
||||||
<button class="taxonomy-clear-btn" type="button" phx-click="clear_import_taxonomy_mapping" phx-target={@myself} phx-value-type={@type} phx-value-name={item.name} title={dgettext("ui", "Clear mapping")}>×</button>
|
<button class="taxonomy-clear-btn" type="button" phx-click="clear_import_taxonomy_mapping" phx-target={@myself} phx-value-type={@type} phx-value-name={item.name} title={dgettext("ui", "Clear mapping")}>×</button>
|
||||||
@@ -1417,7 +1418,9 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
try do
|
try do
|
||||||
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
||||||
rescue
|
rescue
|
||||||
_error -> :ok
|
error ->
|
||||||
|
Logger.debug("swallowed import_editor allow_repo_sandbox error: #{inspect(error)}")
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
:ok
|
:ok
|
||||||
@@ -1449,11 +1452,6 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do
|
|||||||
Map.get(item, :resolution) in ["overwrite", "merge"]
|
Map.get(item, :resolution) in ["overwrite", "merge"]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp present?(value), do: value not in [nil, ""]
|
defp present?(value), do: BDS.Values.present?(value)
|
||||||
defp blank?(value), do: value in [nil, ""]
|
defp blank?(value), do: BDS.Values.blank?(value)
|
||||||
defp blank_to_nil(""), do: nil
|
|
||||||
defp blank_to_nil(value), do: value
|
|
||||||
|
|
||||||
defp maybe_put(map, _key, nil), do: map
|
|
||||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do
|
|||||||
|
|
||||||
alias BDS.{ImportAnalysis, ImportDefinitions, Metadata}
|
alias BDS.{ImportAnalysis, ImportDefinitions, Metadata}
|
||||||
alias BDS.Desktop.{FilePicker, FolderPicker}
|
alias BDS.Desktop.{FilePicker, FolderPicker}
|
||||||
|
require Logger
|
||||||
use Gettext, backend: BDS.Gettext
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
@spec change_definition(term(), term(), term()) :: term()
|
@spec change_definition(term(), term(), term()) :: term()
|
||||||
@@ -160,7 +161,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do
|
|||||||
wxr_file_path: analysis_state.file_path,
|
wxr_file_path: analysis_state.file_path,
|
||||||
last_analysis_result: report
|
last_analysis_result: report
|
||||||
}
|
}
|
||||||
|> maybe_put(:name, suggested_definition_name(report))
|
|> BDS.MapUtils.maybe_put(:name, suggested_definition_name(report))
|
||||||
|
|
||||||
case ImportDefinitions.update_definition(definition_id, attrs) do
|
case ImportDefinitions.update_definition(definition_id, attrs) do
|
||||||
{:ok, _definition} ->
|
{:ok, _definition} ->
|
||||||
@@ -274,17 +275,18 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do
|
|||||||
get_in(report, [:site_info, :url]) || get_in(report, [:site_info, :title])
|
get_in(report, [:site_info, :url]) || get_in(report, [:site_info, :title])
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec maybe_put(term(), term(), term()) :: term()
|
|
||||||
def maybe_put(map, _key, nil), do: map
|
|
||||||
def maybe_put(map, key, value), do: Map.put(map, key, value)
|
|
||||||
|
|
||||||
@spec allow_repo_sandbox(term()) :: term()
|
@spec allow_repo_sandbox(term()) :: term()
|
||||||
def allow_repo_sandbox(pid) when is_pid(pid) do
|
def allow_repo_sandbox(pid) when is_pid(pid) do
|
||||||
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
|
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
|
||||||
try do
|
try do
|
||||||
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), pid)
|
||||||
rescue
|
rescue
|
||||||
_error -> :ok
|
error ->
|
||||||
|
Logger.debug(
|
||||||
|
"swallowed import_editor analysis_state allow_repo_sandbox error: #{inspect(error)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
:ok
|
:ok
|
||||||
@@ -308,5 +310,5 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do
|
|||||||
@spec translate_phase(term()) :: term()
|
@spec translate_phase(term()) :: term()
|
||||||
def translate_phase(other), do: other
|
def translate_phase(other), do: other
|
||||||
|
|
||||||
defp present?(value), do: value not in [nil, ""]
|
defp present?(value), do: BDS.Values.present?(value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.TaxonomyEditing do
|
|||||||
Map.put(socket.assigns.import_editor_taxonomy_edits, definition_id, %{
|
Map.put(socket.assigns.import_editor_taxonomy_edits, definition_id, %{
|
||||||
type: type,
|
type: type,
|
||||||
name: name,
|
name: name,
|
||||||
value: mapped_to |> to_string() |> blank_to_nil()
|
value: mapped_to |> to_string() |> BDS.MapUtils.blank_to_nil()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|> reload.(socket.assigns.workbench)
|
|> reload.(socket.assigns.workbench)
|
||||||
@@ -150,7 +150,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.TaxonomyEditing do
|
|||||||
@spec update_taxonomy_mapping(term(), term(), term(), term()) :: term()
|
@spec update_taxonomy_mapping(term(), term(), term(), term()) :: term()
|
||||||
def update_taxonomy_mapping(report, type, name, mapped_to) do
|
def update_taxonomy_mapping(report, type, name, mapped_to) do
|
||||||
bucket_key = if(type == "categories", do: :categories, else: :tags)
|
bucket_key = if(type == "categories", do: :categories, else: :tags)
|
||||||
normalized_value = mapped_to |> to_string() |> String.trim() |> blank_to_nil()
|
normalized_value = mapped_to |> to_string() |> String.trim() |> BDS.MapUtils.blank_to_nil()
|
||||||
|
|
||||||
updated_report =
|
updated_report =
|
||||||
update_in(report, [:items, bucket_key], fn items ->
|
update_in(report, [:items, bucket_key], fn items ->
|
||||||
@@ -229,7 +229,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.TaxonomyEditing do
|
|||||||
|
|
||||||
@spec normalize_taxonomy_mapping_value(term(), term(), term()) :: term()
|
@spec normalize_taxonomy_mapping_value(term(), term(), term()) :: term()
|
||||||
def normalize_taxonomy_mapping_value(project_id, type, mapped_to) do
|
def normalize_taxonomy_mapping_value(project_id, type, mapped_to) do
|
||||||
normalized_value = mapped_to |> to_string() |> String.trim() |> blank_to_nil()
|
normalized_value = mapped_to |> to_string() |> String.trim() |> BDS.MapUtils.blank_to_nil()
|
||||||
|
|
||||||
case normalized_value do
|
case normalized_value do
|
||||||
nil ->
|
nil ->
|
||||||
@@ -284,7 +284,5 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.TaxonomyEditing do
|
|||||||
def maybe_put_option(opts, _key, nil), do: opts
|
def maybe_put_option(opts, _key, nil), do: opts
|
||||||
def maybe_put_option(opts, key, value), do: Keyword.put(opts, key, value)
|
def maybe_put_option(opts, key, value), do: Keyword.put(opts, key, value)
|
||||||
|
|
||||||
defp present?(value), do: value not in [nil, ""]
|
defp present?(value), do: BDS.Values.present?(value)
|
||||||
defp blank_to_nil(""), do: nil
|
|
||||||
defp blank_to_nil(value), do: value
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -457,7 +457,7 @@
|
|||||||
<div class="panel-tabs flex min-w-0 items-center overflow-x-auto">
|
<div class="panel-tabs flex min-w-0 items-center overflow-x-auto">
|
||||||
<%= for tab <- @panel_tabs do %>
|
<%= for tab <- @panel_tabs do %>
|
||||||
<button
|
<button
|
||||||
class={["panel-tab", "ui-tab", if(@workbench.panel.active_tab == tab, do: "ui-tab-active"), "inline-flex h-9 items-center px-3 text-xs uppercase tracking-wide", if(@workbench.panel.active_tab == tab, do: "active")]}
|
class={["panel-tab", "ui-tab", if(@workbench.panel.active_tab == tab, do: "ui-tab-active"), "inline-flex items-center px-3 text-xs uppercase tracking-wide", if(@workbench.panel.active_tab == tab, do: "active")]}
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="select_panel_tab"
|
phx-click="select_panel_tab"
|
||||||
phx-value-tab={tab}
|
phx-value-tab={tab}
|
||||||
@@ -531,7 +531,7 @@
|
|||||||
><%= @assistant_prompt %></textarea>
|
><%= @assistant_prompt %></textarea>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
class="assistant-sidebar-start-button ui-button ui-button-primary"
|
class="assistant-sidebar-start-button primary ui-button ui-button-primary"
|
||||||
data-testid="assistant-start-button"
|
data-testid="assistant-start-button"
|
||||||
type="submit"
|
type="submit"
|
||||||
disabled={String.trim(@assistant_prompt || "") == ""}
|
disabled={String.trim(@assistant_prompt || "") == ""}
|
||||||
@@ -540,16 +540,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<%= if Enum.empty?(@assistant_messages) do %>
|
<%= unless Enum.empty?(@assistant_messages) do %>
|
||||||
<div class="assistant-sidebar-welcome min-h-0 flex-1 overflow-auto">
|
|
||||||
<%= for card <- @assistant_cards do %>
|
|
||||||
<section class="assistant-card flex flex-col gap-1">
|
|
||||||
<strong><%= card.label %></strong>
|
|
||||||
<span><%= card.text %></span>
|
|
||||||
</section>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
<% else %>
|
|
||||||
<div class="assistant-sidebar-transcript min-h-0 flex-1 overflow-auto">
|
<div class="assistant-sidebar-transcript min-h-0 flex-1 overflow-auto">
|
||||||
<%= for message <- @assistant_messages do %>
|
<%= for message <- @assistant_messages do %>
|
||||||
<article
|
<article
|
||||||
@@ -687,4 +678,6 @@
|
|||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<ShellOverlayComponents.shell_overlay shell_overlay={@shell_overlay} />
|
<ShellOverlayComponents.shell_overlay shell_overlay={@shell_overlay} />
|
||||||
|
<LiveToast.toast_group flash={@flash} connected={assigns[:socket] != nil} toasts_sync={assigns[:toasts_sync]} corner={:bottom_right} toast_class_fn={&toast_class_fn/1} />
|
||||||
|
<GitRun.git_run_modal git_run={@git_run} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
|||||||
|
|
||||||
alias BDS.Desktop.{FilePicker}
|
alias BDS.Desktop.{FilePicker}
|
||||||
alias BDS.Desktop.ShellLive.Notify
|
alias BDS.Desktop.ShellLive.Notify
|
||||||
alias BDS.{AI, I18n, Media}
|
alias BDS.{AI, I18n, Media, Tasks}
|
||||||
alias BDS.Media.Media, as: MediaRecord
|
alias BDS.Media.Media, as: MediaRecord
|
||||||
alias BDS.Media.Translation
|
alias BDS.Media.Translation
|
||||||
alias BDS.Posts.Post
|
alias BDS.Posts.Post
|
||||||
@@ -57,6 +57,17 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
|||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(%{action: :translation_completed} = assigns, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(Map.drop(assigns, [:action, :language]))
|
||||||
|
|> assign(:quick_actions_open?, false)
|
||||||
|
|> assign(:editing_translation, nil)
|
||||||
|
|> build_data()
|
||||||
|
|
||||||
|
{:ok, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def update(assigns, socket) do
|
def update(assigns, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
@@ -552,25 +563,34 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
|||||||
media = socket.assigns.media
|
media = socket.assigns.media
|
||||||
normalized_language = normalize_language(language)
|
normalized_language = normalize_language(language)
|
||||||
source_language = normalize_language(media.language)
|
source_language = normalize_language(media.language)
|
||||||
|
parent = self()
|
||||||
|
|
||||||
case AI.translate_media(media.id, normalized_language, source_language: source_language) do
|
{:ok, _task} =
|
||||||
{:ok, translation} ->
|
Tasks.submit_task(
|
||||||
case Media.upsert_media_translation(media.id, normalized_language, translation) do
|
"Translate Media to #{normalized_language}",
|
||||||
{:ok, _saved_translation} ->
|
fn report ->
|
||||||
socket
|
report.(0.1, "Translating media to #{normalized_language}")
|
||||||
|> assign(:quick_actions_open?, false)
|
|
||||||
|> assign(:editing_translation, nil)
|
|
||||||
|> build_data()
|
|
||||||
|
|
||||||
{:error, reason} ->
|
with {:ok, translation} <-
|
||||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
AI.translate_media(media.id, normalized_language,
|
||||||
|> build_data()
|
source_language: source_language
|
||||||
end
|
),
|
||||||
|
{:ok, _saved_translation} <-
|
||||||
|
Media.upsert_media_translation(media.id, normalized_language, translation) do
|
||||||
|
report.(1.0, "Media translation complete")
|
||||||
|
send(parent, {:editor_translation_completed, :media, media.id, normalized_language})
|
||||||
|
%{media_id: media.id, language: normalized_language}
|
||||||
|
else
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
%{group_id: media.project_id, group_name: "AI"}
|
||||||
|
)
|
||||||
|
|
||||||
{:error, reason} ->
|
socket
|
||||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
|> assign(:quick_actions_open?, false)
|
||||||
|> build_data()
|
|> assign(:editing_translation, nil)
|
||||||
end
|
|> build_data()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -625,7 +645,7 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
|||||||
|
|
||||||
defp preview_url(%MediaRecord{} = media) do
|
defp preview_url(%MediaRecord{} = media) do
|
||||||
if image?(media),
|
if image?(media),
|
||||||
do: "/media-thumbnail/#{media.id}?size=large&t=#{media.updated_at}",
|
do: "/media-file/#{media.id}?t=#{media.updated_at}",
|
||||||
else: nil
|
else: nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@
|
|||||||
<button class="secondary ui-button ui-button-secondary" type="button" phx-click="replace_media_editor_file" phx-target={@myself}>
|
<button class="secondary ui-button ui-button-secondary" type="button" phx-click="replace_media_editor_file" phx-target={@myself}>
|
||||||
<%= dgettext("ui", "Replace File") %>
|
<%= dgettext("ui", "Replace File") %>
|
||||||
</button>
|
</button>
|
||||||
<button class="ui-button ui-button-primary" data-testid="media-save-button" type="button" phx-click="save_media_editor" phx-target={@myself}>
|
<button class="primary ui-button ui-button-primary" data-testid="media-save-button" type="button" phx-click="save_media_editor" phx-target={@myself}>
|
||||||
<%= dgettext("ui", "Save") %>
|
<%= dgettext("ui", "Save") %>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
@@ -211,7 +211,7 @@
|
|||||||
<div class="editor-field linked-posts-section flex flex-col gap-2">
|
<div class="editor-field linked-posts-section flex flex-col gap-2">
|
||||||
<label>
|
<label>
|
||||||
<%= dgettext("ui", "Linked Posts") %>
|
<%= dgettext("ui", "Linked Posts") %>
|
||||||
<button class="add-link-btn" type="button" phx-click="toggle_media_post_picker" phx-target={@myself}>
|
<button class="add-link-btn secondary compact ui-button ui-button-secondary ui-button-compact" type="button" phx-click="toggle_media_post_picker" phx-target={@myself}>
|
||||||
<%= dgettext("ui", "Link to Post") %>
|
<%= dgettext("ui", "Link to Post") %>
|
||||||
</button>
|
</button>
|
||||||
</label>
|
</label>
|
||||||
@@ -296,7 +296,7 @@
|
|||||||
</form>
|
</form>
|
||||||
<div class="translation-modal-footer flex items-center justify-end gap-2">
|
<div class="translation-modal-footer flex items-center justify-end gap-2">
|
||||||
<button class="secondary ui-button ui-button-secondary" type="button" phx-click="close_media_translation_editor" phx-target={@myself}><%= dgettext("ui", "Cancel") %></button>
|
<button class="secondary ui-button ui-button-secondary" type="button" phx-click="close_media_translation_editor" phx-target={@myself}><%= dgettext("ui", "Cancel") %></button>
|
||||||
<button class="ui-button ui-button-primary" type="button" phx-click="save_media_translation" phx-target={@myself}><%= dgettext("ui", "Save") %></button>
|
<button class="primary ui-button ui-button-primary" type="button" phx-click="save_media_translation" phx-target={@myself}><%= dgettext("ui", "Save") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
|||||||
import Phoenix.HTML, only: [raw: 1]
|
import Phoenix.HTML, only: [raw: 1]
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
alias BDS.{Embeddings, Generation, Git, HelpDocs, Posts, Repo}
|
alias BDS.{Embeddings, Git, HelpDocs, Posts, Repo}
|
||||||
alias BDS.Desktop.ShellLive.Notify
|
alias BDS.Desktop.ShellLive.Notify
|
||||||
alias BDS.MapUtils
|
alias BDS.MapUtils
|
||||||
alias BDS.Settings.Setting
|
alias BDS.Settings.Setting
|
||||||
@@ -77,7 +77,6 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
|||||||
def handle_event("apply_site_validation", _params, socket) do
|
def handle_event("apply_site_validation", _params, socket) do
|
||||||
meta = meta(socket.assigns)
|
meta = meta(socket.assigns)
|
||||||
payload = Map.get(meta, :payload, %{})
|
payload = Map.get(meta, :payload, %{})
|
||||||
project_id = Map.get(meta, :project_id, socket.assigns.project_id)
|
|
||||||
|
|
||||||
report = %{
|
report = %{
|
||||||
sitemap_path: Map.get(payload, :sitemap_path),
|
sitemap_path: Map.get(payload, :sitemap_path),
|
||||||
@@ -89,21 +88,8 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
|||||||
existing_html_url_count: Map.get(payload, :existing_html_url_count, 0)
|
existing_html_url_count: Map.get(payload, :existing_html_url_count, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
case Generation.apply_validation(project_id, report) do
|
notify_command("apply_site_validation", %{report: report})
|
||||||
{:ok, result} ->
|
{:noreply, socket}
|
||||||
notify_output(
|
|
||||||
dgettext("ui", "Site Validation"),
|
|
||||||
dgettext("ui", "Validation changes applied"),
|
|
||||||
inspect(result)
|
|
||||||
)
|
|
||||||
|
|
||||||
notify_command("validate_site")
|
|
||||||
{:noreply, socket}
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
error ->
|
|
||||||
notify_output(dgettext("ui", "Site Validation"), inspect(error), nil, "error")
|
|
||||||
{:noreply, socket}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("fix_translation_validation", _params, socket) do
|
def handle_event("fix_translation_validation", _params, socket) do
|
||||||
@@ -278,10 +264,15 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
|||||||
|
|
||||||
@spec markdown_html(String.t()) :: Phoenix.HTML.safe()
|
@spec markdown_html(String.t()) :: Phoenix.HTML.safe()
|
||||||
def markdown_html(content) do
|
def markdown_html(content) do
|
||||||
|
# Match Earmark's defaults (GFM tables, strikethrough, autolinks); escape
|
||||||
|
# raw HTML to text rather than rendering it, as the old escape: true did.
|
||||||
html =
|
html =
|
||||||
case Earmark.as_html(content || "", escape: true) do
|
case MDEx.to_html(content || "",
|
||||||
{:ok, rendered, _messages} -> rendered
|
extension: [table: true, strikethrough: true, autolink: true],
|
||||||
{:error, rendered, _messages} -> rendered
|
render: [escape: true]
|
||||||
|
) do
|
||||||
|
{:ok, rendered} -> rendered
|
||||||
|
{:error, _reason} -> ""
|
||||||
end
|
end
|
||||||
|
|
||||||
raw(html)
|
raw(html)
|
||||||
|
|||||||
52
lib/bds/desktop/shell_live/native_menu_events.ex
Normal file
52
lib/bds/desktop/shell_live/native_menu_events.ex
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.NativeMenuEvents do
|
||||||
|
@moduledoc """
|
||||||
|
Native- and titlebar-menu LiveView events extracted from `BDS.Desktop.ShellLive`.
|
||||||
|
|
||||||
|
`ShellLive` routes the events listed in `events/0` here via a single delegating
|
||||||
|
`handle_event/3`, passing its `handle_native_menu_action/2` callback so the
|
||||||
|
menu-action dispatch logic stays owned by `ShellLive`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellLive.TitlebarMenu
|
||||||
|
|
||||||
|
@events ~w(
|
||||||
|
native_menu_action
|
||||||
|
titlebar_menu_keydown
|
||||||
|
toggle_titlebar_menu
|
||||||
|
hover_titlebar_menu
|
||||||
|
close_titlebar_menu
|
||||||
|
titlebar_menu_action
|
||||||
|
)
|
||||||
|
|
||||||
|
@doc "Event names handled by this module."
|
||||||
|
@spec events() :: [String.t()]
|
||||||
|
def events, do: @events
|
||||||
|
|
||||||
|
@typep socket :: Phoenix.LiveView.Socket.t()
|
||||||
|
@typep native_action :: (socket(), String.t() -> socket())
|
||||||
|
|
||||||
|
@spec handle(String.t(), map(), socket(), native_action()) :: {:noreply, socket()}
|
||||||
|
def handle("native_menu_action", %{"action" => action}, socket, native_action) do
|
||||||
|
{:noreply, native_action.(socket, action)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle("titlebar_menu_keydown", %{"key" => key}, socket, native_action) do
|
||||||
|
{:noreply, TitlebarMenu.handle_keydown(socket, key, native_action)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle("toggle_titlebar_menu", %{"group" => group}, socket, _native_action) do
|
||||||
|
{:noreply, TitlebarMenu.toggle(socket, group)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle("hover_titlebar_menu", %{"group" => group}, socket, _native_action) do
|
||||||
|
{:noreply, TitlebarMenu.hover(socket, group)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle("close_titlebar_menu", _params, socket, _native_action) do
|
||||||
|
{:noreply, TitlebarMenu.close(socket)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle("titlebar_menu_action", %{"action" => action}, socket, native_action) do
|
||||||
|
{:noreply, socket |> TitlebarMenu.close() |> native_action.(action)}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -20,6 +20,23 @@ defmodule BDS.Desktop.ShellLive.Notify do
|
|||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Sends an output entry to a specific LiveView process. Needed when the
|
||||||
|
caller runs in a different process than the shell — e.g. script output
|
||||||
|
callbacks invoked inside the sandboxed script process.
|
||||||
|
"""
|
||||||
|
@spec output_to(pid(), String.t(), String.t(), String.t() | nil, String.t()) :: :ok
|
||||||
|
def output_to(pid, title, message, detail, level) when is_pid(pid) do
|
||||||
|
send(pid, {:editor_output, title, message, detail, level})
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec alert(String.t(), String.t()) :: :ok
|
||||||
|
def alert(title, message) do
|
||||||
|
send(self(), {:shell_alert, title, message})
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
@spec tab_meta(atom(), term(), String.t(), String.t()) :: :ok
|
@spec tab_meta(atom(), term(), String.t(), String.t()) :: :ok
|
||||||
def tab_meta(type, id, title, subtitle) do
|
def tab_meta(type, id, title, subtitle) do
|
||||||
send(self(), {:editor_tab_meta, type, id, %{title: title, subtitle: subtitle || ""}})
|
send(self(), {:editor_tab_meta, type, id, %{title: title, subtitle: subtitle || ""}})
|
||||||
|
|||||||
@@ -5,13 +5,19 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
use Gettext, backend: BDS.Gettext
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
require Logger
|
||||||
|
|
||||||
alias BDS.{I18n, Media, Metadata, Posts, Repo}
|
alias BDS.{I18n, Media, Metadata, Posts, Repo, Slug, Strings}
|
||||||
alias BDS.Media.Media, as: MediaRecord
|
alias BDS.Media.Media, as: MediaRecord
|
||||||
alias BDS.Media.Translation, as: MediaTranslation
|
alias BDS.Media.Translation, as: MediaTranslation
|
||||||
alias BDS.Posts.{Post, PostMedia, Translation}
|
alias BDS.Posts.{Post, PostMedia, Translation}
|
||||||
alias BDS.Tags.Tag
|
alias BDS.Tags.Tag
|
||||||
|
|
||||||
|
# Expected data/DB exceptions for the overlay fallbacks below. Narrowed (not a
|
||||||
|
# broad `rescue error ->`) so programmer bugs (MatchError, FunctionClauseError)
|
||||||
|
# surface unmodified instead of being swallowed into a fallback value.
|
||||||
|
@overlay_rescue [Ecto.NoResultsError, Ecto.QueryError, DBConnection.ConnectionError]
|
||||||
|
|
||||||
embed_templates("overlay_html/*")
|
embed_templates("overlay_html/*")
|
||||||
|
|
||||||
def context(assigns, tab_title, tab_subtitle) do
|
def context(assigns, tab_title, tab_subtitle) do
|
||||||
@@ -70,7 +76,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
{:ok, metadata} = Metadata.get_project_metadata(project_id)
|
{:ok, metadata} = Metadata.get_project_metadata(project_id)
|
||||||
metadata
|
metadata
|
||||||
rescue
|
rescue
|
||||||
_error -> %{main_language: "en", blog_languages: []}
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("project metadata", error)
|
||||||
|
%{main_language: "en", blog_languages: []}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp posts(nil), do: []
|
defp posts(nil), do: []
|
||||||
@@ -137,7 +145,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
select: pm.media_id
|
select: pm.media_id
|
||||||
)
|
)
|
||||||
rescue
|
rescue
|
||||||
_error -> []
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("post media ids for #{post_id}", error)
|
||||||
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp post_media_ids(_tab), do: []
|
defp post_media_ids(_tab), do: []
|
||||||
@@ -150,7 +160,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
)
|
)
|
||||||
|> Map.new(fn {language, status} -> {language, Atom.to_string(status || :draft)} end)
|
|> Map.new(fn {language, status} -> {language, Atom.to_string(status || :draft)} end)
|
||||||
rescue
|
rescue
|
||||||
_error -> %{}
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("post translations for #{post_id}", error)
|
||||||
|
%{}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp existing_translations(%{type: :media, id: media_id}) do
|
defp existing_translations(%{type: :media, id: media_id}) do
|
||||||
@@ -161,7 +173,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
)
|
)
|
||||||
|> Map.new(fn {language, status} -> {language, status} end)
|
|> Map.new(fn {language, status} -> {language, status} end)
|
||||||
rescue
|
rescue
|
||||||
_error -> %{}
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("media translations for #{media_id}", error)
|
||||||
|
%{}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp existing_translations(_tab), do: %{}
|
defp existing_translations(_tab), do: %{}
|
||||||
@@ -179,7 +193,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
_other -> metadata.main_language || "en"
|
_other -> metadata.main_language || "en"
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
_error -> metadata.main_language || "en"
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("post source language for #{post_id}", error)
|
||||||
|
metadata.main_language || "en"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp source_language(%{type: :media, id: media_id}, metadata) do
|
defp source_language(%{type: :media, id: media_id}, metadata) do
|
||||||
@@ -188,19 +204,16 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
_other -> metadata.main_language || "en"
|
_other -> metadata.main_language || "en"
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
_error -> metadata.main_language || "en"
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("media source language for #{media_id}", error)
|
||||||
|
metadata.main_language || "en"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp source_language(_tab, metadata), do: metadata.main_language || "en"
|
defp source_language(_tab, metadata), do: metadata.main_language || "en"
|
||||||
|
|
||||||
defp language_names do
|
defp language_names do
|
||||||
%{
|
I18n.supported_languages()
|
||||||
"en" => "English",
|
|> Enum.into(%{}, fn language -> {language.code, I18n.language_name(language.code)} end)
|
||||||
"de" => "Deutsch",
|
|
||||||
"fr" => "Francais",
|
|
||||||
"it" => "Italiano",
|
|
||||||
"es" => "Espanol"
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp language_flags do
|
defp language_flags do
|
||||||
@@ -231,7 +244,7 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
%{
|
%{
|
||||||
key: "slug",
|
key: "slug",
|
||||||
label: BDS.Gettext.lgettext(page_language, "ui", "Slug"),
|
label: BDS.Gettext.lgettext(page_language, "ui", "Slug"),
|
||||||
current_value: post.slug || slugify(post.title || title),
|
current_value: post.slug || Slug.slugify(post.title || title),
|
||||||
suggested_value: "",
|
suggested_value: "",
|
||||||
locked: post.status == :published,
|
locked: post.status == :published,
|
||||||
loading: true
|
loading: true
|
||||||
@@ -242,7 +255,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
_error -> []
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("post AI fields for #{post_id}", error)
|
||||||
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp ai_fields(%{type: :media, id: media_id}, title, _subtitle, page_language) do
|
defp ai_fields(%{type: :media, id: media_id}, title, _subtitle, page_language) do
|
||||||
@@ -279,7 +294,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
_error -> []
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("media AI fields for #{media_id}", error)
|
||||||
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp ai_fields(_tab, _title, _subtitle, _page_language), do: []
|
defp ai_fields(_tab, _title, _subtitle, _page_language), do: []
|
||||||
@@ -309,7 +326,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
reference_list: reference_list
|
reference_list: reference_list
|
||||||
}
|
}
|
||||||
rescue
|
rescue
|
||||||
_error ->
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("delete media details for #{media_id}", error)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
title: BDS.Gettext.lgettext(page_language, "ui", "Delete Media"),
|
title: BDS.Gettext.lgettext(page_language, "ui", "Delete Media"),
|
||||||
entity_name: media_id,
|
entity_name: media_id,
|
||||||
@@ -330,7 +349,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
reference_list: []
|
reference_list: []
|
||||||
}
|
}
|
||||||
rescue
|
rescue
|
||||||
_error ->
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("delete tag details", error)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
title: BDS.Gettext.lgettext(page_language, "ui", "Delete Tag"),
|
title: BDS.Gettext.lgettext(page_language, "ui", "Delete Tag"),
|
||||||
entity_name: "tag",
|
entity_name: "tag",
|
||||||
@@ -367,7 +388,9 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
message: BDS.Gettext.lgettext(page_language, "ui", "Cannot be undone.")
|
message: BDS.Gettext.lgettext(page_language, "ui", "Cannot be undone.")
|
||||||
}
|
}
|
||||||
rescue
|
rescue
|
||||||
_error ->
|
error in @overlay_rescue ->
|
||||||
|
log_overlay_error("merge tag details for project #{project_id}", error)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
target: "tag",
|
target: "tag",
|
||||||
count: 1,
|
count: 1,
|
||||||
@@ -379,16 +402,10 @@ defmodule BDS.Desktop.ShellLive.OverlayComponents do
|
|||||||
defp canonical_post_url(post) do
|
defp canonical_post_url(post) do
|
||||||
timestamp = post.published_at || post.updated_at || System.system_time(:millisecond)
|
timestamp = post.published_at || post.updated_at || System.system_time(:millisecond)
|
||||||
date = DateTime.from_unix!(timestamp, :millisecond)
|
date = DateTime.from_unix!(timestamp, :millisecond)
|
||||||
"/#{date.year}/#{pad2(date.month)}/#{pad2(date.day)}/#{post.slug || post.id}"
|
"/#{date.year}/#{Strings.pad2(date.month)}/#{Strings.pad2(date.day)}/#{post.slug || post.id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp pad2(value), do: value |> Integer.to_string() |> String.pad_leading(2, "0")
|
defp log_overlay_error(context, error) do
|
||||||
|
Logger.error("overlay component fallback for #{context}: #{Exception.message(error)}")
|
||||||
defp slugify(value) do
|
|
||||||
value
|
|
||||||
|> to_string()
|
|
||||||
|> String.downcase()
|
|
||||||
|> String.replace(~r/[^a-z0-9]+/u, "-")
|
|
||||||
|> String.trim("-")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -39,8 +39,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ai-suggestions-modal-footer">
|
<div class="ai-suggestions-modal-footer">
|
||||||
<button class="button-cancel" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
<button class="button-cancel secondary ui-button ui-button-secondary" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
||||||
<button class="button-apply" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Apply Selected") %></button>
|
<button class="button-apply primary ui-button ui-button-primary" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Apply Selected") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -82,7 +82,7 @@
|
|||||||
<span class="insert-modal-label"><%= dgettext("ui", "Display Text") %></span>
|
<span class="insert-modal-label"><%= dgettext("ui", "Display Text") %></span>
|
||||||
<input class="insert-modal-input" type="text" name="overlay[text]" value={@shell_overlay.external_text} />
|
<input class="insert-modal-input" type="text" name="overlay[text]" value={@shell_overlay.external_text} />
|
||||||
</label>
|
</label>
|
||||||
<button class="insert-modal-submit" type="button" phx-click="overlay_insert_external"><%= dgettext("ui", "Insert") %></button>
|
<button class="insert-modal-submit primary ui-button ui-button-primary" type="button" phx-click="overlay_insert_external"><%= dgettext("ui", "Insert") %></button>
|
||||||
</form>
|
</form>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
@@ -162,8 +162,8 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<div class="confirm-delete-modal-footer">
|
<div class="confirm-delete-modal-footer">
|
||||||
<button class="button-cancel" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
<button class="button-cancel secondary ui-button ui-button-secondary" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
||||||
<button class="button-delete" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Delete") %></button>
|
<button class="button-delete danger ui-button ui-button-danger" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Delete") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -180,8 +180,25 @@
|
|||||||
<div class="confirm-delete-message"><%= @shell_overlay.message %></div>
|
<div class="confirm-delete-message"><%= @shell_overlay.message %></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="confirm-delete-modal-footer">
|
<div class="confirm-delete-modal-footer">
|
||||||
<button class="button-cancel" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
<button class="button-cancel secondary ui-button ui-button-secondary" type="button" phx-click="close_overlay"><%= dgettext("ui", "Cancel") %></button>
|
||||||
<button class="button-apply" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Confirm") %></button>
|
<button class="button-apply primary ui-button ui-button-primary" type="button" phx-click="overlay_confirm"><%= dgettext("ui", "Confirm") %></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<% :alert -> %>
|
||||||
|
<div class="shell-overlay-backdrop confirm-delete-modal-backdrop" data-testid="shell-overlay-backdrop" phx-window-keydown="overlay_keydown">
|
||||||
|
<button class="shell-overlay-dismiss" type="button" phx-click="close_overlay" aria-label={dgettext("ui", "Close")}></button>
|
||||||
|
<div class={["confirm-delete-modal", "alert-modal", "alert-modal-#{@shell_overlay.tone}"]} role="alertdialog" aria-modal="true">
|
||||||
|
<div class="confirm-delete-modal-header">
|
||||||
|
<h2><%= @shell_overlay.title %></h2>
|
||||||
|
<button class="confirm-delete-modal-close" type="button" phx-click="close_overlay">×</button>
|
||||||
|
</div>
|
||||||
|
<div class="confirm-delete-modal-body">
|
||||||
|
<div class="confirm-delete-message"><%= @shell_overlay.message %></div>
|
||||||
|
</div>
|
||||||
|
<div class="confirm-delete-modal-footer">
|
||||||
|
<button class="button-apply primary ui-button ui-button-primary" type="button" phx-click="close_overlay"><%= dgettext("ui", "OK") %></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ defmodule BDS.Desktop.ShellLive.OverlayManager do
|
|||||||
ShellOverlayComponents.markdown_link(result.title, result.canonical_url)}
|
ShellOverlayComponents.markdown_link(result.title, result.canonical_url)}
|
||||||
)
|
)
|
||||||
|
|
||||||
socket
|
assign(socket, :shell_overlay, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
{%{kind: :insert_media}, %{type: :post, id: post_id}} ->
|
{%{kind: :insert_media}, %{type: :post, id: post_id}} ->
|
||||||
@@ -172,7 +172,7 @@ defmodule BDS.Desktop.ShellLive.OverlayManager do
|
|||||||
end
|
end
|
||||||
|
|
||||||
Notify.parent({:post_editor_insert_content, post_id, syntax})
|
Notify.parent({:post_editor_insert_content, post_id, syntax})
|
||||||
socket
|
assign(socket, :shell_overlay, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
_other ->
|
_other ->
|
||||||
@@ -197,10 +197,11 @@ defmodule BDS.Desktop.ShellLive.OverlayManager do
|
|||||||
|
|
||||||
if details do
|
if details do
|
||||||
Notify.parent({:post_editor_insert_content, post_id, details})
|
Notify.parent({:post_editor_insert_content, post_id, details})
|
||||||
|
assign(socket, :shell_overlay, nil)
|
||||||
|
else
|
||||||
|
socket
|
||||||
end
|
end
|
||||||
|
|
||||||
socket
|
|
||||||
|
|
||||||
_other ->
|
_other ->
|
||||||
socket
|
socket
|
||||||
end
|
end
|
||||||
@@ -215,7 +216,7 @@ defmodule BDS.Desktop.ShellLive.OverlayManager do
|
|||||||
case {socket.assigns[:shell_overlay], current_tab} do
|
case {socket.assigns[:shell_overlay], current_tab} do
|
||||||
{%{kind: :language_picker}, %{type: :post, id: post_id}} ->
|
{%{kind: :language_picker}, %{type: :post, id: post_id}} ->
|
||||||
Notify.parent({:post_editor_translate, post_id, code})
|
Notify.parent({:post_editor_translate, post_id, code})
|
||||||
socket
|
assign(socket, :shell_overlay, nil)
|
||||||
|
|
||||||
{%{kind: :language_picker}, %{type: :media, id: media_id}} ->
|
{%{kind: :language_picker}, %{type: :media, id: media_id}} ->
|
||||||
send_update(MediaEditor,
|
send_update(MediaEditor,
|
||||||
@@ -224,7 +225,7 @@ defmodule BDS.Desktop.ShellLive.OverlayManager do
|
|||||||
language: code
|
language: code
|
||||||
)
|
)
|
||||||
|
|
||||||
socket
|
assign(socket, :shell_overlay, nil)
|
||||||
|
|
||||||
_other ->
|
_other ->
|
||||||
socket
|
socket
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ defmodule BDS.Desktop.ShellLive.PanelRenderer do
|
|||||||
alias BDS.PostLinks
|
alias BDS.PostLinks
|
||||||
alias BDS.Posts
|
alias BDS.Posts
|
||||||
alias BDS.Posts.Post
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.UI.TaskGrouping
|
||||||
use Gettext, backend: BDS.Gettext
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
@doc "Render the active panel tab body."
|
@doc "Render the active panel tab body."
|
||||||
@@ -48,34 +49,122 @@ defmodule BDS.Desktop.ShellLive.PanelRenderer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp render_task_entries(assigns) do
|
defp render_task_entries(assigns) do
|
||||||
|
entries = TaskGrouping.build_task_entries(Map.get(assigns.task_status, :tasks, []))
|
||||||
|
collapsed = Map.get(assigns, :collapsed_task_groups) || MapSet.new()
|
||||||
|
|
||||||
|
assigns =
|
||||||
|
assigns
|
||||||
|
|> assign(:task_entries, entries)
|
||||||
|
|> assign(:collapsed_task_groups, collapsed)
|
||||||
|
|
||||||
~H"""
|
~H"""
|
||||||
<%= if Enum.empty?(Map.get(@task_status, :tasks, [])) do %>
|
<%= if Enum.empty?(@task_entries) do %>
|
||||||
<div class="panel-entry ui-panel-entry panel-empty-state ui-empty-state">
|
<div class="panel-entry ui-panel-entry panel-empty-state ui-empty-state">
|
||||||
<strong><%= dgettext("ui", "Tasks") %></strong>
|
<strong><%= dgettext("ui", "Tasks") %></strong>
|
||||||
<span><%= dgettext("ui", "No background tasks running") %></span>
|
<span><%= dgettext("ui", "No background tasks running") %></span>
|
||||||
</div>
|
</div>
|
||||||
<% else %>
|
<% else %>
|
||||||
<div class="task-list flex flex-col gap-2">
|
<div class="task-list flex flex-col gap-2">
|
||||||
<%= for task <- Map.get(@task_status, :tasks, []) do %>
|
<%= for entry <- @task_entries do %>
|
||||||
<div class="panel-entry ui-panel-entry task-entry flex flex-col gap-2">
|
<%= case entry do %>
|
||||||
<div class="task-entry-header flex items-center justify-between gap-2">
|
<% {:single, task} -> %>
|
||||||
<strong><%= task.name %></strong>
|
{render_task_row(assigns, task, false)}
|
||||||
<span class={"task-status task-status-#{task.status}"}><%= Map.get(task, :status_label, task.status |> to_string() |> String.capitalize()) %></span>
|
<% {:group, group_id, group_name, tasks} -> %>
|
||||||
</div>
|
{render_task_group(assigns, group_id, group_name, tasks)}
|
||||||
<span><%= task.message || task.group_name || "" %></span>
|
<% end %>
|
||||||
<%= if is_number(task.progress) do %>
|
|
||||||
<div class="task-progress-row">
|
|
||||||
<progress max="1" value={task.progress}></progress>
|
|
||||||
<span><%= Map.get(task, :progress_label, progress_percent(task.progress)) %></span>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp render_task_group(assigns, group_id, group_name, tasks) do
|
||||||
|
summary = TaskGrouping.summarize_task_group(tasks)
|
||||||
|
expanded = not MapSet.member?(assigns.collapsed_task_groups, group_id)
|
||||||
|
|
||||||
|
assigns =
|
||||||
|
assigns
|
||||||
|
|> assign(:group_id, group_id)
|
||||||
|
|> assign(:group_name, group_name)
|
||||||
|
|> assign(:group_tasks, tasks)
|
||||||
|
|> assign(:group_expanded, expanded)
|
||||||
|
|> assign(:group_meta, task_group_meta(summary))
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<div class="task-group flex flex-col">
|
||||||
|
<button
|
||||||
|
class="panel-entry ui-panel-entry task-entry task-group-toggle flex w-full items-center gap-2 text-left"
|
||||||
|
type="button"
|
||||||
|
phx-click="toggle_task_group"
|
||||||
|
phx-value-group={@group_id}
|
||||||
|
aria-expanded={to_string(@group_expanded)}
|
||||||
|
>
|
||||||
|
<span class="task-group-chevron">{if @group_expanded, do: "▾", else: "▸"}</span>
|
||||||
|
<strong class="task-group-title"><%= @group_name %> (<%= length(@group_tasks) %>)</strong>
|
||||||
|
<span class="task-group-meta text-muted text-xs"><%= @group_meta %></span>
|
||||||
|
</button>
|
||||||
|
<%= if @group_expanded do %>
|
||||||
|
<%= for task <- @group_tasks do %>
|
||||||
|
{render_task_row(assigns, task, true)}
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_task_row(assigns, task, is_child) do
|
||||||
|
assigns =
|
||||||
|
assigns
|
||||||
|
|> assign(:task, task)
|
||||||
|
|> assign(:task_child?, is_child)
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<div class={["panel-entry", "ui-panel-entry", "task-entry", "flex flex-col gap-2", if(@task_child?, do: "ml-4")]}>
|
||||||
|
<div class="task-entry-header flex items-center justify-between gap-2">
|
||||||
|
<strong><%= @task.name %></strong>
|
||||||
|
<span class={"task-status task-status-#{@task.status}"}><%= Map.get(@task, :status_label, @task.status |> to_string() |> String.capitalize()) %></span>
|
||||||
|
</div>
|
||||||
|
<span><%= @task.message || @task.group_name || "" %></span>
|
||||||
|
<%= if is_number(@task.progress) do %>
|
||||||
|
<div class="task-progress-row">
|
||||||
|
<progress max="1" value={@task.progress}></progress>
|
||||||
|
<span><%= Map.get(@task, :progress_label, progress_percent(@task.progress)) %></span>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
<%= if @task.status == :running do %>
|
||||||
|
<div class="task-entry-actions flex justify-end">
|
||||||
|
<button
|
||||||
|
class="ui-button ui-button-secondary inline-flex items-center px-2 py-1 text-xs"
|
||||||
|
data-testid="cancel-task-button"
|
||||||
|
type="button"
|
||||||
|
phx-click="cancel_task"
|
||||||
|
phx-value-id={@task.id}
|
||||||
|
>
|
||||||
|
<%= dgettext("ui", "Cancel") %>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
# "NN% · X Running · Y Pending" — the breakdown only lists non-zero counts,
|
||||||
|
# like the old app's summarizeTaskGroup rendering.
|
||||||
|
defp task_group_meta(summary) do
|
||||||
|
parts =
|
||||||
|
[]
|
||||||
|
|> maybe_append_breakdown(summary.running, dgettext("ui", "Running"))
|
||||||
|
|> maybe_append_breakdown(summary.pending, dgettext("ui", "Pending"))
|
||||||
|
|
||||||
|
suffix = if parts == [], do: "", else: " · " <> Enum.join(parts, " · ")
|
||||||
|
progress_percent(summary.progress) <> suffix
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_append_breakdown(parts, count, _label) when count <= 0, do: parts
|
||||||
|
|
||||||
|
defp maybe_append_breakdown(parts, count, label),
|
||||||
|
do: parts ++ ["#{count} #{label}"]
|
||||||
|
|
||||||
defp render_output_entries(assigns) do
|
defp render_output_entries(assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<%= if Enum.empty?(@output_entries) do %>
|
<%= if Enum.empty?(@output_entries) do %>
|
||||||
@@ -84,6 +173,16 @@ defmodule BDS.Desktop.ShellLive.PanelRenderer do
|
|||||||
<span><%= dgettext("ui", "No shell output yet") %></span>
|
<span><%= dgettext("ui", "No shell output yet") %></span>
|
||||||
</div>
|
</div>
|
||||||
<% else %>
|
<% else %>
|
||||||
|
<div class="output-toolbar flex shrink-0 justify-end">
|
||||||
|
<button
|
||||||
|
class="ui-button ui-button-secondary inline-flex items-center px-2 py-1 text-xs"
|
||||||
|
data-testid="copy-output-button"
|
||||||
|
type="button"
|
||||||
|
phx-click="copy_output"
|
||||||
|
>
|
||||||
|
<%= dgettext("ui", "Copy") %>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<div class="output-list flex flex-col gap-2">
|
<div class="output-list flex flex-col gap-2">
|
||||||
<%= for entry <- @output_entries do %>
|
<%= for entry <- @output_entries do %>
|
||||||
<div class={[
|
<div class={[
|
||||||
@@ -300,5 +399,5 @@ defmodule BDS.Desktop.ShellLive.PanelRenderer do
|
|||||||
"#{rounded}%"
|
"#{rounded}%"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp present?(value), do: value not in [nil, ""]
|
defp present?(value), do: BDS.Values.present?(value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,16 +3,15 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
|
|
||||||
use Phoenix.LiveComponent
|
use Phoenix.LiveComponent
|
||||||
|
|
||||||
alias BDS.{AI, Metadata, Posts, Preview}
|
alias BDS.{AI, Embeddings, Metadata, Posts, Preview, Tasks}
|
||||||
alias BDS.Desktop.ShellData
|
alias BDS.Desktop.ShellData
|
||||||
alias BDS.Desktop.ShellLive.{EditorImageDrop, Notify}
|
alias BDS.Desktop.ShellLive.{EditorImageDrop, Notify}
|
||||||
alias BDS.Desktop.ShellLive.PostEditor.{DraftManagement, ListValues, Persistence, PostMetadata}
|
|
||||||
alias BDS.Posts.Post
|
alias BDS.Posts.Post
|
||||||
alias BDS.Tags
|
alias BDS.Tags
|
||||||
|
alias BDS.UI.PostEditor.{Draft, ListValues, Persistence}
|
||||||
|
|
||||||
import DraftManagement,
|
import Draft,
|
||||||
only: [
|
only: [
|
||||||
current_draft: 4,
|
|
||||||
editing_canonical_language?: 3,
|
editing_canonical_language?: 3,
|
||||||
normalize_language: 2,
|
normalize_language: 2,
|
||||||
normalize_mode: 1,
|
normalize_mode: 1,
|
||||||
@@ -49,7 +48,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
|
|
||||||
use Gettext, backend: BDS.Gettext
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
import PostMetadata,
|
import BDS.UI.PostEditor.Metadata,
|
||||||
only: [
|
only: [
|
||||||
blank?: 1,
|
blank?: 1,
|
||||||
blank_to_nil: 1,
|
blank_to_nil: 1,
|
||||||
@@ -82,6 +81,15 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(%{action: :auto_save} = assigns, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(Map.drop(assigns, [:action]))
|
||||||
|
|> do_save(true)
|
||||||
|
|
||||||
|
{:ok, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def update(%{action: :publish} = assigns, socket) do
|
def update(%{action: :publish} = assigns, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
@@ -109,7 +117,6 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
id: socket.assigns.post_id,
|
id: socket.assigns.post_id,
|
||||||
content: content
|
content: content
|
||||||
})
|
})
|
||||||
|> assign(:shell_overlay, nil)
|
|
||||||
|
|
||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
@@ -123,6 +130,15 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(%{action: :translation_completed, language: language} = assigns, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(Map.drop(assigns, [:action, :language]))
|
||||||
|
|> apply_translation_completed(language)
|
||||||
|
|
||||||
|
{:ok, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def update(%{action: :apply_ai_suggestions, fields: fields} = assigns, socket) do
|
def update(%{action: :apply_ai_suggestions, fields: fields} = assigns, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
@@ -279,6 +295,10 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
{:noreply, do_detect_language(socket)}
|
{:noreply, do_detect_language(socket)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_event("focus_post_editor_tags", _params, socket) do
|
||||||
|
{:noreply, socket |> load_tag_semantic_suggestions() |> build_data()}
|
||||||
|
end
|
||||||
|
|
||||||
def handle_event("add_post_editor_tag", %{"tag" => tag}, socket) do
|
def handle_event("add_post_editor_tag", %{"tag" => tag}, socket) do
|
||||||
{:noreply, do_add_list_value(socket, :tags, tag)}
|
{:noreply, do_add_list_value(socket, :tags, tag)}
|
||||||
end
|
end
|
||||||
@@ -295,18 +315,6 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
{:noreply, do_remove_list_value(socket, :categories, category)}
|
{:noreply, do_remove_list_value(socket, :categories, category)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("insert_content", %{"content" => content}, socket) do
|
|
||||||
socket =
|
|
||||||
socket
|
|
||||||
|> Phoenix.LiveView.push_event("post-editor-insert-content", %{
|
|
||||||
id: socket.assigns.post_id,
|
|
||||||
content: content
|
|
||||||
})
|
|
||||||
|> assign(:shell_overlay, nil)
|
|
||||||
|
|
||||||
{:noreply, socket}
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_event("close_quick_actions", _params, socket) do
|
def handle_event("close_quick_actions", _params, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
@@ -316,6 +324,19 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Semantic tag suggestions read the embeddings index (no live model call), so
|
||||||
|
# they work offline; suggest_tags/2 returns [] when similarity is disabled.
|
||||||
|
defp load_tag_semantic_suggestions(socket) do
|
||||||
|
case socket.assigns.post do
|
||||||
|
%Post{} = post ->
|
||||||
|
{:ok, suggestions} = Embeddings.suggest_tags(post.id, [])
|
||||||
|
assign(socket, :tag_semantic_suggestions, suggestions)
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp component_current_draft(socket, post, metadata, active_language) do
|
defp component_current_draft(socket, post, metadata, active_language) do
|
||||||
persisted = persisted_form(post, metadata, active_language)
|
persisted = persisted_form(post, metadata, active_language)
|
||||||
Map.get(socket.assigns.drafts, active_language, persisted)
|
Map.get(socket.assigns.drafts, active_language, persisted)
|
||||||
@@ -335,6 +356,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
active_language: canonical,
|
active_language: canonical,
|
||||||
drafts: %{},
|
drafts: %{},
|
||||||
tag_query: "",
|
tag_query: "",
|
||||||
|
tag_semantic_suggestions: [],
|
||||||
category_query: "",
|
category_query: "",
|
||||||
quick_actions_open?: false,
|
quick_actions_open?: false,
|
||||||
mode: :markdown,
|
mode: :markdown,
|
||||||
@@ -426,6 +448,11 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
Tags.list_tags(post.project_id),
|
Tags.list_tags(post.project_id),
|
||||||
socket.assigns.tag_query
|
socket.assigns.tag_query
|
||||||
),
|
),
|
||||||
|
tag_semantic_suggestions:
|
||||||
|
Enum.reject(
|
||||||
|
socket.assigns.tag_semantic_suggestions || [],
|
||||||
|
&(&1 in tag_values(form))
|
||||||
|
),
|
||||||
category_suggestions:
|
category_suggestions:
|
||||||
category_suggestions(
|
category_suggestions(
|
||||||
form,
|
form,
|
||||||
@@ -451,7 +478,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_save(socket) do
|
defp do_save(socket, keep_draft? \\ false) do
|
||||||
post = socket.assigns.post
|
post = socket.assigns.post
|
||||||
|
|
||||||
case post do
|
case post do
|
||||||
@@ -463,15 +490,26 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
active_language = socket.assigns.active_language
|
active_language = socket.assigns.active_language
|
||||||
draft = component_current_draft(socket, post, metadata, active_language)
|
draft = component_current_draft(socket, post, metadata, active_language)
|
||||||
|
|
||||||
case persist(post, draft, active_language, metadata, :save) do
|
action = if keep_draft?, do: :auto_save, else: :save
|
||||||
|
|
||||||
|
case persist(post, draft, active_language, metadata, action) do
|
||||||
{:ok, record} ->
|
{:ok, record} ->
|
||||||
refreshed_post = Posts.get_post!(post.id)
|
refreshed_post = Posts.get_post!(post.id)
|
||||||
_refreshed_form = persisted_form(refreshed_post, metadata, active_language)
|
_refreshed_form = persisted_form(refreshed_post, metadata, active_language)
|
||||||
|
|
||||||
|
# Autosave keeps the working draft so the live editor stays the source
|
||||||
|
# of truth; only an explicit save drops it and reloads canonical body.
|
||||||
|
drafts =
|
||||||
|
if keep_draft? do
|
||||||
|
socket.assigns.drafts
|
||||||
|
else
|
||||||
|
Map.delete(socket.assigns.drafts, active_language)
|
||||||
|
end
|
||||||
|
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
|> assign(:post, refreshed_post)
|
|> assign(:post, refreshed_post)
|
||||||
|> assign(:drafts, Map.delete(socket.assigns.drafts, active_language))
|
|> assign(:drafts, drafts)
|
||||||
|> assign(:save_state, save_state_for_action(:save))
|
|> assign(:save_state, save_state_for_action(:save))
|
||||||
|> assign(:dirty?, false)
|
|> assign(:dirty?, false)
|
||||||
|> build_data()
|
|> build_data()
|
||||||
@@ -768,34 +806,56 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
post_id = socket.assigns.post_id
|
post_id = socket.assigns.post_id
|
||||||
normalized_language = normalize_language(language, "")
|
normalized_language = normalize_language(language, "")
|
||||||
source_language = socket.assigns.canonical_language
|
source_language = socket.assigns.canonical_language
|
||||||
|
project_id = socket.assigns.post.project_id
|
||||||
|
parent = self()
|
||||||
|
|
||||||
case AI.translate_post(post_id, normalized_language, source_language: source_language) do
|
{:ok, _task} =
|
||||||
{:ok, translation} ->
|
Tasks.submit_task(
|
||||||
with {:ok, _saved_translation} <-
|
"Translate Post to #{normalized_language}",
|
||||||
Posts.upsert_post_translation(post_id, normalized_language, %{
|
fn report ->
|
||||||
title: translation.title,
|
report.(0.1, "Translating post to #{normalized_language}")
|
||||||
excerpt: translation.excerpt,
|
|
||||||
content: translation.content
|
|
||||||
}) do
|
|
||||||
socket =
|
|
||||||
socket
|
|
||||||
|> assign(:active_language, normalized_language)
|
|
||||||
|> assign(:drafts, Map.delete(socket.assigns.drafts, normalized_language))
|
|
||||||
|> assign(:quick_actions_open?, false)
|
|
||||||
|> build_data()
|
|
||||||
|
|
||||||
Notify.dirty(:post, post_id, false)
|
with {:ok, translation} <-
|
||||||
socket
|
AI.translate_post(post_id, normalized_language,
|
||||||
else
|
source_language: source_language
|
||||||
{:error, reason} ->
|
),
|
||||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
{:ok, _saved_translation} <-
|
||||||
|> build_data()
|
Posts.upsert_post_translation(post_id, normalized_language, %{
|
||||||
end
|
title: translation.title,
|
||||||
|
excerpt: translation.excerpt,
|
||||||
|
content: translation.content
|
||||||
|
}) do
|
||||||
|
report.(1.0, "Post translation complete")
|
||||||
|
send(parent, {:editor_translation_completed, :post, post_id, normalized_language})
|
||||||
|
%{post_id: post_id, language: normalized_language}
|
||||||
|
else
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
%{group_id: project_id, group_name: "AI"}
|
||||||
|
)
|
||||||
|
|
||||||
{:error, reason} ->
|
socket
|
||||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
|> assign(:quick_actions_open?, false)
|
||||||
|
|> build_data()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp apply_translation_completed(socket, language) do
|
||||||
|
case Posts.get_post(socket.assigns.post_id) do
|
||||||
|
nil ->
|
||||||
|
build_data(socket)
|
||||||
|
|
||||||
|
%Post{} = post ->
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:post, post)
|
||||||
|
|> assign(:active_language, language)
|
||||||
|
|> assign(:drafts, Map.delete(socket.assigns.drafts, language))
|
||||||
|> build_data()
|
|> build_data()
|
||||||
end
|
|
||||||
|
Notify.dirty(:post, post.id, false)
|
||||||
|
socket
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -819,7 +879,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
if map_size(attrs) == 0 do
|
if map_size(attrs) == 0 do
|
||||||
assign(socket, :shell_overlay, nil)
|
socket
|
||||||
else
|
else
|
||||||
case Posts.update_post(post_id, attrs) do
|
case Posts.update_post(post_id, attrs) do
|
||||||
{:ok, updated_post} ->
|
{:ok, updated_post} ->
|
||||||
@@ -837,7 +897,6 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
)
|
)
|
||||||
|> assign(:save_state, :dirty)
|
|> assign(:save_state, :dirty)
|
||||||
|> assign(:dirty?, true)
|
|> assign(:dirty?, true)
|
||||||
|> assign(:shell_overlay, nil)
|
|
||||||
|> build_data()
|
|> build_data()
|
||||||
|
|
||||||
Notify.dirty(:post, post_id, true)
|
Notify.dirty(:post, post_id, true)
|
||||||
@@ -941,7 +1000,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
|||||||
active_language = socket.assigns.active_language
|
active_language = socket.assigns.active_language
|
||||||
post = socket.assigns.post
|
post = socket.assigns.post
|
||||||
metadata = socket.assigns.project_metadata
|
metadata = socket.assigns.project_metadata
|
||||||
draft = current_draft(socket.assigns, post, metadata, active_language)
|
draft = component_current_draft(socket, post, metadata, active_language)
|
||||||
updated = Map.put(draft, field, value)
|
updated = Map.put(draft, field, value)
|
||||||
assign(socket, :drafts, Map.put(socket.assigns.drafts, active_language, updated))
|
assign(socket, :drafts, Map.put(socket.assigns.drafts, active_language, updated))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,242 +0,0 @@
|
|||||||
defmodule BDS.Desktop.ShellLive.PostEditor.DraftManagement do
|
|
||||||
@moduledoc false
|
|
||||||
|
|
||||||
import Phoenix.Component, only: [assign: 3]
|
|
||||||
|
|
||||||
alias BDS.Posts
|
|
||||||
alias BDS.Posts.{Post, Translation}
|
|
||||||
alias BDS.Desktop.ShellLive.PostEditor.PostMetadata
|
|
||||||
alias BDS.UI.Workbench
|
|
||||||
|
|
||||||
@spec normalize_mode(term()) :: term()
|
|
||||||
def normalize_mode(mode) when mode in [:markdown, :preview], do: mode
|
|
||||||
@spec normalize_mode(term()) :: term()
|
|
||||||
def normalize_mode("visual"), do: :markdown
|
|
||||||
def normalize_mode("preview"), do: :preview
|
|
||||||
def normalize_mode(_mode), do: :markdown
|
|
||||||
|
|
||||||
@spec normalize_language(term(), term()) :: term()
|
|
||||||
def normalize_language(value, fallback) do
|
|
||||||
case value |> to_string() |> String.trim() do
|
|
||||||
"" -> fallback
|
|
||||||
normalized -> String.downcase(normalized)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec normalize_params(term(), term(), term()) :: term()
|
|
||||||
def normalize_params(params, current_language, next_language) do
|
|
||||||
%{
|
|
||||||
"title" => Map.get(params, "title", ""),
|
|
||||||
"excerpt" => Map.get(params, "excerpt", ""),
|
|
||||||
"content" => Map.get(params, "content", ""),
|
|
||||||
"tags" => Map.get(params, "tags", ""),
|
|
||||||
"categories" => Map.get(params, "categories", ""),
|
|
||||||
"author" => Map.get(params, "author", ""),
|
|
||||||
"language" =>
|
|
||||||
if(current_language == next_language,
|
|
||||||
do: normalize_language(Map.get(params, "language"), current_language),
|
|
||||||
else: next_language
|
|
||||||
),
|
|
||||||
"do_not_translate" => truthy?(Map.get(params, "do_not_translate")),
|
|
||||||
"template_slug" => Map.get(params, "template_slug", "")
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec current_draft(term(), term(), term(), term()) :: term()
|
|
||||||
def current_draft(assigns, %Post{} = post, metadata, active_language) do
|
|
||||||
persisted = persisted_form(post, metadata, active_language)
|
|
||||||
|
|
||||||
assigns.post_editor_drafts
|
|
||||||
|> Map.get(post.id, %{})
|
|
||||||
|> Map.get(active_language, persisted)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec persisted_form(term(), term(), term()) :: term()
|
|
||||||
def persisted_form(%Post{} = post, metadata, active_language) do
|
|
||||||
persisted_form(post, metadata, active_language, PostMetadata.translations(post.id))
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec persisted_form(term(), term(), term(), term()) :: term()
|
|
||||||
def persisted_form(post, metadata, active_language, translations) do
|
|
||||||
canonical_language = PostMetadata.canonical_language(post, metadata)
|
|
||||||
translation = Map.get(translations, active_language)
|
|
||||||
|
|
||||||
if active_language == canonical_language do
|
|
||||||
%{
|
|
||||||
"title" => post.title || "",
|
|
||||||
"excerpt" => post.excerpt || "",
|
|
||||||
"content" => Posts.editor_body(post),
|
|
||||||
"tags" => Enum.join(post.tags || [], ", "),
|
|
||||||
"categories" => Enum.join(post.categories || [], ", "),
|
|
||||||
"author" => post.author || metadata.default_author || "",
|
|
||||||
"language" => canonical_language,
|
|
||||||
"do_not_translate" => post.do_not_translate || false,
|
|
||||||
"template_slug" => post.template_slug || ""
|
|
||||||
}
|
|
||||||
else
|
|
||||||
%{
|
|
||||||
"title" => (translation && translation.title) || "",
|
|
||||||
"excerpt" => (translation && translation.excerpt) || "",
|
|
||||||
"content" => if(translation, do: Posts.editor_body(translation), else: ""),
|
|
||||||
"tags" => Enum.join(post.tags || [], ", "),
|
|
||||||
"categories" => Enum.join(post.categories || [], ", "),
|
|
||||||
"author" => post.author || metadata.default_author || "",
|
|
||||||
"language" => active_language,
|
|
||||||
"do_not_translate" => post.do_not_translate || false,
|
|
||||||
"template_slug" => post.template_slug || ""
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec maybe_update_draft(term(), term(), term(), term(), term(), term(), term()) :: term()
|
|
||||||
def maybe_update_draft(socket, post_id, post, current_language, next_language, draft, true) do
|
|
||||||
workbench = Workbench.mark_dirty(socket.assigns.workbench, :post, post_id)
|
|
||||||
|
|
||||||
socket
|
|
||||||
|> assign(:workbench, workbench)
|
|
||||||
|> assign(
|
|
||||||
:post_editor_drafts,
|
|
||||||
put_nested_map(socket.assigns.post_editor_drafts, post_id, next_language, draft)
|
|
||||||
)
|
|
||||||
|> assign(
|
|
||||||
:post_editor_active_languages,
|
|
||||||
Map.put(socket.assigns.post_editor_active_languages, post_id, next_language)
|
|
||||||
)
|
|
||||||
|> assign(
|
|
||||||
:post_editor_save_states,
|
|
||||||
Map.put(socket.assigns.post_editor_save_states, post_id, :dirty)
|
|
||||||
)
|
|
||||||
|> assign(
|
|
||||||
:tab_meta,
|
|
||||||
Map.put(socket.assigns.tab_meta, {:post, post_id}, %{
|
|
||||||
title: draft["title"],
|
|
||||||
subtitle: Atom.to_string(post.status || :draft)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|> maybe_drop_old_language_draft(post_id, current_language, next_language)
|
|
||||||
end
|
|
||||||
|
|
||||||
def maybe_update_draft(socket, post_id, _post, _current_language, next_language, _draft, false) do
|
|
||||||
assign(
|
|
||||||
socket,
|
|
||||||
:post_editor_active_languages,
|
|
||||||
Map.put(socket.assigns.post_editor_active_languages, post_id, next_language)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec put_draft_field(term(), term(), term(), term(), term(), term()) :: term()
|
|
||||||
def put_draft_field(socket, post_id, post, active_language, field, value) do
|
|
||||||
metadata = PostMetadata.project_metadata(post.project_id)
|
|
||||||
draft = Map.put(current_draft(socket.assigns, post, metadata, active_language), field, value)
|
|
||||||
workbench = Workbench.mark_dirty(socket.assigns.workbench, :post, post_id)
|
|
||||||
|
|
||||||
socket
|
|
||||||
|> assign(:workbench, workbench)
|
|
||||||
|> assign(
|
|
||||||
:post_editor_drafts,
|
|
||||||
put_nested_map(socket.assigns.post_editor_drafts, post_id, active_language, draft)
|
|
||||||
)
|
|
||||||
|> assign(
|
|
||||||
:post_editor_save_states,
|
|
||||||
Map.put(socket.assigns.post_editor_save_states, post_id, :dirty)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec put_query_state(term(), term(), term(), term()) :: term()
|
|
||||||
def put_query_state(socket, post_id, kind, value) do
|
|
||||||
key = query_key(kind)
|
|
||||||
|
|
||||||
assign(
|
|
||||||
socket,
|
|
||||||
key,
|
|
||||||
Map.put(Map.get(socket.assigns, key, %{}), post_id, to_string(value || ""))
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec query_value(term(), term(), term()) :: term()
|
|
||||||
def query_value(assigns, kind, post_id) do
|
|
||||||
assigns
|
|
||||||
|> Map.get(query_key(kind), %{})
|
|
||||||
|> Map.get(post_id, "")
|
|
||||||
end
|
|
||||||
|
|
||||||
defp query_key(:tags), do: :post_editor_tag_queries
|
|
||||||
defp query_key(:categories), do: :post_editor_category_queries
|
|
||||||
|
|
||||||
defp maybe_drop_old_language_draft(socket, _post_id, current_language, next_language)
|
|
||||||
when current_language == next_language,
|
|
||||||
do: socket
|
|
||||||
|
|
||||||
defp maybe_drop_old_language_draft(socket, post_id, current_language, _next_language) do
|
|
||||||
assign(
|
|
||||||
socket,
|
|
||||||
:post_editor_drafts,
|
|
||||||
delete_nested_map(socket.assigns.post_editor_drafts, post_id, current_language)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec toggled_sections(term(), term(), term()) :: term()
|
|
||||||
def toggled_sections(expanded_by_post, post_id, section) do
|
|
||||||
expanded_by_post
|
|
||||||
|> Map.get(post_id, %{metadata: false, excerpt: false})
|
|
||||||
|> Map.put_new(:metadata, false)
|
|
||||||
|> Map.put_new(:excerpt, false)
|
|
||||||
|> Map.update!(section, &(not &1))
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec put_nested_map(term(), term(), term(), term()) :: term()
|
|
||||||
def put_nested_map(map, key, nested_key, value) do
|
|
||||||
Map.update(map, key, %{nested_key => value}, &Map.put(&1, nested_key, value))
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec delete_nested_map(term(), term(), term()) :: term()
|
|
||||||
def delete_nested_map(map, key, nested_key) do
|
|
||||||
case Map.get(map, key) do
|
|
||||||
nil ->
|
|
||||||
map
|
|
||||||
|
|
||||||
nested ->
|
|
||||||
case Map.delete(nested, nested_key) do
|
|
||||||
emptied when map_size(emptied) == 0 -> Map.delete(map, key)
|
|
||||||
remaining -> Map.put(map, key, remaining)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec reload_with_assigned_workbench(term(), term()) :: term()
|
|
||||||
def reload_with_assigned_workbench(socket, reload),
|
|
||||||
do: reload.(socket, socket.assigns.workbench)
|
|
||||||
|
|
||||||
@spec save_state_for_action(term()) :: term()
|
|
||||||
def save_state_for_action(:publish), do: :published
|
|
||||||
def save_state_for_action(_action), do: :saved
|
|
||||||
|
|
||||||
@spec record_title(term(), term()) :: term()
|
|
||||||
def record_title(%Translation{title: title}, post),
|
|
||||||
do: blank_to_nil(title) || post.title || post.slug || post.id
|
|
||||||
|
|
||||||
def record_title(%Post{title: title, slug: slug, id: id}, _post),
|
|
||||||
do: blank_to_nil(title) || blank_to_nil(slug) || id
|
|
||||||
|
|
||||||
@spec record_status(term()) :: term()
|
|
||||||
def record_status(%Translation{status: status}), do: status || :draft
|
|
||||||
def record_status(%Post{status: status}), do: status || :draft
|
|
||||||
|
|
||||||
@spec editing_canonical_language?(term(), term(), term()) :: term()
|
|
||||||
def editing_canonical_language?(translations, active_language, canonical_language) do
|
|
||||||
active_language == canonical_language or not Map.has_key?(translations, active_language)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp truthy?(value) when value in [true, "true", "on", 1, "1"], do: true
|
|
||||||
defp truthy?(_value), do: false
|
|
||||||
|
|
||||||
defp blank_to_nil(value) do
|
|
||||||
value
|
|
||||||
|> to_string()
|
|
||||||
|> String.trim()
|
|
||||||
|> case do
|
|
||||||
"" -> nil
|
|
||||||
trimmed -> trimmed
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -173,9 +173,22 @@
|
|||||||
value={@post_editor.tag_query}
|
value={@post_editor.tag_query}
|
||||||
placeholder={dgettext("ui", "Add tag")}
|
placeholder={dgettext("ui", "Add tag")}
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
|
phx-focus="focus_post_editor_tags"
|
||||||
|
phx-target={@myself}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<%= if String.trim(@post_editor.tag_query || "") == "" and Enum.any?(@post_editor.tag_semantic_suggestions) do %>
|
||||||
|
<div class="tag-suggestions tag-suggestions-semantic mt-2 flex flex-col">
|
||||||
|
<div class="tag-suggestion-section-label"><%= dgettext("ui", "Suggested tags") %></div>
|
||||||
|
<%= for name <- @post_editor.tag_semantic_suggestions do %>
|
||||||
|
<button class="tag-suggestion ai-suggested" type="button" phx-click="add_post_editor_tag" phx-value-tag={name} phx-target={@myself}>
|
||||||
|
<span class="tag-suggestion-name"><%= name %></span>
|
||||||
|
</button>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= if String.trim(@post_editor.tag_query || "") != "" and (Enum.any?(@post_editor.tag_suggestions) or @post_editor.tag_query_addable?) do %>
|
<%= if String.trim(@post_editor.tag_query || "") != "" and (Enum.any?(@post_editor.tag_suggestions) or @post_editor.tag_query_addable?) do %>
|
||||||
<div class="tag-suggestions mt-2 flex flex-col">
|
<div class="tag-suggestions mt-2 flex flex-col">
|
||||||
<%= for tag <- @post_editor.tag_suggestions do %>
|
<%= for tag <- @post_editor.tag_suggestions do %>
|
||||||
@@ -381,7 +394,7 @@
|
|||||||
<div class="editor-toolbar-right ui-toolbar-group flex items-center gap-2">
|
<div class="editor-toolbar-right ui-toolbar-group flex items-center gap-2">
|
||||||
<%= if @post_editor.mode == :markdown do %>
|
<%= if @post_editor.mode == :markdown do %>
|
||||||
<button
|
<button
|
||||||
class="insert-post-link-button"
|
class="insert-post-link-button secondary ui-button ui-button-secondary whitespace-nowrap"
|
||||||
data-testid="editor-toolbar-overlay-button"
|
data-testid="editor-toolbar-overlay-button"
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="open_overlay"
|
phx-click="open_overlay"
|
||||||
@@ -390,7 +403,7 @@
|
|||||||
<%= dgettext("ui", "Insert Link") %>
|
<%= dgettext("ui", "Insert Link") %>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
class="insert-media-button"
|
class="insert-media-button secondary ui-button ui-button-secondary whitespace-nowrap"
|
||||||
data-testid="editor-toolbar-overlay-button"
|
data-testid="editor-toolbar-overlay-button"
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="open_overlay"
|
phx-click="open_overlay"
|
||||||
@@ -399,7 +412,7 @@
|
|||||||
<%= dgettext("ui", "Insert Media") %>
|
<%= dgettext("ui", "Insert Media") %>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
class="add-gallery-images-button"
|
class="add-gallery-images-button secondary ui-button ui-button-secondary whitespace-nowrap"
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="add_gallery_images"
|
phx-click="add_gallery_images"
|
||||||
phx-value-post-id={@post_editor.id}
|
phx-value-post-id={@post_editor.id}
|
||||||
@@ -410,7 +423,7 @@
|
|||||||
|
|
||||||
<%= if @post_editor.gallery_count > 0 do %>
|
<%= if @post_editor.gallery_count > 0 do %>
|
||||||
<button
|
<button
|
||||||
class="gallery-button"
|
class="gallery-button secondary ui-button ui-button-secondary whitespace-nowrap"
|
||||||
data-testid="editor-toolbar-overlay-button"
|
data-testid="editor-toolbar-overlay-button"
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="open_overlay"
|
phx-click="open_overlay"
|
||||||
|
|||||||
@@ -206,11 +206,17 @@ defmodule BDS.Desktop.ShellLive.ScriptEditor do
|
|||||||
%Script{} = script ->
|
%Script{} = script ->
|
||||||
draft = current_draft(socket.assigns, script)
|
draft = current_draft(socket.assigns, script)
|
||||||
|
|
||||||
|
# The sandbox runs in its own process, so streamed script output is
|
||||||
|
# routed back to this LiveView explicitly.
|
||||||
|
live_view = self()
|
||||||
|
title = dgettext("ui", "Scripts")
|
||||||
|
|
||||||
case Scripting.execute_project_script(
|
case Scripting.execute_project_script(
|
||||||
script.project_id,
|
script.project_id,
|
||||||
draft["content"] || "",
|
draft["content"] || "",
|
||||||
draft["entrypoint"] || "main",
|
draft["entrypoint"] || "main",
|
||||||
[]
|
[],
|
||||||
|
on_output: fn text -> Notify.output_to(live_view, title, text, nil, "info") end
|
||||||
) do
|
) do
|
||||||
{:ok, result} ->
|
{:ok, result} ->
|
||||||
notify_output(socket, dgettext("ui", "Scripts"), inspect(result))
|
notify_output(socket, dgettext("ui", "Scripts"), inspect(result))
|
||||||
|
|||||||
@@ -57,6 +57,27 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor do
|
|||||||
@spec handle_event(String.t(), map(), Phoenix.LiveView.Socket.t()) ::
|
@spec handle_event(String.t(), map(), Phoenix.LiveView.Socket.t()) ::
|
||||||
{:noreply, Phoenix.LiveView.Socket.t()}
|
{:noreply, Phoenix.LiveView.Socket.t()}
|
||||||
@impl true
|
@impl true
|
||||||
|
def handle_event("copy_blogmark_bookmarklet", _params, socket) do
|
||||||
|
project_id = socket.assigns.projects.active_project_id
|
||||||
|
bookmarklet = BDS.Scripting.Capabilities.AppShell.blogmark_bookmarklet(project_id: project_id)
|
||||||
|
|
||||||
|
case BDS.Scripting.Capabilities.AppShell.copy_to_clipboard(bookmarklet, []) do
|
||||||
|
true -> Notify.output(dgettext("ui", "Blogmark Bookmarklet"), dgettext("ui", "Bookmarklet copied to clipboard"), "info")
|
||||||
|
_ -> Notify.output(dgettext("ui", "Blogmark Bookmarklet"), dgettext("ui", "Failed to copy bookmarklet to clipboard"), "error")
|
||||||
|
end
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("install_cli_tool", _params, socket) do
|
||||||
|
case BDS.UI.SettingsForm.run_action("install_cli") do
|
||||||
|
{:ok, message} -> LiveToast.send_toast(:info, message)
|
||||||
|
{:error, message} -> LiveToast.send_toast(:error, message)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def handle_event("change_settings_search", %{"query" => query}, socket) do
|
def handle_event("change_settings_search", %{"query" => query}, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
@@ -290,7 +311,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor do
|
|||||||
query,
|
query,
|
||||||
~w(mcp claude copilot gemini opencode mistral codex agent server)
|
~w(mcp claude copilot gemini opencode mistral codex agent server)
|
||||||
),
|
),
|
||||||
data_visible?: section_matches?(query, ~w(data rebuild maintenance folder filesystem)),
|
data_visible?: section_matches?(query, ~w(data rebuild maintenance folder filesystem cli)),
|
||||||
supported_languages: @supported_languages,
|
supported_languages: @supported_languages,
|
||||||
protected_categories: ManagedCategories.protected_categories()
|
protected_categories: ManagedCategories.protected_categories()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
use Phoenix.Component
|
use Phoenix.Component
|
||||||
|
|
||||||
alias BDS.AI
|
alias BDS.AI
|
||||||
|
alias BDS.Desktop.ShellLive.Notify
|
||||||
alias BDS.Desktop.ShellLive.SettingsEditor.EditorSettings
|
alias BDS.Desktop.ShellLive.SettingsEditor.EditorSettings
|
||||||
|
require Logger
|
||||||
use Gettext, backend: BDS.Gettext
|
use Gettext, backend: BDS.Gettext
|
||||||
|
|
||||||
@spec ai_form(term()) :: term()
|
@spec ai_form(term()) :: term()
|
||||||
@@ -70,6 +72,8 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
|
|
||||||
with {:ok, endpoint} <- endpoint_refresh_attrs(endpoint_key, attrs),
|
with {:ok, endpoint} <- endpoint_refresh_attrs(endpoint_key, attrs),
|
||||||
{:ok, models} <- AI.list_endpoint_models(endpoint) do
|
{:ok, models} <- AI.list_endpoint_models(endpoint) do
|
||||||
|
notify_models_loaded(models)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> assign(
|
|> assign(
|
||||||
:settings_editor_endpoint_models,
|
:settings_editor_endpoint_models,
|
||||||
@@ -82,16 +86,45 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
|> reload.(socket.assigns.workbench)
|
|> reload.(socket.assigns.workbench)
|
||||||
else
|
else
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
|
message = ai_error_message(reason)
|
||||||
|
Notify.alert(dgettext("ui", "Could not load models"), message)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> append_output.(dgettext("ui", "AI Settings"), inspect(reason), nil, "error")
|
|> append_output.(dgettext("ui", "AI Settings"), message, nil, "error")
|
||||||
|> reload.(socket.assigns.workbench)
|
|> reload.(socket.assigns.workbench)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec save_ai(term(), term(), term()) :: term()
|
@spec save_ai(term(), term(), term()) :: term()
|
||||||
def save_ai(socket, reload, append_output) do
|
def save_ai(socket, reload, append_output) do
|
||||||
|
do_save_ai(socket, reload, append_output)
|
||||||
|
rescue
|
||||||
|
error ->
|
||||||
|
Logger.error("AI settings save crashed: #{Exception.format(:error, error, __STACKTRACE__)}")
|
||||||
|
surface_ai_error(socket, reload, append_output, error)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_save_ai(socket, reload, append_output) do
|
||||||
attrs = ai_attrs(socket.assigns)
|
attrs = ai_attrs(socket.assigns)
|
||||||
|
|
||||||
|
case save_attrs(attrs) do
|
||||||
|
:ok ->
|
||||||
|
LiveToast.send_toast(:info, dgettext("ui", "AI settings saved."))
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:settings_editor_ai_draft, %{})
|
||||||
|
|> assign(:offline_mode, attrs.offline_mode)
|
||||||
|
|> reload.(socket.assigns.workbench)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
surface_ai_error(socket, reload, append_output, reason)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# The full save pipeline as a pure function so the TUI settings form
|
||||||
|
# (BDS.UI.SettingsForm) drives the exact same writes as the GUI editor.
|
||||||
|
@spec save_attrs(map()) :: :ok | {:error, term()}
|
||||||
|
def save_attrs(attrs) do
|
||||||
with :ok <-
|
with :ok <-
|
||||||
put_endpoint_preferences(
|
put_endpoint_preferences(
|
||||||
:online,
|
:online,
|
||||||
@@ -141,18 +174,40 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
attrs.offline_chat_images
|
attrs.offline_chat_images
|
||||||
),
|
),
|
||||||
:ok <- EditorSettings.put_global_setting("ai.system_prompt", attrs.system_prompt) do
|
:ok <- EditorSettings.put_global_setting("ai.system_prompt", attrs.system_prompt) do
|
||||||
socket
|
:ok
|
||||||
|> assign(:settings_editor_ai_draft, %{})
|
|
||||||
|> assign(:offline_mode, attrs.offline_mode)
|
|
||||||
|> reload.(socket.assigns.workbench)
|
|
||||||
else
|
|
||||||
{:error, reason} ->
|
|
||||||
socket
|
|
||||||
|> append_output.(dgettext("ui", "AI Settings"), inspect(reason), nil, "error")
|
|
||||||
|> reload.(socket.assigns.workbench)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp notify_models_loaded([]) do
|
||||||
|
LiveToast.send_toast(
|
||||||
|
:info,
|
||||||
|
dgettext(
|
||||||
|
"ui",
|
||||||
|
"The endpoint returned no models. You can still type a model name manually."
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp notify_models_loaded(models) do
|
||||||
|
LiveToast.send_toast(
|
||||||
|
:info,
|
||||||
|
dgettext("ui", "Loaded %{count} models.", count: length(models))
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp surface_ai_error(socket, reload, append_output, reason) do
|
||||||
|
message = ai_error_message(reason)
|
||||||
|
Notify.alert(dgettext("ui", "Could not save AI settings"), message)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> append_output.(dgettext("ui", "AI Settings"), message, nil, "error")
|
||||||
|
|> reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp ai_error_message(%{__exception__: true} = error), do: Exception.message(error)
|
||||||
|
defp ai_error_message(reason) when is_binary(reason), do: reason
|
||||||
|
defp ai_error_message(reason), do: inspect(reason)
|
||||||
|
|
||||||
@spec reset_ai_prompt(term(), term(), term()) :: term()
|
@spec reset_ai_prompt(term(), term(), term()) :: term()
|
||||||
def reset_ai_prompt(socket, reload, append_output) do
|
def reset_ai_prompt(socket, reload, append_output) do
|
||||||
case EditorSettings.put_global_setting("ai.system_prompt", "") do
|
case EditorSettings.put_global_setting("ai.system_prompt", "") do
|
||||||
@@ -171,12 +226,16 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
defp safe_endpoint(kind) do
|
defp safe_endpoint(kind) do
|
||||||
case AI.get_endpoint(kind) do
|
case AI.get_endpoint(kind) do
|
||||||
{:ok, ep} -> ep
|
{:ok, ep} -> ep
|
||||||
_error -> nil
|
error ->
|
||||||
|
Logger.debug("swallowed AI settings safe_endpoint error: #{inspect(error)}")
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp ai_attrs(assigns) do
|
defp ai_attrs(assigns) do
|
||||||
draft = Map.get(assigns, :settings_editor_ai_draft, %{})
|
# Merge the stored form with the edit draft so untouched fields (URLs, API
|
||||||
|
# keys) are preserved on save instead of being read as nil and wiped.
|
||||||
|
draft = Map.merge(ai_form(assigns), Map.get(assigns, :settings_editor_ai_draft, %{}))
|
||||||
|
|
||||||
%{
|
%{
|
||||||
online_url: blank_to_nil(Map.get(draft, "online_url")),
|
online_url: blank_to_nil(Map.get(draft, "online_url")),
|
||||||
@@ -309,7 +368,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.AISettings do
|
|||||||
defp normalize_endpoint_result({:ok, _endpoint}), do: :ok
|
defp normalize_endpoint_result({:ok, _endpoint}), do: :ok
|
||||||
defp normalize_endpoint_result({:error, reason}), do: {:error, reason}
|
defp normalize_endpoint_result({:error, reason}), do: {:error, reason}
|
||||||
|
|
||||||
defp truthy?(value), do: value in [true, "true", "on", "1", 1]
|
defp truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
|
|
||||||
defp blank_to_nil(nil), do: nil
|
defp blank_to_nil(nil), do: nil
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.EditorSettings do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp truthy?(value), do: value in [true, "true", "on", "1", 1]
|
defp truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
defp boolean_string(true), do: "true"
|
defp boolean_string(true), do: "true"
|
||||||
defp boolean_string(false), do: "false"
|
defp boolean_string(false), do: "false"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.ManagedCategories do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp truthy?(value), do: value in [true, "true", "on", "1", 1]
|
defp truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
|
|
||||||
defp blank_to_nil(nil), do: nil
|
defp blank_to_nil(nil), do: nil
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.ProjectSettings do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp truthy?(value), do: value in [true, "true", "on", "1", 1]
|
defp truthy?(value), do: BDS.Values.truthy?(value)
|
||||||
|
|
||||||
defp parse_integer(nil, fallback), do: fallback
|
defp parse_integer(nil, fallback), do: fallback
|
||||||
defp parse_integer(value, _fallback) when is_integer(value), do: value
|
defp parse_integer(value, _fallback) when is_integer(value), do: value
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user