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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .agents/notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[1] In this sandbox, dependency installs can fail for two environment reasons: `pnpm install` may require `CI=true` in non-TTY mode, and external registry resolution (`registry.npmjs.org`) may be unavailable, which blocks lockfile/dependency updates.
[2] In this sandbox, dependency installs can fail for two environment reasons: `pnpm install` may require `CI=true` in non-TTY mode (and sometimes `--no-frozen-lockfile` when package.json changed), and external registry resolution (`registry.npmjs.org`) may be unavailable, which blocks lockfile/dependency updates.

[2] In this sandbox, integration tests that bind a local HTTP listener (e.g., `startWebServer` on `127.0.0.1`) can fail with `listen EPERM`; validate those tests in a less restricted environment.
[3] In this sandbox, integration tests or dev servers that bind a local HTTP listener (e.g., `startWebServer`/Vite on `127.0.0.1`) can fail with `listen EPERM`; validate those flows in a less restricted environment.

[0] If `pnpm add` fails with `ERR_PNPM_UNEXPECTED_STORE`, rerun with `--store-dir` pointing to the store path used by current `node_modules` (for this repo: `/Users/sergiishulga/Library/pnpm/store/v10`).

Expand Down Expand Up @@ -36,7 +36,7 @@
[0] When adding a new DB migration, update CLI integration assertions that hard-code `db doctor` migration counts (e.g., expected `applied_count`) to prevent false negatives.
[0] In sandboxed tests, default `~/.stash/audio` can be unwritable; set `STASH_AUDIO_DIR` to a temp path for worker-based TTS tests/commands.
[0] Coqui CLI variants differ: some support `--text_file`, others only `--text`. Keep provider invocation backward compatible by retrying with inline `--text` when `--text_file`/`--progress_bar` is rejected.
[0] CLI integration tests execute `dist/apps/cli/src/cli.js`; always run `pnpm run build` after source edits and before targeted integration runs, otherwise tests can fail against stale command behavior.
[0] CLI integration tests execute `apps/cli/dist/cli.js`; always run `pnpm run build` after source edits and before targeted integration runs, otherwise tests can fail against stale command behavior.
[0] With VS Code `node-terminal` launch running `pnpm run dev -- web`, the debugger UI can show wrapper-process disconnect messages (`Debugger attached` / `Waiting for the debugger to disconnect...`) even while the actual `stash web` child process is still running; verify via `/api/health` and use an attach config for breakpoints in the real process.
[0] `scripts/with-env.mjs` should forward `SIGINT`/`SIGTERM`/`SIGHUP` to its spawned child; otherwise VS Code stop/terminate can kill the wrapper and orphan long-running child processes (e.g., `stash web`) on their ports.
[0] For X/Twitter pages, `page.content()` can include the `<noscript>` "JavaScript is not available" fallback even when Playwright successfully rendered the app DOM. Treat that message as non-authoritative and key extraction off rendered `data-testid` content instead.
Expand All @@ -50,3 +50,4 @@
[0] In SQLite table-rebuild migrations (`__new_*` + rename), `CHECK` constraints must reference unqualified column names; qualifiers like `__new_table.column` can break after rename.
[0] Drizzle SQLite migrator runs migrations in a transaction; `PRAGMA foreign_keys=OFF` inside migration SQL may not disable FK checks. Table-copy migrations should filter/clean orphan rows explicitly (e.g., `INNER JOIN` parents) instead of relying on that pragma.
[0] CI `validate` workflow currently runs `format:check`, `lint`, `test`, and `apps/web` build. Before pushing, run all four locally to avoid fast-follow CI fix commits.
[0] `stash db doctor` does not create parent directories for a missing DB path; if `STASH_DB_PATH` points to a non-existent directory, command can fail with `unable to open database file`.
6 changes: 4 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ pnpm run db:doctor -- --json
```
4. Test CLI behavior from compiled output:
```bash
node dist/apps/cli/src/cli.js --help
node dist/apps/cli/src/cli.js list --help
node apps/cli/dist/cli.js --help
node apps/cli/dist/cli.js list --help
```

## Documentation Maintenance Rule
Expand Down Expand Up @@ -370,3 +370,5 @@ Updates should include:
- Prefer Drizzle ORM for database access in application/runtime code.
- Use raw SQL string queries only when Drizzle does not support the required functionality clearly or safely (for example, specialized migration-runner behavior).
- Prefer explicit return types on functions and methods (especially exported/public APIs and non-trivial helpers).
- Dependency injection pattern: prefer `createXService`/`createCoreServices` factory objects over classes, and inject least-privilege service slices into API route plugins and command handlers.
- In this sandbox, local listener startup (for example Vite/dev servers on `127.0.0.1`) may fail with `listen EPERM`; validate listener-bound flows in a less restricted environment when needed.
8 changes: 7 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
"name": "stash-api-app",
"version": "0.1.0",
"private": true,
"type": "module"
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@stash/core": "workspace:*"
}
}
22 changes: 15 additions & 7 deletions apps/api/src/app/create-api-app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Fastify, { type FastifyInstance } from "fastify"
import { createCoreServices } from "@stash/core"
import { extractRoutes } from "../features/extract/routes.js"
import { inboxRoutes } from "../features/inbox/routes.js"
import { itemRoutes } from "../features/item/routes.js"
Expand All @@ -15,6 +16,10 @@ export function createApiApp(options: CreateApiAppOptions): FastifyInstance {
const server = Fastify({
logger: false,
})
const services = createCoreServices({
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
})

// Fastify's default JSON parser rejects empty bodies (e.g. DELETE with Content-Type: application/json).
// Override it to treat empty bodies as undefined instead of an error.
Expand All @@ -36,13 +41,16 @@ export function createApiApp(options: CreateApiAppOptions): FastifyInstance {
async (api) => {
api.get("/health", async () => ({ ok: true }))

api.register(inboxRoutes, options)
api.register(itemRoutes, options)
api.register(saveRoutes, options)
api.register(tagsRoutes, options)
api.register(statusRoutes, options)
api.register(extractRoutes, options)
api.register(ttsRoutes, options)
api.register(inboxRoutes, { itemsService: services.items })
api.register(itemRoutes, { itemsService: services.items })
api.register(saveRoutes, { itemsService: services.items })
api.register(tagsRoutes, { tagsService: services.tags })
api.register(statusRoutes, { statusService: services.status })
api.register(extractRoutes, { extractService: services.extract })
api.register(ttsRoutes, {
ttsJobsService: services.ttsJobs,
audioDir: options.audioDir,
})
},
{ prefix: "/api" },
)
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FastifyError, FastifyInstance, FastifyReply } from "fastify"

import { StashError } from "../../../../packages/core/src/errors.js"
import { StashError } from "@stash/core"

function sendError(reply: FastifyReply, statusCode: number, code: string, message: string): void {
void reply.code(statusCode).send({
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/app/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import http from "node:http"

import { startTtsWorker } from "../../../../packages/core/src/features/tts/jobs.js"
import { resolveAudioDir } from "../../../../packages/core/src/lib/paths.js"
import { resolveAudioDir, startTtsWorker } from "@stash/core"
import { createApiApp } from "./create-api-app.js"
import { serveStatic } from "./static.js"

Expand Down
15 changes: 7 additions & 8 deletions apps/api/src/features/extract/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FastifyPluginAsync } from "fastify"

import { extractItem } from "../../../../../packages/core/src/features/extract/service.js"
import type { ApiRouteOptions } from "../options.js"
import type { ExtractService } from "@stash/core"
import { parseAutoTags, parseForce, parseItemId } from "./dto.js"

const extractParamsSchema = {
Expand All @@ -20,7 +19,7 @@ const extractBodySchema = {
},
} as const

export const extractRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, options) => {
export const extractRoutes: FastifyPluginAsync<ExtractRoutesOptions> = async (fastify, options) => {
fastify.post(
"/items/:id/extract",
{
Expand All @@ -31,11 +30,7 @@ export const extractRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify
},
async (request) => {
const autoTags = parseAutoTags(request.body)
const result = await extractItem(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
const result = await options.extractService.extractItem(
parseItemId(request.params as Record<string, string>),
{
force: parseForce(request.body),
Expand All @@ -50,3 +45,7 @@ export const extractRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify
},
)
}

export type ExtractRoutesOptions = {
extractService: Pick<ExtractService, "extractItem">
}
6 changes: 1 addition & 5 deletions apps/api/src/features/inbox/dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
ListItemsInput,
ListItemsStatusFilter,
TagMode,
} from "../../../../../packages/core/src/types.js"
import type { ListItemsInput, ListItemsStatusFilter, TagMode } from "@stash/core"
import { asNonNegativeInt, asPositiveInt } from "../../shared/validation/parse.js"

export function parseInboxQuery(query: URLSearchParams): ListItemsInput {
Expand Down
17 changes: 7 additions & 10 deletions apps/api/src/features/inbox/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FastifyPluginAsync } from "fastify"

import { listItems } from "../../../../../packages/core/src/features/items/service.js"
import type { ApiRouteOptions } from "../options.js"
import type { ItemsService } from "@stash/core"
import { getSearchParams } from "../../shared/request/search-params.js"
import { parseInboxQuery } from "./dto.js"

Expand All @@ -24,7 +23,7 @@ const inboxQuerySchema = {
},
} as const

export const inboxRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, options) => {
export const inboxRoutes: FastifyPluginAsync<InboxRoutesOptions> = async (fastify, options) => {
fastify.get(
"/items",
{
Expand All @@ -33,13 +32,7 @@ export const inboxRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify,
},
},
async (request) => {
const result = listItems(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
parseInboxQuery(getSearchParams(request)),
)
const result = options.itemsService.listItems(parseInboxQuery(getSearchParams(request)))

return {
ok: true,
Expand All @@ -48,3 +41,7 @@ export const inboxRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify,
},
)
}

export type InboxRoutesOptions = {
itemsService: Pick<ItemsService, "listItems">
}
18 changes: 7 additions & 11 deletions apps/api/src/features/item/routes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { FastifyPluginAsync } from "fastify"

import { getItem } from "../../../../../packages/core/src/features/items/service.js"
import { StashError } from "../../../../../packages/core/src/errors.js"
import type { ApiRouteOptions } from "../options.js"
import { StashError, type ItemsService } from "@stash/core"
import { parseItemId } from "./dto.js"

const itemParamsSchema = {
Expand All @@ -13,7 +11,7 @@ const itemParamsSchema = {
required: ["id"],
} as const

export const itemRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, options) => {
export const itemRoutes: FastifyPluginAsync<ItemRoutesOptions> = async (fastify, options) => {
fastify.get(
"/items/:id",
{
Expand All @@ -23,13 +21,7 @@ export const itemRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, o
},
async (request) => {
const itemId = parseItemId(request.params as Record<string, string>)
const item = getItem(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
itemId,
)
const item = options.itemsService.getItem(itemId)

if (!item) {
throw new StashError(`Item ${itemId} not found.`, "NOT_FOUND", 3, 404)
Expand All @@ -42,3 +34,7 @@ export const itemRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, o
},
)
}

export type ItemRoutesOptions = {
itemsService: Pick<ItemsService, "getItem">
}
3 changes: 1 addition & 2 deletions apps/api/src/features/save/dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StashError } from "../../../../../packages/core/src/errors.js"
import type { SaveItemInput } from "../../../../../packages/core/src/types.js"
import { StashError, type SaveItemInput } from "@stash/core"

export function parseSaveBody(body: unknown): SaveItemInput {
if (!body || typeof body !== "object") {
Expand Down
17 changes: 7 additions & 10 deletions apps/api/src/features/save/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FastifyPluginAsync } from "fastify"

import { saveItem } from "../../../../../packages/core/src/features/items/service.js"
import type { ApiRouteOptions } from "../options.js"
import type { ItemsService } from "@stash/core"
import { parseSaveBody } from "./dto.js"

const saveBodySchema = {
Expand All @@ -19,7 +18,7 @@ const saveBodySchema = {
required: ["url"],
} as const

export const saveRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, options) => {
export const saveRoutes: FastifyPluginAsync<SaveRoutesOptions> = async (fastify, options) => {
fastify.post(
"/items",
{
Expand All @@ -28,13 +27,7 @@ export const saveRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, o
},
},
async (request) => {
const result = await saveItem(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
parseSaveBody(request.body),
)
const result = await options.itemsService.saveItem(parseSaveBody(request.body))

return {
ok: true,
Expand All @@ -43,3 +36,7 @@ export const saveRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, o
},
)
}

export type SaveRoutesOptions = {
itemsService: Pick<ItemsService, "saveItem">
}
2 changes: 1 addition & 1 deletion apps/api/src/features/status/dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StashError } from "../../../../../packages/core/src/errors.js"
import { StashError } from "@stash/core"
import { asPositiveInt } from "../../shared/validation/parse.js"

export function parseItemId(params: Record<string, string>): number {
Expand Down
25 changes: 8 additions & 17 deletions apps/api/src/features/status/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FastifyPluginAsync } from "fastify"

import { markRead, markUnread } from "../../../../../packages/core/src/features/status/service.js"
import type { ApiRouteOptions } from "../options.js"
import type { StatusService } from "@stash/core"
import { parseItemId, parseStatusBody } from "./dto.js"

const statusParamsSchema = {
Expand All @@ -23,7 +22,7 @@ const statusBodySchema = {
required: ["status"],
} as const

export const statusRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify, options) => {
export const statusRoutes: FastifyPluginAsync<StatusRoutesOptions> = async (fastify, options) => {
fastify.patch(
"/items/:id/status",
{
Expand All @@ -39,20 +38,8 @@ export const statusRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify,

const result =
status === "read"
? markRead(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
itemId,
)
: markUnread(
{
dbPath: options.dbPath,
migrationsDir: options.migrationsDir,
},
itemId,
)
? options.statusService.markRead(itemId)
: options.statusService.markUnread(itemId)

return {
ok: true,
Expand All @@ -61,3 +48,7 @@ export const statusRoutes: FastifyPluginAsync<ApiRouteOptions> = async (fastify,
},
)
}

export type StatusRoutesOptions = {
statusService: Pick<StatusService, "markRead" | "markUnread">
}
2 changes: 1 addition & 1 deletion apps/api/src/features/tags/dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StashError } from "../../../../../packages/core/src/errors.js"
import { StashError } from "@stash/core"
import { asNonNegativeInt, asPositiveInt } from "../../shared/validation/parse.js"

export function parseTagsListQuery(query: URLSearchParams): { limit: number; offset: number } {
Expand Down
Loading