@mjasnikovs/pi-nop 0.1.2 → 0.1.4

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.
Files changed (177) hide show
  1. package/README.md +4 -3
  2. package/package.json +3 -2
  3. package/src/config/command.ts +46 -4
  4. package/src/config/config.ts +86 -11
  5. package/src/search/search.ts +17 -7
  6. package/src/subagent/answer.ts +41 -16
  7. package/src/subagent/fetch/cmaps/78-EUC-H.bcmap +0 -0
  8. package/src/subagent/fetch/cmaps/78-EUC-V.bcmap +0 -0
  9. package/src/subagent/fetch/cmaps/78-H.bcmap +0 -0
  10. package/src/subagent/fetch/cmaps/78-RKSJ-H.bcmap +0 -0
  11. package/src/subagent/fetch/cmaps/78-RKSJ-V.bcmap +0 -0
  12. package/src/subagent/fetch/cmaps/78-V.bcmap +0 -0
  13. package/src/subagent/fetch/cmaps/78ms-RKSJ-H.bcmap +0 -0
  14. package/src/subagent/fetch/cmaps/78ms-RKSJ-V.bcmap +0 -0
  15. package/src/subagent/fetch/cmaps/83pv-RKSJ-H.bcmap +0 -0
  16. package/src/subagent/fetch/cmaps/90ms-RKSJ-H.bcmap +0 -0
  17. package/src/subagent/fetch/cmaps/90ms-RKSJ-V.bcmap +0 -0
  18. package/src/subagent/fetch/cmaps/90msp-RKSJ-H.bcmap +0 -0
  19. package/src/subagent/fetch/cmaps/90msp-RKSJ-V.bcmap +0 -0
  20. package/src/subagent/fetch/cmaps/90pv-RKSJ-H.bcmap +0 -0
  21. package/src/subagent/fetch/cmaps/90pv-RKSJ-V.bcmap +0 -0
  22. package/src/subagent/fetch/cmaps/Add-H.bcmap +0 -0
  23. package/src/subagent/fetch/cmaps/Add-RKSJ-H.bcmap +0 -0
  24. package/src/subagent/fetch/cmaps/Add-RKSJ-V.bcmap +0 -0
  25. package/src/subagent/fetch/cmaps/Add-V.bcmap +0 -0
  26. package/src/subagent/fetch/cmaps/Adobe-CNS1-0.bcmap +0 -0
  27. package/src/subagent/fetch/cmaps/Adobe-CNS1-1.bcmap +0 -0
  28. package/src/subagent/fetch/cmaps/Adobe-CNS1-2.bcmap +0 -0
  29. package/src/subagent/fetch/cmaps/Adobe-CNS1-3.bcmap +0 -0
  30. package/src/subagent/fetch/cmaps/Adobe-CNS1-4.bcmap +0 -0
  31. package/src/subagent/fetch/cmaps/Adobe-CNS1-5.bcmap +0 -0
  32. package/src/subagent/fetch/cmaps/Adobe-CNS1-6.bcmap +0 -0
  33. package/src/subagent/fetch/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  34. package/src/subagent/fetch/cmaps/Adobe-GB1-0.bcmap +0 -0
  35. package/src/subagent/fetch/cmaps/Adobe-GB1-1.bcmap +0 -0
  36. package/src/subagent/fetch/cmaps/Adobe-GB1-2.bcmap +0 -0
  37. package/src/subagent/fetch/cmaps/Adobe-GB1-3.bcmap +0 -0
  38. package/src/subagent/fetch/cmaps/Adobe-GB1-4.bcmap +0 -0
  39. package/src/subagent/fetch/cmaps/Adobe-GB1-5.bcmap +0 -0
  40. package/src/subagent/fetch/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  41. package/src/subagent/fetch/cmaps/Adobe-Japan1-0.bcmap +0 -0
  42. package/src/subagent/fetch/cmaps/Adobe-Japan1-1.bcmap +0 -0
  43. package/src/subagent/fetch/cmaps/Adobe-Japan1-2.bcmap +0 -0
  44. package/src/subagent/fetch/cmaps/Adobe-Japan1-3.bcmap +0 -0
  45. package/src/subagent/fetch/cmaps/Adobe-Japan1-4.bcmap +0 -0
  46. package/src/subagent/fetch/cmaps/Adobe-Japan1-5.bcmap +0 -0
  47. package/src/subagent/fetch/cmaps/Adobe-Japan1-6.bcmap +0 -0
  48. package/src/subagent/fetch/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  49. package/src/subagent/fetch/cmaps/Adobe-Korea1-0.bcmap +0 -0
  50. package/src/subagent/fetch/cmaps/Adobe-Korea1-1.bcmap +0 -0
  51. package/src/subagent/fetch/cmaps/Adobe-Korea1-2.bcmap +0 -0
  52. package/src/subagent/fetch/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  53. package/src/subagent/fetch/cmaps/B5-H.bcmap +0 -0
  54. package/src/subagent/fetch/cmaps/B5-V.bcmap +0 -0
  55. package/src/subagent/fetch/cmaps/B5pc-H.bcmap +0 -0
  56. package/src/subagent/fetch/cmaps/B5pc-V.bcmap +0 -0
  57. package/src/subagent/fetch/cmaps/CNS-EUC-H.bcmap +0 -0
  58. package/src/subagent/fetch/cmaps/CNS-EUC-V.bcmap +0 -0
  59. package/src/subagent/fetch/cmaps/CNS1-H.bcmap +0 -0
  60. package/src/subagent/fetch/cmaps/CNS1-V.bcmap +0 -0
  61. package/src/subagent/fetch/cmaps/CNS2-H.bcmap +0 -0
  62. package/src/subagent/fetch/cmaps/CNS2-V.bcmap +3 -0
  63. package/src/subagent/fetch/cmaps/ETHK-B5-H.bcmap +0 -0
  64. package/src/subagent/fetch/cmaps/ETHK-B5-V.bcmap +0 -0
  65. package/src/subagent/fetch/cmaps/ETen-B5-H.bcmap +0 -0
  66. package/src/subagent/fetch/cmaps/ETen-B5-V.bcmap +0 -0
  67. package/src/subagent/fetch/cmaps/ETenms-B5-H.bcmap +3 -0
  68. package/src/subagent/fetch/cmaps/ETenms-B5-V.bcmap +0 -0
  69. package/src/subagent/fetch/cmaps/EUC-H.bcmap +0 -0
  70. package/src/subagent/fetch/cmaps/EUC-V.bcmap +0 -0
  71. package/src/subagent/fetch/cmaps/Ext-H.bcmap +0 -0
  72. package/src/subagent/fetch/cmaps/Ext-RKSJ-H.bcmap +0 -0
  73. package/src/subagent/fetch/cmaps/Ext-RKSJ-V.bcmap +0 -0
  74. package/src/subagent/fetch/cmaps/Ext-V.bcmap +0 -0
  75. package/src/subagent/fetch/cmaps/GB-EUC-H.bcmap +0 -0
  76. package/src/subagent/fetch/cmaps/GB-EUC-V.bcmap +0 -0
  77. package/src/subagent/fetch/cmaps/GB-H.bcmap +6 -0
  78. package/src/subagent/fetch/cmaps/GB-V.bcmap +0 -0
  79. package/src/subagent/fetch/cmaps/GBK-EUC-H.bcmap +0 -0
  80. package/src/subagent/fetch/cmaps/GBK-EUC-V.bcmap +0 -0
  81. package/src/subagent/fetch/cmaps/GBK2K-H.bcmap +0 -0
  82. package/src/subagent/fetch/cmaps/GBK2K-V.bcmap +0 -0
  83. package/src/subagent/fetch/cmaps/GBKp-EUC-H.bcmap +0 -0
  84. package/src/subagent/fetch/cmaps/GBKp-EUC-V.bcmap +0 -0
  85. package/src/subagent/fetch/cmaps/GBT-EUC-H.bcmap +0 -0
  86. package/src/subagent/fetch/cmaps/GBT-EUC-V.bcmap +0 -0
  87. package/src/subagent/fetch/cmaps/GBT-H.bcmap +0 -0
  88. package/src/subagent/fetch/cmaps/GBT-V.bcmap +0 -0
  89. package/src/subagent/fetch/cmaps/GBTpc-EUC-H.bcmap +0 -0
  90. package/src/subagent/fetch/cmaps/GBTpc-EUC-V.bcmap +0 -0
  91. package/src/subagent/fetch/cmaps/GBpc-EUC-H.bcmap +0 -0
  92. package/src/subagent/fetch/cmaps/GBpc-EUC-V.bcmap +0 -0
  93. package/src/subagent/fetch/cmaps/H.bcmap +0 -0
  94. package/src/subagent/fetch/cmaps/HKdla-B5-H.bcmap +0 -0
  95. package/src/subagent/fetch/cmaps/HKdla-B5-V.bcmap +0 -0
  96. package/src/subagent/fetch/cmaps/HKdlb-B5-H.bcmap +0 -0
  97. package/src/subagent/fetch/cmaps/HKdlb-B5-V.bcmap +0 -0
  98. package/src/subagent/fetch/cmaps/HKgccs-B5-H.bcmap +0 -0
  99. package/src/subagent/fetch/cmaps/HKgccs-B5-V.bcmap +0 -0
  100. package/src/subagent/fetch/cmaps/HKm314-B5-H.bcmap +0 -0
  101. package/src/subagent/fetch/cmaps/HKm314-B5-V.bcmap +0 -0
  102. package/src/subagent/fetch/cmaps/HKm471-B5-H.bcmap +0 -0
  103. package/src/subagent/fetch/cmaps/HKm471-B5-V.bcmap +0 -0
  104. package/src/subagent/fetch/cmaps/HKscs-B5-H.bcmap +0 -0
  105. package/src/subagent/fetch/cmaps/HKscs-B5-V.bcmap +0 -0
  106. package/src/subagent/fetch/cmaps/Hankaku.bcmap +0 -0
  107. package/src/subagent/fetch/cmaps/Hiragana.bcmap +0 -0
  108. package/src/subagent/fetch/cmaps/KSC-EUC-H.bcmap +0 -0
  109. package/src/subagent/fetch/cmaps/KSC-EUC-V.bcmap +0 -0
  110. package/src/subagent/fetch/cmaps/KSC-H.bcmap +0 -0
  111. package/src/subagent/fetch/cmaps/KSC-Johab-H.bcmap +0 -0
  112. package/src/subagent/fetch/cmaps/KSC-Johab-V.bcmap +0 -0
  113. package/src/subagent/fetch/cmaps/KSC-V.bcmap +0 -0
  114. package/src/subagent/fetch/cmaps/KSCms-UHC-H.bcmap +0 -0
  115. package/src/subagent/fetch/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  116. package/src/subagent/fetch/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  117. package/src/subagent/fetch/cmaps/KSCms-UHC-V.bcmap +0 -0
  118. package/src/subagent/fetch/cmaps/KSCpc-EUC-H.bcmap +0 -0
  119. package/src/subagent/fetch/cmaps/KSCpc-EUC-V.bcmap +0 -0
  120. package/src/subagent/fetch/cmaps/Katakana.bcmap +0 -0
  121. package/src/subagent/fetch/cmaps/LICENSE +36 -0
  122. package/src/subagent/fetch/cmaps/NWP-H.bcmap +0 -0
  123. package/src/subagent/fetch/cmaps/NWP-V.bcmap +0 -0
  124. package/src/subagent/fetch/cmaps/RKSJ-H.bcmap +0 -0
  125. package/src/subagent/fetch/cmaps/RKSJ-V.bcmap +0 -0
  126. package/src/subagent/fetch/cmaps/Roman.bcmap +0 -0
  127. package/src/subagent/fetch/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  128. package/src/subagent/fetch/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  129. package/src/subagent/fetch/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  130. package/src/subagent/fetch/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  131. package/src/subagent/fetch/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  132. package/src/subagent/fetch/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  133. package/src/subagent/fetch/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  134. package/src/subagent/fetch/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  135. package/src/subagent/fetch/cmaps/UniGB-UCS2-H.bcmap +0 -0
  136. package/src/subagent/fetch/cmaps/UniGB-UCS2-V.bcmap +0 -0
  137. package/src/subagent/fetch/cmaps/UniGB-UTF16-H.bcmap +0 -0
  138. package/src/subagent/fetch/cmaps/UniGB-UTF16-V.bcmap +0 -0
  139. package/src/subagent/fetch/cmaps/UniGB-UTF32-H.bcmap +0 -0
  140. package/src/subagent/fetch/cmaps/UniGB-UTF32-V.bcmap +0 -0
  141. package/src/subagent/fetch/cmaps/UniGB-UTF8-H.bcmap +0 -0
  142. package/src/subagent/fetch/cmaps/UniGB-UTF8-V.bcmap +0 -0
  143. package/src/subagent/fetch/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  144. package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  145. package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  146. package/src/subagent/fetch/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  147. package/src/subagent/fetch/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  148. package/src/subagent/fetch/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  149. package/src/subagent/fetch/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  150. package/src/subagent/fetch/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  151. package/src/subagent/fetch/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  152. package/src/subagent/fetch/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  153. package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  154. package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  155. package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  156. package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  157. package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  158. package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  159. package/src/subagent/fetch/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  160. package/src/subagent/fetch/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  161. package/src/subagent/fetch/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  162. package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  163. package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  164. package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  165. package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  166. package/src/subagent/fetch/cmaps/UniKS-UCS2-H.bcmap +0 -0
  167. package/src/subagent/fetch/cmaps/UniKS-UCS2-V.bcmap +0 -0
  168. package/src/subagent/fetch/cmaps/UniKS-UTF16-H.bcmap +0 -0
  169. package/src/subagent/fetch/cmaps/UniKS-UTF16-V.bcmap +0 -0
  170. package/src/subagent/fetch/cmaps/UniKS-UTF32-H.bcmap +0 -0
  171. package/src/subagent/fetch/cmaps/UniKS-UTF32-V.bcmap +0 -0
  172. package/src/subagent/fetch/cmaps/UniKS-UTF8-H.bcmap +0 -0
  173. package/src/subagent/fetch/cmaps/UniKS-UTF8-V.bcmap +0 -0
  174. package/src/subagent/fetch/cmaps/V.bcmap +0 -0
  175. package/src/subagent/fetch/cmaps/WP-Symbol.bcmap +0 -0
  176. package/src/subagent/fetch/errors.ts +0 -2
  177. package/src/subagent/fetch/pdf.ts +74 -72
