Консольные агенты claude -p вместо рутин: шаги 2-4, Slack Socket Mode, миграция 006, удаление agent_mcp
- khbuild_assistant_prep: agents (cli-runner, сессии, проверка и повторы формата, шаг 2), slack (Socket Mode, приём, шаг 4, Drive/rclone), промты в promts/ - миграция 006: память менеджера, списки дня, журналы проблем/сообщений/запусков; удалены active_task*, ap_agent_invocation - удалён package agent_mcp; Dockerfile с claude/ffmpeg/exiftool/whisper/rclone; compose-шаблон сервиса агентов - docs: структура approved/proposed (modules, data_objects, roles), скрипт сводок Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,10 +3,18 @@ import Fastify, { type FastifyInstance } from "fastify";
|
||||
import type { Pool } from "khb_common";
|
||||
import type { PrepJob, PrepResult } from "./jobs.js";
|
||||
|
||||
export interface ChatApi {
|
||||
/** Имитация входящего сообщения менеджера (без Slack): текст и локальные файлы; publish - слать ли ответ в Slack. */
|
||||
simulate(opts: { managerId?: string; text: string; files?: string[]; publish?: boolean }): Promise<unknown>;
|
||||
/** Системное событие ассистента (напоминания 12:00/13:00/16:00 и т.п.) всем менеджерам режима. */
|
||||
event(kind: string): Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface PrepApiDeps {
|
||||
pool: Pool;
|
||||
token: string;
|
||||
run(job: PrepJob, opts: { projectId?: string; dryRun?: boolean }): Promise<PrepResult>;
|
||||
chat?: ChatApi;
|
||||
}
|
||||
|
||||
interface RunState {
|
||||
@@ -51,6 +59,34 @@ export function buildApi(deps: PrepApiDeps): FastifyInstance {
|
||||
return reply.code(202).send({ runId: st.id, status: "running" });
|
||||
});
|
||||
|
||||
// шаг 4: асинхронные запуски (агент работает минуты), состояние по id
|
||||
const chatRuns = new Map<string, { status: "running" | "ok" | "error"; result?: unknown; error?: string }>();
|
||||
const startChat = (fn: () => Promise<unknown>) => {
|
||||
const id = randomUUID();
|
||||
const st: { status: "running" | "ok" | "error"; result?: unknown; error?: string } = { status: "running" };
|
||||
chatRuns.set(id, st);
|
||||
if (chatRuns.size > 50) chatRuns.delete(chatRuns.keys().next().value as string);
|
||||
fn().then(
|
||||
(r) => Object.assign(st, { status: "ok", result: r }),
|
||||
(e) => Object.assign(st, { status: "error", error: e instanceof Error ? e.message : String(e) }),
|
||||
);
|
||||
return id;
|
||||
};
|
||||
app.post<{ Body: { managerId?: string; text?: string; files?: string[]; publish?: boolean } }>("/chat/simulate", async (req, reply) => {
|
||||
if (!deps.chat) return reply.code(501).send({ error: "chat не настроен" });
|
||||
const b = req.body ?? {};
|
||||
if (!b.text) return reply.code(400).send({ error: "нужен text" });
|
||||
return reply.code(202).send({ chatRunId: startChat(() => deps.chat!.simulate({ managerId: b.managerId, text: b.text!, files: b.files, publish: b.publish })) });
|
||||
});
|
||||
app.post<{ Body: { kind?: string } }>("/chat/event", async (req, reply) => {
|
||||
if (!deps.chat) return reply.code(501).send({ error: "chat не настроен" });
|
||||
const kind = req.body?.kind;
|
||||
if (!kind) return reply.code(400).send({ error: "нужен kind" });
|
||||
return reply.code(202).send({ chatRunId: startChat(() => deps.chat!.event(kind)) });
|
||||
});
|
||||
app.get<{ Params: { id: string } }>("/chat/runs/:id", async (req, reply) => chatRuns.get(req.params.id) ?? reply.code(404).send({ error: "not found" }));
|
||||
app.get("/agent/runs", async () => (await deps.pool.query("select id, manager_person_id, step, attempt, resumed, status, started_at, duration_ms, cost_usd, error, changes from agent_run order by started_at desc limit 20")).rows);
|
||||
|
||||
app.get<{ Params: { id: string } }>("/runs/:id", async (req, reply) => runs.get(req.params.id) ?? reply.code(404).send({ error: "not found" }));
|
||||
return app;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user