@ankit6055/lex 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +84 -32
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ![Lex running in the terminal, showing the startup banner, selected model, and prompt](docs/lex-terminal.png)
4
4
 
5
- [Watch the 30-second demo](Lex%20Demo.mp4) to see Lex fix a bug and run the tests.
5
+ Website: [lex-agent-phi.vercel.app](https://lex-agent-phi.vercel.app) · Install: `npx @ankit6055/lex` · [Watch the Lex demo on Google Drive](https://drive.google.com/file/d/1RlC1GBGPRzCb9zrqRU85cTlKMXddtYzA/view?usp=sharing)
6
6
 
7
7
  Lex is an open-source AI coding agent for your terminal. It reads and edits project files, runs commands with your approval, and streams its work as it goes. Use a hosted model with your own API key or connect to a local model through Ollama or LM Studio.
8
8
 
package/dist/cli.js CHANGED
@@ -134,7 +134,7 @@ var package_default;
134
134
  var init_package = __esm(() => {
135
135
  package_default = {
136
136
  name: "@ankit6055/lex",
137
- version: "0.3.0",
137
+ version: "0.3.1",
138
138
  description: "A minimal, open-source, model-agnostic AI coding harness for your terminal",
139
139
  type: "module",
140
140
  bin: {
@@ -60198,19 +60198,27 @@ async function saveKey(p, key) {
60198
60198
  `, { mode: 384 });
60199
60199
  await chmod(authPath(), 384);
60200
60200
  }
60201
- async function login(io) {
60201
+ async function login(io, opts = {}) {
60202
60202
  const keyed = (await getProviders()).filter((p) => p.env);
60203
- const label = (p) => `${p.label} (${p.id})`;
60204
- const choice = await io.choose("Which provider?", keyed.map(label));
60205
- const c = choice.trim().toLowerCase();
60206
- const provider = keyed.find((p) => label(p) === choice) ?? keyed.find((p) => p.id === c) ?? keyed.find((p) => c.includes(p.id));
60207
- if (!provider)
60208
- throw new Error(`Unknown provider: ${choice}`);
60209
- const key = (await io.secret(`Paste your ${provider.env}: `)).trim();
60203
+ let provider;
60204
+ if (opts.providerId) {
60205
+ provider = keyed.find((p) => p.id === opts.providerId.toLowerCase());
60206
+ if (!provider)
60207
+ throw new Error(`Unknown provider "${opts.providerId}". Choose one of: ${keyed.map((p) => p.id).join(", ")}`);
60208
+ } else {
60209
+ const label = (p) => `${p.label} (${p.id})`;
60210
+ const choice = await io.choose("Which provider?", keyed.map(label));
60211
+ const c = choice.trim().toLowerCase();
60212
+ provider = keyed.find((p) => label(p) === choice) ?? keyed.find((p) => p.id === c) ?? keyed.find((p) => c.includes(p.id));
60213
+ if (!provider)
60214
+ throw new Error(choice.trim() ? `Unknown provider: ${choice}` : "Login cancelled; nothing saved.");
60215
+ }
60216
+ const key = (opts.key ?? await io.secret(`Paste your ${provider.env}: `)).trim();
60210
60217
  if (!key)
60211
60218
  throw new Error("No key entered; nothing saved.");
60212
60219
  await saveKey(provider.id, key);
60213
- io.info(`Saved ${provider.id} key to ${authPath()} (chmod 600).`);
60220
+ io.info(`Saved your ${provider.id} key to ${authPath()} (only you can read it).`);
60221
+ return provider.id;
60214
60222
  }
60215
60223
  function redact(s) {
60216
60224
  return s.replace(/\b(vck_|sk-ant-|sk-proj-|sk-or-v1-|sk-|xai-|gsk_|hf_|nvapi-|fw_|csk-)[A-Za-z0-9_-]{8,}/g, "$1***").replace(/\bAIza[0-9A-Za-z_-]{20,}/g, "AIza***").replace(/(authorization|x-api-key|x-goog-api-key)(["']?\s*[:=]\s*["']?)(bearer\s+)?[^\s"',}]+/gi, "$1$2$3***");
@@ -60221,6 +60229,19 @@ var init_auth = __esm(() => {
60221
60229
  });
60222
60230
 
60223
60231
  // src/models.ts
60232
+ function lexCommand() {
60233
+ return process.env.npm_command === "exec" || /[\\/]_npx[\\/]/.test(process.argv[1] ?? "") ? "npx @ankit6055/lex" : "lex";
60234
+ }
60235
+ function noKeyMessage() {
60236
+ const cmd = lexCommand();
60237
+ return `No API key found. lex is installed fine; it just needs a key to talk to a model.
60238
+
60239
+ Log in now: ${cmd} login
60240
+ Or set one of these and run ${cmd} again:
60241
+ AI_GATEWAY_API_KEY (recommended: one key, every model. vercel.com/ai-gateway)
60242
+ ANTHROPIC_API_KEY OPENAI_API_KEY OPENROUTER_API_KEY GEMINI_API_KEY XAI_API_KEY
60243
+ DEEPSEEK_API_KEY MISTRAL_API_KEY GROQ_API_KEY ... (see: ${cmd} --help)`;
60244
+ }
60224
60245
  function parseSpec(spec, hasKey, providers = BUILTIN_PROVIDERS) {
60225
60246
  const s = spec.trim();
60226
60247
  const known = (p) => providers.some((d) => d.id === p);
@@ -60292,14 +60313,16 @@ async function resolveModel(spec) {
60292
60313
  } else {
60293
60314
  const first = providers.find((p) => !p.local && keys[p.id]);
60294
60315
  if (!first)
60295
- throw new ModelError(NO_KEY_MESSAGE);
60316
+ throw new NoKeyError;
60296
60317
  providerId = first.id;
60297
60318
  id = "";
60298
60319
  }
60299
60320
  const p = providers.find((d) => d.id === providerId);
60300
60321
  const apiKey = keys[p.id];
60301
60322
  if (p.env && !apiKey) {
60302
- throw new ModelError(providers.some((d) => d.env && keys[d.id]) ? `No ${p.id} key. Set ${p.env} or run: lex login` : NO_KEY_MESSAGE);
60323
+ if (!providers.some((d) => d.env && keys[d.id]))
60324
+ throw new NoKeyError;
60325
+ throw new ModelError(`No ${p.id} key. Set ${p.env} or run: ${lexCommand()} login ${p.id}`);
60303
60326
  }
60304
60327
  if (!id) {
60305
60328
  const live = await liveModels(p, apiKey);
@@ -60333,18 +60356,21 @@ async function listAvailable() {
60333
60356
  return ids.map((id) => ({ spec: `${p.id}:${id}`, provider: p.id, id }));
60334
60357
  });
60335
60358
  }
60336
- var DEFAULTS, NO_KEY_MESSAGE = `No API key found. Lex is bring-your-own-key. Set one of:
60337
- AI_GATEWAY_API_KEY (recommended: one key, every model. vercel.com/ai-gateway)
60338
- ANTHROPIC_API_KEY OPENAI_API_KEY OPENROUTER_API_KEY GEMINI_API_KEY XAI_API_KEY
60339
- DEEPSEEK_API_KEY MISTRAL_API_KEY GROQ_API_KEY ... (see: lex --help)
60340
- or run: lex login`, ModelError, toGatewayClaude = (id) => id.replace(/-(\d+)-(\d+)$/, "-$1.$2"), toAnthropicClaude = (id) => id.replace(/(\d+)\.(\d+)$/, "$1-$2"), BARE_PREFIXES, liveCache, STRICT_CATALOGS;
60359
+ var DEFAULTS, NO_KEY_MESSAGE, ModelError, NoKeyError, toGatewayClaude = (id) => id.replace(/-(\d+)-(\d+)$/, "-$1.$2"), toAnthropicClaude = (id) => id.replace(/(\d+)\.(\d+)$/, "$1-$2"), BARE_PREFIXES, liveCache, STRICT_CATALOGS;
60341
60360
  var init_models = __esm(() => {
60342
60361
  init_providers();
60343
60362
  init_auth();
60344
60363
  DEFAULTS = Object.fromEntries(BUILTIN_PROVIDERS.filter((p) => p.models.length).map((p) => [p.id, p.models[0]]));
60364
+ NO_KEY_MESSAGE = noKeyMessage();
60345
60365
  ModelError = class ModelError extends Error {
60346
60366
  name = "ModelError";
60347
60367
  };
60368
+ NoKeyError = class NoKeyError extends ModelError {
60369
+ name = "NoKeyError";
60370
+ constructor() {
60371
+ super(noKeyMessage());
60372
+ }
60373
+ };
60348
60374
  BARE_PREFIXES = [
60349
60375
  { test: /^claude-/, provider: "anthropic", creator: "anthropic", toGateway: toGatewayClaude },
60350
60376
  { test: /^(gpt-|o\d)/, provider: "openai", creator: "openai" },
@@ -63986,22 +64012,29 @@ async function runTui(o) {
63986
64012
  return answer;
63987
64013
  }
63988
64014
  };
64015
+ screen.setView(() => {
64016
+ if (!panel)
64017
+ return { lines: [] };
64018
+ const v = panel.render(screen.cols);
64019
+ return { lines: v.lines, cursor: v.cursor };
64020
+ });
63989
64021
  const approver = createApprover(io, o.mode);
63990
64022
  const makeSession = () => createSession({ root: o.root, io, approver, model: o.model, maxSteps: o.maxSteps });
63991
64023
  let session;
63992
64024
  try {
63993
64025
  session = await makeSession();
63994
64026
  } catch (e) {
63995
- if (!(e instanceof ModelError && e.message === NO_KEY_MESSAGE)) {
64027
+ if (!(e instanceof NoKeyError)) {
63996
64028
  screen.stop();
63997
64029
  throw e;
63998
64030
  }
63999
- screen.print(t.gold(NO_KEY_MESSAGE));
64000
- if (await io.confirm("Set up a key now?") === "n") {
64031
+ screen.print(` ${t.gold("No API key yet.")} ${t.mist("Pick a provider and paste your key to get started (esc to cancel).")}`);
64032
+ try {
64033
+ await login(io);
64034
+ } catch {
64001
64035
  screen.stop();
64002
- throw e;
64036
+ throw new NoKeyError;
64003
64037
  }
64004
- await login(io);
64005
64038
  session = await makeSession();
64006
64039
  }
64007
64040
  const modes = o.mode === "yolo" ? [...MODE_CYCLE, "yolo"] : MODE_CYCLE;
@@ -64708,12 +64741,9 @@ async function startSession(o, io) {
64708
64741
  try {
64709
64742
  return await make();
64710
64743
  } catch (e) {
64711
- if (!(e instanceof ModelError && e.message === NO_KEY_MESSAGE && process.stdin.isTTY))
64712
- throw e;
64713
- say(import_picocolors5.default.yellow(NO_KEY_MESSAGE));
64714
- say();
64715
- if (await io.confirm("Set up a key now? [y/n]") === "n")
64744
+ if (!(e instanceof NoKeyError && process.stdin.isTTY))
64716
64745
  throw e;
64746
+ say(import_picocolors5.default.yellow("No API key yet. Pick a provider and paste your key to get started (ctrl+c to cancel)."));
64717
64747
  await login(io);
64718
64748
  return make();
64719
64749
  }
@@ -64859,7 +64889,7 @@ Usage
64859
64889
  lex interactive session in the current directory
64860
64890
  lex "fix the failing test" one-shot: run, print, exit
64861
64891
  lex compare --models a,b "task" same task on several models (omit --models to pick them)
64862
- lex login save an API key to ~/.lex/auth.json
64892
+ lex login [provider] save an API key (or: echo $KEY | lex login <provider>)
64863
64893
  lex models list models your keys can reach
64864
64894
 
64865
64895
  Options
@@ -64886,7 +64916,18 @@ function fail(msg, code = 1) {
64886
64916
  }
64887
64917
  async function oneShot(o) {
64888
64918
  const io = createIO();
64889
- const session = await createSession({ root: o.root, io, approver: createApprover(io, o.mode), model: o.model, maxSteps: o.maxSteps });
64919
+ const make = () => createSession({ root: o.root, io, approver: createApprover(io, o.mode), model: o.model, maxSteps: o.maxSteps });
64920
+ let session;
64921
+ try {
64922
+ session = await make();
64923
+ } catch (e) {
64924
+ if (!(e instanceof NoKeyError && process.stdin.isTTY))
64925
+ throw e;
64926
+ process.stderr.write(import_picocolors6.default.yellow(`No API key yet. Pick a provider and paste your key to get started (ctrl+c to cancel).
64927
+ `));
64928
+ await login(io);
64929
+ session = await make();
64930
+ }
64890
64931
  const controller = new AbortController;
64891
64932
  const onSigint = () => {
64892
64933
  if (controller.signal.aborted)
@@ -64945,13 +64986,24 @@ Run lex --help for usage.`);
64945
64986
  }
64946
64987
  const [sub, ...rest] = positionals;
64947
64988
  switch (sub) {
64948
- case "login":
64949
- await login(createIO());
64989
+ case "login": {
64990
+ const providerId = rest[0];
64991
+ if (!process.stdin.isTTY) {
64992
+ if (!providerId)
64993
+ fail(`Usage: echo "$YOUR_KEY" | ${lexCommand()} login <provider> (e.g. gateway, anthropic, openai)`);
64994
+ let key = "";
64995
+ for await (const chunk of process.stdin)
64996
+ key += chunk;
64997
+ await login(createIO(), { providerId, key });
64998
+ } else {
64999
+ await login(createIO(), { providerId });
65000
+ }
64950
65001
  return 0;
65002
+ }
64951
65003
  case "models": {
64952
65004
  const models = await listAvailable();
64953
65005
  if (!models.length)
64954
- fail(NO_KEY_MESSAGE);
65006
+ fail(noKeyMessage());
64955
65007
  for (const m of models)
64956
65008
  console.log(m.spec);
64957
65009
  return 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ankit6055/lex",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A minimal, open-source, model-agnostic AI coding harness for your terminal",
5
5
  "type": "module",
6
6
  "bin": {