feat(discord): SetSpark record client for the Discord Sage, fixed verbs against setspark-api, connector-verified approvals (#1509)
Row 25, parts 2a and 2b, against the shared-signals contract a5425a2. Model side: eight fixed verbs in the pi extension (record_list, record_get, record_create, record_update, resolve_id, open_approval_request, get_approval_request, create_document), each one HTTP call with arguments checked before any request. Writes carry an idempotency key <principal>:<message id>:<call index> and an audit context. The seat key is read from a 0600 file on every call and never cached, printed or journaled. Connector side: append-only approval ledger, Approve button and exact "approve" reply resolved by the connector against the required approvers, confirmation message posted as button evidence, bind and add_approval through the service under connector keys, retry of unknown entries on start. Evidence: node tests 162 pass, scripts/test-discord.sh 63/63. Review by rev-code-02, round 1 approved (#1509 comment 26467, tree 7872d8c5). Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
// `--no-builtin-tools --extension <this file> --tools <enabled names>` so pi
|
||||
// exposes exactly the tools the binding enables and none of its own: the
|
||||
// three reads always, write_file and edit_file only when a root is marked
|
||||
// write: true, web_fetch and web_search only with a web key
|
||||
// (enabledToolNames in ../src/tools.mjs decides, in one place).
|
||||
// write: true, web_fetch and web_search only with a web key, the SetSpark
|
||||
// record verbs only with a setspark key (enabledToolNames in
|
||||
// ../src/tools.mjs decides, in one place).
|
||||
//
|
||||
// Every decision lives in ../src/tools.mjs; this file only registers the
|
||||
// tools with pi and reads its configuration from the one environment
|
||||
@@ -17,6 +18,10 @@
|
||||
import { Type } from "typebox";
|
||||
import { TOOLS_ENV, TOOL_DESCRIPTIONS, READ_MAX_LINES, loadToolsConfig, createToolSet, enabledToolNames } from "../src/tools.mjs";
|
||||
import { COMMIT_MESSAGE_MAX, COMMIT_PATHS_MAX, VAULT_PREFIXES } from "../src/git.mjs";
|
||||
import { SETSPARK_TOOL_NAMES, RECORD_TYPES, LIST_MAX, FILTERS_MAX, DOCUMENT_MAX_CHARS } from "../src/setspark.mjs";
|
||||
|
||||
const recordType = () => Type.Union(RECORD_TYPES.map((t) => Type.Literal(t)), { description: `Record type: ${RECORD_TYPES.join(", ")}` });
|
||||
const recordId = (what) => Type.String({ description: `${what} id, such as WI-101`, pattern: "^[A-Z]{2,5}-[0-9]{1,8}$" });
|
||||
|
||||
const PARAMS = {
|
||||
list_dir: () => Type.Object({
|
||||
@@ -70,16 +75,65 @@ const PARAMS = {
|
||||
prefix: Type.Union(VAULT_PREFIXES.map((p) => Type.Literal(p)), { description: `Record prefix: ${VAULT_PREFIXES.join(", ")}` }),
|
||||
title: Type.String({ description: "What the record will be about, one line" }),
|
||||
}),
|
||||
record_list: () => Type.Object({
|
||||
record_type: recordType(),
|
||||
filters: Type.Optional(Type.Record(Type.String(), Type.String(), { description: `Exact property values to match, at most ${FILTERS_MAX}, such as {"status": "active"}` })),
|
||||
limit: Type.Optional(Type.Integer({ description: `Records per page, at most ${LIST_MAX}`, minimum: 1, maximum: LIST_MAX })),
|
||||
offset: Type.Optional(Type.Integer({ description: "Records to skip, for the next page", minimum: 0 })),
|
||||
}),
|
||||
record_get: () => Type.Object({
|
||||
id: recordId("Record"),
|
||||
}),
|
||||
record_create: () => Type.Object({
|
||||
record_type: recordType(),
|
||||
record: Type.Record(Type.String(), Type.Any(), { description: "The record's properties; title is required, the service allocates the id" }),
|
||||
}),
|
||||
record_update: () => Type.Object({
|
||||
id: recordId("Record"),
|
||||
revision: Type.Integer({ description: "The revision you read with record_get", minimum: 1 }),
|
||||
fields: Type.Record(Type.String(), Type.Any(), { description: "The properties to change and their new values" }),
|
||||
}),
|
||||
resolve_id: () => Type.Object({
|
||||
query: Type.String({ description: "An exact record id or part of a title" }),
|
||||
}),
|
||||
open_approval_request: () => Type.Object({
|
||||
decision_id: recordId("Decision"),
|
||||
proposal_version: Type.Integer({ description: "The decision's proposal version, from record_get", minimum: 1 }),
|
||||
proposal_digest: Type.String({ description: "The decision's proposal digest, from record_get" }),
|
||||
}),
|
||||
get_approval_request: () => Type.Object({
|
||||
request_id: Type.Integer({ description: "The request id from open_approval_request", minimum: 1 }),
|
||||
}),
|
||||
create_document: () => Type.Object({
|
||||
collection: Type.String({ description: "Outline collection name" }),
|
||||
title: Type.String({ description: "Document title" }),
|
||||
text: Type.Optional(Type.String({ description: `Markdown body, at most ${DOCUMENT_MAX_CHARS} characters` })),
|
||||
source: Type.Optional(Type.String({ description: "Where the content came from, one line" })),
|
||||
}),
|
||||
};
|
||||
|
||||
// The envelope's first line names the requester by the name the binding
|
||||
// gives that Discord user; the connector writes it, the user cannot. A
|
||||
// prompt that does not start with an envelope leaves the requester unset,
|
||||
// and git_commit then refuses.
|
||||
const ENVELOPE_REQUESTER = /^\[discord [^\n]*? requester="([^"\n\]]{1,100})"[^\n]*\]\n/;
|
||||
export function requesterOf(prompt) {
|
||||
const m = typeof prompt === "string" ? prompt.match(ENVELOPE_REQUESTER) : null;
|
||||
// gives that Discord user, the author's id and the message id; the
|
||||
// connector writes it, the user cannot. A prompt that does not start with
|
||||
// an envelope leaves them unset, and git_commit and the SetSpark writes
|
||||
// then refuse.
|
||||
const ENVELOPE = /^\[discord [^\n]*\]\n/;
|
||||
const field = (line, name, re) => {
|
||||
const m = line.match(new RegExp(` ${name}=${re}(?= |\\])`));
|
||||
return m ? m[1] : null;
|
||||
};
|
||||
export function turnOf(prompt) {
|
||||
const m = typeof prompt === "string" ? prompt.match(ENVELOPE) : null;
|
||||
if (!m) return { requester: null, turnId: null, authorId: null };
|
||||
const line = m[0];
|
||||
return {
|
||||
requester: field(line, "requester", '"([^"\\n\\]]{1,100})"'),
|
||||
authorId: field(line, "author", "([0-9]{15,20})"),
|
||||
turnId: field(line, "message", "([0-9]{15,20})"),
|
||||
};
|
||||
}
|
||||
export function requesterOf(prompt) {
|
||||
return turnOf(prompt).requester;
|
||||
}
|
||||
|
||||
export default function (pi) {
|
||||
@@ -97,7 +151,7 @@ export default function (pi) {
|
||||
const gitRoots = config.roots.filter((r) => r.git).map((r) => r.name).join(", ");
|
||||
|
||||
pi.on("before_agent_start", async (event) => {
|
||||
tools.setRequester(requesterOf(event.prompt));
|
||||
tools.setTurn(turnOf(event.prompt));
|
||||
});
|
||||
pi.on("agent_start", async () => {
|
||||
tools.resetBudget();
|
||||
@@ -105,7 +159,7 @@ export default function (pi) {
|
||||
|
||||
for (const name of enabledToolNames(config)) {
|
||||
const d = TOOL_DESCRIPTIONS[name];
|
||||
const suffix = name.startsWith("web_") ? "" : name.startsWith("git_") || name === "reserve_id" ? ` Roots with git: ${gitRoots}.` : ` Declared roots: ${rootNames}.`;
|
||||
const suffix = name.startsWith("web_") || SETSPARK_TOOL_NAMES.includes(name) ? "" : name.startsWith("git_") || name === "reserve_id" ? ` Roots with git: ${gitRoots}.` : ` Declared roots: ${rootNames}.`;
|
||||
pi.registerTool({
|
||||
name,
|
||||
label: d.label,
|
||||
|
||||
Reference in New Issue
Block a user