package/README.md CHANGED
@@ -30,8 +30,7 @@ pi install npm:@mjasnikovs/pi-nop
30
30
  ```
31
31
 
32
32
  Needs pi 1.6.0 or later. `subagent-docs` needs `npm` on PATH, and `tar` too outside Windows.
33
- Windows 10 1803 and Server 2019 or later ship the `tar.exe` it uses. `subagent-fetch` needs
34
- `pdftotext` from poppler 26.05.0 or later on PATH for PDF pages.
33
+ Windows 10 1803 and Server 2019 or later ship the `tar.exe` it uses.
35
34
 
36
35
  ## Tools
37
36
 
@@ -85,7 +84,9 @@ and 1 file written outside the plan. Run `/plan` again to see which steps are do
85
84
 
86
85
  `model` is `provider/id`, as pi lists it. `thinkingLevel` is `off`, `minimal`, `low`, `medium`,
87
86
  `high`, `xhigh` or `max`. One unknown key or bad value makes pi-nop ignore the whole file, with a
88
- warning. A model pi no longer lists falls back to pi's current one, with a warning.
87
+ warning that names it. The first `/nop-config` change then saves the old file as
88
+ `pi-nop.json.broken`, or `.broken-2` and on when older copies are there. A model pi no longer
89
+ lists falls back to pi's current one, with a warning.
89
90
 
90
91
  `PI_NOP_SEARCH_ENGINE` overrides the engine. `PI_NOP_SEARCH_ENDPOINT` overrides its URL.
91
92
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mjasnikovs/pi-nop",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Read-only subagents for pi: repo search, web fetch, npm package docs, web search, and a /plan panel.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -52,7 +52,8 @@
52
52
  "linkedom": "0.18.13",
53
53
  "turndown": "7.2.4",
54
54
  "turndown-plugin-gfm": "1.0.2",
55
- "typescript": "6.0.3"
55
+ "typescript": "6.0.3",
56
+ "unpdf": "2.2.3"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@earendil-works/pi-agent-core": "1.0.0",
@@ -4,6 +4,7 @@ import type {SettingItem} from '@earendil-works/pi-tui'
4
4
  import {SelectList, SettingsList} from '@earendil-works/pi-tui'
5
5
  import {readFileSync} from 'node:fs'
6
6
 
7
+ import {describe} from '../subagent/describe'
7
8
  import {boxed} from './box'
8
9
  import {
9
10
  applyChange,
@@ -11,7 +12,8 @@ import {
11
12
  modelOf,
12
13
  modelSpec,
13
14
  read,
14
- safeConfig,
15
+ loadSafely,
16
+ setAsideBroken,
15
17
  saveConfig,
16
18
  settings,
17
19
  tools
@@ -54,6 +56,9 @@ const row = (config: NopConfig, models: string[], setting: Setting): SettingItem
54
56
  : {values: offered(setting)})
55
57
  })
56
58
 
59
+ const savedValue = (config: NopConfig, id: string): string =>
60
+ String(read(config, settings.find(setting => setting.id === id) as Setting))
61
+
57
62
  const sections = [...new Set(settings.map(setting => setting.section))]
58
63
  const same = (text: string): string => text
59
64
 
@@ -76,6 +81,30 @@ const dropStaleModels = (config: NopConfig, models: string[]): NopConfig =>
76
81
  : applyChange(fixed, modelOf(tool).id, inherit)
77
82
  }, config)
78
83
 
84
+ // Another editor may have changed the file since the panel opened.
85
+ const synced = (
86
+ ctx: ExtensionCommandContext,
87
+ config: NopConfig,
88
+ available: string[]
89
+ ): NopConfig => {
90
+ const now = loadSafely()
91
+
92
+ if (now.broken) {
93
+ ctx.ui.notify(`Saved the broken pi-nop config as ${setAsideBroken()}`, 'info')
94
+ return config
95
+ }
96
+
97
+ const fresh = dropStaleModels(now.config, available)
98
+
99
+ if (JSON.stringify(fresh) !== JSON.stringify(config))
100
+ ctx.ui.notify(
101
+ 'pi-nop config changed while the panel was open; this change went into the new one. '
102
+ + 'Reopen /nop-config to see every value.',
103
+ 'info'
104
+ )
105
+ return fresh
106
+ }
107
+
79
108
  const availableModels = (ctx: ExtensionCommandContext): string[] =>
80
109
  ctx.modelRegistry.getAvailable().map(modelSpec)
81
110
 
@@ -94,7 +123,11 @@ export default function (pi: ExtensionAPI) {
94
123
  handler: async (_args, ctx) => {
95
124
  const available = availableModels(ctx)
96
125
  const models = offeredModels(ctx, available)
97
- let config = dropStaleModels(safeConfig(ctx), available)
126
+ const fix =
127
+ ctx.mode === 'tui' ?
128
+ 'Change a setting here to copy it aside and start fresh.'
129
+ : undefined
130
+ let config = dropStaleModels(loadSafely(ctx, fix).config, available)
98
131
 
99
132
  if (ctx.mode !== 'tui') {
100
133
  ctx.ui.notify(
@@ -116,8 +149,17 @@ export default function (pi: ExtensionAPI) {
116
149
  Math.min(items.length, maxVisible),
117
150
  getSettingsListTheme(),
118
151
  (id, value) => {
119
- config = applyChange(config, id, value)
120
- saveConfig(config)
152
+ try {
153
+ const next = applyChange(synced(ctx, config, available), id, value)
154
+ saveConfig(next)
155
+ config = next
156
+ } catch (failure) {
157
+ list.updateValue(id, savedValue(config, id))
158
+ ctx.ui.notify(
159
+ `pi-nop config not saved: ${describe(failure)}`,
160
+ 'error'
161
+ )
162
+ }
121
163
  },
122
164
  () => done(undefined)
123
165
  )
@@ -1,4 +1,4 @@
1
- import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs'
1
+ import {copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs'
2
2
  import {join} from 'node:path'
3
3
  import type {ExtensionContext, ModelRegistry} from '@earendil-works/pi-coding-agent'
4
4
  import {getAgentDir} from '@earendil-works/pi-coding-agent'
@@ -23,8 +23,9 @@ const thinkingLevels = [
23
23
  const inherit = 'inherit'
24
24
  const unlimited = 'unlimited'
25
25
  const off = 'off'
26
- const brokenConfig =
27
- 'pi-nop config is broken, using the host model and thinking level. Run /nop-config to fix it.'
26
+ const brokenConfig = (path: string, reason: string, fix: string): string =>
27
+ `pi-nop config ${path} is broken: ${reason}. Every pi-nop setting uses its default until it is `
28
+ + `fixed. ${fix}`
28
29
  const missingModel = (spec: string): string =>
29
30
  `pi-nop model ${spec} is not available, using the host model. Run /nop-config to pick another.`
30
31
 
@@ -157,24 +158,97 @@ const configSchema = fileSchema()
157
158
  type NopConfig = Static<typeof configSchema>
158
159
 
159
160
  const configPath = (): string => join(getAgentDir(), configFile)
161
+ // Notepad and some Windows editors start the file with a byte order mark, which JSON refuses.
162
+ const configText = (): string => readFileSync(configPath(), 'utf8').replace(/^\uFEFF/, '')
160
163
 
161
164
  const loadConfig = (): NopConfig =>
162
- existsSync(configPath()) ?
163
- Value.Parse(configSchema, JSON.parse(readFileSync(configPath(), 'utf8')))
164
- : {}
165
+ existsSync(configPath()) ? Value.Parse(configSchema, JSON.parse(configText())) : {}
165
166
 
166
167
  const saveConfig = (config: NopConfig): void => {
167
168
  mkdirSync(getAgentDir(), {recursive: true})
168
169
  writeFileSync(configPath(), JSON.stringify(config, null, 2) + '\n')
169
170
  }
170
171
 
171
- const safeConfig = (ctx?: ExtensionContext): NopConfig => {
172
+ const valueAt = (value: unknown, keys: string[]): unknown =>
173
+ keys.reduce<unknown>((node, key) => (node as Record<string, unknown> | undefined)?.[key], value)
174
+
175
+ // The panel lists settings as subagent.model, which a user may copy into the file flat.
176
+ const unknownKey = (key: string): string => {
177
+ const [head, tail] = path(key)
178
+ return settings.some(setting => setting.id === key) && tail !== undefined ?
179
+ `${key} goes nested, as {"${head}": {"${tail}": ...}}`
180
+ : `${key} is not a setting`
181
+ }
182
+
183
+ // A list or an object could be any size, and the warning is one line.
184
+ const shown = (value: unknown): string => {
185
+ if (Array.isArray(value)) return 'a list'
186
+ return typeof value === 'object' && value !== null ? 'an object' : JSON.stringify(value)
187
+ }
188
+
189
+ const schemaFault = (value: unknown): string => {
190
+ const errors = Value.Errors(configSchema, value)
191
+ const unknown = errors.find(error => error.keyword === 'additionalProperties')
192
+ const first = unknown ?? errors[0]
193
+ const keys = first?.instancePath.split('/').slice(1) ?? []
194
+
195
+ if (unknown !== undefined) {
196
+ // typebox does not escape / in a path, so the unknown key is read from its own error.
197
+ const [name] = (unknown.params as {additionalProperties: string[]}).additionalProperties
198
+ return unknownKey([...keys, name].join('.'))
199
+ }
200
+
201
+ if (keys.length === 0) return 'not a JSON object'
202
+ return `${keys.join('.')} is ${shown(valueAt(value, keys))}`
203
+ }
204
+
205
+ const brokenReason = (): string => {
206
+ let text: string
207
+ let parsed: unknown
208
+
209
+ try {
210
+ text = configText()
211
+ } catch (error) {
212
+ return `not readable (${String((error as NodeJS.ErrnoException).code)})`
213
+ }
214
+
172
215
  try {
173
- return loadConfig()
216
+ parsed = JSON.parse(text)
174
217
  } catch {
175
- ctx?.ui.notify(brokenConfig, 'warning')
176
- return {}
218
+ return 'not valid JSON'
177
219
  }
220
+
221
+ return schemaFault(parsed)
222
+ }
223
+
224
+ type Loaded = {config: NopConfig; broken: boolean}
225
+
226
+ // Outside the tui /nop-config only prints the values.
227
+ const fixHint = (ctx?: ExtensionContext): string =>
228
+ ctx?.mode === 'tui' ? 'Run /nop-config to fix it.' : 'Fix the file by hand.'
229
+
230
+ const loadSafely = (ctx?: ExtensionContext, fix = fixHint(ctx)): Loaded => {
231
+ try {
232
+ return {config: loadConfig(), broken: false}
233
+ } catch {
234
+ ctx?.ui.notify(brokenConfig(configPath(), brokenReason(), fix), 'warning')
235
+ return {config: {}, broken: true}
236
+ }
237
+ }
238
+
239
+ const safeConfig = (ctx?: ExtensionContext): NopConfig => loadSafely(ctx).config
240
+
241
+ const freeAside = (taken: number): string => {
242
+ const aside = `${configPath()}.broken${taken === 0 ? '' : `-${String(taken + 1)}`}`
243
+ return existsSync(aside) ? freeAside(taken + 1) : aside
244
+ }
245
+
246
+ // A save over a broken file would lose what the user wrote, so a copy goes aside, past older
247
+ // ones. A copy, not a move: a symlinked config must stay a link for the save to go through.
248
+ const setAsideBroken = (): string => {
249
+ const aside = freeAside(0)
250
+ copyFileSync(configPath(), aside)
251
+ return aside
178
252
  }
179
253
 
180
254
  const stored = (config: NopConfig, id: string): unknown =>
@@ -212,13 +286,13 @@ const findModel = (registry: ModelRegistry, spec?: string): Model<Api> | undefin
212
286
 
213
287
  export {
214
288
  applyChange,
215
- brokenConfig,
216
289
  concurrency,
217
290
  engine,
218
291
  engines,
219
292
  findModel,
220
293
  inherit,
221
294
  loadConfig,
295
+ loadSafely,
222
296
  missingModel,
223
297
  modelOf,
224
298
  modelSpec,
@@ -226,6 +300,7 @@ export {
226
300
  read,
227
301
  restarts,
228
302
  safeConfig,
303
+ setAsideBroken,
229
304
  saveConfig,
230
305
  searchTool,
231
306
  settings,
@@ -1,17 +1,30 @@
1
- import type {ExtensionAPI} from '@earendil-works/pi-coding-agent'
1
+ import type {ExtensionAPI, ExtensionContext} from '@earendil-works/pi-coding-agent'
2
2
  import {Type} from 'typebox'
3
3
  import Value from 'typebox/value'
4
4
 
5
- import {engine, read, safeConfig, searchTool} from '../config/config'
5
+ import {engine, engines as engineNames, read, safeConfig, searchTool} from '../config/config'
6
6
  import type {EngineName} from '../config/config'
7
7
  import {brave} from './brave'
8
8
  import {ddg} from './ddg'
9
9
  import {exa} from './exa'
10
10
  import type {Engine, SearchResult} from './engines'
11
- import {httpOnly} from './engines'
11
+ import {httpOnly, SearchErrors} from './engines'
12
12
 
13
13
  const engineOverride = 'PI_NOP_SEARCH_ENGINE'
14
14
  const engines: Record<EngineName, Engine> = {exa, brave, ddg}
15
+ const listed = `${engineNames.slice(0, -1).join(', ')} and ${String(engineNames.at(-1))}`
16
+
17
+ // The config was checked as it loaded, so only the environment can name an unknown engine.
18
+ const engineName = (ctx: ExtensionContext): EngineName => {
19
+ const override = process.env[engineOverride]
20
+
21
+ // `export PI_NOP_SEARCH_ENGINE=` unsets it in effect.
22
+ if (override === undefined || override === '') return read(safeConfig(ctx), engine)
23
+ if (Value.Check(engine.schema, override)) return override
24
+ throw new Error(
25
+ `${SearchErrors.Failed}${engineOverride} is "${override}", and the engines are ${listed}`
26
+ )
27
+ }
15
28
 
16
29
  const formatResult = (result: SearchResult, index: number): string => {
17
30
  const lead = `${index + 1}. [${result.title}](${result.url})`
@@ -42,10 +55,7 @@ export default function (pi: ExtensionAPI) {
42
55
  })
43
56
  }),
44
57
  async execute(_toolCallId, params, signal, _onUpdate, ctx) {
45
- const name = Value.Parse(
46
- engine.schema,
47
- process.env[engineOverride] ?? read(safeConfig(ctx), engine)
48
- )
58
+ const name = engineName(ctx)
49
59
  const results = httpOnly(await engines[name](params.query, signal))
50
60
  return {
51
61
  content: [{type: 'text', text: formatResults(params.query, results)}],
@@ -50,30 +50,55 @@ type Answer = {
50
50
  const isCoverage = (value: string): value is Coverage =>
51
51
  (coverages as readonly string[]).includes(value)
52
52
 
53
- const labelled = (line: string | undefined, label: string): string | undefined =>
54
- line?.startsWith(label) ? line.slice(label.length).trim() : undefined
53
+ // A markup run after the colon closes a dressed label, as in **COVERED:**, only before a space.
54
+ const labelPattern = (label: string, flags: string): RegExp =>
55
+ new RegExp(`^[*_\`#>\\s-]*${label}[*_\`\\s]*:(?:[*_\`]+(?=\\s|$))?`, flags)
55
56
 
