pi-obsidian-memory 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +173 -7
- package/package.json +41 -11
- package/skills/obsidian-memory/SKILL.md +101 -0
- package/src/domain/capture/constants.ts +5 -0
- package/src/domain/capture/index.ts +6 -0
- package/src/domain/capture/input-problems.ts +9 -0
- package/src/domain/capture/schemas.ts +92 -0
- package/src/domain/capture/tally.ts +26 -0
- package/src/domain/capture/types.ts +49 -0
- package/src/domain/capture/validate.ts +75 -0
- package/src/domain/command-flags/constants.ts +141 -0
- package/src/domain/command-flags/index.ts +1 -0
- package/src/domain/config/change.ts +105 -0
- package/src/domain/config/constants.ts +25 -0
- package/src/domain/config/diagnostics.ts +35 -0
- package/src/domain/config/disclosures.ts +8 -0
- package/src/domain/config/fields.ts +7 -0
- package/src/domain/config/index.ts +11 -0
- package/src/domain/config/inspection.ts +43 -0
- package/src/domain/config/invariants.ts +68 -0
- package/src/domain/config/legacy-capture.ts +17 -0
- package/src/domain/config/observations.ts +109 -0
- package/src/domain/config/parse.ts +25 -0
- package/src/domain/config/paths.ts +61 -0
- package/src/domain/config/registration.ts +34 -0
- package/src/domain/config/repair.ts +36 -0
- package/src/domain/config/resources.ts +65 -0
- package/src/domain/config/rules.ts +49 -0
- package/src/domain/config/schemas.ts +155 -0
- package/src/domain/config/settings.ts +23 -0
- package/src/domain/config/types.ts +239 -0
- package/src/domain/decision-model/constants.ts +7 -0
- package/src/domain/decision-model/cutoffs.ts +8 -0
- package/src/domain/decision-model/endpoint.ts +11 -0
- package/src/domain/decision-model/failure-detail.ts +8 -0
- package/src/domain/decision-model/index.ts +6 -0
- package/src/domain/decision-model/rank.ts +36 -0
- package/src/domain/decision-model/types.ts +77 -0
- package/src/domain/embeddings/constants.ts +55 -0
- package/src/domain/embeddings/identity.ts +30 -0
- package/src/domain/embeddings/index.ts +5 -0
- package/src/domain/embeddings/schemas.ts +25 -0
- package/src/domain/embeddings/types.ts +52 -0
- package/src/domain/embeddings/vectors.ts +53 -0
- package/src/domain/folder-index/constants.ts +9 -0
- package/src/domain/folder-index/display.ts +22 -0
- package/src/domain/folder-index/index.ts +5 -0
- package/src/domain/folder-index/listing.ts +58 -0
- package/src/domain/folder-index/render.ts +114 -0
- package/src/domain/folder-index/types.ts +41 -0
- package/src/domain/index.ts +22 -0
- package/src/domain/lifecycle/index.ts +2 -0
- package/src/domain/lifecycle/lifecycle.ts +39 -0
- package/src/domain/lifecycle/types.ts +17 -0
- package/src/domain/loaded-memory/budget.ts +14 -0
- package/src/domain/loaded-memory/constants.ts +9 -0
- package/src/domain/loaded-memory/index.ts +4 -0
- package/src/domain/loaded-memory/render.ts +30 -0
- package/src/domain/loaded-memory/types.ts +19 -0
- package/src/domain/memory/draft.ts +12 -0
- package/src/domain/memory/index.ts +4 -0
- package/src/domain/memory/schemas.ts +15 -0
- package/src/domain/memory/target.ts +27 -0
- package/src/domain/memory/types.ts +20 -0
- package/src/domain/memory-agent/index.ts +5 -0
- package/src/domain/memory-agent/input-problems.ts +9 -0
- package/src/domain/memory-agent/refusal.ts +6 -0
- package/src/domain/memory-agent/schemas.ts +112 -0
- package/src/domain/memory-agent/types.ts +25 -0
- package/src/domain/memory-agent/validate.ts +67 -0
- package/src/domain/names/constants.ts +32 -0
- package/src/domain/names/index.ts +2 -0
- package/src/domain/names/names.ts +83 -0
- package/src/domain/note/check.ts +68 -0
- package/src/domain/note/constants.ts +15 -0
- package/src/domain/note/frontmatter-fields.ts +175 -0
- package/src/domain/note/frontmatter.ts +138 -0
- package/src/domain/note/index.ts +9 -0
- package/src/domain/note/parse.ts +41 -0
- package/src/domain/note/render.ts +27 -0
- package/src/domain/note/schema-errors.ts +41 -0
- package/src/domain/note/schemas.ts +38 -0
- package/src/domain/note/types.ts +53 -0
- package/src/domain/note/validate.ts +19 -0
- package/src/domain/path-platform/index.ts +2 -0
- package/src/domain/path-platform/path-module.ts +11 -0
- package/src/domain/path-platform/types.ts +1 -0
- package/src/domain/search-catalog/constants.ts +1 -0
- package/src/domain/search-catalog/coverage.ts +35 -0
- package/src/domain/search-catalog/dont-forget.ts +10 -0
- package/src/domain/search-catalog/fingerprint.ts +9 -0
- package/src/domain/search-catalog/index.ts +10 -0
- package/src/domain/search-catalog/inspection.ts +47 -0
- package/src/domain/search-catalog/plan.ts +68 -0
- package/src/domain/search-catalog/ranking.ts +27 -0
- package/src/domain/search-catalog/schemas.ts +49 -0
- package/src/domain/search-catalog/types.ts +103 -0
- package/src/domain/search-catalog/vector-refresh.ts +22 -0
- package/src/domain/shell-edit/command-segments.ts +28 -0
- package/src/domain/shell-edit/command-words.ts +22 -0
- package/src/domain/shell-edit/constants.ts +125 -0
- package/src/domain/shell-edit/executed-files.ts +154 -0
- package/src/domain/shell-edit/index.ts +6 -0
- package/src/domain/shell-edit/powershell-files.ts +38 -0
- package/src/domain/shell-edit/rules.ts +19 -0
- package/src/domain/shell-edit/types.ts +75 -0
- package/src/domain/tool-flags/constants.ts +178 -0
- package/src/domain/tool-flags/index.ts +2 -0
- package/src/domain/tool-flags/types.ts +7 -0
- package/src/domain/validation/constants.ts +1 -0
- package/src/domain/validation/describe.ts +137 -0
- package/src/domain/validation/index.ts +3 -0
- package/src/domain/validation/types.ts +3 -0
- package/src/domain/vault-access/absolute-candidate.ts +81 -0
- package/src/domain/vault-access/argument-tokens.ts +22 -0
- package/src/domain/vault-access/command-mentions-vault.ts +115 -0
- package/src/domain/vault-access/constants.ts +54 -0
- package/src/domain/vault-access/index.ts +7 -0
- package/src/domain/vault-access/types.ts +18 -0
- package/src/domain/vault-access/vault-access-refusal.ts +36 -0
- package/src/domain/vault-access/vault-location.ts +22 -0
- package/src/domain/vault-assets/index.ts +2 -0
- package/src/domain/vault-assets/render.ts +51 -0
- package/src/domain/vault-assets/types.ts +36 -0
- package/src/domain/vault-catalog/analyze.ts +37 -0
- package/src/domain/vault-catalog/classify.ts +37 -0
- package/src/domain/vault-catalog/dont-forget.ts +6 -0
- package/src/domain/vault-catalog/export-entries.ts +10 -0
- package/src/domain/vault-catalog/index.ts +5 -0
- package/src/domain/vault-catalog/types.ts +114 -0
- package/src/domain/vault-diagnosis/diagnose.ts +79 -0
- package/src/domain/vault-diagnosis/index.ts +2 -0
- package/src/domain/vault-diagnosis/types.ts +31 -0
- package/src/domain/vault-path/constants.ts +13 -0
- package/src/domain/vault-path/index.ts +2 -0
- package/src/domain/vault-path/vault-path.ts +65 -0
- package/src/pi/capture/cadence.ts +49 -0
- package/src/pi/capture/completion.ts +26 -0
- package/src/pi/capture/constants.ts +5 -0
- package/src/pi/capture/handlers.ts +30 -0
- package/src/pi/capture/index.ts +8 -0
- package/src/pi/capture/runtime.ts +83 -0
- package/src/pi/capture/schemas.ts +8 -0
- package/src/pi/capture/tool-calls.ts +6 -0
- package/src/pi/capture/transcript.ts +55 -0
- package/src/pi/capture/types.ts +46 -0
- package/src/pi/change-report/constants.ts +15 -0
- package/src/pi/change-report/index.ts +3 -0
- package/src/pi/change-report/report.ts +54 -0
- package/src/pi/change-report/types.ts +8 -0
- package/src/pi/context/constants.ts +5 -0
- package/src/pi/context/index.ts +3 -0
- package/src/pi/context/status.ts +15 -0
- package/src/pi/context/types.ts +98 -0
- package/src/pi/extension.ts +100 -0
- package/src/pi/index.ts +26 -0
- package/src/pi/memory-add/constants.ts +13 -0
- package/src/pi/memory-add/index.ts +5 -0
- package/src/pi/memory-add/memory-add.ts +31 -0
- package/src/pi/memory-add/respond.ts +55 -0
- package/src/pi/memory-add/schemas.ts +29 -0
- package/src/pi/memory-add/types.ts +15 -0
- package/src/pi/memory-archive/constants.ts +8 -0
- package/src/pi/memory-archive/index.ts +4 -0
- package/src/pi/memory-archive/memory-archive.ts +29 -0
- package/src/pi/memory-archive/schemas.ts +15 -0
- package/src/pi/memory-archive/types.ts +8 -0
- package/src/pi/memory-block/block.ts +75 -0
- package/src/pi/memory-block/constants.ts +24 -0
- package/src/pi/memory-block/index.ts +4 -0
- package/src/pi/memory-block/render.ts +54 -0
- package/src/pi/memory-block/types.ts +24 -0
- package/src/pi/memory-doctor/constants.ts +43 -0
- package/src/pi/memory-doctor/index.ts +4 -0
- package/src/pi/memory-doctor/memory-doctor.ts +135 -0
- package/src/pi/memory-doctor/report.ts +101 -0
- package/src/pi/memory-doctor/types.ts +13 -0
- package/src/pi/memory-export/constants.ts +15 -0
- package/src/pi/memory-export/index.ts +4 -0
- package/src/pi/memory-export/memory-export.ts +57 -0
- package/src/pi/memory-export/name.ts +9 -0
- package/src/pi/memory-export/types.ts +4 -0
- package/src/pi/memory-get/constants.ts +12 -0
- package/src/pi/memory-get/index.ts +5 -0
- package/src/pi/memory-get/memory-get.ts +23 -0
- package/src/pi/memory-get/respond.ts +38 -0
- package/src/pi/memory-get/schemas.ts +13 -0
- package/src/pi/memory-get/types.ts +10 -0
- package/src/pi/memory-init/constants.ts +17 -0
- package/src/pi/memory-init/index.ts +2 -0
- package/src/pi/memory-init/memory-init.ts +63 -0
- package/src/pi/memory-search/constants.ts +16 -0
- package/src/pi/memory-search/index.ts +5 -0
- package/src/pi/memory-search/memory-search.ts +22 -0
- package/src/pi/memory-search/respond.ts +62 -0
- package/src/pi/memory-search/schemas.ts +11 -0
- package/src/pi/memory-search/types.ts +10 -0
- package/src/pi/memory-session/constants.ts +7 -0
- package/src/pi/memory-session/index.ts +4 -0
- package/src/pi/memory-session/keeper.ts +101 -0
- package/src/pi/memory-session/respond.ts +6 -0
- package/src/pi/memory-session/types.ts +23 -0
- package/src/pi/memory-settings/constants.ts +69 -0
- package/src/pi/memory-settings/edits.ts +46 -0
- package/src/pi/memory-settings/index.ts +9 -0
- package/src/pi/memory-settings/lines.ts +119 -0
- package/src/pi/memory-settings/models.ts +81 -0
- package/src/pi/memory-settings/projects.ts +46 -0
- package/src/pi/memory-settings/providers.ts +77 -0
- package/src/pi/memory-settings/save.ts +16 -0
- package/src/pi/memory-settings/screen.ts +27 -0
- package/src/pi/memory-settings/types.ts +87 -0
- package/src/pi/memory-setup/constants.ts +33 -0
- package/src/pi/memory-setup/disclosure.ts +22 -0
- package/src/pi/memory-setup/index.ts +4 -0
- package/src/pi/memory-setup/memory-setup.ts +153 -0
- package/src/pi/memory-setup/types.ts +6 -0
- package/src/pi/memory-succeed/constants.ts +10 -0
- package/src/pi/memory-succeed/index.ts +4 -0
- package/src/pi/memory-succeed/memory-succeed.ts +34 -0
- package/src/pi/memory-succeed/schemas.ts +30 -0
- package/src/pi/memory-succeed/types.ts +8 -0
- package/src/pi/memory-update/constants.ts +8 -0
- package/src/pi/memory-update/index.ts +4 -0
- package/src/pi/memory-update/memory-update.ts +29 -0
- package/src/pi/memory-update/schemas.ts +24 -0
- package/src/pi/memory-update/types.ts +8 -0
- package/src/pi/paths/constants.ts +7 -0
- package/src/pi/paths/index.ts +2 -0
- package/src/pi/paths/paths.ts +15 -0
- package/src/pi/paths/platform.ts +7 -0
- package/src/pi/registrar.ts +25 -0
- package/src/pi/registration-report/constants.ts +13 -0
- package/src/pi/registration-report/index.ts +2 -0
- package/src/pi/registration-report/report.ts +26 -0
- package/src/pi/relevant-memory/constants.ts +3 -0
- package/src/pi/relevant-memory/index.ts +4 -0
- package/src/pi/relevant-memory/prompt.ts +23 -0
- package/src/pi/relevant-memory/runtime.ts +88 -0
- package/src/pi/relevant-memory/types.ts +14 -0
- package/src/pi/semantic/constants.ts +3 -0
- package/src/pi/semantic/index.ts +2 -0
- package/src/pi/semantic/runtime.ts +59 -0
- package/src/pi/session-start/constants.ts +7 -0
- package/src/pi/session-start/index.ts +3 -0
- package/src/pi/session-start/session-start.ts +48 -0
- package/src/pi/session-start/types.ts +5 -0
- package/src/pi/shell-edit-guard/guard.ts +41 -0
- package/src/pi/shell-edit-guard/index.ts +3 -0
- package/src/pi/shell-edit-guard/prompt.ts +30 -0
- package/src/pi/shell-edit-guard/types.ts +13 -0
- package/src/pi/types.ts +44 -0
- package/src/pi/vault-guard/constants.ts +1 -0
- package/src/pi/vault-guard/index.ts +4 -0
- package/src/pi/vault-guard/request.ts +39 -0
- package/src/pi/vault-guard/types.ts +5 -0
- package/src/pi/vault-guard/vault-guard.ts +21 -0
- package/src/services/capture-cadence/domain-rules.ts +8 -0
- package/src/services/capture-cadence/index.ts +1 -0
- package/src/services/clock/clock.ts +7 -0
- package/src/services/clock/index.ts +1 -0
- package/src/services/config/canonicalize.ts +163 -0
- package/src/services/config/constants.ts +1 -0
- package/src/services/config/fully-qualified-path.ts +14 -0
- package/src/services/config/index.ts +12 -0
- package/src/services/config/path-identity.ts +65 -0
- package/src/services/config/publication.ts +75 -0
- package/src/services/config/read.ts +47 -0
- package/src/services/config/registration.ts +26 -0
- package/src/services/config/resolve-input.ts +27 -0
- package/src/services/config/snapshot.ts +26 -0
- package/src/services/config/target.ts +2 -0
- package/src/services/config/types.ts +97 -0
- package/src/services/config/vault-access.ts +2 -0
- package/src/services/config/volume-namespace.ts +5 -0
- package/src/services/config/write.ts +63 -0
- package/src/services/decision-model/constants.ts +16 -0
- package/src/services/decision-model/decide.ts +42 -0
- package/src/services/decision-model/diagnostics.ts +6 -0
- package/src/services/decision-model/endpoint.ts +1 -0
- package/src/services/decision-model/failure-detail.ts +1 -0
- package/src/services/decision-model/index.ts +7 -0
- package/src/services/decision-model/interruption.ts +29 -0
- package/src/services/decision-model/jev.ts +61 -0
- package/src/services/decision-model/laya-batch.ts +64 -0
- package/src/services/decision-model/laya-http-failure.ts +41 -0
- package/src/services/decision-model/laya-response.ts +95 -0
- package/src/services/decision-model/laya.ts +35 -0
- package/src/services/decision-model/preflight.ts +24 -0
- package/src/services/decision-model/prompts.ts +36 -0
- package/src/services/decision-model/schemas.ts +43 -0
- package/src/services/decision-model/score-candidates.ts +41 -0
- package/src/services/decision-model/types.ts +182 -0
- package/src/services/embeddings/asset-download.ts +45 -0
- package/src/services/embeddings/asset-fetch.ts +39 -0
- package/src/services/embeddings/asset-inspection.ts +36 -0
- package/src/services/embeddings/batch.ts +32 -0
- package/src/services/embeddings/chunks.ts +80 -0
- package/src/services/embeddings/configuration.ts +8 -0
- package/src/services/embeddings/constants.ts +24 -0
- package/src/services/embeddings/errors.ts +11 -0
- package/src/services/embeddings/google.ts +90 -0
- package/src/services/embeddings/http.ts +64 -0
- package/src/services/embeddings/index.ts +17 -0
- package/src/services/embeddings/inference.ts +35 -0
- package/src/services/embeddings/local.ts +138 -0
- package/src/services/embeddings/model-asset.ts +68 -0
- package/src/services/embeddings/model-cache.ts +30 -0
- package/src/services/embeddings/openai.ts +64 -0
- package/src/services/embeddings/operation.ts +31 -0
- package/src/services/embeddings/responses.ts +41 -0
- package/src/services/embeddings/runtime.ts +13 -0
- package/src/services/embeddings/schemas.ts +53 -0
- package/src/services/embeddings/types.ts +116 -0
- package/src/services/id/id.ts +5 -0
- package/src/services/id/index.ts +1 -0
- package/src/services/index.ts +11 -0
- package/src/services/llm/capture.ts +19 -0
- package/src/services/llm/check-chat-models.ts +38 -0
- package/src/services/llm/complete.ts +57 -0
- package/src/services/llm/constants.ts +32 -0
- package/src/services/llm/domain-rules.ts +2 -0
- package/src/services/llm/index.ts +6 -0
- package/src/services/llm/lifetime.ts +16 -0
- package/src/services/llm/retrieve.ts +19 -0
- package/src/services/llm/schemas.ts +9 -0
- package/src/services/llm/types.ts +94 -0
- package/src/services/search-index/catalog-query.ts +34 -0
- package/src/services/search-index/catalog-statements.ts +33 -0
- package/src/services/search-index/catalog.ts +19 -0
- package/src/services/search-index/cleanup.ts +13 -0
- package/src/services/search-index/close.ts +5 -0
- package/src/services/search-index/constants.ts +102 -0
- package/src/services/search-index/dont-forget.ts +1 -0
- package/src/services/search-index/fingerprint.ts +17 -0
- package/src/services/search-index/folder-index-statements.ts +17 -0
- package/src/services/search-index/folder-index-storage.ts +54 -0
- package/src/services/search-index/folder-indexes.ts +13 -0
- package/src/services/search-index/index.ts +14 -0
- package/src/services/search-index/inspection.ts +1 -0
- package/src/services/search-index/inventory.ts +67 -0
- package/src/services/search-index/legacy.ts +64 -0
- package/src/services/search-index/location.ts +12 -0
- package/src/services/search-index/open.ts +46 -0
- package/src/services/search-index/query.ts +39 -0
- package/src/services/search-index/ranking.ts +12 -0
- package/src/services/search-index/reconcile.ts +93 -0
- package/src/services/search-index/recovery.ts +56 -0
- package/src/services/search-index/refusals.ts +21 -0
- package/src/services/search-index/schema.ts +42 -0
- package/src/services/search-index/schemas.ts +181 -0
- package/src/services/search-index/search.ts +5 -0
- package/src/services/search-index/statements.ts +18 -0
- package/src/services/search-index/storage.ts +207 -0
- package/src/services/search-index/types.ts +113 -0
- package/src/services/search-index/vector-statements.ts +18 -0
- package/src/services/search-index/vector-storage.ts +169 -0
- package/src/services/search-index/vectors.ts +18 -0
- package/src/services/search-index/write.ts +9 -0
- package/src/services/shell-edit/ask.ts +53 -0
- package/src/services/shell-edit/domain-rules.ts +2 -0
- package/src/services/shell-edit/index.ts +4 -0
- package/src/services/shell-edit/schemas.ts +23 -0
- package/src/services/shell-edit/types.ts +61 -0
- package/src/services/shell-scripts/constants.ts +1 -0
- package/src/services/shell-scripts/files.ts +16 -0
- package/src/services/shell-scripts/index.ts +5 -0
- package/src/services/shell-scripts/package-scripts.ts +74 -0
- package/src/services/shell-scripts/schemas.ts +3 -0
- package/src/services/shell-scripts/scripts.ts +27 -0
- package/src/services/shell-scripts/types.ts +11 -0
- package/src/services/vault/archive.ts +31 -0
- package/src/services/vault/bootstrap/ensure.ts +50 -0
- package/src/services/vault/bootstrap/index.ts +2 -0
- package/src/services/vault/bootstrap/paths.ts +57 -0
- package/src/services/vault/bootstrap/types.ts +17 -0
- package/src/services/vault/bootstrap/write.ts +47 -0
- package/src/services/vault/catalog/bytes.ts +51 -0
- package/src/services/vault/catalog/dont-forget.ts +1 -0
- package/src/services/vault/catalog/enumerate.ts +69 -0
- package/src/services/vault/catalog/export.ts +21 -0
- package/src/services/vault/catalog/failures.ts +16 -0
- package/src/services/vault/catalog/index.ts +10 -0
- package/src/services/vault/catalog/inspect.ts +41 -0
- package/src/services/vault/catalog/observe.ts +35 -0
- package/src/services/vault/catalog/resolve.ts +94 -0
- package/src/services/vault/catalog/stat.ts +23 -0
- package/src/services/vault/catalog/types.ts +69 -0
- package/src/services/vault/catalog/visit.ts +58 -0
- package/src/services/vault/complete-blank.ts +66 -0
- package/src/services/vault/constants.ts +3 -0
- package/src/services/vault/context/index.ts +1 -0
- package/src/services/vault/context/types.ts +10 -0
- package/src/services/vault/create.ts +19 -0
- package/src/services/vault/display.ts +6 -0
- package/src/services/vault/domain-rules.ts +10 -0
- package/src/services/vault/draft.ts +2 -0
- package/src/services/vault/filesystem/failures.ts +53 -0
- package/src/services/vault/filesystem/hash.ts +5 -0
- package/src/services/vault/filesystem/index.ts +5 -0
- package/src/services/vault/filesystem/read.ts +61 -0
- package/src/services/vault/filesystem/types.ts +36 -0
- package/src/services/vault/filesystem/write.ts +80 -0
- package/src/services/vault/find.ts +27 -0
- package/src/services/vault/folder-contents.ts +31 -0
- package/src/services/vault/folder-index/index.ts +2 -0
- package/src/services/vault/folder-index/read.ts +87 -0
- package/src/services/vault/folder-index/types.ts +19 -0
- package/src/services/vault/folder-listing.ts +1 -0
- package/src/services/vault/index.ts +21 -0
- package/src/services/vault/inspect-folder.ts +13 -0
- package/src/services/vault/list.ts +121 -0
- package/src/services/vault/memory-folder.ts +24 -0
- package/src/services/vault/paths.ts +11 -0
- package/src/services/vault/publish.ts +52 -0
- package/src/services/vault/read.ts +46 -0
- package/src/services/vault/refresh-folders.ts +197 -0
- package/src/services/vault/refresh-plan.ts +79 -0
- package/src/services/vault/refresh.ts +8 -0
- package/src/services/vault/refusals.ts +23 -0
- package/src/services/vault/relocate.ts +131 -0
- package/src/services/vault/relocation-checks.ts +89 -0
- package/src/services/vault/resolve-stored-file.ts +43 -0
- package/src/services/vault/resolve.ts +109 -0
- package/src/services/vault/succeed.ts +45 -0
- package/src/services/vault/types.ts +199 -0
- package/src/services/vault/update-file.ts +58 -0
- package/src/services/vault/update.ts +22 -0
- package/src/services/vault/write-new-file.ts +37 -0
- package/src/shared/index.ts +1 -0
- package/src/shared/result/index.ts +1 -0
- package/src/shared/result/types.ts +17 -0
- package/src/usecases/add-memory/add-memory.ts +83 -0
- package/src/usecases/add-memory/index.ts +4 -0
- package/src/usecases/add-memory/maintenance.ts +23 -0
- package/src/usecases/add-memory/schemas.ts +12 -0
- package/src/usecases/add-memory/types.ts +47 -0
- package/src/usecases/archive-memory/archive-memory.ts +27 -0
- package/src/usecases/archive-memory/index.ts +3 -0
- package/src/usecases/archive-memory/schemas.ts +11 -0
- package/src/usecases/archive-memory/types.ts +17 -0
- package/src/usecases/capture-memory/apply.ts +73 -0
- package/src/usecases/capture-memory/capture-memory.ts +117 -0
- package/src/usecases/capture-memory/constants.ts +15 -0
- package/src/usecases/capture-memory/index.ts +5 -0
- package/src/usecases/capture-memory/observe.ts +122 -0
- package/src/usecases/capture-memory/settings.ts +20 -0
- package/src/usecases/capture-memory/types.ts +103 -0
- package/src/usecases/check-shell-edit/check-shell-edit.ts +28 -0
- package/src/usecases/check-shell-edit/index.ts +2 -0
- package/src/usecases/check-shell-edit/types.ts +30 -0
- package/src/usecases/check-vault-access/check-vault-access.ts +23 -0
- package/src/usecases/check-vault-access/constants.ts +7 -0
- package/src/usecases/check-vault-access/index.ts +4 -0
- package/src/usecases/check-vault-access/raw-device.ts +10 -0
- package/src/usecases/check-vault-access/targets.ts +49 -0
- package/src/usecases/check-vault-access/types.ts +33 -0
- package/src/usecases/complete-blank-memory/complete-blank-memory.ts +48 -0
- package/src/usecases/complete-blank-memory/constants.ts +3 -0
- package/src/usecases/complete-blank-memory/index.ts +4 -0
- package/src/usecases/complete-blank-memory/schemas.ts +14 -0
- package/src/usecases/complete-blank-memory/types.ts +28 -0
- package/src/usecases/diagnose-memory/constants.ts +3 -0
- package/src/usecases/diagnose-memory/decision.ts +36 -0
- package/src/usecases/diagnose-memory/diagnose-memory.ts +52 -0
- package/src/usecases/diagnose-memory/embeddings.ts +39 -0
- package/src/usecases/diagnose-memory/index.ts +5 -0
- package/src/usecases/diagnose-memory/types.ts +86 -0
- package/src/usecases/diagnose-memory/vault.ts +33 -0
- package/src/usecases/export-memory/collect.ts +20 -0
- package/src/usecases/export-memory/constants.ts +3 -0
- package/src/usecases/export-memory/export-memory.ts +84 -0
- package/src/usecases/export-memory/index.ts +3 -0
- package/src/usecases/export-memory/types.ts +28 -0
- package/src/usecases/get-memory/get-memory.ts +97 -0
- package/src/usecases/get-memory/index.ts +2 -0
- package/src/usecases/get-memory/types.ts +25 -0
- package/src/usecases/index.ts +25 -0
- package/src/usecases/list-memory/index.ts +2 -0
- package/src/usecases/list-memory/leased/index.ts +2 -0
- package/src/usecases/list-memory/leased/read.ts +16 -0
- package/src/usecases/list-memory/leased/types.ts +21 -0
- package/src/usecases/list-memory/list-memory.ts +33 -0
- package/src/usecases/list-memory/types.ts +8 -0
- package/src/usecases/load-dont-forget/index.ts +2 -0
- package/src/usecases/load-dont-forget/load-dont-forget.ts +111 -0
- package/src/usecases/load-dont-forget/types.ts +26 -0
- package/src/usecases/memory-change/change.ts +42 -0
- package/src/usecases/memory-change/index.ts +5 -0
- package/src/usecases/memory-change/maintenance.ts +11 -0
- package/src/usecases/memory-change/partial.ts +18 -0
- package/src/usecases/memory-change/receipt.ts +40 -0
- package/src/usecases/memory-change/types.ts +44 -0
- package/src/usecases/operation-context/context.ts +37 -0
- package/src/usecases/operation-context/index.ts +2 -0
- package/src/usecases/operation-context/types.ts +19 -0
- package/src/usecases/registration/check.ts +6 -0
- package/src/usecases/registration/entry.ts +33 -0
- package/src/usecases/registration/index.ts +8 -0
- package/src/usecases/registration/init.ts +94 -0
- package/src/usecases/registration/prepare-init.ts +43 -0
- package/src/usecases/registration/prepare.ts +35 -0
- package/src/usecases/registration/propose.ts +34 -0
- package/src/usecases/registration/publication.ts +90 -0
- package/src/usecases/registration/setup.ts +41 -0
- package/src/usecases/registration/types.ts +107 -0
- package/src/usecases/relevant-memory/agent.ts +55 -0
- package/src/usecases/relevant-memory/constants.ts +28 -0
- package/src/usecases/relevant-memory/cutoffs.ts +29 -0
- package/src/usecases/relevant-memory/decision.ts +102 -0
- package/src/usecases/relevant-memory/index.ts +11 -0
- package/src/usecases/relevant-memory/loaded.ts +31 -0
- package/src/usecases/relevant-memory/refresh.ts +30 -0
- package/src/usecases/relevant-memory/retrieve-memory.ts +62 -0
- package/src/usecases/relevant-memory/schemas.ts +20 -0
- package/src/usecases/relevant-memory/session-memory.ts +53 -0
- package/src/usecases/relevant-memory/track-dont-forget.ts +13 -0
- package/src/usecases/relevant-memory/types.ts +106 -0
- package/src/usecases/resolve-memory/index.ts +2 -0
- package/src/usecases/resolve-memory/leased/index.ts +2 -0
- package/src/usecases/resolve-memory/leased/resolve.ts +109 -0
- package/src/usecases/resolve-memory/leased/types.ts +35 -0
- package/src/usecases/resolve-memory/resolve-memory.ts +23 -0
- package/src/usecases/resolve-memory/types.ts +7 -0
- package/src/usecases/search-memory/constants.ts +1 -0
- package/src/usecases/search-memory/index.ts +3 -0
- package/src/usecases/search-memory/search-memory.ts +132 -0
- package/src/usecases/search-memory/types.ts +39 -0
- package/src/usecases/semantic/constants.ts +7 -0
- package/src/usecases/semantic/index.ts +5 -0
- package/src/usecases/semantic/prepare.ts +30 -0
- package/src/usecases/semantic/rank.ts +83 -0
- package/src/usecases/semantic/refresh-vectors.ts +95 -0
- package/src/usecases/semantic/types.ts +17 -0
- package/src/usecases/semantic-port/index.ts +1 -0
- package/src/usecases/semantic-port/types.ts +20 -0
- package/src/usecases/semantic-provider/constants.ts +6 -0
- package/src/usecases/semantic-provider/index.ts +5 -0
- package/src/usecases/semantic-provider/local.ts +33 -0
- package/src/usecases/semantic-provider/remote.ts +10 -0
- package/src/usecases/semantic-provider/size.ts +10 -0
- package/src/usecases/semantic-provider/types.ts +19 -0
- package/src/usecases/session/index.ts +2 -0
- package/src/usecases/session/lease.ts +70 -0
- package/src/usecases/session/receipts.ts +46 -0
- package/src/usecases/session/state.ts +106 -0
- package/src/usecases/session/types.ts +45 -0
- package/src/usecases/settings/change.ts +142 -0
- package/src/usecases/settings/index.ts +3 -0
- package/src/usecases/settings/prepare.ts +24 -0
- package/src/usecases/settings/types.ts +32 -0
- package/src/usecases/succeed-memory/index.ts +3 -0
- package/src/usecases/succeed-memory/schemas.ts +13 -0
- package/src/usecases/succeed-memory/succeed-memory.ts +34 -0
- package/src/usecases/succeed-memory/types.ts +18 -0
- package/src/usecases/synchronize-memory/index.ts +2 -0
- package/src/usecases/synchronize-memory/leased/aliases.ts +55 -0
- package/src/usecases/synchronize-memory/leased/folder-states.ts +58 -0
- package/src/usecases/synchronize-memory/leased/index.ts +2 -0
- package/src/usecases/synchronize-memory/leased/inspection.ts +32 -0
- package/src/usecases/synchronize-memory/leased/maintenance.ts +41 -0
- package/src/usecases/synchronize-memory/leased/publication.ts +55 -0
- package/src/usecases/synchronize-memory/leased/scan.ts +37 -0
- package/src/usecases/synchronize-memory/leased/synchronize.ts +102 -0
- package/src/usecases/synchronize-memory/leased/types.ts +60 -0
- package/src/usecases/synchronize-memory/synchronize-memory.ts +26 -0
- package/src/usecases/synchronize-memory/types.ts +5 -0
- package/src/usecases/update-memory/index.ts +3 -0
- package/src/usecases/update-memory/schemas.ts +14 -0
- package/src/usecases/update-memory/types.ts +16 -0
- package/src/usecases/update-memory/update-memory.ts +31 -0
- package/CHANGELOG.md +0 -21
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
|
|
3
|
+
import type { PathPlatform } from "#src/domain"
|
|
4
|
+
import { parseConfig } from "#src/domain"
|
|
5
|
+
import { canonicalizeConfigLocation } from "./canonicalize.ts"
|
|
6
|
+
import type { ReadConfigResult } from "./types.ts"
|
|
7
|
+
|
|
8
|
+
export async function readConfig(path: string, platform: PathPlatform): Promise<ReadConfigResult> {
|
|
9
|
+
let text: string
|
|
10
|
+
try {
|
|
11
|
+
text = await readFile(path, "utf8")
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
14
|
+
return {
|
|
15
|
+
succeeded: false,
|
|
16
|
+
reason: "not-configured",
|
|
17
|
+
configPath: path,
|
|
18
|
+
problems: ["memory is not configured"],
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let code = "UNKNOWN"
|
|
23
|
+
if (error instanceof Error && "code" in error && typeof error.code === "string") {
|
|
24
|
+
code = error.code
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
succeeded: false,
|
|
29
|
+
reason: "unreadable",
|
|
30
|
+
configPath: path,
|
|
31
|
+
code,
|
|
32
|
+
problems: [`${path}: cannot read config (${code})`],
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const parsed = parseConfig(text, platform)
|
|
37
|
+
if (!parsed.succeeded) {
|
|
38
|
+
return { succeeded: false, reason: "invalid-config", configPath: path, problems: parsed.problems }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const canonical = await canonicalizeConfigLocation({ config: parsed.output, configPath: path, platform })
|
|
42
|
+
if (!canonical.succeeded) {
|
|
43
|
+
return { succeeded: false, reason: "invalid-config", configPath: path, problems: canonical.problems }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return parsed
|
|
47
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ChatModelReference,
|
|
3
|
+
ConfigFields,
|
|
4
|
+
ConfigRepairDraft,
|
|
5
|
+
InitProposal,
|
|
6
|
+
MemoryConfig,
|
|
7
|
+
ParseConfigResult,
|
|
8
|
+
ProjectEntry,
|
|
9
|
+
ResolveProjectResult,
|
|
10
|
+
SettingChange,
|
|
11
|
+
SettingNumberKind,
|
|
12
|
+
SettingNumberResult,
|
|
13
|
+
SetupDisclosure,
|
|
14
|
+
SetupProposal,
|
|
15
|
+
} from "#src/domain"
|
|
16
|
+
export {
|
|
17
|
+
changeSetting,
|
|
18
|
+
inspectConfigForRepair,
|
|
19
|
+
parseSettingNumber,
|
|
20
|
+
prepareSetupDefaults,
|
|
21
|
+
proposeInit,
|
|
22
|
+
proposeSetup,
|
|
23
|
+
resolveProject,
|
|
24
|
+
setupDisclosures,
|
|
25
|
+
} from "#src/domain"
|
|
26
|
+
export { checkConfigSnapshot } from "./publication.ts"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isAbsolute, parse, resolve, sep } from "node:path"
|
|
2
|
+
|
|
3
|
+
import type { CanonicalPathResult, RegistrationVaultInput } from "./types.ts"
|
|
4
|
+
|
|
5
|
+
export function resolveRegistrationVault(request: RegistrationVaultInput): CanonicalPathResult {
|
|
6
|
+
if (!fullyQualified(request.workingFolder)) {
|
|
7
|
+
return { succeeded: false, problems: ["workingFolder must be a fully qualified absolute path"] }
|
|
8
|
+
}
|
|
9
|
+
if (request.vault.trim() === "" || ambiguousWindowsPath(request.vault)) {
|
|
10
|
+
return { succeeded: false, problems: ["vault must be a nonblank unambiguous path"] }
|
|
11
|
+
}
|
|
12
|
+
if (isAbsolute(request.vault) && !fullyQualified(request.vault)) {
|
|
13
|
+
return { succeeded: false, problems: ["vault must include its absolute root"] }
|
|
14
|
+
}
|
|
15
|
+
return { succeeded: true, output: resolve(request.workingFolder, request.vault) }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function fullyQualified(path: string): boolean {
|
|
19
|
+
if (!isAbsolute(path) || ambiguousWindowsPath(path)) return false
|
|
20
|
+
const root = parse(path).root.replaceAll("\\", "/")
|
|
21
|
+
if (sep === "\\" && !root.includes(":") && !root.startsWith("//")) return false
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function ambiguousWindowsPath(path: string): boolean {
|
|
26
|
+
return /^[A-Za-z]:(?![/\\])/.test(path) || /^\\(?!\\)/.test(path)
|
|
27
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createHash } from "node:crypto"
|
|
2
|
+
import { readFile } from "node:fs/promises"
|
|
3
|
+
|
|
4
|
+
import { assessConfig } from "#src/domain"
|
|
5
|
+
import type { ConfigLocation, ReadConfigSnapshotResult } from "./types.ts"
|
|
6
|
+
|
|
7
|
+
export async function readConfigSnapshot(request: ConfigLocation): Promise<ReadConfigSnapshotResult> {
|
|
8
|
+
try {
|
|
9
|
+
const bytes = await readFile(request.path)
|
|
10
|
+
const text = bytes.toString("utf8")
|
|
11
|
+
return {
|
|
12
|
+
succeeded: true,
|
|
13
|
+
output: {
|
|
14
|
+
state: "present",
|
|
15
|
+
hash: createHash("sha256").update(bytes).digest("hex"),
|
|
16
|
+
text,
|
|
17
|
+
...assessConfig(text, request.platform),
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
22
|
+
return { succeeded: true, output: { state: "missing", hash: null } }
|
|
23
|
+
}
|
|
24
|
+
return { succeeded: false, problems: [`${request.path}: ${String(error)}`] }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { ConfigRepairDraft, MemoryConfig, ParseConfigResult, PathPlatform } from "#src/domain"
|
|
2
|
+
import type { Failed, Succeeded } from "#src/shared"
|
|
3
|
+
|
|
4
|
+
export type ConfigFailureReason = "not-configured" | "invalid-config" | "unreadable"
|
|
5
|
+
|
|
6
|
+
export interface ConfigRefusal extends Failed {
|
|
7
|
+
reason: ConfigFailureReason
|
|
8
|
+
configPath: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ConfigUnreadable extends ConfigRefusal {
|
|
12
|
+
reason: "unreadable"
|
|
13
|
+
code: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ReadConfigResult = Succeeded<MemoryConfig> | ConfigRefusal | ConfigUnreadable
|
|
17
|
+
|
|
18
|
+
export interface ConfigLocation {
|
|
19
|
+
path: string
|
|
20
|
+
platform: PathPlatform
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RegistrationVaultInput {
|
|
24
|
+
vault: string
|
|
25
|
+
workingFolder: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface MissingConfigSnapshot {
|
|
29
|
+
state: "missing"
|
|
30
|
+
hash: null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface PresentConfigSnapshot {
|
|
34
|
+
state: "present"
|
|
35
|
+
hash: string
|
|
36
|
+
text: string
|
|
37
|
+
lexicalParse: ParseConfigResult
|
|
38
|
+
repair: ConfigRepairDraft
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type ConfigSnapshot = MissingConfigSnapshot | PresentConfigSnapshot
|
|
42
|
+
export type ReadConfigSnapshotResult = Succeeded<ConfigSnapshot> | Failed
|
|
43
|
+
|
|
44
|
+
export interface ConfigRegistrationLocation {
|
|
45
|
+
config: MemoryConfig
|
|
46
|
+
configPath: string
|
|
47
|
+
platform: PathPlatform
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type ConfigRegistrationLocationResult = Succeeded<ConfigRegistrationLocation> | Failed
|
|
51
|
+
|
|
52
|
+
export interface RegistrationPaths extends ConfigRegistrationLocation {
|
|
53
|
+
indexPath: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type RegistrationPathsResult = Succeeded<RegistrationPaths> | Failed
|
|
57
|
+
export type CanonicalPathResult = Succeeded<string> | Failed
|
|
58
|
+
export type CanonicalConfigResult = Succeeded<MemoryConfig> | Failed
|
|
59
|
+
|
|
60
|
+
export interface WriteConfigRequest extends ConfigLocation {
|
|
61
|
+
indexPath: string
|
|
62
|
+
config: MemoryConfig
|
|
63
|
+
expectedHash: string | null
|
|
64
|
+
replacementConfirmed?: true
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ConfigPublication {
|
|
68
|
+
hash: string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ConfigWritten extends Succeeded<ConfigPublication> {
|
|
72
|
+
warnings: string[]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type ConfigWriteFailureReason = "conflict" | "invalid-config" | "replacement-required" | "unavailable"
|
|
76
|
+
|
|
77
|
+
export interface ConfigWriteRefusal extends Failed {
|
|
78
|
+
reason: ConfigWriteFailureReason
|
|
79
|
+
warnings: string[]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type WriteConfigResult = ConfigWritten | ConfigWriteRefusal
|
|
83
|
+
export type CheckConfigSnapshotResult = Succeeded<ConfigSnapshot> | ConfigWriteRefusal
|
|
84
|
+
|
|
85
|
+
export interface ConfigPublicationRequest {
|
|
86
|
+
request: WriteConfigRequest
|
|
87
|
+
temporaryPath: string
|
|
88
|
+
warnings: string[]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface PathIdentity {
|
|
92
|
+
device: string
|
|
93
|
+
inode: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type FileIdentityResult = Succeeded<PathIdentity> | Failed
|
|
97
|
+
export type PathIdentitiesResult = Succeeded<PathIdentity[]> | Failed
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import { mkdir, open } from "node:fs/promises"
|
|
3
|
+
import { basename, dirname, join } from "node:path"
|
|
4
|
+
|
|
5
|
+
import { canonicalizeRegistration } from "./canonicalize.ts"
|
|
6
|
+
import { checkConfigSnapshot, publishConfig, removeConfigArtifact } from "./publication.ts"
|
|
7
|
+
import type { WriteConfigRequest, WriteConfigResult } from "./types.ts"
|
|
8
|
+
|
|
9
|
+
export async function writeConfig(request: WriteConfigRequest): Promise<WriteConfigResult> {
|
|
10
|
+
const canonical = await canonicalizeRegistration({
|
|
11
|
+
config: request.config,
|
|
12
|
+
configPath: request.path,
|
|
13
|
+
indexPath: request.indexPath,
|
|
14
|
+
platform: request.platform,
|
|
15
|
+
})
|
|
16
|
+
if (!canonical.succeeded) {
|
|
17
|
+
return { ...canonical, reason: "invalid-config", warnings: [] }
|
|
18
|
+
}
|
|
19
|
+
const resolved = { ...request, path: canonical.output.configPath }
|
|
20
|
+
try {
|
|
21
|
+
await mkdir(dirname(resolved.path), { recursive: true })
|
|
22
|
+
return await writeWithLock(resolved)
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return { succeeded: false, reason: "unavailable", problems: [String(error)], warnings: [] }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function writeWithLock(request: WriteConfigRequest): Promise<WriteConfigResult> {
|
|
29
|
+
const lockPath = join(dirname(request.path), `.${basename(request.path)}.lock`)
|
|
30
|
+
const lock = await open(lockPath, "wx")
|
|
31
|
+
const warnings: string[] = []
|
|
32
|
+
try {
|
|
33
|
+
return await writeCheckedConfig(request, warnings)
|
|
34
|
+
} finally {
|
|
35
|
+
try {
|
|
36
|
+
await lock.close()
|
|
37
|
+
} catch (error) {
|
|
38
|
+
warnings.push(`${lockPath}: could not close lock (${String(error)})`)
|
|
39
|
+
}
|
|
40
|
+
await removeConfigArtifact(lockPath, warnings)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function writeCheckedConfig(request: WriteConfigRequest, warnings: string[]): Promise<WriteConfigResult> {
|
|
45
|
+
const temporaryPath = join(dirname(request.path), `.${basename(request.path)}.tmp-${randomUUID()}`)
|
|
46
|
+
try {
|
|
47
|
+
const snapshot = await checkConfigSnapshot(request)
|
|
48
|
+
if (!snapshot.succeeded) {
|
|
49
|
+
return { ...snapshot, warnings }
|
|
50
|
+
}
|
|
51
|
+
return await publishConfig({ request, temporaryPath, warnings })
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof Error && "code" in error && error.code === "EEXIST") {
|
|
54
|
+
return {
|
|
55
|
+
succeeded: false,
|
|
56
|
+
reason: "conflict",
|
|
57
|
+
problems: ["config publication destination already exists"],
|
|
58
|
+
warnings,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { succeeded: false, reason: "unavailable", problems: [String(error)], warnings }
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const defaultLayaSettings = {
|
|
2
|
+
provider: "laya",
|
|
3
|
+
endpoint: "http://127.0.0.1:8000/v1/systemone",
|
|
4
|
+
model: "multilingual",
|
|
5
|
+
maxTokens: 8192,
|
|
6
|
+
headMaxTokens: 192,
|
|
7
|
+
} as const
|
|
8
|
+
|
|
9
|
+
export const defaultJevModel = "jev-latest"
|
|
10
|
+
export const maxDecisionResponseBytes = 65_536
|
|
11
|
+
|
|
12
|
+
export const modelVersionPattern = /^\d+(?:\.\d+)*$/
|
|
13
|
+
|
|
14
|
+
export const layaNoulLabels = { true: "A", false: "B" } as const
|
|
15
|
+
export const layaServeMaxBatchStates = 64
|
|
16
|
+
export const maxLayaBatchResponseBytes = layaServeMaxBatchStates * maxDecisionResponseBytes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { DecisionResult } from "#src/domain"
|
|
2
|
+
import { initialDiagnostics } from "./diagnostics.ts"
|
|
3
|
+
import { interruptibleDecision } from "./interruption.ts"
|
|
4
|
+
import { classifyJev } from "./jev.ts"
|
|
5
|
+
import { classifyLaya } from "./laya.ts"
|
|
6
|
+
import { decisionPreflight } from "./preflight.ts"
|
|
7
|
+
import { candidatePrompt, memoryPrompt } from "./prompts.ts"
|
|
8
|
+
import type { CandidateDecisionCall, DecisionContext, DecisionPrompt, MemoryDecisionCall } from "./types.ts"
|
|
9
|
+
|
|
10
|
+
export type { DecisionCutoffs, DecisionProvider } from "#src/domain"
|
|
11
|
+
export { defaultCutoffs, rankCandidates, shouldRetrieveMemory } from "#src/domain"
|
|
12
|
+
|
|
13
|
+
async function decide(context: DecisionContext, prompt: DecisionPrompt): Promise<DecisionResult> {
|
|
14
|
+
const diagnostics = initialDiagnostics(context)
|
|
15
|
+
const refusal = decisionPreflight(context)
|
|
16
|
+
if (refusal) return { succeeded: false, code: refusal, diagnostics }
|
|
17
|
+
return interruptibleDecision(context, diagnostics, {
|
|
18
|
+
async run(signal) {
|
|
19
|
+
const call = {
|
|
20
|
+
prompt,
|
|
21
|
+
diagnostics,
|
|
22
|
+
signal,
|
|
23
|
+
question: questionName(prompt),
|
|
24
|
+
}
|
|
25
|
+
if (context.provider === "laya") return classifyLaya(context, call)
|
|
26
|
+
return classifyJev(context, call)
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function questionName(prompt: DecisionPrompt): "needed" | "relevant" {
|
|
32
|
+
if ("needed" in prompt.questions) return "needed"
|
|
33
|
+
return "relevant"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function decideMemory(call: MemoryDecisionCall): Promise<DecisionResult> {
|
|
37
|
+
return decide(call.context, memoryPrompt(call.request))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function scoreCandidate(call: CandidateDecisionCall): Promise<DecisionResult> {
|
|
41
|
+
return decide(call.context, candidatePrompt(call.request))
|
|
42
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DecisionDiagnostics } from "#src/domain"
|
|
2
|
+
import type { DecisionContext } from "./types.ts"
|
|
3
|
+
|
|
4
|
+
export function initialDiagnostics(context: DecisionContext): DecisionDiagnostics {
|
|
5
|
+
return { provider: context.provider, requestedModel: context.model, reportedModel: null, usage: null }
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isLoopbackEndpoint } from "#src/domain"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { boundedFailureDetail } from "#src/domain"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { DecisionDiagnostics, DecisionResult } from "#src/domain"
|
|
2
|
+
import type { DecisionContext, DecisionInterruption, DecisionOperation, InterruptibleOperation } from "./types.ts"
|
|
3
|
+
|
|
4
|
+
export function interruptibleDecision(
|
|
5
|
+
context: DecisionContext,
|
|
6
|
+
diagnostics: DecisionDiagnostics,
|
|
7
|
+
operation: DecisionOperation,
|
|
8
|
+
): Promise<DecisionResult> {
|
|
9
|
+
return interruptible(context, (code) => ({ succeeded: false, code, diagnostics }), operation)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function interruptible<Outcome>(
|
|
13
|
+
context: DecisionContext,
|
|
14
|
+
interrupted: DecisionInterruption<Outcome>,
|
|
15
|
+
operation: InterruptibleOperation<Outcome>,
|
|
16
|
+
): Promise<Outcome> {
|
|
17
|
+
const { signal } = context
|
|
18
|
+
if (signal.aborted) return interrupted("aborted")
|
|
19
|
+
const interruption = Promise.withResolvers<Outcome>()
|
|
20
|
+
const onAbort = () => interruption.resolve(interrupted("aborted"))
|
|
21
|
+
signal.addEventListener("abort", onAbort, { once: true })
|
|
22
|
+
try {
|
|
23
|
+
return await Promise.race([operation.run(signal), interruption.promise])
|
|
24
|
+
} catch {
|
|
25
|
+
return interrupted("unavailable")
|
|
26
|
+
} finally {
|
|
27
|
+
signal.removeEventListener("abort", onAbort)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Value } from "typebox/value"
|
|
2
|
+
|
|
3
|
+
import type { DecisionResult, DecisionUsage } from "#src/domain"
|
|
4
|
+
import { modelVersionPattern } from "./constants.ts"
|
|
5
|
+
import { JevResultSchema } from "./schemas.ts"
|
|
6
|
+
import type { JevContext, ProviderCall } from "./types.ts"
|
|
7
|
+
|
|
8
|
+
function modelFamily(id: string): string {
|
|
9
|
+
return id.slice(0, id.lastIndexOf("-"))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function modelTail(id: string): string {
|
|
13
|
+
return id.slice(id.lastIndexOf("-") + 1)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function answeredByRequestedModel(requested: string, reported: string): boolean {
|
|
17
|
+
if (reported === requested) return true
|
|
18
|
+
if (!requested.includes("-") || !reported.includes("-")) return false
|
|
19
|
+
return (
|
|
20
|
+
modelFamily(requested) === modelFamily(reported) &&
|
|
21
|
+
!modelVersionPattern.test(modelTail(requested)) &&
|
|
22
|
+
modelVersionPattern.test(modelTail(reported))
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function classifyJev(context: JevContext, call: ProviderCall): Promise<DecisionResult> {
|
|
27
|
+
const diagnostics = call.diagnostics
|
|
28
|
+
const result = await context.classifier.classify({
|
|
29
|
+
model: { provider: "typesafe", id: context.model },
|
|
30
|
+
context: call.prompt,
|
|
31
|
+
options: { signal: call.signal, maxRetries: 0 },
|
|
32
|
+
})
|
|
33
|
+
if (!Value.Check(JevResultSchema, result)) return { succeeded: false, code: "invalid-response", diagnostics }
|
|
34
|
+
if (result.stopReason === "aborted") return { succeeded: false, code: "aborted", diagnostics }
|
|
35
|
+
if (result.stopReason === "error") return { succeeded: false, code: "unavailable", diagnostics }
|
|
36
|
+
const answer = result.answers[call.question]
|
|
37
|
+
if (
|
|
38
|
+
!answeredByRequestedModel(context.model, result.model) ||
|
|
39
|
+
answer?.type !== "bool" ||
|
|
40
|
+
typeof answer.probability !== "number"
|
|
41
|
+
) {
|
|
42
|
+
return { succeeded: false, code: "invalid-response", diagnostics }
|
|
43
|
+
}
|
|
44
|
+
let usage: DecisionUsage | null = null
|
|
45
|
+
if (result.usage) usage = { inputTokens: result.usage.input, outputTokens: result.usage.output }
|
|
46
|
+
return {
|
|
47
|
+
succeeded: true,
|
|
48
|
+
output: {
|
|
49
|
+
probability: answer.probability,
|
|
50
|
+
diagnostics: { ...diagnostics, reportedModel: result.model, usage },
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function classifyJevSettled(context: JevContext, call: ProviderCall): Promise<DecisionResult> {
|
|
56
|
+
try {
|
|
57
|
+
return await classifyJev(context, call)
|
|
58
|
+
} catch {
|
|
59
|
+
return { succeeded: false, code: "unavailable", diagnostics: call.diagnostics }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Value } from "typebox/value"
|
|
2
|
+
|
|
3
|
+
import type { DecisionFailureCode, DecisionResult } from "#src/domain"
|
|
4
|
+
import { layaServeMaxBatchStates, maxLayaBatchResponseBytes } from "./constants.ts"
|
|
5
|
+
import { layaHeaders, layaNativeQuestions } from "./laya.ts"
|
|
6
|
+
import { layaHttpFailures } from "./laya-http-failure.ts"
|
|
7
|
+
import { interpretLayaValue, readBoundedText } from "./laya-response.ts"
|
|
8
|
+
import { LayaBatchResponseSchema } from "./schemas.ts"
|
|
9
|
+
import type { DecisionBatchItem, LayaBatchCall, LayaContext, LayaResultCandidate } from "./types.ts"
|
|
10
|
+
|
|
11
|
+
function failAll(items: DecisionBatchItem[], code: DecisionFailureCode): DecisionResult[] {
|
|
12
|
+
return items.map((item) => ({ succeeded: false, code, diagnostics: item.diagnostics }))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function readResults(response: Response): Promise<LayaResultCandidate[] | null> {
|
|
16
|
+
const text = await readBoundedText(response, maxLayaBatchResponseBytes)
|
|
17
|
+
if (typeof text !== "string") return null
|
|
18
|
+
try {
|
|
19
|
+
const parsed = JSON.parse(text)
|
|
20
|
+
if (!Value.Check(LayaBatchResponseSchema, parsed)) return null
|
|
21
|
+
return parsed.results
|
|
22
|
+
} catch {
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function classifyChunk(context: LayaContext, items: DecisionBatchItem[], signal: AbortSignal) {
|
|
28
|
+
const first = items[0]
|
|
29
|
+
if (!first) return []
|
|
30
|
+
const response = await fetch(`${context.endpoint}/batch`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: layaHeaders(context),
|
|
33
|
+
signal,
|
|
34
|
+
redirect: "manual",
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
model: context.model,
|
|
37
|
+
states: items.map((item) => item.prompt.state),
|
|
38
|
+
questions: layaNativeQuestions(first.prompt),
|
|
39
|
+
max_len: context.maxTokens,
|
|
40
|
+
head_max_len: context.headMaxTokens,
|
|
41
|
+
}),
|
|
42
|
+
})
|
|
43
|
+
if (!response.ok)
|
|
44
|
+
return layaHttpFailures(
|
|
45
|
+
response,
|
|
46
|
+
items.map((item) => item.diagnostics),
|
|
47
|
+
)
|
|
48
|
+
const results = await readResults(response)
|
|
49
|
+
if (!results || results.length !== items.length) return failAll(items, "invalid-response")
|
|
50
|
+
return items.map((item, index): DecisionResult => {
|
|
51
|
+
const value = results[index]
|
|
52
|
+
if (!value) return { succeeded: false, code: "invalid-response", diagnostics: item.diagnostics }
|
|
53
|
+
return interpretLayaValue(value, { question: "relevant", diagnostics: item.diagnostics })
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function classifyLayaBatch(context: LayaContext, call: LayaBatchCall): Promise<DecisionResult[]> {
|
|
58
|
+
const results: DecisionResult[] = []
|
|
59
|
+
for (let start = 0; start < call.items.length; start += layaServeMaxBatchStates) {
|
|
60
|
+
const chunk = call.items.slice(start, start + layaServeMaxBatchStates)
|
|
61
|
+
results.push(...(await classifyChunk(context, chunk, call.signal)))
|
|
62
|
+
}
|
|
63
|
+
return results
|
|
64
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Value } from "typebox/value"
|
|
2
|
+
|
|
3
|
+
import type { DecisionDiagnostics, DecisionFailure } from "#src/domain"
|
|
4
|
+
import { maxDecisionResponseBytes } from "./constants.ts"
|
|
5
|
+
import { boundedFailureDetail } from "./failure-detail.ts"
|
|
6
|
+
import { readBoundedText } from "./laya-response.ts"
|
|
7
|
+
import { LayaHttpDetailSchema } from "./schemas.ts"
|
|
8
|
+
|
|
9
|
+
async function serverDetail(response: Response): Promise<string> {
|
|
10
|
+
if (response.status !== 422) {
|
|
11
|
+
await response.body?.cancel()
|
|
12
|
+
return ""
|
|
13
|
+
}
|
|
14
|
+
const text = await readBoundedText(response, maxDecisionResponseBytes)
|
|
15
|
+
if (typeof text !== "string") return ""
|
|
16
|
+
try {
|
|
17
|
+
const parsed = JSON.parse(text)
|
|
18
|
+
if (!Value.Check(LayaHttpDetailSchema, parsed)) return ""
|
|
19
|
+
return boundedFailureDetail(parsed.detail)
|
|
20
|
+
} catch {
|
|
21
|
+
return ""
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function httpFailure(status: number, detail: string, diagnostics: DecisionDiagnostics): DecisionFailure {
|
|
26
|
+
const failure: DecisionFailure = { succeeded: false, code: "http-error", httpStatus: status, diagnostics }
|
|
27
|
+
if (detail) failure.detail = detail
|
|
28
|
+
return failure
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function layaHttpFailure(response: Response, diagnostics: DecisionDiagnostics): Promise<DecisionFailure> {
|
|
32
|
+
return httpFailure(response.status, await serverDetail(response), diagnostics)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function layaHttpFailures(
|
|
36
|
+
response: Response,
|
|
37
|
+
diagnostics: DecisionDiagnostics[],
|
|
38
|
+
): Promise<DecisionFailure[]> {
|
|
39
|
+
const detail = await serverDetail(response)
|
|
40
|
+
return diagnostics.map((item) => httpFailure(response.status, detail, item))
|
|
41
|
+
}
|