opencode-tweaks 0.0.0-stage → 0.4.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.
@@ -0,0 +1,73 @@
1
+ import type { Context } from "@opencode/plugin/tui/context"
2
+ import {
3
+ isSettingValue,
4
+ type ResolvedSetting,
5
+ type SettingValue,
6
+ type StoredSettings,
7
+ type Tweak,
8
+ type TweakSetting,
9
+ } from "./types"
10
+
11
+ export interface SettingsStore {
12
+ read(tweak: Tweak, setting: TweakSetting): ResolvedSetting
13
+ set(tweak: Tweak, setting: TweakSetting, value: SettingValue): Promise<void>
14
+ reset(tweak: Tweak, setting: TweakSetting): Promise<void>
15
+ }
16
+
17
+ interface OptionsTree {
18
+ readonly [key: string]: unknown
19
+ }
20
+
21
+ /**
22
+ * Reads settings in priority order: value chosen in `/tweaks`, then the plugin
23
+ * options in `opencode.json`, then the definition default. Writes only touch
24
+ * the local layer, so the config file stays the user's to edit.
25
+ */
26
+ export function createSettingsStore(context: Context): SettingsStore {
27
+ const [stored, update] = context.storage.store<StoredSettings>("settings", { initial: {} })
28
+ const options = context.options as OptionsTree
29
+
30
+ function read(tweak: Tweak, setting: TweakSetting): ResolvedSetting {
31
+ const userValue = stored[tweak.id]?.[setting.id]
32
+ if (isSettingValue(setting, userValue)) {
33
+ return { value: userValue, source: "user" }
34
+ }
35
+
36
+ const tweakOptions = options[tweak.id]
37
+ const configValue =
38
+ tweakOptions && typeof tweakOptions === "object"
39
+ ? (tweakOptions as OptionsTree)[setting.id]
40
+ : undefined
41
+
42
+ if (isSettingValue(setting, configValue)) {
43
+ return { value: configValue, source: "config" }
44
+ }
45
+
46
+ return { value: setting.default, source: "default" }
47
+ }
48
+
49
+ async function set(tweak: Tweak, setting: TweakSetting, value: SettingValue): Promise<void> {
50
+ if (!isSettingValue(setting, value)) return
51
+
52
+ await update((draft) => {
53
+ const group = draft[tweak.id]
54
+ if (group) {
55
+ group[setting.id] = value
56
+ } else {
57
+ draft[tweak.id] = { [setting.id]: value }
58
+ }
59
+ })
60
+ }
61
+
62
+ async function reset(tweak: Tweak, setting: TweakSetting): Promise<void> {
63
+ await update((draft) => {
64
+ const group = draft[tweak.id]
65
+ if (!group) return
66
+
67
+ delete group[setting.id]
68
+ if (Object.keys(group).length === 0) delete draft[tweak.id]
69
+ })
70
+ }
71
+
72
+ return { read, set, reset }
73
+ }