mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 15:01:24 +00:00
feat(coding-agent): add ctx.getSystemPrompt() to extension context
Adds a method to access the effective system prompt (after any per-turn extension modifications) from the extension context. Implementation: - Add systemPrompt getter to AgentSession reading from agent.state.systemPrompt - Wire getSystemPrompt through ExtensionContextActions to ExtensionRunner - Add getSystemPrompt to interactive-mode's shortcut context - Update docs with ctx.getSystemPrompt() section - Add system-prompt-header.ts example - Add example to docs reference table Closes #1098
This commit is contained in:
parent
abd0c47b03
commit
99281e5913
7 changed files with 46 additions and 0 deletions
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added `ctx.getSystemPrompt()` to extension context for accessing the current effective system prompt ([#1098](https://github.com/badlogic/pi-mono/pull/1098) by [@kaofelix](https://github.com/kaofelix))
|
||||||
|
|
||||||
## [0.50.5] - 2026-01-30
|
## [0.50.5] - 2026-01-30
|
||||||
|
|
||||||
## [0.50.4] - 2026-01-30
|
## [0.50.4] - 2026-01-30
|
||||||
|
|
|
||||||
|
|
@ -654,6 +654,17 @@ ctx.compact({
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### ctx.getSystemPrompt()
|
||||||
|
|
||||||
|
Returns the current effective system prompt. This includes any modifications made by `before_agent_start` handlers for the current turn.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
pi.on("before_agent_start", (event, ctx) => {
|
||||||
|
const prompt = ctx.getSystemPrompt();
|
||||||
|
console.log(`System prompt length: ${prompt.length}`);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
## ExtensionCommandContext
|
## ExtensionCommandContext
|
||||||
|
|
||||||
Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.
|
Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.
|
||||||
|
|
@ -1688,6 +1699,7 @@ All examples in [examples/extensions/](../examples/extensions/).
|
||||||
| `dirty-repo-guard.ts` | Warn on dirty git repo | `on("session_before_*")`, `exec` |
|
| `dirty-repo-guard.ts` | Warn on dirty git repo | `on("session_before_*")`, `exec` |
|
||||||
| `input-transform.ts` | Transform user input | `on("input")` |
|
| `input-transform.ts` | Transform user input | `on("input")` |
|
||||||
| `model-status.ts` | React to model changes | `on("model_select")`, `setStatus` |
|
| `model-status.ts` | React to model changes | `on("model_select")`, `setStatus` |
|
||||||
|
| `system-prompt-header.ts` | Display system prompt info | `on("agent_start")`, `getSystemPrompt` |
|
||||||
| `claude-rules.ts` | Load rules from files | `on("session_start")`, `on("before_agent_start")` |
|
| `claude-rules.ts` | Load rules from files | `on("session_start")`, `on("before_agent_start")` |
|
||||||
| `file-trigger.ts` | File watcher triggers messages | `sendMessage` |
|
| `file-trigger.ts` | File watcher triggers messages | `sendMessage` |
|
||||||
| **Compaction & Sessions** |||
|
| **Compaction & Sessions** |||
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
/**
|
||||||
|
* Displays a status widget showing the system prompt length.
|
||||||
|
*
|
||||||
|
* Demonstrates ctx.getSystemPrompt() for accessing the effective system prompt.
|
||||||
|
*/
|
||||||
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
||||||
|
|
||||||
|
export default function (pi: ExtensionAPI) {
|
||||||
|
pi.on("agent_start", (_event, ctx) => {
|
||||||
|
const prompt = ctx.getSystemPrompt();
|
||||||
|
ctx.ui.setStatus("system-prompt", `System: ${prompt.length} chars`);
|
||||||
|
});
|
||||||
|
|
||||||
|
pi.on("session_shutdown", (_event, ctx) => {
|
||||||
|
ctx.ui.setStatus("system-prompt", undefined);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -514,6 +514,11 @@ export class AgentSession {
|
||||||
return this.agent.state.isStreaming;
|
return this.agent.state.isStreaming;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Current effective system prompt (includes any per-turn extension modifications) */
|
||||||
|
get systemPrompt(): string {
|
||||||
|
return this.agent.state.systemPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
/** Current retry attempt (0 if not retrying) */
|
/** Current retry attempt (0 if not retrying) */
|
||||||
get retryAttempt(): number {
|
get retryAttempt(): number {
|
||||||
return this._retryAttempt;
|
return this._retryAttempt;
|
||||||
|
|
@ -1756,6 +1761,7 @@ export class AgentSession {
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
|
getSystemPrompt: () => this.systemPrompt,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,7 @@ export class ExtensionRunner {
|
||||||
private hasPendingMessagesFn: () => boolean = () => false;
|
private hasPendingMessagesFn: () => boolean = () => false;
|
||||||
private getContextUsageFn: () => ContextUsage | undefined = () => undefined;
|
private getContextUsageFn: () => ContextUsage | undefined = () => undefined;
|
||||||
private compactFn: (options?: CompactOptions) => void = () => {};
|
private compactFn: (options?: CompactOptions) => void = () => {};
|
||||||
|
private getSystemPromptFn: () => string = () => "";
|
||||||
private newSessionHandler: NewSessionHandler = async () => ({ cancelled: false });
|
private newSessionHandler: NewSessionHandler = async () => ({ cancelled: false });
|
||||||
private forkHandler: ForkHandler = async () => ({ cancelled: false });
|
private forkHandler: ForkHandler = async () => ({ cancelled: false });
|
||||||
private navigateTreeHandler: NavigateTreeHandler = async () => ({ cancelled: false });
|
private navigateTreeHandler: NavigateTreeHandler = async () => ({ cancelled: false });
|
||||||
|
|
@ -203,6 +204,7 @@ export class ExtensionRunner {
|
||||||
this.shutdownHandler = contextActions.shutdown;
|
this.shutdownHandler = contextActions.shutdown;
|
||||||
this.getContextUsageFn = contextActions.getContextUsage;
|
this.getContextUsageFn = contextActions.getContextUsage;
|
||||||
this.compactFn = contextActions.compact;
|
this.compactFn = contextActions.compact;
|
||||||
|
this.getSystemPromptFn = contextActions.getSystemPrompt;
|
||||||
|
|
||||||
// Process provider registrations queued during extension loading
|
// Process provider registrations queued during extension loading
|
||||||
for (const { name, config } of this.runtime.pendingProviderRegistrations) {
|
for (const { name, config } of this.runtime.pendingProviderRegistrations) {
|
||||||
|
|
@ -421,6 +423,7 @@ export class ExtensionRunner {
|
||||||
shutdown: () => this.shutdownHandler(),
|
shutdown: () => this.shutdownHandler(),
|
||||||
getContextUsage: () => this.getContextUsageFn(),
|
getContextUsage: () => this.getContextUsageFn(),
|
||||||
compact: (options) => this.compactFn(options),
|
compact: (options) => this.compactFn(options),
|
||||||
|
getSystemPrompt: () => this.getSystemPromptFn(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -260,6 +260,8 @@ export interface ExtensionContext {
|
||||||
getContextUsage(): ContextUsage | undefined;
|
getContextUsage(): ContextUsage | undefined;
|
||||||
/** Trigger compaction without awaiting completion. */
|
/** Trigger compaction without awaiting completion. */
|
||||||
compact(options?: CompactOptions): void;
|
compact(options?: CompactOptions): void;
|
||||||
|
/** Get the current effective system prompt. */
|
||||||
|
getSystemPrompt(): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1084,6 +1086,7 @@ export interface ExtensionContextActions {
|
||||||
shutdown: () => void;
|
shutdown: () => void;
|
||||||
getContextUsage: () => ContextUsage | undefined;
|
getContextUsage: () => ContextUsage | undefined;
|
||||||
compact: (options?: CompactOptions) => void;
|
compact: (options?: CompactOptions) => void;
|
||||||
|
getSystemPrompt: () => string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1120,6 +1120,7 @@ export class InteractiveMode {
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
|
getSystemPrompt: () => this.session.systemPrompt,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set up the extension shortcut handler on the default editor
|
// Set up the extension shortcut handler on the default editor
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue