Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Merge remote-tracking branch 'origin/fix/grok-session-lifecycle' into train/grok-acp

milind-sonidb30c2ca3888
diff --cc server/drivers/acp/driver-hooks.ts
index 65f8d6ee71,e65c172602..83eea38aec
--- a/server/drivers/acp/driver-hooks.ts
+++ b/server/drivers/acp/driver-hooks.ts
@@@ -29,24 -31,17 +31,33 @@@ export interface AcpDriverHooks
/** The failure a turn's thrown error stands for, in the person's words (a usage limit with its reset), or undefined to
* keep the error as it is. Must not throw. */
reportedFailure?(error: unknown): Error | undefined;
+ /** initialize's own `_meta` (beside clientCapabilities' vendor `_meta`), for an agent that reads who its client is there. */
+ initializeMeta?: Record<string, unknown>;
+ /** session/cancel's `_meta`, for an agent that reads how its turn was stopped there. */
+ cancelMeta?: Record<string, unknown>;
+ /** An agent that also ends each prompt with a notification of its own, which may come before the prompt's reply or
+ * without it. */
+ promptCompletion?: AcpPromptCompletion;
+ /** For an agent that can leave session/load unanswered after replaying the history: how long the replay must go quiet
+ * before the load counts as done. Undefined waits for the answer. */
+ loadReplayQuietMs?(turn: SendTurnInput): number | undefined;
+ /** The part of spawnArgs a running process must match for a turn to reuse it. What it leaves out, configureSession
+ * changes inside the live session, throwing LiveSwitchRefused when the agent refuses; a reused session then restarts. */
+ spawnContractArgs?(config: AcpConfig, turn: SendTurnInput): string[];
}
+/** How an agent names the end of a prompt outside its session/prompt reply. */
+export interface AcpPromptCompletion {
+ /** `_meta` each session/prompt carries, so the agent names that prompt when it ends. */
+ meta(promptId: string): Record<string, unknown>;
+ /** The prompt a notification ends (its session, and its id or null when it names none) and its answer in a session/prompt
+ * reply's shape; null for any other notification. */
+ read(msg: { method?: unknown; params?: unknown }): { sessionId: string; promptId: string | null; result: { stopReason: string; error?: string } } | null;
+}
+
+ /** configureSession could not change a running session's model or effort in place. */
+ export class LiveSwitchRefused extends Error {}
+
const MAX_LINE_CHARS = 64 * 1024;
export class SignInRequests {
diff --cc server/drivers/acp/grok.ts
index 5d0a1ef1be,db1e9056eb..ec7fc84816
--- a/server/drivers/acp/grok.ts
+++ b/server/drivers/acp/grok.ts
@@@ -10,9 -10,10 +10,11 @@@ import { harnessHome, splitCliString }
import type { DeviceSignIn, DeviceSignInState } from "../device-auth.ts";
import { decodeInjectId, hostApiKey, localHost, mergeLocalInject } from "../local-inject.ts";
import { createAcpDriver, type AcpSupport } from "./core.ts";
+ import { LiveSwitchRefused } from "./driver-hooks.ts";
+ import { grokLoginCheck } from "./grok-sign-in.ts";
import { closeEndsBackgroundWork } from "./background-work.ts";
import { readGrokFollowUp } from "./grok-follow-up.ts";
+import { GROK_CANCEL_META, GROK_INITIALIZE_META, GROK_PROMPT_COMPLETION, grokPromptOutcome, grokReportedFailure } from "./grok-signals.ts";
import type { AcpPromptSteer } from "./steer.ts";
import { bundledEngineCatalog } from "../../model-manifest/bundled.ts";
import { currentEngineCatalog } from "../../model-manifest/manifest.ts";
diff --cc server/drivers/acp/host.ts
index 3f890b2848,4f2cd0bfbe..feeb0f8e11
--- a/server/drivers/acp/host.ts
+++ b/server/drivers/acp/host.ts
@@@ -361,9 -307,9 +363,12 @@@ export class AcpHost
} else if (msg.id !== undefined && msg.method) {
if (link?.inbound && !link.closed) link.inbound.request(msg);
else this.options.unrouted(msg, (obj) => this.write(obj, link?.threadId ?? null));
- } else if (msg.method && link && !link.closed) {
- if (msg.method === "session/update") link.onUpdate?.(msg.params);
- link.inbound?.notification(msg);
+ } else if (msg.method) {
+ this.#promptEnded(msg);
- if (link && !link.closed) link.inbound?.notification(msg);
++ if (link && !link.closed) {
++ if (msg.method === "session/update") link.onUpdate?.(msg.params);
++ link.inbound?.notification(msg);
++ }
}
}
}