File size: 2,348 Bytes
bc575bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import { getPowerShellConfig } from "../../utils/shell.ts";
import {
type BashOperations,
type BashSpawnContext,
type BashSpawnHook,
type BashToolDetails,
type BashToolInput,
type BashToolOptions,
type createBashTool,
createLocalShellOperations,
createShellToolDefinition,
type ShellToolConfig,
} from "./bash.ts";
import { wrapToolDefinition } from "./tool-definition-wrapper.ts";
const UTF8_OUTPUT_PREFIX = "try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}\n";
export const powershellToolSystemPromptContribution = {
snippet: "Execute PowerShell commands",
guidelines: ["You can inspect PI_* environment variables for current model and session details."],
} as const;
export type PowerShellOperations = BashOperations;
export type PowerShellSpawnContext = BashSpawnContext;
export type PowerShellSpawnHook = BashSpawnHook;
export type PowerShellToolDetails = BashToolDetails;
export type PowerShellToolInput = BashToolInput;
export interface PowerShellToolOptions
extends Pick<BashToolOptions, "operations" | "exposeSessionEnvironment" | "spawnHook"> {}
export function createLocalPowerShellOperations(): PowerShellOperations {
const operations = createLocalShellOperations("PowerShell", getPowerShellConfig);
return {
exec: (command, cwd, options) => operations.exec(`${UTF8_OUTPUT_PREFIX}${command}`, cwd, options),
};
}
const powershellToolConfig: ShellToolConfig = {
name: "powershell",
label: "powershell",
shellName: "PowerShell",
prompt: "PS>",
promptSnippet: powershellToolSystemPromptContribution.snippet,
promptGuidelines: powershellToolSystemPromptContribution.guidelines,
tempFilePrefix: "pi-powershell",
};
export function createPowerShellToolDefinition(
cwd: string,
options?: PowerShellToolOptions,
): ReturnType<typeof createShellToolDefinition> {
return createShellToolDefinition(cwd, powershellToolConfig, {
...options,
operations: options?.operations ?? createLocalPowerShellOperations(),
});
}
export function createPowerShellTool(cwd: string, options?: PowerShellToolOptions): ReturnType<typeof createBashTool> {
const definition = createPowerShellToolDefinition(cwd, options);
const tool = wrapToolDefinition(definition);
Object.assign(tool, {
promptSnippet: definition.promptSnippet,
promptGuidelines: definition.promptGuidelines,
});
return tool;
}
|