56
- // Labels are found on trimmed lines, but the answer keeps its raw lines: blank lines and
57
- // indentation are part of a copied code block.
57
+ const labelled = (line: string | undefined, label: string, flags = 'i'): string | undefined => {
58
+ if (line === undefined) return undefined
59
+
60
+ const found = labelPattern(label, flags).exec(line)
61
+ return found === null ? undefined : line.slice(found[0].length).trim()
62
+ }
63
+
64
+ // A fence tag is one word, as c#, then attributes, as title="a". Other text is a quote. Each
65
+ // attribute has one parse only, so a looped line cannot backtrack exponentially.
66
+ const fence = /^(?:`{3,}|~{3,})(?:\s*[^\s`~]+)?(?:\s+(?:[\w-]+=(?:"[^"]*"|[^\s"]\S*)|\{[^}]*\}))*$/
67
+ const isContent = (line: string): boolean => line !== '' && !fence.test(line)
68
+
69
+ // Capitals only, so the answer may say Evidence:. The first splits, so no quote hides above it.
70
+ const dividerAt = (trimmed: string[], answerAt: number): number =>
71
+ trimmed.findIndex(
72
+ (line, index) => index > answerAt && labelled(line, 'EVIDENCE', '') !== undefined
73
+ )
74
+
75
+ // `no, partially` is a hedge, and read as no it would need no evidence.
76
+ const coverageOf = (line: string | undefined): Coverage | undefined => {
77
+ const said = labelled(line, 'COVERED')?.replace(/[*_`]/g, '').toLowerCase()
78
+ const word = /^([a-z]+)[.!]?$/.exec(said ?? '')?.[1]
79
+ return word !== undefined && isCoverage(word) ? word : undefined
80
+ }
81
+
82
+ // The answer keeps raw lines: blank lines and indentation belong to a copied code block.
58
83
  const parseAnswer = (text: string): Answer | undefined => {
59
84
  const lines = text.replaceAll('\r\n', '\n').split('\n')
60
85
  const trimmed = lines.map(line => line.trim())
61
- const coveredAt = trimmed.findIndex(line => line !== '')
62
- const answerAt = trimmed.findIndex((line, index) => index > coveredAt && line !== '')
63
- const divider = trimmed.findIndex(
64
- (line, index) => index > answerAt && line.startsWith('EVIDENCE:')
65
- )
66
- const covered = labelled(trimmed[coveredAt], 'COVERED:')?.toLowerCase()
67
- const first = labelled(trimmed[answerAt], 'ANSWER:')
86
+ const coveredAt = trimmed.findIndex(isContent)
87
+ const answerAt = trimmed.findIndex((line, index) => index > coveredAt && isContent(line))
88
+ const divider = dividerAt(trimmed, answerAt)
89
+ const covered = coverageOf(trimmed[coveredAt])
90
+ const first = labelled(trimmed[answerAt], 'ANSWER')
68
91
 
69
- if (covered === undefined || !isCoverage(covered)) return undefined
70
- if (first === undefined || divider === -1) return undefined
92
+ if (covered === undefined || first === undefined || divider === -1) return undefined
71
93
 
72
94
  const answer = [first, ...lines.slice(answerAt + 1, divider)].join('\n').trim()
73
95
 
74
96
  if (answer === '') return undefined
75
97
 
76
- const evidence = [labelled(trimmed[divider], 'EVIDENCE:') ?? '', ...trimmed.slice(divider + 1)]
98
+ const evidence = [
99
+ labelled(trimmed[divider], 'EVIDENCE', '') ?? '',
100
+ ...trimmed.slice(divider + 1)
101
+ ]
77
102
  return {covered, answer, evidence: evidence.filter(line => line !== '')}
78
103
  }
79
104
 
@@ -127,9 +152,9 @@ const missingFrom = (evidence: string[], sources: Source[]): string[] => {
127
152
  return pending.map(({line}) => line)
128
153
  }
129
154
 
130
- // A fence, a lone `}` or an emphasis-only line claims nothing, so it is dropped, not failed.
155
+ // A fence, even ```ts, a lone `}` or an emphasis-only line claims nothing, so it is dropped.
131
156
  const substantive = (evidence: string[]): string[] =>
132
- evidence.filter(line => /[\p{L}\p{N}]/u.test(normalize(line)))
157
+ evidence.filter(line => isContent(line.trim()) && /[\p{L}\p{N}]/u.test(normalize(line)))
133
158
 
134
159
  // The layout is returned as is, so the caller sees the coverage and the evidence too.
135
160
  const verifyAnswer = (text: string, sources: Source[]): string => {
@@ -0,0 +1,3 @@
1
+ �RCopyright 1990-2009 Adobe Systems Incorporated.
2
+ All rights reserved.
3
+ See ./LICENSE�CNS2-H
@@ -0,0 +1,3 @@
1
+ �RCopyright 1990-2009 Adobe Systems Incorporated.
2
+ All rights reserved.
3
+ See ./LICENSE� ETen-B5-H` ^
@@ -0,0 +1,4 @@
1
+ �RCopyright 1990-2009 Adobe Systems Incorporated.
2
+ All rights reserved.
3
+ See ./LICENSE!!��]aX!!]`�21�> �p �z�$]��"R�d�-U�7�*�
4
+ 4�%�+ �Z �{�/�%�<�9K�b�1]�.�"� �`]�,�"]�
5
+ �"]�h�"]�F�"]�$�"]��"]�`�"]�>�"]��"]�z�"]�X�"]�6�"]��"]�r�"]�P�"]�.�"]� �"]�j�"]�H�"]�&�"]��"]�b�"]�@�"]��"]�|�"]�Z�"]�8�"]��"]�t�"]�R�"]�0�"]��"]�l�"]�J�"]�(�"]��"]�d�"]�B�"]� �"X�~�']�W�"]�5�"]��"]�q�"]�O�"]�-�"]� �"]�i�"]�G�"]�%�"]��"]�a�"]�?�"]��"]�{�"]�Y�"]�7�"]��"]�s�"]�Q�"]�/�"]�
6
+ �"]�k�"]�I�"]�'�"]��"]�c�"]�A�"]��"]�}�"]�[�"]�9