@iiiokojiadbi/shift 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +12 -2
- package/core/about.ts +98 -0
- package/core/archive.ts +41 -0
- package/core/delivery.ts +131 -0
- package/core/docs.ts +396 -0
- package/core/flows.ts +468 -0
- package/core/glossary.ts +75 -0
- package/core/hint.ts +241 -0
- package/core/history.ts +73 -0
- package/core/index.ts +25 -0
- package/core/input.ts +190 -0
- package/core/layout.ts +173 -0
- package/core/loop.ts +142 -0
- package/core/markdown.ts +190 -0
- package/core/paths.ts +51 -0
- package/core/project.ts +27 -0
- package/core/records.ts +272 -0
- package/core/settings.ts +239 -0
- package/core/setup.ts +105 -0
- package/core/store.ts +144 -0
- package/core/tasks.ts +172 -0
- package/core/texts/doctrine.ts +373 -0
- package/core/texts/prompts.ts +261 -0
- package/core/texts/rows.ts +46 -0
- package/core/texts/tools.ts +244 -0
- package/core/tools.ts +193 -0
- package/core/track.ts +100 -0
- package/core/types.ts +129 -0
- package/index.ts +322 -0
- package/package.json +29 -4
- package/store.ts +56 -0
package/core/layout.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
// The mod's name: the tag its texts in the model's context are wrapped in name it.
|
|
3
|
+
export const MOD = 'shift'
|
|
4
|
+
|
|
5
|
+
export const TOOL = 'shift'
|
|
6
|
+
|
|
7
|
+
export const GUIDE_TOOL = 'shift_guide'
|
|
8
|
+
|
|
9
|
+
export const HISTORY_TOOL = 'shift_history'
|
|
10
|
+
|
|
11
|
+
export const OPEN_TOOL = 'task_open'
|
|
12
|
+
|
|
13
|
+
export const ARCHIVE_TOOL = 'task_archive'
|
|
14
|
+
|
|
15
|
+
export const TRACK_TOOL = 'task_track'
|
|
16
|
+
|
|
17
|
+
export const STATUS_TOOL = 'shift_status'
|
|
18
|
+
|
|
19
|
+
export const SETUP_TOOL = 'shift_setup'
|
|
20
|
+
|
|
21
|
+
export const OUTLINE_TOOL = 'doc_outline'
|
|
22
|
+
|
|
23
|
+
export const MENTIONS_TOOL = 'docs_mentioning'
|
|
24
|
+
|
|
25
|
+
export const RESUME_COMMAND = 'unshift'
|
|
26
|
+
|
|
27
|
+
export const TASKS_COMMAND = 'shift-tasks'
|
|
28
|
+
|
|
29
|
+
// Consecutive self-continued shifts before the mod stops and waits for the owner.
|
|
30
|
+
export const MAX_AUTO_STREAK = 3
|
|
31
|
+
|
|
32
|
+
// The board is the state of the work; past this it has turned into a diary.
|
|
33
|
+
export const BOARD_MAX_LINES = 150
|
|
34
|
+
|
|
35
|
+
// Owner decisions in force for the whole project; past this the code of rules has turned into a log.
|
|
36
|
+
export const OWNER_MAX_LINES = 150
|
|
37
|
+
|
|
38
|
+
// TASKS.md: one short line a task and its links.
|
|
39
|
+
export const TASKS_MAX_LINES = 150
|
|
40
|
+
|
|
41
|
+
// GLOSSARY.md comes into every session whole.
|
|
42
|
+
export const GLOSSARY_MAX_LINES = 400
|
|
43
|
+
|
|
44
|
+
// The docs map in SHIFT.md, in characters; past it only folders and file names are listed.
|
|
45
|
+
export const DOCS_MAP_MAX_CHARS = 100000
|
|
46
|
+
|
|
47
|
+
// The pinned blocks of the docs, in characters; past it the map says so, and shows them all still.
|
|
48
|
+
export const DOCS_PINNED_MAX_CHARS = 4000
|
|
49
|
+
|
|
50
|
+
// A knowledge file past this is split by topic.
|
|
51
|
+
export const KNOWLEDGE_MAX_LINES = 400
|
|
52
|
+
|
|
53
|
+
export const DIR = '.shift'
|
|
54
|
+
// Where the records lived before the mod was named shift.
|
|
55
|
+
export const OLD_DIR = '.infinity-shift'
|
|
56
|
+
|
|
57
|
+
// The names of the records inside the records folder, and inside each shift's folder, which keeps them under the
|
|
58
|
+
// same names: the one list reading, snapshotting and the setup's tree go by.
|
|
59
|
+
export const NAMES = {
|
|
60
|
+
board: 'board.md',
|
|
61
|
+
letter: 'letter.md',
|
|
62
|
+
owner: 'OWNER.md',
|
|
63
|
+
oldOwner: 'owner.md',
|
|
64
|
+
glossary: 'GLOSSARY.md',
|
|
65
|
+
shift: 'SHIFT.md',
|
|
66
|
+
taskList: 'TASKS.md',
|
|
67
|
+
settings: 'settings.toml',
|
|
68
|
+
tasks: 'tasks',
|
|
69
|
+
task: 'task.md',
|
|
70
|
+
track: 'track.md',
|
|
71
|
+
notes: 'notes',
|
|
72
|
+
knowledge: 'knowledge',
|
|
73
|
+
archive: 'archive',
|
|
74
|
+
index: 'index.md',
|
|
75
|
+
shifts: 'shifts',
|
|
76
|
+
about: 'about.md',
|
|
77
|
+
summary: 'summary.md',
|
|
78
|
+
} as const
|
|
79
|
+
|
|
80
|
+
export const BOARD = `${DIR}/${NAMES.board}`
|
|
81
|
+
|
|
82
|
+
export const LETTER = `${DIR}/${NAMES.letter}`
|
|
83
|
+
// The owner's settings for the project.
|
|
84
|
+
export const SETTINGS = `${DIR}/${NAMES.settings}`
|
|
85
|
+
// Loaded into every session, at these places unless the settings move them.
|
|
86
|
+
export const SHIFT_FILE = `${DIR}/${NAMES.shift}`
|
|
87
|
+
export const GLOSSARY = `${DIR}/${NAMES.glossary}`
|
|
88
|
+
export const TASK_LIST = `${DIR}/${NAMES.taskList}`
|
|
89
|
+
|
|
90
|
+
export const OWNER = `${DIR}/${NAMES.owner}`
|
|
91
|
+
|
|
92
|
+
// A text written with the default places of OWNER.md, GLOSSARY.md and SHIFT.md, at the places the project's settings
|
|
93
|
+
// give instead: every text names its files once, by the defaults, and is moved here.
|
|
94
|
+
// One pass, so a moved path is never moved again, whatever it looks like.
|
|
95
|
+
export function atPlaces(text: string, files: { owner: string; glossary: string; shift: string }): string {
|
|
96
|
+
const places: Record<string, string> = { [OWNER]: files.owner, [GLOSSARY]: files.glossary, [SHIFT_FILE]: files.shift }
|
|
97
|
+
|
|
98
|
+
return text.replace(new RegExp([OWNER, GLOSSARY, SHIFT_FILE].map(escapeRegExp).join('|'), 'g'), found => places[found] ?? found)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function escapeRegExp(text: string): string {
|
|
102
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
103
|
+
}
|
|
104
|
+
// The name owner decisions had before OWNER.md.
|
|
105
|
+
export const OLD_OWNER = `${DIR}/${NAMES.oldOwner}`
|
|
106
|
+
|
|
107
|
+
export const KNOWLEDGE = `${DIR}/${NAMES.knowledge}`
|
|
108
|
+
|
|
109
|
+
export const TASKS = `${DIR}/${NAMES.tasks}`
|
|
110
|
+
|
|
111
|
+
export const ARCHIVE = `${DIR}/${NAMES.archive}`
|
|
112
|
+
|
|
113
|
+
export const INDEX = `${ARCHIVE}/${NAMES.index}`
|
|
114
|
+
|
|
115
|
+
export const SHIFTS = `${DIR}/${NAMES.shifts}`
|
|
116
|
+
|
|
117
|
+
// A file of a task's folder under `base`, the records folder or a shift's.
|
|
118
|
+
export function taskFile(base: string, name: string, file: 'task' | 'track'): string {
|
|
119
|
+
return `${base}/${NAMES.tasks}/${name}/${NAMES[file]}`
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const BOARD_SECTIONS = ['Goal', 'Decisions', 'Dead ends'] as const
|
|
123
|
+
|
|
124
|
+
// Owner decisions moved from the board to their own file; a board that still has the section is refused with the move to make.
|
|
125
|
+
export const OWNER_SECTION = 'Owner decisions'
|
|
126
|
+
|
|
127
|
+
export const TASK_SECTIONS = ['Goal', 'Done when', 'Owner decisions', 'Decisions', 'Dead ends'] as const
|
|
128
|
+
|
|
129
|
+
export const LETTER_SECTIONS = ['Where I am', 'Next', 'Unverified', 'Hold'] as const
|
|
130
|
+
// The letter's steps, the ones the agent continues from, and what the summarizer is told to keep.
|
|
131
|
+
export const NEXT = LETTER_SECTIONS[1]
|
|
132
|
+
export const HOLD = LETTER_SECTIONS[3]
|
|
133
|
+
// The board's section the open tasks lived in before TASKS.md.
|
|
134
|
+
export const OLD_TASKS_SECTION = 'Tasks'
|
|
135
|
+
// What follows a shift, and how an archived task ended.
|
|
136
|
+
export const AFTER = ['continue', 'wait'] as const
|
|
137
|
+
export const OUTCOMES = ['done', 'dropped'] as const
|
|
138
|
+
|
|
139
|
+
// Where a task stands on the board; closing it is archiving it.
|
|
140
|
+
export const FOCUS = ['in progress', 'deferred', 'waiting for owner'] as const
|
|
141
|
+
|
|
142
|
+
// Where an item of a task's track stands: open ones still to do, closed ones done or dropped.
|
|
143
|
+
const OPEN = ['in progress', 'open'] as const
|
|
144
|
+
const CLOSING = ['done', 'dropped'] as const
|
|
145
|
+
export const STATUSES = [...OPEN, ...CLOSING] as const
|
|
146
|
+
export const CLOSED: ReadonlySet<string> = new Set(CLOSING)
|
|
147
|
+
|
|
148
|
+
// Kinds of knowledge files and the fields every `###` entry of a kind carries; a glossary is `- **term** — meaning` lines.
|
|
149
|
+
export const KINDS = {
|
|
150
|
+
traps: ['**Cause.**', '**Do.**'],
|
|
151
|
+
lessons: ['**Why.**', '**Not when.**'],
|
|
152
|
+
runbook: ['**Shows.**'],
|
|
153
|
+
facts: ['**Source.**'],
|
|
154
|
+
} as const satisfies Record<string, readonly string[]>
|
|
155
|
+
|
|
156
|
+
// The description of a shift, as `about.md` and the history show it; an archived task's line, as the index shows it.
|
|
157
|
+
export const ABOUT_MAX = 400
|
|
158
|
+
|
|
159
|
+
export const HISTORY_DEFAULT = 10
|
|
160
|
+
|
|
161
|
+
export const HISTORY_MAX = 50
|
|
162
|
+
|
|
163
|
+
// How many shifts' descriptions go back with the message after a shift.
|
|
164
|
+
export const RECENT_SHIFTS = 10
|
|
165
|
+
|
|
166
|
+
// A task whose files went unchanged through this many shifts is named after a shift: waiting may have become stuck.
|
|
167
|
+
export const STUCK_SHIFTS = 5
|
|
168
|
+
|
|
169
|
+
// How far back the snapshots are compared for it.
|
|
170
|
+
export const STUCK_LOOKBACK = 30
|
|
171
|
+
|
|
172
|
+
export const NAME = /^[a-z0-9][a-z0-9-]*$/
|
|
173
|
+
export const NAME_RULE = 'lowercase latin, digits and dashes'
|
package/core/loop.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import { settleShift, shiftNotMade, shiftUnsettled } from './flows'
|
|
3
|
+
import { type GaugeKeeper, takeHint } from './hint'
|
|
4
|
+
import { MAX_AUTO_STREAK } from './layout'
|
|
5
|
+
import { basename } from './paths'
|
|
6
|
+
import type { Project } from './project'
|
|
7
|
+
import { compactInstructions, FAILURE_NOTE, NOT_MADE_NOTE, UNSETTLED_NOTE } from './texts/prompts'
|
|
8
|
+
import { DELIVERED, NOT_CONTINUED } from './texts/rows'
|
|
9
|
+
import type { PendingShift, Settled, WaitNote } from './types'
|
|
10
|
+
|
|
11
|
+
// The shift loop: from an accepted shift through the let-go to continuing or waiting for the owner, and the owner's
|
|
12
|
+
// next message. The state and its transitions are the core's; an adapter keeps the state where its harness keeps
|
|
13
|
+
// state and does only what its harness alone can: let the history go, start a turn, show a row.
|
|
14
|
+
|
|
15
|
+
export type LoopState = {
|
|
16
|
+
// The shift accepted in this turn, let go once the turn ends.
|
|
17
|
+
pending: PendingShift | null
|
|
18
|
+
// `continue` shifts in a row without the owner.
|
|
19
|
+
streak: number
|
|
20
|
+
// The letter and the records waiting for the owner's next message.
|
|
21
|
+
note: WaitNote | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type Level = 'info' | 'warning' | 'error'
|
|
25
|
+
|
|
26
|
+
export type Loop = {
|
|
27
|
+
read(): Promise<LoopState>
|
|
28
|
+
// A change applied to the state as it is then.
|
|
29
|
+
update(change: (current: LoopState) => LoopState): Promise<void>
|
|
30
|
+
tell(row: string, level: Level): void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// What only the harness does at a let-go.
|
|
34
|
+
export type LetGoHarness = {
|
|
35
|
+
// How full the history is now; undefined before the first reading.
|
|
36
|
+
fill(): Promise<number | undefined>
|
|
37
|
+
// Lets the history go with the instructions: what it went into and the fills around it, or why it did not.
|
|
38
|
+
letGo(
|
|
39
|
+
instructions: string,
|
|
40
|
+
): Promise<{ messages: readonly { role: string; text: string }[]; before?: number; after?: number } | { skip: string }>
|
|
41
|
+
// Starts the agent's next turn with the prompt.
|
|
42
|
+
continueWith(prompt: string): Promise<void>
|
|
43
|
+
// What the harness refreshes once the history is let go, before the message after it.
|
|
44
|
+
afterLetGo?(): Promise<void>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const FRESH_LOOP: LoopState = { pending: null, streak: 0, note: null }
|
|
48
|
+
|
|
49
|
+
// An accepted shift waits for its turn to end.
|
|
50
|
+
export async function holdShift(loop: Loop, shift: PendingShift): Promise<void> {
|
|
51
|
+
await loop.update(state => ({ ...state, pending: shift }))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// A hint for the agent if the fill calls for one; none while a shift is pending.
|
|
55
|
+
export async function loopHint(loop: Loop, gauge: GaugeKeeper): Promise<string | null> {
|
|
56
|
+
if ((await loop.read()).pending !== null) return null
|
|
57
|
+
|
|
58
|
+
return takeHint(gauge)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Once the turn has ended: a pending shift lets the history go, unless the turn was aborted. Every outcome is written
|
|
62
|
+
// to the shift's about.md and told to the owner; nothing is lost silently.
|
|
63
|
+
export async function afterTurn(project: Project, loop: Loop, harness: LetGoHarness, aborted: boolean): Promise<void> {
|
|
64
|
+
const shift = (await loop.read()).pending
|
|
65
|
+
if (shift === null) return
|
|
66
|
+
await loop.update(current => ({ ...current, pending: null }))
|
|
67
|
+
const before = await harness.fill()
|
|
68
|
+
const name = basename(shift.dir)
|
|
69
|
+
if (aborted) {
|
|
70
|
+
const why = 'the turn was aborted'
|
|
71
|
+
loop.tell(await shiftNotMade(project, shift, why, before), 'warning')
|
|
72
|
+
await tellAgent(loop, harness, shift, NOT_MADE_NOTE(name, why), false)
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const done = await harness.letGo(compactInstructions(shift)).catch((error: unknown) => ({ skip: `the let-go failed: ${String(error)}` }))
|
|
77
|
+
if ('skip' in done) {
|
|
78
|
+
loop.tell(await shiftNotMade(project, shift, done.skip, before), 'error')
|
|
79
|
+
await tellAgent(loop, harness, shift, NOT_MADE_NOTE(name, done.skip), true)
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
let settled: Settled
|
|
83
|
+
try {
|
|
84
|
+
await harness.afterLetGo?.()
|
|
85
|
+
settled = await settleShift(project, {
|
|
86
|
+
shift,
|
|
87
|
+
messages: done.messages,
|
|
88
|
+
before: done.before ?? before,
|
|
89
|
+
after: done.after ?? (await harness.fill()) ?? 0,
|
|
90
|
+
// Read now: the owner may have spoken while the history was let go.
|
|
91
|
+
streak: (await loop.read()).streak,
|
|
92
|
+
})
|
|
93
|
+
} catch (error) {
|
|
94
|
+
const why = String(error)
|
|
95
|
+
loop.tell(await shiftUnsettled(project, shift, why, done.before ?? before), 'error')
|
|
96
|
+
await tellAgent(loop, harness, shift, UNSETTLED_NOTE(name, why, shift.dir), true)
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
if (settled.after === 'wait') {
|
|
100
|
+
const { note } = settled
|
|
101
|
+
await loop.update(current => ({ ...current, note }))
|
|
102
|
+
loop.tell(settled.row, 'info')
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
const { streak } = settled
|
|
106
|
+
await loop.update(current => ({ ...current, streak }))
|
|
107
|
+
loop.tell(settled.row, 'info')
|
|
108
|
+
// The shift is settled and written; a turn that does not start is told, not recorded against it.
|
|
109
|
+
await harness.continueWith(settled.prompt).catch((error: unknown) => loop.tell(NOT_CONTINUED(String(error)), 'error'))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// A failed shift is told to the agent, who was told it was accepted: at once when it was to continue and may go on,
|
|
113
|
+
// within the same count of runs without the owner; otherwise with the owner's next message.
|
|
114
|
+
async function tellAgent(loop: Loop, harness: LetGoHarness, shift: PendingShift, context: string, goOn: boolean): Promise<void> {
|
|
115
|
+
const { streak } = await loop.read()
|
|
116
|
+
if (goOn && shift.after === 'continue' && streak < MAX_AUTO_STREAK) {
|
|
117
|
+
await loop.update(current => ({ ...current, streak: current.streak + 1 }))
|
|
118
|
+
await harness.continueWith(context).catch((error: unknown) => loop.tell(NOT_CONTINUED(String(error)), 'error'))
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
await loop.update(current => ({ ...current, note: { context, dir: shift.dir, files: [FAILURE_NOTE] } }))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The owner spoke: the count of shifts without them starts over.
|
|
125
|
+
export async function ownerSpoke(loop: Loop): Promise<void> {
|
|
126
|
+
await loop.update(state => ({ ...state, streak: 0 }))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// The note waiting for the owner's message, if any, and what clears it once it went with that message: a failed
|
|
130
|
+
// delivery keeps it for the next one.
|
|
131
|
+
export async function waitingNote(loop: Loop): Promise<{ context: string; delivered: () => Promise<void> } | null> {
|
|
132
|
+
const { note } = await loop.read()
|
|
133
|
+
if (note === null) return null
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
context: note.context,
|
|
137
|
+
delivered: async () => {
|
|
138
|
+
await loop.update(state => ({ ...state, note: null }))
|
|
139
|
+
loop.tell(DELIVERED(note.files, note.dir), 'info')
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
}
|
package/core/markdown.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import type { Item } from './types'
|
|
3
|
+
|
|
4
|
+
// Every record and document is read by these: a heading is a `#` line outside code fences, so a `## ` inside a
|
|
5
|
+
// fenced command or example — in the letter's Hold, a runbook entry, a document — never cuts what it is in.
|
|
6
|
+
|
|
7
|
+
export type Heading = { level: number; title: string; line: number; note: string }
|
|
8
|
+
|
|
9
|
+
const HEADING = /^ {0,3}(#{1,6}) +(.+?)\s*$/
|
|
10
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/
|
|
11
|
+
|
|
12
|
+
// The lines inside code fences, the fences included: an opener of three or more backticks or tildes, closed by a line
|
|
13
|
+
// of the same character at least as long with nothing else on it. A backtick opener with a backtick after it is
|
|
14
|
+
// inline code, and an opener never closed is no fence, so a stray one never hides the rest of a record.
|
|
15
|
+
function fenced(lines: readonly string[]): Set<number> {
|
|
16
|
+
const inside = new Set<number>()
|
|
17
|
+
for (let at = 0; at < lines.length; at++) {
|
|
18
|
+
const [, mark = '', info = ''] = FENCE.exec(lines[at] ?? '') ?? []
|
|
19
|
+
if (mark === '' || (mark.startsWith('`') && info.includes('`'))) continue
|
|
20
|
+
const closer = new RegExp(`^ {0,3}${mark.startsWith('`') ? '`' : '~'}{${mark.length},}\\s*$`)
|
|
21
|
+
const close = lines.findIndex((line, index) => index > at && closer.test(line))
|
|
22
|
+
if (close === -1) continue
|
|
23
|
+
for (let index = at; index <= close; index++) inside.add(index)
|
|
24
|
+
at = close
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return inside
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Every heading outside code fences: its level, its title, its line from 1, and the `> ` line right under it.
|
|
31
|
+
export function headings(text: string): Heading[] {
|
|
32
|
+
const lines = text.split('\n')
|
|
33
|
+
const inside = fenced(lines)
|
|
34
|
+
const found: Heading[] = []
|
|
35
|
+
for (const [index, line] of lines.entries()) {
|
|
36
|
+
const match = inside.has(index) ? null : HEADING.exec(line)
|
|
37
|
+
if (match === null) continue
|
|
38
|
+
const under = lines[index + 1] ?? ''
|
|
39
|
+
found.push({
|
|
40
|
+
level: match[1]?.length ?? 1,
|
|
41
|
+
title: match[2] ?? '',
|
|
42
|
+
line: index + 1,
|
|
43
|
+
note: under.startsWith('> ') ? under.slice(2).trim() : '',
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return found
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const PIN = /^ {0,3}> ?\[!IMPORTANT\]\s*$/
|
|
51
|
+
const QUOTED = /^ {0,3}> ?/
|
|
52
|
+
|
|
53
|
+
// A pinned block: a `> [!IMPORTANT]` quote, as GitHub draws it, outside code fences.
|
|
54
|
+
export type Pin = { section: string | null; text: string }
|
|
55
|
+
|
|
56
|
+
// The pinned blocks of a text: each one's lines without their `> `, and the `##` or `###` section it is in.
|
|
57
|
+
export function pinned(text: string): Pin[] {
|
|
58
|
+
const lines = text.split('\n')
|
|
59
|
+
const inside = fenced(lines)
|
|
60
|
+
const marks = headings(text).filter(heading => heading.level >= 2)
|
|
61
|
+
const found: Pin[] = []
|
|
62
|
+
for (let at = 0; at < lines.length; at++) {
|
|
63
|
+
if (inside.has(at) || !PIN.test(lines[at] ?? '')) continue
|
|
64
|
+
let end = at + 1
|
|
65
|
+
while (end < lines.length && !inside.has(end) && QUOTED.test(lines[end] ?? '')) end++
|
|
66
|
+
const body = lines
|
|
67
|
+
.slice(at + 1, end)
|
|
68
|
+
.map(line => line.replace(QUOTED, ''))
|
|
69
|
+
.join('\n')
|
|
70
|
+
.trim()
|
|
71
|
+
if (body !== '') found.push({ section: marks.filter(heading => heading.line <= at).at(-1)?.title ?? null, text: body })
|
|
72
|
+
at = end - 1
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return found
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The blocks under the headings of `level`: each title with the text up to the next heading of that level or above.
|
|
79
|
+
export function blocks(text: string, level: number): { title: string; body: string }[] {
|
|
80
|
+
const lines = text.split('\n')
|
|
81
|
+
const marks = headings(text).filter(heading => heading.level <= level)
|
|
82
|
+
|
|
83
|
+
return marks.flatMap((heading, at) =>
|
|
84
|
+
heading.level === level
|
|
85
|
+
? [{ title: heading.title, body: lines.slice(heading.line, (marks[at + 1]?.line ?? lines.length + 1) - 1).join('\n') }]
|
|
86
|
+
: [],
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// `## heading` blocks of a markdown file, trimmed; text before the first heading is dropped.
|
|
91
|
+
export function sections(text: string): Map<string, string> {
|
|
92
|
+
return new Map(blocks(text, 2).map(block => [block.title, block.body.trim()]))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The `# ` title; null without one.
|
|
96
|
+
export function title(text: string): string | null {
|
|
97
|
+
return headings(text).find(heading => heading.level === 1)?.title ?? null
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function lineCount(text: string): number {
|
|
101
|
+
return text.trimEnd().split('\n').length
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The refusal for a record over its line limit, with what to do about it; null within it.
|
|
105
|
+
export function overLimit(where: string, text: string, max: number, advice: string): string | null {
|
|
106
|
+
const lines = lineCount(text)
|
|
107
|
+
|
|
108
|
+
return lines > max ? `${where} is ${lines} lines, the limit is ${max}: ${advice}` : null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The refusal for a date in a record, with why it has none; null without one.
|
|
112
|
+
export function dated(where: string, text: string, why: string): string | null {
|
|
113
|
+
const date = findDate(text)
|
|
114
|
+
|
|
115
|
+
return date === null ? null : `A date in ${where}: "${date}". ${why} Remove it.`
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const quote = (names: readonly string[], separator = ', ') => names.map(name => `"${name}"`).join(separator)
|
|
119
|
+
|
|
120
|
+
// Whether a value is one of a list of literals, narrowed to it.
|
|
121
|
+
export function isOneOf<T extends string>(list: readonly T[], value: unknown): value is T {
|
|
122
|
+
return (list as readonly unknown[]).includes(value)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function missingSections(text: string, required: readonly string[], where: string): string | null {
|
|
126
|
+
const found = sections(text)
|
|
127
|
+
const missing = required.filter(name => !found.has(name))
|
|
128
|
+
if (missing.length > 0) return `${where} lacks sections: ${missing.map(m => `"## ${m}"`).join(', ')}.`
|
|
129
|
+
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const DATES = [/\b(19|20)\d{2}-\d{2}-\d{2}\b/, /\b\d{1,2}\.\d{1,2}\.(19|20)\d{2}\b/]
|
|
134
|
+
|
|
135
|
+
export function findDate(text: string): string | null {
|
|
136
|
+
for (const line of text.split('\n')) {
|
|
137
|
+
if (DATES.some(re => re.test(line))) return line.trim()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Every `- ` line of a text as an item; a line without a `[status]` has an empty one.
|
|
144
|
+
export function itemsOf(text: string): Item[] {
|
|
145
|
+
return text.split('\n').flatMap((raw, index) => {
|
|
146
|
+
if (!raw.startsWith('- ')) return []
|
|
147
|
+
const line = raw.trim()
|
|
148
|
+
const match = /^- \[([^\]]+)\] *(.*)$/.exec(line)
|
|
149
|
+
const rest = match?.[2] ?? line.slice(2).trim()
|
|
150
|
+
const cut = rest.indexOf(' — ')
|
|
151
|
+
|
|
152
|
+
return [
|
|
153
|
+
{
|
|
154
|
+
status: match?.[1] ?? '',
|
|
155
|
+
name: cut === -1 ? rest : rest.slice(0, cut),
|
|
156
|
+
note: cut === -1 ? null : rest.slice(cut + 3),
|
|
157
|
+
rest,
|
|
158
|
+
line,
|
|
159
|
+
index,
|
|
160
|
+
},
|
|
161
|
+
]
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function formatItem(status: string, name: string, note: string | null): string {
|
|
166
|
+
return `- [${status}] ${name}${note === null ? '' : ` — ${note}`}`
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function formatK(n: number): string {
|
|
170
|
+
return n >= 1000 ? `${(n / 1000).toFixed(0)}K` : String(n)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// `key: value` lines between the leading `---` fences.
|
|
174
|
+
export function frontmatter(text: string): Map<string, string> {
|
|
175
|
+
const found = new Map<string, string>()
|
|
176
|
+
const match = /^---\n([\s\S]*?)\n---/.exec(text)
|
|
177
|
+
for (const line of (match?.[1] ?? '').split('\n')) {
|
|
178
|
+
const colon = line.indexOf(':')
|
|
179
|
+
if (colon > 0) found.set(line.slice(0, colon).trim(), line.slice(colon + 1).trim())
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return found
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function bullets(body: string | undefined): string[] {
|
|
186
|
+
return (body ?? '')
|
|
187
|
+
.split('\n')
|
|
188
|
+
.filter(line => line.startsWith('- '))
|
|
189
|
+
.map(line => line.slice(2))
|
|
190
|
+
}
|
package/core/paths.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
// Paths as the mod writes them: `/`-separated, from the project's root unless absolute. One place, so a path with no
|
|
3
|
+
// folder means the same everywhere: its folder is `.`.
|
|
4
|
+
|
|
5
|
+
export function dirname(path: string): string {
|
|
6
|
+
const at = path.lastIndexOf('/')
|
|
7
|
+
|
|
8
|
+
return at === -1 ? '.' : at === 0 ? '/' : path.slice(0, at)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// The last part, a trailing `/` ignored.
|
|
12
|
+
export function basename(path: string): string {
|
|
13
|
+
return (
|
|
14
|
+
path
|
|
15
|
+
.split('/')
|
|
16
|
+
.filter(part => part !== '')
|
|
17
|
+
.at(-1) ?? ''
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// `name` inside `dir`, where `.` is the folder itself.
|
|
22
|
+
export function join(dir: string, name: string): string {
|
|
23
|
+
return dir === '.' || dir === '' ? name : name === '.' ? dir : `${dir}/${name}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// `.`, `..` and empty parts folded away; null when it climbs out of where it started.
|
|
27
|
+
export function normalize(path: string): string | null {
|
|
28
|
+
const parts: string[] = []
|
|
29
|
+
for (const part of path.split('/')) {
|
|
30
|
+
if (part === '' || part === '.') continue
|
|
31
|
+
if (part !== '..') parts.push(part)
|
|
32
|
+
else if (parts.pop() === undefined) return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return parts.join('/')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// An absolute path for one the agent gave: absolute as is, else from the root.
|
|
39
|
+
export function resolvePath(root: string, path: string): string {
|
|
40
|
+
return path.startsWith('/') ? path : `${root}/${path.replace(/^\.\//, '')}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A path from the root for one that may be absolute.
|
|
44
|
+
export function fromRoot(root: string, file: string): string {
|
|
45
|
+
return file.startsWith(`${root}/`) ? file.slice(root.length + 1) : file.replace(/^\.\//, '')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Relative, without a way out of the project.
|
|
49
|
+
export function isInside(path: string): boolean {
|
|
50
|
+
return path !== '' && !path.startsWith('/') && !path.split('/').includes('..')
|
|
51
|
+
}
|
package/core/project.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import { SETTINGS } from './layout'
|
|
3
|
+
import { DEFAULT_SETTINGS, parseSettings, type Settings } from './settings'
|
|
4
|
+
import type { Store } from './store'
|
|
5
|
+
|
|
6
|
+
// One project as an operation of the mod sees it: its files and its settings, read once when the operation starts.
|
|
7
|
+
// A settings file that does not parse leaves the mod on with the defaults — turning it off by a typo would be silent —
|
|
8
|
+
// and its problem is told to the owner at the session's start and refuses a shift until the owner fixes it.
|
|
9
|
+
export type Project = {
|
|
10
|
+
store: Store
|
|
11
|
+
root: string
|
|
12
|
+
settings: Settings
|
|
13
|
+
// What is wrong with settings.toml; null when it parses or there is none.
|
|
14
|
+
problem: string | null
|
|
15
|
+
// settings.toml as the owner wrote it, null without one: a shift snapshots it.
|
|
16
|
+
settingsText: string | null
|
|
17
|
+
// The keys the file sets itself; the rest are at their defaults.
|
|
18
|
+
set: ReadonlySet<string>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function loadProject(store: Store, root: string): Promise<Project> {
|
|
22
|
+
const settingsText = await store.read(`${root}/${SETTINGS}`)
|
|
23
|
+
const parsed = settingsText === null ? null : parseSettings(settingsText)
|
|
24
|
+
if (typeof parsed === 'string') return { store, root, settings: DEFAULT_SETTINGS, problem: parsed, settingsText, set: new Set() }
|
|
25
|
+
|
|
26
|
+
return { store, root, settings: parsed?.settings ?? DEFAULT_SETTINGS, problem: null, settingsText, set: parsed?.set ?? new Set() }
|
|
27
|
+
}
|