All files / llm-bridge/src index.ts

0% Statements 0/21
0% Branches 0/16
0% Functions 0/3
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                                             
/**
 * `ocap-llm-bridge` entry point.
 *
 * Reads the Unix-socket path from the first positional argument or the
 * `LLM_BRIDGE_SOCKET` env var, plus gateway URL, bearer token, and
 * agent model from `OPENCLAW_GATEWAY_URL` / `OPENCLAW_GATEWAY_TOKEN` /
 * `OPENCLAW_AGENT_MODEL`, then runs the bridge until the kernel-side
 * socket closes (or the process is signalled).
 */
 
import { makeConversation } from './conversation.ts';
import { makeOpenClawClient } from './openclaw-client.ts';
import { runBridge } from './run-bridge.ts';
 
export { makeConversation } from './conversation.ts';
export { makeOpenClawClient } from './openclaw-client.ts';
export { runBridge } from './run-bridge.ts';
export type {
  ChatMessage,
  ChatRole,
  OpenClawClient,
  OpenClawClientConfig,
} from './openclaw-client.ts';
export type { Conversation } from './conversation.ts';
export type {
  IngestRequest,
  MatchEntry,
  MatchesReply,
  MethodDigest,
  QueryRequest,
  Reply,
  Request,
  ServiceDigest,
} from './protocol.ts';
 
const DEFAULT_GATEWAY_URL = 'http://127.0.0.1:18789';
const DEFAULT_AGENT_MODEL = 'openclaw';
 
if (import.meta.url === `file://${process.argv[1]}`) {
  // Treated as a script invocation.
  main().catch((error: unknown) => {
    const message =
      error instanceof Error ? (error.stack ?? error.message) : String(error);
    // eslint-disable-next-line no-console
    console.error(`[llm-bridge] fatal: ${message}`);
    process.exitCode = 1;
  });
}
 
/**
 * Main entry point used when the package is invoked as a CLI.
 */
async function main(): Promise<void> {
  const socketPath =
    // eslint-disable-next-line n/no-process-env
    process.env.LLM_BRIDGE_SOCKET ?? process.argv[2];
  if (!socketPath) {
    throw new Error(
      'expected the kernel IO socket path as the first positional argument or in $LLM_BRIDGE_SOCKET',
    );
  }
  // eslint-disable-next-line n/no-process-env
  const token = process.env.OPENCLAW_GATEWAY_TOKEN;
  if (!token) {
    throw new Error(
      'expected the openclaw gateway bearer token in $OPENCLAW_GATEWAY_TOKEN',
    );
  }
  const baseUrl =
    // eslint-disable-next-line n/no-process-env
    process.env.OPENCLAW_GATEWAY_URL ?? DEFAULT_GATEWAY_URL;
  const model =
    // eslint-disable-next-line n/no-process-env
    process.env.OPENCLAW_AGENT_MODEL ?? DEFAULT_AGENT_MODEL;
 
  const log = (message: string): void => {
    // eslint-disable-next-line no-console
    console.error(`[llm-bridge] ${message}`);
  };
  const client = makeOpenClawClient({ baseUrl, token, model, log });
  const conversation = makeConversation(client);
 
  log(`starting; gateway=${baseUrl} model=${model} socket=${socketPath}`);
 
  await runBridge({ socketPath, conversation, log });
}