mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): name the tools that changed within a Code Mode namespace
This commit is contained in:
@@ -41,10 +41,14 @@ ${render(current)}`
|
||||
)
|
||||
const parts = [
|
||||
...block("New tool namespaces are available:", sections.added),
|
||||
...block(
|
||||
"These namespaces changed; their listings supersede the previous ones:",
|
||||
sections.changed.map((change) => change.current),
|
||||
),
|
||||
...(sections.changed.length === 0
|
||||
? []
|
||||
: [
|
||||
[
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:",
|
||||
...sections.changed.map((change) => changes(change.previous, change.current)),
|
||||
].join("\n"),
|
||||
]),
|
||||
...(sections.removed.length === 0
|
||||
? []
|
||||
: [
|
||||
@@ -58,6 +62,37 @@ ${render(current)}`
|
||||
return delta.length < replacement.length ? delta : replacement
|
||||
}
|
||||
|
||||
// One namespace's change: the tools added, changed or gone under its new header, or its whole listing when that is
|
||||
// shorter. A namespace too large to list in full can drop a name without losing the tool, so it says "listed".
|
||||
function changes(previous: CodeModeCatalog.Section, current: CodeModeCatalog.Section) {
|
||||
const names = Instructions.diffByKey(previous.names, current.names, toolName, (before, after) => before !== after)
|
||||
const pinned = Instructions.diffByKey(
|
||||
previous.pinned,
|
||||
current.pinned,
|
||||
(entry) => entry.path,
|
||||
(before, after) => before.line !== after.line,
|
||||
)
|
||||
const gone = [
|
||||
...pinned.removed.map((entry) => entry.path.slice(current.name.length + 1)),
|
||||
...names.removed.map(toolName),
|
||||
]
|
||||
const capped = current.pinned.length + current.names.length < current.count
|
||||
const listed = [...names.added, ...names.changed.map((change) => change.current)]
|
||||
const delta = [
|
||||
CodeModeCatalog.namespaceLine(current),
|
||||
...[...pinned.added, ...pinned.changed.map((change) => change.current)].map((entry) => entry.line),
|
||||
...(listed.length === 0 ? [] : [` - new or changed: ${listed.join(", ")}`]),
|
||||
...(gone.length === 0 ? [] : [` - no longer ${capped ? "listed" : "available"}: ${gone.join(", ")}`]),
|
||||
].join("\n")
|
||||
const whole = CodeModeCatalog.renderSection(current)
|
||||
return delta.length < whole.length ? delta : whole
|
||||
}
|
||||
|
||||
function toolName(listing: string) {
|
||||
const open = listing.indexOf("(")
|
||||
return open === -1 ? listing : listing.slice(0, open)
|
||||
}
|
||||
|
||||
function block(heading: string, sections: ReadonlyArray<CodeModeCatalog.Section>) {
|
||||
return sections.length === 0 ? [] : [[heading, ...sections.map(CodeModeCatalog.renderSection)].join("\n")]
|
||||
}
|
||||
|
||||
@@ -148,11 +148,32 @@ describe("CodeModeInstructions.update", () => {
|
||||
)
|
||||
})
|
||||
|
||||
test("restates only the sections that changed", () => {
|
||||
const text = update([...tools("alpha", 3), ...tools("beta", 2)], [...tools("alpha", 4), ...tools("beta", 2)])
|
||||
expect(text).toBe(
|
||||
"The Code Mode tool catalog has changed.\n\nThese namespaces changed; their listings supersede the previous ones:\n- alpha (4 tools)\n - tool0(id), tool1(id), tool2(id), tool3(id)",
|
||||
test("names the tools that changed in a namespace", () => {
|
||||
expect(update([...tools("alpha", 3), ...tools("beta", 2)], [...tools("alpha", 4), ...tools("beta", 2)])).toBe(
|
||||
"The Code Mode tool catalog has changed.\n\n" +
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" +
|
||||
"- alpha (4 tools)\n - new or changed: tool3(id)",
|
||||
)
|
||||
const changed = [entry("alpha.tool0", "x", { parameters: ["id", "…"] }), ...tools("alpha", 3).slice(1)]
|
||||
expect(update(tools("alpha", 3), changed)).toContain("- alpha (3 tools)\n - new or changed: tool0(id, …)")
|
||||
expect(update(tools("alpha", 10), tools("alpha", 10).slice(1))).toContain(
|
||||
"- alpha (9 tools)\n - no longer available: tool0",
|
||||
)
|
||||
})
|
||||
|
||||
test("says a name is no longer listed when its namespace is too large to list in full", () => {
|
||||
const before = summarize(tools("bulk", 400), 40)
|
||||
const after = summarize([entry("bulk.a_first", "First", { parameters: ["id"] }), ...tools("bulk", 400)], 40)
|
||||
const text = CodeModeInstructions.update(before, after)
|
||||
expect(text).toContain(" - new or changed: a_first(id)")
|
||||
expect(text).toMatch(/ {2}- no longer listed: tool\d+/)
|
||||
expect(text).not.toContain("no longer available")
|
||||
})
|
||||
|
||||
test("restates a namespace's whole listing when that is shorter than its changes", () => {
|
||||
const text = update(tools("alpha", 2), [entry("alpha.renamed", "x", { parameters: ["id"] })])
|
||||
expect(text).toContain("- alpha (1 tool)\n - renamed(id)")
|
||||
expect(text).not.toContain("new or changed")
|
||||
})
|
||||
|
||||
test("restates a section whose description changed", () => {
|
||||
|
||||
@@ -2429,7 +2429,9 @@ describe("SessionRunnerLLM", () => {
|
||||
yield* s.runPrompt("Fourth")
|
||||
const grown = yield* section("alpha")
|
||||
expect(updates(s.requests.at(-1)).at(-1)).toBe(
|
||||
`The Code Mode tool catalog has changed.\n\nThese namespaces changed; their listings supersede the previous ones:\n${grown}`,
|
||||
"The Code Mode tool catalog has changed.\n\n" +
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" +
|
||||
"- alpha (13 tools)\n - new or changed: alpha_12(id, …)",
|
||||
)
|
||||
|
||||
// Compaction restates the stored catalog as the new baseline and has no update to send.
|
||||
|
||||
Reference in New Issue
Block a user