@synadia-ai/agent-fabric-pi 0.1.0 → 0.2.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/dist/index.js DELETED
@@ -1,108 +0,0 @@
1
- import { spBinary as spBinary$1, resolveScratchPad, resolveEdgeSubject, fabricTracing, loopGuard, ServedPromptEnv, exportScratchPad, ScratchPadHandOn, bindActiveTrace, traceHeaders, activeTrace, SP_VARS, READS_REFERENCES_KEY, spPort as spPort$1 } from '@synadia-ai/agent-fabric';
2
- export { EDGE_SUBJECT_OPTION, EDGE_SUBJECT_VAR, LOOP_GUARD_CODE, SCRATCHPAD_OPTION, SP_VARS, ScratchPadHandOn, loopGuard, loopGuardDescription, resolveEdgeSubject } from '@synadia-ai/agent-fabric';
3
- import { fileURLToPath } from 'url';
4
-
5
- // src/extension.ts
6
- var SKILL_DIR = fileURLToPath(new URL("../skill/scratchpad", import.meta.url));
7
- function spBinary(env) {
8
- return spBinary$1(env, SKILL_DIR);
9
- }
10
- function spPort(settings, creator, env) {
11
- return spPort$1(settings, creator, env, SKILL_DIR);
12
- }
13
-
14
- // src/extension.ts
15
- var EXTENSION_NAME = "agent-fabric";
16
- var IDENTITY_OFF_WARNING = `${EXTENSION_NAME}: senderIdentity is off; trace records are signed, so none will be published and each counts as dropped on the heartbeat. Set senderIdentity to signed.`;
17
- async function fabricExtension(ctx, env = process.env) {
18
- return createFabricExtension(ctx, env, await resolveScratchPad(ctx, env));
19
- }
20
- function createFabricExtension(ctx, env = process.env, scratchPad) {
21
- const edgeSubject = resolveEdgeSubject(ctx.options, env);
22
- const tracing = fabricTracing({ edgeSubject });
23
- const guard = loopGuard();
24
- const log = ctx.logger;
25
- const scopes = /* @__PURE__ */ new Map();
26
- const scopeOf = (request) => request === void 0 ? void 0 : scopes.get(request.id);
27
- let warnedIdentityOff = false;
28
- const scratchPadOn = scratchPad !== void 0;
29
- const promptEnv = new ServedPromptEnv(env);
30
- if (scratchPad !== void 0) exportScratchPad(scratchPad, env);
31
- const handOn = scratchPad !== void 0 ? new ScratchPadHandOn() : void 0;
32
- return {
33
- name: EXTENSION_NAME,
34
- // The service: tracing first, so the guard runs inside the scope tracing
35
- // bound and a malformed pair is refused with 400 before the guard
36
- // looks; the plugin appends its own (the tools' scope) after these.
37
- // The ScratchPad extension's, last, remembers the references each
38
- // served prompt brings, which PI may hand on.
39
- requestInterceptors: [
40
- tracing.requestInterceptor,
41
- guard.requestInterceptor,
42
- ...handOn !== void 0 ? [handOn.requestInterceptor] : []
43
- ],
44
- // The client PI's agent tools prompt through: the edge and the pair,
45
- // then the waiting mark. Both read the ambient scope, so the order
46
- // between them does not matter.
47
- promptInterceptors: [tracing.promptInterceptor, guard.promptInterceptor],
48
- heartbeatExtras: tracing.heartbeatExtras,
49
- ...handOn !== void 0 ? {
50
- // References in PI's `prompt_agent`: granted to a receiver that
51
- // reads them, removed with a note for any other.
52
- toolExtensions: [handOn],
53
- // So a caller's `prompt_agent` hands references to PI.
54
- metadata: { [READS_REFERENCES_KEY]: "true" }
55
- } : {},
56
- // No `stopping`: an
57
- // edge record is published before the prompt it describes and counted
58
- // as it goes, so there is nothing to flush when the plugin stops.
59
- started({ agents, service }) {
60
- if (ctx.settings.senderIdentity !== "signed" && !warnedIdentityOff) {
61
- warnedIdentityOff = true;
62
- log.warn(IDENTITY_OFF_WARNING);
63
- }
64
- log.info(
65
- `${EXTENSION_NAME}: tracing ${service.subject.prompt}` + (edgeSubject === null ? " (propagate-only: lineage forwarded, no edge record published)" : `, edge records on ${edgeSubject}`)
66
- );
67
- if (scratchPad !== void 0 && handOn !== void 0) {
68
- env[SP_VARS.creator] = service.subject.prompt;
69
- const port = spPort(scratchPad, service.subject.prompt, env);
70
- if (port !== void 0) handOn.start({ scratchPad: port, agents, service, logger: log });
71
- else
72
- log.warn(
73
- `${EXTENSION_NAME}: no sp for this platform (set SCRATCHPAD_BIN); references in prompt_agent are not handed on, and grant requests not answered`
74
- );
75
- log.info(
76
- `${EXTENSION_NAME}: ScratchPad volume ${scratchPad.volume}` + (scratchPad.dir !== void 0 ? `, workspace ${scratchPad.dir}` : ", no workspace") + ", for the skill's scripts/sp"
77
- );
78
- }
79
- },
80
- events: {
81
- promptAccepted(request) {
82
- const scope = activeTrace();
83
- if (scope !== void 0) scopes.set(request.id, scope);
84
- },
85
- promptEnded(request) {
86
- scopes.delete(request.id);
87
- promptEnv.leave(request);
88
- },
89
- aroundInject(request, run) {
90
- const scope = scopes.get(request.id);
91
- if (scratchPadOn) promptEnv.enter(request, scope);
92
- return scope === void 0 ? run() : bindActiveTrace(scope, run);
93
- },
94
- providerHeaders(request) {
95
- const scope = scopes.get(request.id);
96
- return scope === void 0 ? void 0 : bindActiveTrace(scope, traceHeaders);
97
- },
98
- aroundToolCall(request, _toolName, run) {
99
- const scope = scopeOf(request);
100
- return scope === void 0 ? run() : bindActiveTrace(scope, () => guard.aroundToolCall(scope, run));
101
- }
102
- }
103
- };
104
- }
105
-
106
- export { EXTENSION_NAME, IDENTITY_OFF_WARNING, createFabricExtension, fabricExtension as default, fabricExtension, spBinary };
107
- //# sourceMappingURL=index.js.map
108
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/scratchpad.ts","../src/extension.ts"],"names":["sharedSpBinary","sharedSpPort"],"mappings":";;;;;AA4BO,IAAM,YAAY,aAAA,CAAc,IAAI,IAAI,qBAAA,EAAuB,MAAA,CAAA,IAAA,CAAY,GAAG,CAAC,CAAA;AAG/E,SAAS,SAAS,GAAA,EAA6D;AACpF,EAAA,OAAOA,UAAA,CAAe,KAAK,SAAS,CAAA;AACtC;AAGO,SAAS,MAAA,CACd,QAAA,EACA,OAAA,EACA,GAAA,EACwB;AACxB,EAAA,OAAOC,QAAA,CAAa,QAAA,EAAU,OAAA,EAAS,GAAA,EAAK,SAAS,CAAA;AACvD;;;ACcO,IAAM,cAAA,GAAiB;AAGvB,IAAM,oBAAA,GACX,G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AK;AAGtC,QAAA,MAAM,KAAA,GAAQ,QAAQ,OAAO,CAAA;AAC7B,QAAA,OAAO,KAAA,KAAU,MAAA,GACb,GAAA,EAAI,GACJ,eAAA,CAAgB,KAAA,EAAO,MAAM,KAAA,CAAM,cAAA,CAAe,KAAA,EAAO,GAAG,CAAC,CAAA;AAAA,MACnE;AAAA;AACF,GACF;AACF","file":"index.js","sourcesContent":["// ScratchPad for PI's model (`docs/plugins.md` §9): the pieces every add-on\n// shares, from the fabric package (`plugin-scratchpad.ts` there), with this\n// package's copy of the skill (`skill/scratchpad`, installed by\n// `npm run build:sp` from `plugins/scratchpad-skill`). PI's bash tool passes\n// PI's process environment to every command it runs (measured on PI 0.85.1,\n// §9.4), and the plugin hands PI one prompt at a time, so the per-prompt\n// variables are sound; a command PI runs for local typing during a remote\n// turn gets the served prompt's (§7, \"PI's turn boundaries\").\n\nimport { fileURLToPath } from \"node:url\";\nimport {\n spBinary as sharedSpBinary,\n spPort as sharedSpPort,\n type ScratchPad,\n type ScratchPadSettings,\n} from \"@synadia-ai/agent-fabric\";\n\nexport {\n exportScratchPad,\n resolveScratchPad,\n SCRATCHPAD_OPTION,\n ScratchPadHandOn,\n ServedPromptEnv,\n SP_VARS,\n type ScratchPadSettings,\n} from \"@synadia-ai/agent-fabric\";\n\n/** This package's skill directory: `src/` in the tests, `dist/` when installed, one level below it. */\nexport const SKILL_DIR = fileURLToPath(new URL(\"../skill/scratchpad\", import.meta.url));\n\n/** The sp binary this package ships for this machine, or `SCRATCHPAD_BIN`. */\nexport function spBinary(env: Record<string, string | undefined>): string | undefined {\n return sharedSpBinary(env, SKILL_DIR);\n}\n\n/** The ScratchPad port over this package's sp, for the extension's grants. */\nexport function spPort(\n settings: ScratchPadSettings,\n creator: string,\n env: Record<string, string | undefined>,\n): ScratchPad | undefined {\n return sharedSpPort(settings, creator, env, SKILL_DIR);\n}\n","// The fabric add-on for the PI channel plugin: the factory the plugin calls\n// once at start, and the extension it returns (`docs/plugins.md` §2.3, §2.4,\n// §3.1, §3.2).\n//\n// What it does, in one screen. The fabric package's `fabricTracing()` gives\n// the three hooks the SDK takes; the plugin puts the request interceptor and\n// the heartbeat extras on its `AgentService` and the prompt interceptor on\n// the `Agents` client its agent tools prompt through. That alone makes PI\n// what an SDK-built agent is on the wire: the envelope's pair adopted or a\n// root minted per served prompt, an `edge` record before every prompt PI's\n// model sends, the record counts on the heartbeat. The harness piece is the\n// scope: PI's loop does not run in the handler's async context, so the\n// add-on keeps each served request's scope by the plugin's request id and\n// binds it where the plugin says the request is being worked on —\n// `aroundInject` around the hand-over to PI's loop, `aroundToolCall` around\n// every agent-tool call, and `providerHeaders`, which returns the fabric's\n// two headers for the request so the proxy files PI's own model calls on\n// the served thread. The `409` loop guard is its own two interceptors,\n// after tracing's, and its `aroundToolCall` inside the add-on's, which lifts\n// the mark once the request's calls have settled (`loopGuard`, from the\n// fabric package).\n//\n// ScratchPad (§9): when a volume is configured, the add-on sets the\n// environment the package's skill (`skill/scratchpad`, given to PI with\n// `--skill`) reads through `scripts/sp`: the connection, the workspace and\n// the volume before the plugin connects, the served prompt's trace and\n// caller from its hand-over to its end. It registers PI as reading\n// references, hands the references PI's model puts into `prompt_agent`\n// to the fabric package's ScratchPad extension (`scratchpad.ts`), and\n// answers grant requests for what PI published, by an SDK-built agent's\n// rule.\n\nimport {\n activeTrace,\n bindActiveTrace,\n fabricTracing,\n loopGuard,\n resolveEdgeSubject,\n traceHeaders,\n type AgentExtension,\n type AgentExtensionContext,\n type ServedRequest,\n type TraceScope,\n READS_REFERENCES_KEY,\n} from \"@synadia-ai/agent-fabric\";\nimport {\n exportScratchPad,\n resolveScratchPad,\n ScratchPadHandOn,\n ServedPromptEnv,\n SP_VARS,\n spPort,\n type ScratchPadSettings,\n} from \"./scratchpad.js\";\n\n/** The extension's name, as the plugin's status output lists it. */\nexport const EXTENSION_NAME = \"agent-fabric\";\n\n/** The warning logged once at start when the plugin's identity is off. */\nexport const IDENTITY_OFF_WARNING =\n `${EXTENSION_NAME}: senderIdentity is off; trace records are signed, so none will be ` +\n \"published and each counts as dropped on the heartbeat. Set senderIdentity to signed.\";\n\n/**\n * The factory: the module's default export. Called once by the plugin,\n * before it connects, with what it knows then. Resolves ScratchPad's\n * settings (which may read the plugin's NATS context) and builds the\n * extension with them. Throws `NatsAgentError` for an edge subject that can\n * never be published to.\n *\n * `env` is where the variables are read from, and where ScratchPad's are\n * set for PI's bash tool; a test passes its own.\n */\nexport async function fabricExtension(\n ctx: AgentExtensionContext,\n env: NodeJS.ProcessEnv = process.env,\n): Promise<AgentExtension> {\n return createFabricExtension(ctx, env, await resolveScratchPad(ctx, env));\n}\n\n/**\n * The extension, built synchronously from settings already resolved:\n * without `scratchPad`, tracing and the guard alone.\n */\nexport function createFabricExtension(\n ctx: AgentExtensionContext,\n env: NodeJS.ProcessEnv = process.env,\n scratchPad?: ScratchPadSettings,\n): AgentExtension {\n const edgeSubject = resolveEdgeSubject(ctx.options, env);\n const tracing = fabricTracing({ edgeSubject });\n const guard = loopGuard();\n const log = ctx.logger;\n\n // Each served request's scope, the one tracing bound around its handler,\n // by the plugin's request id: kept from `promptAccepted`, which the plugin\n // calls inside the handler, to `promptEnded`. `bindActiveTrace` runs its\n // function inside an `AsyncLocalStorage.run`, which *replaces* the\n // ambient context — as it must: PI's turn, its provider requests and the\n // tools it executes all run in the async context of whoever called\n // `pi.sendUserMessage()`, and when the plugin injects the next queued\n // prompt from the previous turn's settle hook that context is the\n // previous request's (measured on PI 0.85.1, `docs/plugins.md` §3.5 gap 3).\n const scopes = new Map<string, TraceScope>();\n const scopeOf = (request: ServedRequest | undefined): TraceScope | undefined =>\n request === undefined ? undefined : scopes.get(request.id);\n\n let warnedIdentityOff = false;\n const scratchPadOn = scratchPad !== undefined;\n const promptEnv = new ServedPromptEnv(env);\n // Set before the plugin connects, so a command PI runs at any time finds them.\n if (scratchPad !== undefined) exportScratchPad(scratchPad, env);\n const handOn = scratchPad !== undefined ? new ScratchPadHandOn() : undefined;\n\n return {\n name: EXTENSION_NAME,\n // The service: tracing first, so the guard runs inside the scope tracing\n // bound and a malformed pair is refused with 400 before the guard\n // looks; the plugin appends its own (the tools' scope) after these.\n // The ScratchPad extension's, last, remembers the references each\n // served prompt brings, which PI may hand on.\n requestInterceptors: [\n tracing.requestInterceptor,\n guard.requestInterceptor,\n ...(handOn !== undefined ? [handOn.requestInterceptor] : []),\n ],\n // The client PI's agent tools prompt through: the edge and the pair,\n // then the waiting mark. Both read the ambient scope, so the order\n // between them does not matter.\n promptInterceptors: [tracing.promptInterceptor, guard.promptInterceptor],\n heartbeatExtras: tracing.heartbeatExtras,\n ...(handOn !== undefined\n ? {\n // References in PI's `prompt_agent`: granted to a receiver that\n // reads them, removed with a note for any other.\n toolExtensions: [handOn],\n // So a caller's `prompt_agent` hands references to PI.\n metadata: { [READS_REFERENCES_KEY]: \"true\" },\n }\n : {}),\n // No `stopping`: an\n // edge record is published before the prompt it describes and counted\n // as it goes, so there is nothing to flush when the plugin stops.\n\n started({ agents, service }) {\n if (ctx.settings.senderIdentity !== \"signed\" && !warnedIdentityOff) {\n warnedIdentityOff = true;\n log.warn(IDENTITY_OFF_WARNING);\n }\n log.info(\n `${EXTENSION_NAME}: tracing ${service.subject.prompt}` +\n (edgeSubject === null\n ? \" (propagate-only: lineage forwarded, no edge record published)\"\n : `, edge records on ${edgeSubject}`),\n );\n if (scratchPad !== undefined && handOn !== undefined) {\n env[SP_VARS.creator] = service.subject.prompt;\n const port = spPort(scratchPad, service.subject.prompt, env);\n if (port !== undefined) handOn.start({ scratchPad: port, agents, service, logger: log });\n else\n log.warn(\n `${EXTENSION_NAME}: no sp for this platform (set SCRATCHPAD_BIN); ` +\n \"references in prompt_agent are not handed on, and grant requests not answered\",\n );\n log.info(\n `${EXTENSION_NAME}: ScratchPad volume ${scratchPad.volume}` +\n (scratchPad.dir !== undefined ? `, workspace ${scratchPad.dir}` : \", no workspace\") +\n \", for the skill's scripts/sp\",\n );\n }\n },\n\n events: {\n promptAccepted(request) {\n // In the handler's async context, inside the scope tracing bound.\n const scope = activeTrace();\n if (scope !== undefined) scopes.set(request.id, scope);\n },\n promptEnded(request) {\n scopes.delete(request.id);\n promptEnv.leave(request);\n },\n aroundInject(request, run) {\n const scope = scopes.get(request.id);\n // The hand-over to PI's loop: from here to its end this prompt is\n // the one PI works on, and the one `scripts/sp` works for.\n if (scratchPadOn) promptEnv.enter(request, scope);\n return scope === undefined ? run() : bindActiveTrace(scope, run);\n },\n providerHeaders(request) {\n // One call per provider request: `traceHeaders()` counts each\n // against the scope, so an edge PI's model spawns later says how\n // many calls came before it — retries included.\n const scope = scopes.get(request.id);\n return scope === undefined ? undefined : bindActiveTrace(scope, traceHeaders);\n },\n aroundToolCall(request, _toolName, run) {\n // The same binding as the injection's, for the case PI's tool\n // runner does not inherit it; a no-op rebind when it does.\n const scope = scopeOf(request);\n return scope === undefined\n ? run()\n : bindActiveTrace(scope, () => guard.aroundToolCall(scope, run));\n },\n },\n };\n}\n"]}