@sage-protocol/pi-adapter 0.4.3 → 0.5.1

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.
@@ -1,171 +0,0 @@
1
- /**
2
- * @file behavior/resolver.ts
3
- *
4
- * Responsibility: Resolve behavior keys into BehaviorPlans via Sage MCP.
5
- *
6
- * Invariants:
7
- * - Resolution always goes through the MCP bridge (never direct file reads).
8
- * - Returns a validated BehaviorPlan or throws with a clear error.
9
- * - The resolver is stateless — no caching (Sage MCP handles that).
10
- */
11
- /**
12
- * Resolve a behavior key into a BehaviorPlan via Sage MCP.
13
- *
14
- * Calls `get_prompt(key, expand: ["behaviorPlan", "behaviorSkillBodies"])`
15
- * through the MCP bridge and extracts the plan from the response.
16
- */
17
- export async function resolveBehaviorPlan(bridge, key) {
18
- if (!bridge.isReady()) {
19
- throw new Error(`Cannot resolve behavior '${key}': Sage MCP bridge is not connected`);
20
- }
21
- // Call Sage MCP to get the behavior with plan expansion.
22
- // The expand modes progressively resolve: parse → resolve → plan → bodies.
23
- const result = await bridge.callCodeModeExecute({
24
- domain: "prompts",
25
- action: "get",
26
- params: {
27
- key,
28
- expand: ["behaviorPlan", "behaviorSkillBodies"],
29
- },
30
- });
31
- return extractPlanFromResult(result, key);
32
- }
33
- /**
34
- * Extract and validate a BehaviorPlan from an MCP result.
35
- *
36
- * The MCP response may contain the plan in several locations depending
37
- * on the Sage MCP version:
38
- * - result.content[].text (JSON string)
39
- * - result.extra.behaviorPlan (object)
40
- * - result.plan (object)
41
- */
42
- function extractPlanFromResult(result, key) {
43
- if (!result || typeof result !== "object") {
44
- throw new Error(`Empty response when resolving behavior '${key}'`);
45
- }
46
- const obj = result;
47
- // Try structured plan field first (multiple nesting levels)
48
- const promptEnvelope = obj.prompt;
49
- const planCandidate = obj.plan ??
50
- obj.behaviorPlan ??
51
- obj.extra?.behaviorPlan ??
52
- promptEnvelope?.behaviorPlan;
53
- if (planCandidate && typeof planCandidate === "object") {
54
- return validatePlan(planCandidate, key);
55
- }
56
- // Try parsing from MCP content text blocks
57
- if (Array.isArray(obj.content)) {
58
- for (const block of obj.content) {
59
- if (!block || typeof block !== "object")
60
- continue;
61
- const text = block.text;
62
- if (typeof text !== "string")
63
- continue;
64
- // Try JSON parse — the plan may be embedded as JSON
65
- try {
66
- const parsed = JSON.parse(text);
67
- if (parsed &&
68
- typeof parsed === "object" &&
69
- Array.isArray(parsed.steps)) {
70
- return validatePlan(parsed, key);
71
- }
72
- // Check nested
73
- if (parsed.plan &&
74
- typeof parsed.plan === "object" &&
75
- Array.isArray(parsed.plan.steps)) {
76
- return validatePlan(parsed.plan, key);
77
- }
78
- if (parsed.behaviorPlan && typeof parsed.behaviorPlan === "object") {
79
- return validatePlan(parsed.behaviorPlan, key);
80
- }
81
- // Check inside prompt envelope — get_prompt returns { prompt: { behaviorPlan: ... } }
82
- if (parsed.prompt &&
83
- typeof parsed.prompt === "object" &&
84
- parsed.prompt.behaviorPlan &&
85
- typeof parsed.prompt.behaviorPlan === "object") {
86
- return validatePlan(parsed.prompt.behaviorPlan, key);
87
- }
88
- }
89
- catch {
90
- // Not JSON — may be the rendered markdown controller prompt.
91
- // Extract plan structure from markdown if possible.
92
- const markdownPlan = parseMarkdownPlan(text, key);
93
- if (markdownPlan)
94
- return markdownPlan;
95
- }
96
- }
97
- }
98
- throw new Error(`Could not extract behavior plan from response for '${key}'. ` +
99
- `The behavior may not exist or may not have the 'behavior' field in its prompt extra.`);
100
- }
101
- /** Validate a raw object as a BehaviorPlan. */
102
- function validatePlan(raw, key) {
103
- const steps = raw.steps;
104
- if (!Array.isArray(steps) || steps.length === 0) {
105
- throw new Error(`Behavior '${key}' resolved but has no steps`);
106
- }
107
- const validSteps = steps.map((step, i) => {
108
- if (!step || typeof step !== "object") {
109
- throw new Error(`Step ${i} in behavior '${key}' is not an object`);
110
- }
111
- const s = step;
112
- return {
113
- id: typeof s.id === "string" ? s.id : `step-${i + 1}`,
114
- title: typeof s.title === "string" ? s.title : `Step ${i + 1}`,
115
- skill_key: typeof s.skill_key === "string" ? s.skill_key : "",
116
- instruction: typeof s.instruction === "string" ? s.instruction : "",
117
- uses: Array.isArray(s.uses) ? s.uses : [],
118
- produces: Array.isArray(s.produces)
119
- ? s.produces
120
- : [`output-${i + 1}`],
121
- };
122
- });
123
- return {
124
- version: typeof raw.version === "string" ? raw.version : "1",
125
- inputs: Array.isArray(raw.inputs)
126
- ? raw.inputs.map((inp) => ({
127
- name: String(inp.name ?? ""),
128
- description: typeof inp.description === "string" ? inp.description : undefined,
129
- required: Boolean(inp.required),
130
- }))
131
- : [{ name: "task", required: true, description: "The task to perform" }],
132
- steps: validSteps,
133
- };
134
- }
135
- /**
136
- * Best-effort extraction of plan steps from Sage's rendered markdown controller prompt.
137
- *
138
- * When the MCP response is the rendered markdown (not raw JSON), parse step
139
- * headers like "### Step 1: Skill Name" to reconstruct the plan structure.
140
- */
141
- function parseMarkdownPlan(markdown, _key) {
142
- const stepPattern = /^###?\s+Step\s+(\d+):\s+(.+)$/gm;
143
- const steps = [];
144
- let match;
145
- while ((match = stepPattern.exec(markdown)) !== null) {
146
- const stepNum = parseInt(match[1], 10);
147
- const title = match[2].trim();
148
- // Extract skill key from the line after "**Skill**: `key`"
149
- const afterHeader = markdown.slice(match.index + match[0].length, match.index + match[0].length + 500);
150
- const skillMatch = afterHeader.match(/\*\*Skill\*\*:\s*`([^`]+)`/);
151
- const instructionMatch = afterHeader.match(/\*\*Instruction\*\*:\s*(.+?)(?:\n\*\*|\n###|$)/s);
152
- steps.push({
153
- id: `step-${stepNum}`,
154
- title,
155
- skill_key: skillMatch?.[1] ?? title.toLowerCase().replace(/\s+/g, "-"),
156
- instruction: instructionMatch?.[1]?.trim() ?? `Apply the ${title} skill`,
157
- uses: stepNum > 1 ? [`step-${stepNum - 1}`] : [],
158
- produces: [`output-${stepNum}`],
159
- });
160
- }
161
- if (steps.length === 0)
162
- return null;
163
- return {
164
- version: "1",
165
- inputs: [
166
- { name: "task", required: true, description: "The task to perform" },
167
- ],
168
- steps,
169
- };
170
- }
171
- //# sourceMappingURL=resolver.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../../src/behavior/resolver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACxC,MAAqB,EACrB,GAAW;IAEX,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACd,4BAA4B,GAAG,qCAAqC,CACpE,CAAC;IACH,CAAC;IAED,yDAAyD;IACzD,2EAA2E;IAC3E,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,mBAAmB,CAAC;QAC/C,MAAM,EAAE,SAAS;QACjB,MAAM,EAAE,KAAK;QACb,MAAM,EAAE;YACP,GAAG;YACH,MAAM,EAAE,CAAC,cAAc,EAAE,qBAAqB,CAAC;SAC/C;KACD,CAAC,CAAC;IAEH,OAAO,qBAAqB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,qBAAqB,CAAC,MAAe,EAAE,GAAW;IAC1D,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,2CAA2C,GAAG,GAAG,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,GAAG,GAAG,MAAiC,CAAC;IAE9C,4DAA4D;IAC5D,MAAM,cAAc,GAAG,GAAG,CAAC,MAA6C,CAAC;IACzE,MAAM,aAAa,GAClB,GAAG,CAAC,IAAI;QACR,GAAG,CAAC,YAAY;QACf,GAAG,CAAC,KAAiC,EAAE,YAAY;QACpD,cAAc,EAAE,YAAY,CAAC;IAC9B,IAAI,aAAa,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE,CAAC;QACxD,OAAO,YAAY,CAAC,aAA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AAC,CAAC,CAAC,EAAE;YAChD,QAAQ,EAAE,CAAC,UAAU,OAAO,EAAE,CAAC;SAC/B,CAAC,CAAC;IACJ,CAAC;IAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAEpC,OAAO;QACN,OAAO,EAAE,GAAG;QACZ,MAAM,EAAE;YACP,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAE,qBAAqB,EAAE;SACpE;QACD,KAAK;KACL,CAAC;AACH,CAAC"}
@@ -1,16 +0,0 @@
1
- /**
2
- * @file behavior/types.ts
3
- *
4
- * Responsibility: Define the canonical types for Sage behavior plans and the
5
- * BehaviorExecutor plugin interface that bridges Sage's behavior engine to
6
- * any AI harness's subagent system.
7
- *
8
- * Invariants:
9
- * - Types mirror the Sage core Rust structs (crates/core/src/library/behavior.rs).
10
- * - The BehaviorExecutor interface is the ONLY contract between the adapter core
11
- * and harness-specific executors — core never imports from executor implementations.
12
- * - All step IDs are unique within a plan.
13
- * - Steps form a linear dependency chain (step-N uses output of step-N-1).
14
- */
15
- export {};
16
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/behavior/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,4CAA4C;AAC5C,cAAc,sBAAsB,CAAC;AAErC,yCAAyC;AACzC,cAAc,qBAAqB,CAAC"}