@deepseek-ai/cordis-plugin-include 1.1.0 → 1.2.0-alpha.2

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/src/patch.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { EntryOptions } from '@deepseek-ai/cordis-plugin-loader'
2
- import { type Dict, omit } from '@deepseek-ai/cosmokit'
2
+ import type { Dict } from '@deepseek-ai/cosmokit'
3
3
  import { applyChanges, detach, flatten, type Journal, place } from './journal.ts'
4
4
 
5
5
  export interface PatchOptions {
@@ -41,79 +41,10 @@ export function ensureInsertIds(patches: PatchOptions[] | undefined, used: (id:
41
41
  }
42
42
 
43
43
  /**
44
- * Overlay `patches` onto a fresh clone of `data`. Inserted entries are cloned
45
- * too, so the tree owns every object it mounts.
44
+ * Who supplies an entry, or one key of it, in the patched tree: the file, a
45
+ * patch that overrides that key, or the patch the entry is written in.
46
46
  */
47
- export function applyPatches(data: EntryOptions[], patches: PatchOptions[] | undefined, warn: Warn): EntryOptions[] {
48
- data = structuredClone(data)
49
- if (!patches?.length) return data
50
-
51
- const entryMap = new Map<string, EntryOptions>()
52
- const buildMap = (entries: EntryOptions[]) => {
53
- for (const entry of entries) {
54
- if (entry.id) entryMap.set(entry.id, entry)
55
- if (entry.group && Array.isArray(entry.config)) {
56
- buildMap(entry.config)
57
- }
58
- }
59
- }
60
- buildMap(data)
61
-
62
- for (const patch of patches) {
63
- const { id, insert, name, ...overrides } = patch
64
-
65
- if (insert) {
66
- const clone = structuredClone(insert)
67
- if (id) {
68
- const target = entryMap.get(id)
69
- if (!target) {
70
- warn('patch insert: entry %C not found', id)
71
- continue
72
- }
73
- if (!target.group) {
74
- warn('patch insert: entry %C is not a group', id)
75
- continue
76
- }
77
- if (!Array.isArray(target.config)) target.config = []
78
- target.config.push(...clone)
79
- } else {
80
- data.push(...clone)
81
- }
82
- buildMap(clone)
83
- continue
84
- }
85
-
86
- if (!id) {
87
- warn('patch: id is required for non-insert patches')
88
- continue
89
- }
90
-
91
- const target = entryMap.get(id)
92
- if (!target) {
93
- warn('patch: entry %C not found', id)
94
- continue
95
- }
96
-
97
- if (name && name !== target.name) {
98
- warn('patch: name mismatch for %C (expected %C, got %C), skipping', id, target.name, name)
99
- continue
100
- }
101
-
102
- for (const [key, value] of Object.entries(overrides)) {
103
- if (key === 'id') continue
104
- target[key] = value
105
- }
106
- }
107
-
108
- return data
109
- }
110
-
111
- /**
112
- * Who owns an entry, or one key of it, in the patched tree: the file, a patch
113
- * that overrides that key (the last one wins), or the patch that inserted the
114
- * whole entry.
115
- */
116
- export type EntryOwner = EntryOwner.File | EntryOwner.Patch | EntryOwner.Insert
47
+ export type EntryOwner = EntryOwner.File | EntryOwner.Patch | EntryOwner.Embedded
117
48
 
118
49
  export namespace EntryOwner {
119
50
  export interface File {
@@ -126,56 +57,34 @@ export namespace EntryOwner {
126
57
  index: number
127
58
  }
128
59
 
129
- export interface Insert {
130
- type: 'insert'
60
+ /** An entry written in `patches[index][key]`. */
61
+ export interface Embedded {
62
+ type: 'embedded'
131
63
  /** Index into the patch list. */
132
64
  index: number
133
- /** The insert list (or nested group config) holding the entry. */
134
- list: EntryOptions[]
135
- options: EntryOptions
136
- /** The group the entry is inserted into (`null` for the root). */
137
- parent: string | null
65
+ key: 'insert' | 'config'
138
66
  }
139
67
  }
140
68
 
69
+ const FILE: EntryOwner.File = { type: 'file' }
70
+
71
+ /** Where every entry and key of a patched tree comes from. */
141
72
  export class PatchIndex {
142
- private keys = new Map<string, Map<string, number>>()
143
- private inserts = new Map<string, EntryOwner.Insert>()
144
-
145
- constructor(public patches: PatchOptions[]) {
146
- patches.forEach((patch, index) => {
147
- const { id, insert } = patch
148
- if (insert) {
149
- const walk = (list: EntryOptions[], parent: string | null) => {
150
- for (const options of list) {
151
- this.inserts.set(options.id, { type: 'insert', index, list, options, parent })
152
- if (options.group && Array.isArray(options.config)) walk(options.config, options.id)
153
- }
154
- }
155
- walk(insert, id ?? null)
156
- return
157
- }
158
- if (!id) return
159
- const keys = this.keys.get(id) ?? new Map<string, number>()
160
- for (const key of Object.keys(omit(patch, ['id', 'insert', 'name']))) {
161
- keys.set(key, index)
162
- }
163
- this.keys.set(id, keys)
164
- })
165
- }
73
+ constructor(
74
+ private owners: Map<string, EntryOwner.File | EntryOwner.Embedded>,
75
+ private keys: Map<string, Map<string, number>>,
76
+ ) {}
166
77
 
167
78
  /** Owner of the entry as a whole. */
168
- entry(id: string | null): EntryOwner {
169
- if (id === null) return { type: 'file' }
170
- return this.inserts.get(id) ?? { type: 'file' }
79
+ entry(id: string | null) {
80
+ if (id === null) return FILE
81
+ return this.owners.get(id) ?? FILE
171
82
  }
172
83
 
173
84
  /** Owner of one key of an entry. */
174
85
  key(id: string, key: string): EntryOwner {
175
- const insert = this.inserts.get(id)
176
- if (insert) return insert
177
86
  const index = this.keys.get(id)?.get(key)
178
- return index === undefined ? { type: 'file' } : { type: 'patch', index }
87
+ return index === undefined ? this.entry(id) : { type: 'patch', index }
179
88
  }
180
89
 
181
90
  fileOwned(id: string, key: string | null) {
@@ -183,21 +92,131 @@ export class PatchIndex {
183
92
  }
184
93
  }
185
94
 
95
+ export interface PatchResult {
96
+ data: EntryOptions[]
97
+ index: PatchIndex
98
+ }
99
+
100
+ /**
101
+ * Overlay `patches` onto a fresh clone of `data`. Entry lists from patches
102
+ * are cloned too, so the tree owns every entry it mounts. The index is built
103
+ * in the same pass, so a patch that does not apply owns nothing.
104
+ */
105
+ export function applyPatches(data: EntryOptions[], patches: PatchOptions[] | undefined, warn: Warn = () => {}): PatchResult {
106
+ data = structuredClone(data)
107
+ const entries = new Map<string, EntryOptions>()
108
+ const owners = new Map<string, EntryOwner.File | EntryOwner.Embedded>()
109
+ const keys = new Map<string, Map<string, number>>()
110
+
111
+ const mount = (list: EntryOptions[], owner: EntryOwner.File | EntryOwner.Embedded) => {
112
+ for (const options of list) {
113
+ if (options.id) {
114
+ entries.set(options.id, options)
115
+ owners.set(options.id, owner)
116
+ // earlier overrides were aimed at the entry this one replaces
117
+ keys.delete(options.id)
118
+ }
119
+ if (options.group && Array.isArray(options.config)) mount(options.config, owner)
120
+ }
121
+ }
122
+
123
+ const unmount = (list: EntryOptions[]) => {
124
+ for (const options of list) {
125
+ if (options.id) {
126
+ entries.delete(options.id)
127
+ owners.delete(options.id)
128
+ keys.delete(options.id)
129
+ }
130
+ if (options.group && Array.isArray(options.config)) unmount(options.config)
131
+ }
132
+ }
133
+
134
+ mount(data, FILE)
135
+
136
+ patches?.forEach((patch, index) => {
137
+ const { id, insert, name, ...overrides } = patch
138
+ const target = id ? entries.get(id) : undefined
139
+
140
+ if (insert) {
141
+ if (id && !target) return warn('patch insert: entry %C not found', id)
142
+ if (target && !target.group) return warn('patch insert: entry %C is not a group', id)
143
+ const clone = structuredClone(insert)
144
+ if (target) {
145
+ if (!Array.isArray(target.config)) target.config = []
146
+ target.config.push(...clone)
147
+ } else {
148
+ data.push(...clone)
149
+ }
150
+ mount(clone, { type: 'embedded', index, key: 'insert' })
151
+ return
152
+ }
153
+
154
+ if (!id) return warn('patch: id is required for non-insert patches')
155
+ if (!target) return warn('patch: entry %C not found', id)
156
+ if (name && name !== target.name) {
157
+ return warn('patch: name mismatch for %C (expected %C, got %C), skipping', id, target.name, name)
158
+ }
159
+
160
+ // the entries of a group config the patch replaces go away with it
161
+ if ('config' in overrides && target.group && Array.isArray(target.config)) {
162
+ unmount(target.config)
163
+ }
164
+
165
+ const overridden = keys.get(id) ?? new Map<string, number>()
166
+ keys.set(id, overridden)
167
+ for (const [key, value] of Object.entries(overrides)) {
168
+ target[key] = value
169
+ overridden.set(key, index)
170
+ }
171
+
172
+ // entries of a group config the patch supplies can be patched in turn
173
+ if ('config' in overrides && target.group && Array.isArray(target.config)) {
174
+ // ids go on the patch itself, before the tree takes its own copy
175
+ ensureIds(target.config, id => entries.has(id))
176
+ target.config = structuredClone(target.config)
177
+ mount(target.config, { type: 'embedded', index, key: 'config' })
178
+ }
179
+ })
180
+
181
+ return { data, index: new PatchIndex(owners, keys) }
182
+ }
183
+
186
184
  /**
187
185
  * Route a journal to its owners: file-owned changes are applied to `data`,
188
186
  * patch-owned ones to `patches`. Both are mutated in place, so pass clones.
189
187
  * Returns whether any patch changed.
190
188
  */
191
189
  export function routeJournal(journal: Journal, data: EntryOptions[], patches: PatchOptions[], warn: Warn) {
192
- const index = new PatchIndex(patches)
190
+ let { index } = applyPatches(data, patches)
193
191
  let patched = false
194
192
 
195
- const insertList = (owner: EntryOwner.Insert) => owner.options.config ??= []
193
+ // entries that move, appear or go away change the owners of later records
194
+ const reindex = () => {
195
+ index = applyPatches(data, patches).index
196
+ }
197
+
198
+ // the list an entry is written in, and the group its top level belongs to
199
+ const container = (owner: EntryOwner.File | EntryOwner.Embedded) => {
200
+ if (owner.type === 'file') return { list: data, parent: null }
201
+ const patch = patches[owner.index]
202
+ return { list: patch[owner.key] as EntryOptions[], parent: patch.id ?? null }
203
+ }
204
+
205
+ // the list holding the entries of a group, when a patch supplies them
206
+ const children = (parent: string) => {
207
+ const owner = index.key(parent, 'config')
208
+ if (owner.type === 'file') return
209
+ if (owner.type === 'patch') {
210
+ const config = patches[owner.index].config
211
+ return Array.isArray(config) ? config : undefined
212
+ }
213
+ const found = flatten(container(owner).list).get(parent)
214
+ return found && (found.options.config ??= [])
215
+ }
196
216
 
197
217
  const placeAt = (options: EntryOptions, parent: string | null, position: number) => {
198
- const owner = index.entry(parent)
199
- if (owner.type === 'insert') {
200
- const list = insertList(owner)
218
+ const list = parent === null ? undefined : children(parent)
219
+ if (list) {
201
220
  list.splice(Math.min(position, list.length), 0, options)
202
221
  patched = true
203
222
  } else if (!place(data, options, parent, position)) {
@@ -207,40 +226,33 @@ export function routeJournal(journal: Journal, data: EntryOptions[], patches: Pa
207
226
 
208
227
  for (const [id, record] of journal) {
209
228
  const owner = index.entry(id)
229
+ const { list, parent: base } = container(owner)
230
+ const embedded = owner.type === 'embedded'
231
+
210
232
  if (record.kind === 'remove') {
211
- if (owner.type === 'insert') {
212
- owner.list.splice(owner.list.indexOf(owner.options), 1)
213
- patched = true
214
- } else {
215
- detach(data, id)
216
- }
233
+ detach(list, id)
234
+ patched ||= embedded
235
+ reindex()
217
236
  continue
218
237
  }
219
238
 
220
- const current = owner.type === 'insert' ? owner : flatten(data).get(id)
239
+ const current = flatten(list).get(id)
221
240
  if (!current) {
222
241
  const options = { id } as EntryOptions
223
242
  applyChanges(options, record.changes)
224
243
  placeAt(options, record.parent ?? null, record.position)
244
+ reindex()
225
245
  continue
226
246
  }
227
247
 
228
- if (record.parent !== undefined && current.parent !== record.parent) {
248
+ // an entry at the top of a patch list belongs to the group the patch targets
249
+ if (record.parent !== undefined && (current.parent ?? base) !== record.parent) {
229
250
  // moving across owners: carry the full options over
230
- if (owner.type === 'insert') {
231
- owner.list.splice(owner.list.indexOf(owner.options), 1)
232
- patched = true
233
- } else {
234
- detach(data, id)
235
- }
251
+ detach(list, id)
252
+ patched ||= embedded
236
253
  applyChanges(current.options, record.changes)
237
254
  placeAt(current.options, record.parent, record.position)
238
- continue
239
- }
240
-
241
- if (owner.type === 'insert') {
242
- applyChanges(current.options, record.changes)
243
- patched = true
255
+ reindex()
244
256
  continue
245
257
  }
246
258
 
@@ -251,6 +263,7 @@ export function routeJournal(journal: Journal, data: EntryOptions[], patches: Pa
251
263
  ;(patchChanges[keyOwner.index] ??= {})[key] = record.changes[key]
252
264
  return false
253
265
  })
266
+ patched ||= embedded
254
267
  for (const [i, changes] of Object.entries(patchChanges)) {
255
268
  applyChanges(patches[i] as any, changes)
256
269
  patched = true