@asterism-app/builtin-api 0.26.2 → 0.28.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/docs/agents.md +8 -0
- package/examples/agent-input.ts +8 -4
- package/package.json +1 -1
- package/src/agent-services.ts +3 -1
- package/src/agent.ts +1 -1
- package/src/input.ts +34 -1
package/docs/agents.md
CHANGED
|
@@ -14,6 +14,8 @@ Sender attribution explains where input came from independently of the Agent tha
|
|
|
14
14
|
|
|
15
15
|
Keep the body useful as ordinary readable text. For example, a pull-request notification can state the repository, change, author, and URL. The integration understands those facts and should explain them; the receiving Agent's instructions supply its responsibilities. A domain event does not need a private machine payload disguised as a message.
|
|
16
16
|
|
|
17
|
+
A resource event can separate the two audiences. Leading YAML frontmatter carries metadata of any shape for the receiving Agent, such as the event kind and the identifiers its tools accept; the Markdown after it is the message people read in the conversation, which hides the frontmatter. Put each fact where its reader needs it rather than repeating identifiers in prose or squeezing the readable summary into metadata. A body may be empty when the following parts already form the message, as when a chat event is followed by the received messages themselves.
|
|
18
|
+
|
|
17
19
|
Use retained attachments when the message must preserve submitted bytes. Use file references when the message should carry an address for later consumption. Forwarding an address does not mean its content has been read or captured.
|
|
18
20
|
|
|
19
21
|
## Choose admission and scheduling intentionally
|
|
@@ -32,6 +34,12 @@ Creating an Agent establishes an object; submitting input asks it to work. An in
|
|
|
32
34
|
|
|
33
35
|
Put required initialization in creation or prepared initial work. Observe creation facts for reactions after commitment, not as a blocking initialization hook. Resource presence is a lifetime concern: reconstruction can make an existing Agent present again without creating a new Agent.
|
|
34
36
|
|
|
37
|
+
## Treat the default as a creation preference
|
|
38
|
+
|
|
39
|
+
An integration that offers a new conversation can follow the user's default Agent configuration instead of hard-coding a bundled preset. Read that preference when choosing the initial configuration; keep an explicit user choice and the configuration of an existing Agent independent of later default changes.
|
|
40
|
+
|
|
41
|
+
The preference is only a configuration ID, not a copy of its instructions or a resolved model. It remains explicit if its target is disabled, deleted, or renamed. An integration should present that unavailable choice or expose the creation failure rather than silently substituting another role.
|
|
42
|
+
|
|
35
43
|
## Route through explicit relationships
|
|
36
44
|
|
|
37
45
|
The publishing integration chooses recipients. A bridge can use its direct links to find Agents without obtaining their private state or inheriting their access to other resources. The reverse link is a separate relationship.
|
package/examples/agent-input.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { nanoid } from "nanoid";
|
|
2
|
-
import { SubmitAgentInput, CancelAgent, type AgentMessageSender, type SubmitAgentInputData, type AgentInputPart } from "@asterism-app/builtin-api/contracts";
|
|
2
|
+
import { SubmitAgentInput, CancelAgent, resourceEventText, type AgentMessageSender, type SubmitAgentInputData, type AgentInputPart } from "@asterism-app/builtin-api/contracts";
|
|
3
|
+
import type { JsonObject } from "@asterism-app/sdk/contracts";
|
|
3
4
|
import { PluginApiError, type ObjectReader, type PluginContext, type ResourceRef, type ResourceActionOptions } from "@asterism-app/sdk/runtime";
|
|
4
5
|
|
|
5
6
|
/** Construct once and retain the complete input for any retry. Delivery mode is independent of source. */
|
|
@@ -13,12 +14,15 @@ export function agentMessage(sender: Extract<AgentMessageSender, { kind: "agent"
|
|
|
13
14
|
return { submissionId: nanoid(16), mode, parts, sender };
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
/**
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Resource callbacks format facts as readable Markdown, add identifiers as frontmatter metadata, and capture the source name.
|
|
19
|
+
* For example: resourceEvent(source, { event: "pull_request.opened", number: 128 }, "Ada opened **Fix login** in web", "follow").
|
|
20
|
+
*/
|
|
21
|
+
export function resourceEvent(source: ObjectReader, metadata: JsonObject, body: string, mode: SubmitAgentInputData["mode"]): SubmitAgentInputData {
|
|
18
22
|
const resource = source.snapshot();
|
|
19
23
|
if (!resource) throw new PluginApiError("not_found", "The event source is unavailable.");
|
|
20
24
|
return {
|
|
21
|
-
submissionId: nanoid(16), mode, parts: [{ kind: "text", text }],
|
|
25
|
+
submissionId: nanoid(16), mode, parts: [{ kind: "text", text: resourceEventText(metadata, body) }],
|
|
22
26
|
sender: { kind: "event", resource: { ...source.ref }, name: resource.title },
|
|
23
27
|
};
|
|
24
28
|
}
|
package/package.json
CHANGED
package/src/agent-services.ts
CHANGED
|
@@ -2,8 +2,10 @@ import type { ResourceRef, SessionDirectory } from "@asterism-app/sdk/contracts"
|
|
|
2
2
|
import { PluginApiError, type PluginContext } from "@asterism-app/sdk/runtime";
|
|
3
3
|
import type { PeekClient } from "./peek.ts";
|
|
4
4
|
|
|
5
|
-
/** Agent
|
|
5
|
+
/** Agent defaults and resource operations for trusted plugins; resource operations take authorized references. */
|
|
6
6
|
export interface AgentServices {
|
|
7
|
+
/** Current default configuration ID for new conversations, read on access. The configuration may be disabled or missing; this does not select a model or change existing Agents. */
|
|
8
|
+
readonly defaultAgentId: string;
|
|
7
9
|
describe(target: ResourceRef): { type: string };
|
|
8
10
|
peek(target: ResourceRef): PeekClient;
|
|
9
11
|
}
|
package/src/agent.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { agentMessageSenderSchema } from "./messaging.ts";
|
|
|
7
7
|
|
|
8
8
|
/** Agents are ordinary resources: creation events record only identity; mutable data such as model selection belongs to private resource state. */
|
|
9
9
|
export const Agent = builtin.resource("agent", z.object({}));
|
|
10
|
-
/**
|
|
10
|
+
/** Factory default and baseline for custom Agent configurations; the user-selected default is exposed by AgentServices.defaultAgentId. */
|
|
11
11
|
export const defaultAgentPreset = "worker";
|
|
12
12
|
/** Pending input is delivered at request or turn boundaries; execution starts immediately when idle. */
|
|
13
13
|
export const agentInputModeSchema = z.enum(["steer", "follow"]);
|
package/src/input.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { modelInputPartSchema, fileReferencePartSchema } from "@asterism-app/sdk/contracts";
|
|
2
|
+
import { modelInputPartSchema, fileReferencePartSchema, type Json, type JsonObject } from "@asterism-app/sdk/contracts";
|
|
3
3
|
|
|
4
4
|
export const agentInputPartSchema = z.union([modelInputPartSchema, fileReferencePartSchema]);
|
|
5
5
|
export type AgentInputPart = z.infer<typeof agentInputPartSchema>;
|
|
@@ -10,6 +10,39 @@ export function inputFileName(part: Exclude<AgentInputPart, { kind: "text" }>):
|
|
|
10
10
|
export const agentInputPartsSchema = z.array(agentInputPartSchema).min(1).max(128)
|
|
11
11
|
.refine(parts => parts.some(part => part.kind !== "text" || part.text.trim().length > 0), "A message requires text or a file.")
|
|
12
12
|
.refine(parts => parts.flatMap(part => part.kind === "text" ? [part.text] : []).join("\n").length <= 100_000, "The message text exceeds the size limit.");
|
|
13
|
+
/** Strings that YAML reads back as the same string without quotes; anything else is written as JSON, which YAML also accepts. */
|
|
14
|
+
const plainScalar = /^[A-Za-z_][\w./@-]*$/u;
|
|
15
|
+
const reservedScalar = /^(?:true|false|null|yes|no|on|off|y|n)$/iu;
|
|
16
|
+
const frontmatterKey = /^[A-Za-z_][\w.-]*$/u;
|
|
17
|
+
function scalar(value: Json): string {
|
|
18
|
+
return typeof value === "string" && plainScalar.test(value) && !reservedScalar.test(value) ? value : JSON.stringify(value);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Resource event text: YAML frontmatter with structured metadata for the receiving Agent, then the Markdown body people read.
|
|
22
|
+
* Each metadata value occupies one line, so no metadata line can close the block. Omitted metadata produces the body alone.
|
|
23
|
+
*/
|
|
24
|
+
export function resourceEventText(metadata: JsonObject, body: string): string {
|
|
25
|
+
const entries = Object.entries(metadata).filter(([, value]) => value !== undefined);
|
|
26
|
+
if (!entries.length) return body;
|
|
27
|
+
for (const [key] of entries) if (!frontmatterKey.test(key)) throw new Error(`Invalid event metadata key: ${key}`);
|
|
28
|
+
return ["---", ...entries.map(([key, value]) => `${key}: ${scalar(value)}`), "---", "", body].join("\n");
|
|
29
|
+
}
|
|
30
|
+
/** Separates leading frontmatter from the body; text without a complete leading block is all body. */
|
|
31
|
+
export function splitResourceEventText(text: string): { metadata: string | null; body: string } {
|
|
32
|
+
if (!text.startsWith("---\n")) return { metadata: null, body: text };
|
|
33
|
+
const end = text.indexOf("\n---", 3);
|
|
34
|
+
if (end < 0 || (text.length > end + 4 && text[end + 4] !== "\n")) return { metadata: null, body: text };
|
|
35
|
+
return { metadata: text.slice(4, end), body: text.slice(end + 5).replace(/^\n/u, "") };
|
|
36
|
+
}
|
|
37
|
+
/** Parts shown to people: an event's leading frontmatter is model-facing metadata and is omitted from display. */
|
|
38
|
+
export function displayInputParts(message: { sender: { kind: string }; parts: readonly AgentInputPart[] }): readonly AgentInputPart[] {
|
|
39
|
+
if (message.sender.kind !== "event") return message.parts;
|
|
40
|
+
return message.parts.flatMap((part, index) => {
|
|
41
|
+
if (index !== 0 || part.kind !== "text") return [part];
|
|
42
|
+
const { body } = splitResourceEventText(part.text);
|
|
43
|
+
return body.trim() ? [{ ...part, text: body }] : [];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
13
46
|
export function inputSummary(parts: readonly AgentInputPart[]): string {
|
|
14
47
|
return parts.flatMap(part => part.kind === "text" ? [part.text] : []).join("\n").trim()
|
|
15
48
|
|| parts.flatMap(part => part.kind === "text" ? [] : [inputFileName(part)]).join(", ");
|