Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
fix: parse separate JSON objects in skill selection replies (#641)
jiangjiang24848b056a2618f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c18f77b8..ab30fd9d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,6 +18,14 @@ now read, each on its own labelled line. The URI leads and the name and descripta long name cannot push the pointer past the result cap; a server's `title` is shown over its `name`when it gives one. A part that already carried text is unchanged.+### Skill selection keeps capabilities named across multiple JSON replies++When a model wraps its skill choice in prose or sends a revised JSON object, OpenBot reads each+complete `skills` list and offers the union of the named skills' granted tools. This also works with+Anthropic's OpenAI-compatible endpoint, which may ignore the request for bare JSON. Previously a+reply containing multiple objects fell back to offering every tool; replies with no valid `skills`+list still do.+### Dictate messages and talk to a coworker in a live voice callDeployments can configure transcription separately from their Bots' models, with a waveform composerdiff --git a/server/src/plugins/selection.ts b/server/src/plugins/selection.tsindex 1cd9314c..105ff53e 100644--- a/server/src/plugins/selection.ts+++ b/server/src/plugins/selection.ts@@ -129,33 +129,58 @@ export function readChosenSkills(skills: readonly SelectableSkill[],): string[] | null {/*- * The object in the answer, not the answer as a whole.+ * Every complete object with a skills list, not the answer as a whole.** `response_format` asks for bare JSON and does not guarantee it: Anthropic's OpenAI-compatible* endpoint ignores the field, and a model left to itself often fences its object or leads with a* sentence. Parsed whole, every such answer read as a selector that could not say, and a Bot on- * that model was offered its entire catalogue on every run. The router reads its answer from the- * same completer this way already (`classify.ts`); an answer with no object in it is still null.+ * that model was offered its entire catalogue on every run. A greedy brace match also swallows+ * two adjacent objects into invalid JSON. Unioning their lists avoids dropping a skill when the+ * model revises its selection. The router at `server/src/routing/classify.ts` still uses the+ * greedy match; this scanner is local to skill selection. An answer with no usable object is null.*/- const object = answer.match(/\{[\s\S]*\}/);- if (!object) return null;- let parsed: unknown;- try {- parsed = JSON.parse(object[0]);- } catch {- return null;- }- if (typeof parsed !== "object" || parsed === null) return null;- const chosen = (parsed as { skills?: unknown }).skills;- if (!Array.isArray(chosen)) return null;const known = new Set(skills.map((skill) => skill.slug));- return [- ...new Set(- chosen.filter(- (slug): slug is string => typeof slug === "string" && known.has(slug),- ),- ),- ];+ const selected = new Set<string>();+ let foundSkillsList = false;+ let start = -1;+ let depth = 0;+ let inString = false;+ let escaped = false;+ for (let index = 0; index < answer.length; index += 1) {+ const char = answer[index];+ if (depth === 0) {+ if (char === "{") {+ start = index;+ depth = 1;+ }+ continue;+ }+ if (inString) {+ if (escaped) escaped = false;+ else if (char === "\\") escaped = true;+ else if (char === '"') inString = false;+ continue;+ }+ if (char === '"') inString = true;+ else if (char === "{") depth += 1;+ else if (char === "}") {+ depth -= 1;+ if (depth !== 0) continue;+ try {+ const parsed: unknown = JSON.parse(answer.slice(start, index + 1));+ if (typeof parsed !== "object" || parsed === null) continue;+ const chosen = (parsed as { skills?: unknown }).skills;+ if (!Array.isArray(chosen)) continue;+ foundSkillsList = true;+ for (const slug of chosen) {+ if (typeof slug === "string" && known.has(slug)) selected.add(slug);+ }+ } catch {+ // A brace pair in the surrounding prose is not necessarily JSON.+ }+ }+ }+ return foundSkillsList ? [...selected] : null;}/**diff --git a/server/tests/plugin-selection.test.ts b/server/tests/plugin-selection.test.tsindex 776d0a06..67e6b844 100644--- a/server/tests/plugin-selection.test.ts+++ b/server/tests/plugin-selection.test.ts@@ -307,6 +307,47 @@ describe("reading pass one's answer", () => {expect(readChosenSkills(padded, skills)).toEqual(["slack-digest"]);});+ test("an extra JSON object after the selection does not swallow it", async () => {+ const answer = `Here is {the choice}:\n${JSON.stringify({ skills: ["drive-audit"], reason: { text: "read {drive}" } })}\n${JSON.stringify({ note: "done" })}`;+ expect(readChosenSkills(answer, skills)).toEqual(["drive-audit"]);++ const selection = await selectTools({+ tools: manyTools,+ skills,+ text: "read my drive",+ choose: async () => answer,+ });+ expect(selection.reason).toBe("selected");+ expect(selection.offered.map((entry) => entry.ref)).not.toContain(+ "slack/tool_0",+ );+ });++ test("multiple skill selections keep every named skill and its tools", async () => {+ const answer = [+ JSON.stringify({ skills: ["slack-digest"] }),+ JSON.stringify({ skills: ["drive-audit", "slack-digest"] }),+ ].join("\n");+ expect(readChosenSkills(answer, skills)).toEqual([+ "slack-digest",+ "drive-audit",+ ]);++ const selection = await selectTools({+ tools: manyTools,+ skills,+ text: "compare the drive report with the slack thread",+ choose: async () => answer,+ });+ expect(selection.reason).toBe("selected");+ expect(selection.offered.map((entry) => entry.ref)).toContain(+ "drive/tool_0",+ );+ expect(selection.offered.map((entry) => entry.ref)).toContain(+ "slack/tool_0",+ );+ });+test("an answer naming only unknown slugs is an empty choice, not a failure", () => {// The model answered; it just named nothing real. That is "none apply", and the caller offers// everything either way, but the two are different facts and the row says which.