Archived
Add model and thinking controls
This commit is contained in:
@@ -18,21 +18,6 @@ export function registerSessionProxyRoutes(app: FastifyInstance, daemon = new Se
|
||||
|
||||
app.get("/api/sessiond/health", (_request, reply) => proxy({ method: "GET", url: "/api/health" }, reply));
|
||||
|
||||
app.get<{ Querystring: { cwd?: string } }>("/api/sessions", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Body: { cwd: string } }>("/api/sessions", (request, reply) => proxy(request, reply));
|
||||
app.get<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/messages", (request, reply) => proxy(request, reply));
|
||||
app.get<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/status", (request, reply) => proxy(request, reply));
|
||||
app.get<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/commands", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string }; Body: { text: string; streamingBehavior?: "steer" | "followUp" } }>("/api/sessions/:sessionId/prompt", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string }; Body: { text: string } }>("/api/sessions/:sessionId/shell", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string }; Body: { text: string } }>("/api/sessions/:sessionId/commands/run", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string }; Body: { requestId: string; value: string } }>("/api/sessions/:sessionId/commands/respond", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/abort", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/stop", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/archive", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/restore", (request, reply) => proxy(request, reply));
|
||||
app.post<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/detach-parent", (request, reply) => proxy(request, reply));
|
||||
|
||||
app.get<{ Params: { sessionId: string } }>("/api/sessions/:sessionId/events", { websocket: true }, (socket, request) => {
|
||||
bridgeSockets(socket, daemon.connectWebSocket(`/sessions/${request.params.sessionId}/events`));
|
||||
});
|
||||
@@ -40,6 +25,9 @@ export function registerSessionProxyRoutes(app: FastifyInstance, daemon = new Se
|
||||
app.get("/api/sessions/events", { websocket: true }, (socket) => {
|
||||
bridgeSockets(socket, daemon.connectWebSocket("/sessions/events"));
|
||||
});
|
||||
|
||||
app.all("/api/sessions", (request, reply) => proxy(request, reply));
|
||||
app.all("/api/sessions/*", (request, reply) => proxy(request, reply));
|
||||
}
|
||||
|
||||
function stripApiPrefix(url: string): string {
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
type AgentSession,
|
||||
type CreateAgentSessionRuntimeFactory,
|
||||
} from "@earendil-works/pi-coding-agent";
|
||||
import type { ClientCommand, ClientCommandResult, ClientMessagePage, ClientSession, ClientSessionStatus, SessionUiEvent } from "../types.js";
|
||||
import type { ClientCommand, ClientCommandResult, ClientMessagePage, ClientSession, ClientSessionModel, ClientSessionStatus, ClientThinkingLevel, SessionUiEvent } from "../types.js";
|
||||
import type { SessionEventHub } from "../realtime/sessionEventHub.js";
|
||||
import { BUILTIN_COMMANDS } from "./builtinCommands.js";
|
||||
import { SessionCommandService } from "./sessionCommandService.js";
|
||||
@@ -151,6 +151,65 @@ export class PiSessionService {
|
||||
return this.statusFromSession(await this.getOrOpen(sessionId));
|
||||
}
|
||||
|
||||
async availableModels(sessionId: string): Promise<ClientSessionModel[]> {
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
session.modelRegistry.refresh();
|
||||
const models = session.scopedModels.length > 0
|
||||
? session.scopedModels.map((scoped) => scoped.model)
|
||||
: session.modelRegistry.getAvailable();
|
||||
return models.map(modelToClientModel);
|
||||
}
|
||||
|
||||
async setModel(sessionId: string, provider: string, modelId: string): Promise<ClientSessionStatus> {
|
||||
await this.assertWritable(sessionId);
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
session.modelRegistry.refresh();
|
||||
const candidates = session.scopedModels.length > 0
|
||||
? session.scopedModels.map((scoped) => scoped.model)
|
||||
: session.modelRegistry.getAvailable();
|
||||
const model = candidates.find((candidate) => candidate.provider === provider && candidate.id === modelId)
|
||||
?? session.modelRegistry.find(provider, modelId);
|
||||
if (model === undefined) throw new Error(`Model not found: ${provider}/${modelId}`);
|
||||
await session.setModel(model);
|
||||
this.publishActivity(session, `model: ${model.id}`, "idle", model.provider);
|
||||
this.publishStatus(session);
|
||||
return this.statusFromSession(session);
|
||||
}
|
||||
|
||||
async cycleModel(sessionId: string, direction: "forward" | "backward"): Promise<ClientSessionStatus> {
|
||||
await this.assertWritable(sessionId);
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
const result = await session.cycleModel(direction);
|
||||
if (result === undefined) throw new Error(session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available");
|
||||
this.publishActivity(session, `model: ${result.model.id}`, "idle", result.model.provider);
|
||||
this.publishStatus(session);
|
||||
return this.statusFromSession(session);
|
||||
}
|
||||
|
||||
async availableThinkingLevels(sessionId: string): Promise<ClientThinkingLevel[]> {
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
return session.getAvailableThinkingLevels();
|
||||
}
|
||||
|
||||
async setThinkingLevel(sessionId: string, level: ClientThinkingLevel): Promise<ClientSessionStatus> {
|
||||
await this.assertWritable(sessionId);
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
session.setThinkingLevel(level);
|
||||
this.publishActivity(session, `thinking: ${session.thinkingLevel}`, "idle");
|
||||
this.publishStatus(session);
|
||||
return this.statusFromSession(session);
|
||||
}
|
||||
|
||||
async cycleThinkingLevel(sessionId: string): Promise<ClientSessionStatus> {
|
||||
await this.assertWritable(sessionId);
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
const level = session.cycleThinkingLevel();
|
||||
if (level === undefined) throw new Error("Current model does not support thinking");
|
||||
this.publishActivity(session, `thinking: ${level}`, "idle");
|
||||
this.publishStatus(session);
|
||||
return this.statusFromSession(session);
|
||||
}
|
||||
|
||||
async commands(sessionId: string): Promise<ClientCommand[]> {
|
||||
const session = await this.getOrOpen(sessionId);
|
||||
const commands: ClientCommand[] = [...BUILTIN_COMMANDS];
|
||||
@@ -402,19 +461,7 @@ export class PiSessionService {
|
||||
|
||||
private statusFromSession(session: AgentSession): ClientSessionStatus {
|
||||
const stats = session.getSessionStats();
|
||||
const model = session.model === undefined
|
||||
? undefined
|
||||
: (() => {
|
||||
const name = getString(session.model, "name");
|
||||
const reasoning = getProperty(session.model, "reasoning");
|
||||
return {
|
||||
provider: session.model.provider,
|
||||
id: session.model.id,
|
||||
...(name === undefined ? {} : { name }),
|
||||
contextWindow: session.model.contextWindow,
|
||||
...(reasoning === undefined ? {} : { reasoning }),
|
||||
};
|
||||
})();
|
||||
const model = session.model === undefined ? undefined : modelToClientModel(session.model);
|
||||
const contextUsage = session.getContextUsage();
|
||||
return {
|
||||
sessionId: session.sessionId,
|
||||
@@ -432,6 +479,19 @@ export class PiSessionService {
|
||||
}
|
||||
}
|
||||
|
||||
function modelToClientModel(model: AgentSession["model"]): ClientSessionModel {
|
||||
if (model === undefined) return {};
|
||||
const name = getString(model, "name");
|
||||
const reasoning = getProperty(model, "reasoning");
|
||||
return {
|
||||
provider: model.provider,
|
||||
id: model.id,
|
||||
...(name === undefined ? {} : { name }),
|
||||
contextWindow: model.contextWindow,
|
||||
...(reasoning === undefined ? {} : { reasoning }),
|
||||
};
|
||||
}
|
||||
|
||||
async function clearParentSession(sessionFile: string): Promise<void> {
|
||||
const content = await readFile(sessionFile, "utf8");
|
||||
const newlineIndex = content.indexOf("\n");
|
||||
@@ -445,8 +505,7 @@ async function clearParentSession(sessionFile: string): Promise<void> {
|
||||
}
|
||||
|
||||
function clearSessionQueue(session: AgentSession): void {
|
||||
const candidate = session as AgentSession & { clearQueue?: () => unknown };
|
||||
candidate.clearQueue?.();
|
||||
session.clearQueue();
|
||||
}
|
||||
|
||||
function hasQueuedMessageText(session: AgentSession, text: string): boolean {
|
||||
@@ -454,10 +513,9 @@ function hasQueuedMessageText(session: AgentSession, text: string): boolean {
|
||||
}
|
||||
|
||||
function queuedMessagesFromSession(session: AgentSession): { kind: "steer" | "followUp"; text: string }[] {
|
||||
const candidate = session as AgentSession & { getSteeringMessages?: () => string[]; getFollowUpMessages?: () => string[] };
|
||||
return [
|
||||
...(candidate.getSteeringMessages?.() ?? []).map((text) => ({ kind: "steer" as const, text })),
|
||||
...(candidate.getFollowUpMessages?.() ?? []).map((text) => ({ kind: "followUp" as const, text })),
|
||||
...session.getSteeringMessages().map((text) => ({ kind: "steer" as const, text })),
|
||||
...session.getFollowUpMessages().map((text) => ({ kind: "followUp" as const, text })),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,54 @@ export function registerSessionRoutes(app: FastifyInstance, sessions: PiSessionS
|
||||
}
|
||||
});
|
||||
|
||||
app.get<{ Params: { sessionId: string } }>(`${prefix}/sessions/:sessionId/models`, async (request, reply) => {
|
||||
try {
|
||||
return { models: await sessions.availableModels(request.params.sessionId) };
|
||||
} catch (error) {
|
||||
return reply.code(404).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.post<{ Params: { sessionId: string }; Body: { provider: string; modelId: string } }>(`${prefix}/sessions/:sessionId/model`, async (request, reply) => {
|
||||
try {
|
||||
return await sessions.setModel(request.params.sessionId, request.body.provider, request.body.modelId);
|
||||
} catch (error) {
|
||||
return reply.code(400).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.post<{ Params: { sessionId: string }; Body: { direction?: "forward" | "backward" } }>(`${prefix}/sessions/:sessionId/model/cycle`, async (request, reply) => {
|
||||
try {
|
||||
return await sessions.cycleModel(request.params.sessionId, request.body.direction ?? "forward");
|
||||
} catch (error) {
|
||||
return reply.code(400).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.get<{ Params: { sessionId: string } }>(`${prefix}/sessions/:sessionId/thinking-levels`, async (request, reply) => {
|
||||
try {
|
||||
return { levels: await sessions.availableThinkingLevels(request.params.sessionId) };
|
||||
} catch (error) {
|
||||
return reply.code(404).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.post<{ Params: { sessionId: string }; Body: { level: "off" | "minimal" | "low" | "medium" | "high" | "xhigh" } }>(`${prefix}/sessions/:sessionId/thinking-level`, async (request, reply) => {
|
||||
try {
|
||||
return await sessions.setThinkingLevel(request.params.sessionId, request.body.level);
|
||||
} catch (error) {
|
||||
return reply.code(400).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.post<{ Params: { sessionId: string } }>(`${prefix}/sessions/:sessionId/thinking-level/cycle`, async (request, reply) => {
|
||||
try {
|
||||
return await sessions.cycleThinkingLevel(request.params.sessionId);
|
||||
} catch (error) {
|
||||
return reply.code(400).send({ error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
|
||||
app.get<{ Params: { sessionId: string } }>(`${prefix}/sessions/:sessionId/commands`, async (request, reply) => {
|
||||
try {
|
||||
return await sessions.commands(request.params.sessionId);
|
||||
|
||||
@@ -4,6 +4,8 @@ export type {
|
||||
SessionInfo as ClientSession,
|
||||
MessagePage as ClientMessagePage,
|
||||
SessionStatus as ClientSessionStatus,
|
||||
SessionModel as ClientSessionModel,
|
||||
ThinkingLevel as ClientThinkingLevel,
|
||||
SlashCommand as ClientCommand,
|
||||
FileSuggestion as ClientFileSuggestion,
|
||||
CommandOption as ClientCommandOption,
|
||||
|
||||
Reference in New Issue
Block a user