interact.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Command } from "commander";
  2. import {
  3. processProjectInteractionInput,
  4. type InteractionRuntimeTools,
  5. } from "@actalk/inkos-core";
  6. import { createInteractionTools } from "../interaction/tools.js";
  7. export interface InteractCommandHooks {
  8. readonly runInteraction?: (params: {
  9. readonly projectRoot: string;
  10. readonly input: string;
  11. readonly activeBookId?: string;
  12. readonly tools: InteractionRuntimeTools;
  13. }) => Promise<{
  14. readonly request: unknown;
  15. readonly responseText?: string;
  16. readonly session: {
  17. readonly automationMode: string;
  18. readonly activeBookId?: string;
  19. readonly currentExecution?: unknown;
  20. readonly pendingDecision?: unknown;
  21. readonly messages: ReadonlyArray<unknown>;
  22. readonly events: ReadonlyArray<unknown>;
  23. };
  24. }>;
  25. readonly createTools?: (projectRoot: string) => Promise<InteractionRuntimeTools>;
  26. readonly readInput?: () => Promise<string>;
  27. }
  28. async function readInteractionInput(
  29. args: ReadonlyArray<string>,
  30. explicitMessage: string | undefined,
  31. readInput?: () => Promise<string>,
  32. ): Promise<string> {
  33. const explicit = explicitMessage?.trim();
  34. if (explicit) {
  35. return explicit;
  36. }
  37. const inline = args.join(" ").trim();
  38. if (inline) {
  39. return inline;
  40. }
  41. if (readInput) {
  42. const provided = (await readInput()).trim();
  43. if (provided) {
  44. return provided;
  45. }
  46. }
  47. if (!process.stdin.isTTY) {
  48. const chunks: Buffer[] = [];
  49. for await (const chunk of process.stdin) {
  50. chunks.push(chunk as Buffer);
  51. }
  52. const piped = Buffer.concat(chunks).toString("utf-8").trim();
  53. if (piped) {
  54. return piped;
  55. }
  56. }
  57. throw new Error("Interaction message is required. Pass text arguments or pipe input via stdin.");
  58. }
  59. export function createInteractCommand(hooks: InteractCommandHooks = {}): Command {
  60. return new Command("interact")
  61. .description("Run a shared natural-language interaction against the current project")
  62. .argument("[message...]", "Natural-language message")
  63. .option("--message <text>", "Explicit natural-language message")
  64. .option("--book <bookId>", "Bind a specific active book for this interaction")
  65. .option("--json", "Emit structured JSON for external agents")
  66. .action(async (messageArgs: ReadonlyArray<string>, opts) => {
  67. const input = await readInteractionInput(messageArgs, opts.message, hooks.readInput);
  68. const projectRoot = process.cwd();
  69. const tools = hooks.createTools
  70. ? await hooks.createTools(projectRoot)
  71. : hooks.runInteraction
  72. ? ({} as InteractionRuntimeTools)
  73. : await createInteractionTools(projectRoot, undefined, { requireApiKey: false });
  74. const runInteraction = hooks.runInteraction ?? processProjectInteractionInput;
  75. const result = await runInteraction({
  76. projectRoot,
  77. input,
  78. activeBookId: opts.book,
  79. tools,
  80. });
  81. if (opts.json) {
  82. process.stdout.write(`${JSON.stringify({
  83. request: result.request,
  84. responseText: result.responseText,
  85. session: result.session,
  86. currentExecution: result.session.currentExecution ?? null,
  87. pendingDecision: result.session.pendingDecision ?? null,
  88. events: result.session.events,
  89. }, null, 2)}\n`);
  90. return;
  91. }
  92. const text = result.responseText
  93. ?? (result.session.messages.at(-1) && "content" in (result.session.messages.at(-1) as Record<string, unknown>)
  94. ? String((result.session.messages.at(-1) as Record<string, unknown>).content)
  95. : "");
  96. if (text) {
  97. process.stdout.write(`${text}\n`);
  98. }
  99. });
  100. }