@synadia-ai/agent-fabric-pi 0.1.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/CHANGELOG.md +17 -0
- package/LICENSE +201 -0
- package/README.md +281 -0
- package/dist/index.cjs +154 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/package.json +87 -0
- package/skill/scratchpad/LICENSE +201 -0
- package/skill/scratchpad/SKILL.md +208 -0
- package/skill/scratchpad/THIRD_PARTY_NOTICES +1067 -0
- package/skill/scratchpad/bin/BUILD.json +367 -0
- package/skill/scratchpad/bin/COMMIT +1 -0
- package/skill/scratchpad/bin/sp-darwin-amd64 +0 -0
- package/skill/scratchpad/bin/sp-darwin-arm64 +0 -0
- package/skill/scratchpad/bin/sp-linux-amd64 +0 -0
- package/skill/scratchpad/bin/sp-linux-arm64 +0 -0
- package/skill/scratchpad/scripts/sp +231 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../node_modules/tsup/assets/cjs_shims.js","../src/scratchpad.ts","../src/extension.ts"],"names":["fileURLToPath","sharedSpBinary","sharedSpPort","resolveScratchPad","resolveEdgeSubject","fabricTracing","loopGuard","ServedPromptEnv","exportScratchPad","ScratchPadHandOn","READS_REFERENCES_KEY","SP_VARS","activeTrace","bindActiveTrace","traceHeaders"],"mappings":";;;;;;;;AAKA,IAAM,gBAAA,GAAmB,MACvB,OAAO,QAAA,KAAa,WAAA,GAChB,IAAI,GAAA,CAAI,CAAA,KAAA,EAAQ,UAAU,CAAA,CAAE,CAAA,CAAE,IAAA,GAC7B,QAAA,CAAS,aAAA,IAAiB,QAAA,CAAS,aAAA,CAAc,OAAA,CAAQ,WAAA,EAAY,KAAM,QAAA,GAC1E,QAAA,CAAS,aAAA,CAAc,GAAA,GACvB,IAAI,GAAA,CAAI,SAAA,EAAW,QAAA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GnC,QAAA,IAAI,YAAA,EAAc,SAAA,CAAU,KAAA,CAAM,OAAA,EAAS,KAAK,CAAA;AAChD,QAAA,OAAO,UAAU,MAAA,GAAY,GAAA,EAAI,GAAIC,2BAAA,CAAgB,OAAO,GAAG,CAAA;AAAA,MACjE,CAAA;AAAA,MACA,gBAAgB,OAAA,EAAS;AAIvB,QAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,GAAA,CAAI,OAAA,CAAQ,EAAE,CAAA;AACnC,QAAA,OAAO,KAAA,KAAU,MAAA,GAAY,MAAA,GAAYA,2BAAA,CAAgB,OAAOC,wBAAY,CAAA;AAAA,MAC9E,CAAA;AAAA,MACA,cAAA,CAAe,OAAA,EAAS,SAAA,EAAW,GAAA,EAAK;AAGtC,QAAA,MAAM,KAAA,GAAQ,QAAQ,OAAO,CAAA;AAC7B,QAAA,OAAO,KAAA,KAAU,MAAA,GACb,GAAA,EAAI,GACJD,2BAAA,CAAgB,KAAA,EAAO,MAAM,KAAA,CAAM,cAAA,CAAe,KAAA,EAAO,GAAG,CAAC,CAAA;AAAA,MACnE;AAAA;AACF,GACF;AACF","file":"index.cjs","sourcesContent":["// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","// 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"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AgentExtensionContext, ScratchPadSettings, AgentExtension } from '@synadia-ai/agent-fabric';
|
|
2
|
+
export { AgentExtension, AgentExtensionContext, AgentExtensionFactory, AgentExtensionHandles, AgentExtensionSettings, EDGE_SUBJECT_OPTION, EDGE_SUBJECT_VAR, PluginHarness as Harness, HarnessEvents, LOOP_GUARD_CODE, LoopGuard, Outcome, SCRATCHPAD_OPTION, SP_VARS, ScratchPadHandOn, ServedRequest, loopGuard, loopGuardDescription, resolveEdgeSubject } from '@synadia-ai/agent-fabric';
|
|
3
|
+
|
|
4
|
+
/** The sp binary this package ships for this machine, or `SCRATCHPAD_BIN`. */
|
|
5
|
+
declare function spBinary(env: Record<string, string | undefined>): string | undefined;
|
|
6
|
+
|
|
7
|
+
/** The extension's name, as the plugin's status output lists it. */
|
|
8
|
+
declare const EXTENSION_NAME = "agent-fabric";
|
|
9
|
+
/** The warning logged once at start when the plugin's identity is off. */
|
|
10
|
+
declare const IDENTITY_OFF_WARNING: string;
|
|
11
|
+
/**
|
|
12
|
+
* The factory: the module's default export. Called once by the plugin,
|
|
13
|
+
* before it connects, with what it knows then. Resolves ScratchPad's
|
|
14
|
+
* settings (which may read the plugin's NATS context) and builds the
|
|
15
|
+
* extension with them. Throws `NatsAgentError` for an edge subject that can
|
|
16
|
+
* never be published to.
|
|
17
|
+
*
|
|
18
|
+
* `env` is where the variables are read from, and where ScratchPad's are
|
|
19
|
+
* set for PI's bash tool; a test passes its own.
|
|
20
|
+
*/
|
|
21
|
+
declare function fabricExtension(ctx: AgentExtensionContext, env?: NodeJS.ProcessEnv): Promise<AgentExtension>;
|
|
22
|
+
/**
|
|
23
|
+
* The extension, built synchronously from settings already resolved:
|
|
24
|
+
* without `scratchPad`, tracing and the guard alone.
|
|
25
|
+
*/
|
|
26
|
+
declare function createFabricExtension(ctx: AgentExtensionContext, env?: NodeJS.ProcessEnv, scratchPad?: ScratchPadSettings): AgentExtension;
|
|
27
|
+
|
|
28
|
+
export { EXTENSION_NAME, IDENTITY_OFF_WARNING, createFabricExtension, fabricExtension as default, fabricExtension, spBinary };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AgentExtensionContext, ScratchPadSettings, AgentExtension } from '@synadia-ai/agent-fabric';
|
|
2
|
+
export { AgentExtension, AgentExtensionContext, AgentExtensionFactory, AgentExtensionHandles, AgentExtensionSettings, EDGE_SUBJECT_OPTION, EDGE_SUBJECT_VAR, PluginHarness as Harness, HarnessEvents, LOOP_GUARD_CODE, LoopGuard, Outcome, SCRATCHPAD_OPTION, SP_VARS, ScratchPadHandOn, ServedRequest, loopGuard, loopGuardDescription, resolveEdgeSubject } from '@synadia-ai/agent-fabric';
|
|
3
|
+
|
|
4
|
+
/** The sp binary this package ships for this machine, or `SCRATCHPAD_BIN`. */
|
|
5
|
+
declare function spBinary(env: Record<string, string | undefined>): string | undefined;
|
|
6
|
+
|
|
7
|
+
/** The extension's name, as the plugin's status output lists it. */
|
|
8
|
+
declare const EXTENSION_NAME = "agent-fabric";
|
|
9
|
+
/** The warning logged once at start when the plugin's identity is off. */
|
|
10
|
+
declare const IDENTITY_OFF_WARNING: string;
|
|
11
|
+
/**
|
|
12
|
+
* The factory: the module's default export. Called once by the plugin,
|
|
13
|
+
* before it connects, with what it knows then. Resolves ScratchPad's
|
|
14
|
+
* settings (which may read the plugin's NATS context) and builds the
|
|
15
|
+
* extension with them. Throws `NatsAgentError` for an edge subject that can
|
|
16
|
+
* never be published to.
|
|
17
|
+
*
|
|
18
|
+
* `env` is where the variables are read from, and where ScratchPad's are
|
|
19
|
+
* set for PI's bash tool; a test passes its own.
|
|
20
|
+
*/
|
|
21
|
+
declare function fabricExtension(ctx: AgentExtensionContext, env?: NodeJS.ProcessEnv): Promise<AgentExtension>;
|
|
22
|
+
/**
|
|
23
|
+
* The extension, built synchronously from settings already resolved:
|
|
24
|
+
* without `scratchPad`, tracing and the guard alone.
|
|
25
|
+
*/
|
|
26
|
+
declare function createFabricExtension(ctx: AgentExtensionContext, env?: NodeJS.ProcessEnv, scratchPad?: ScratchPadSettings): AgentExtension;
|
|
27
|
+
|
|
28
|
+
export { EXTENSION_NAME, IDENTITY_OFF_WARNING, createFabricExtension, fabricExtension as default, fabricExtension, spBinary };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,GAAG,cAAc,CAAA,uJAAA;AAanB,eAAsB,eAAA,CACpB,GAAA,EACA,GAAA,GAAyB,OAAA,CAAQ,G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AAA,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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@synadia-ai/agent-fabric-pi",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Synadia Agent Fabric add-on for the PI channel plugin: the fabric's tracing on PI's service, its client and its own model calls, the loop guard, and ScratchPad's skill with the sp command line, as one extension module the plugin loads.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"skill",
|
|
21
|
+
"README.md",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"build:sp": "sh ../scratchpad-skill/install.sh",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"lint:fix": "eslint . --fix",
|
|
35
|
+
"format": "prettier --write .",
|
|
36
|
+
"format:check": "prettier --check .",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:unit": "vitest run test/unit",
|
|
39
|
+
"test:integration": "vitest run test/integration",
|
|
40
|
+
"test:watch": "vitest",
|
|
41
|
+
"check": "npm run typecheck && npm run lint && npm run format:check && npm run test",
|
|
42
|
+
"prepack": "npm run build:sp",
|
|
43
|
+
"prepublishOnly": "npm run build"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"nats",
|
|
47
|
+
"ai",
|
|
48
|
+
"agents",
|
|
49
|
+
"agent-fabric",
|
|
50
|
+
"tracing",
|
|
51
|
+
"pi",
|
|
52
|
+
"synadia",
|
|
53
|
+
"sdk"
|
|
54
|
+
],
|
|
55
|
+
"license": "Apache-2.0",
|
|
56
|
+
"author": "Synadia Communications",
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/synadia-ai/agent-fabric-sdk.git",
|
|
60
|
+
"directory": "plugins/pi"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://github.com/synadia-ai/agent-fabric-sdk/tree/main/plugins/pi",
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/synadia-ai/agent-fabric-sdk/issues"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
},
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@synadia-ai/agent-fabric": "^0.1.0",
|
|
71
|
+
"@synadia-ai/agent-service": "^0.6.0",
|
|
72
|
+
"@synadia-ai/agents": "^0.6.0"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@nats-io/nats-core": "^3.4.0",
|
|
76
|
+
"@nats-io/transport-node": "^3.4.0",
|
|
77
|
+
"@types/node": "^20.16.0",
|
|
78
|
+
"ajv": "^8.17.0",
|
|
79
|
+
"eslint": "^9.0.0",
|
|
80
|
+
"eslint-config-prettier": "^9.1.0",
|
|
81
|
+
"prettier": "3.9.5",
|
|
82
|
+
"tsup": "^8.3.0",
|
|
83
|
+
"typescript": "~5.6.0",
|
|
84
|
+
"typescript-eslint": "^8.10.0",
|
|
85
|
+
"vitest": "^3.2.0"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a fee
|
|
167
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
168
|
+
obligations and/or rights consistent with this License. However, in
|
|
169
|
+
accepting such obligations, You may act only on Your own behalf and
|
|
170
|
+
on Your sole responsibility, not on behalf of any other Contributor,
|
|
171
|
+
and only if You agree to indemnify, defend, and hold each Contributor
|
|
172
|
+
harmless for any liability incurred by, or claims asserted against,
|
|
173
|
+
such Contributor by reason of your accepting any such warranty or
|
|
174
|
+
additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Synadia Communications
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing permissions
|
|
201
|
+
and limitations under the License.
|