@uncarved/jade 0.4.3 → 0.5.0

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 (39) hide show
  1. package/README.md +317 -279
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/session.d.ts +4 -2
  5. package/dist/session.js +54 -6
  6. package/dist/session.js.map +1 -1
  7. package/package.json +3 -3
  8. package/plugins/keys/index.ts +2 -2
  9. package/plugins/memory/index.ts +6 -4
  10. package/plugins/memory/memory.ts +101 -31
  11. package/plugins/models/index.ts +67 -9
  12. package/plugins/models/provider.ts +81 -24
  13. package/plugins/serve/index.ts +229 -0
  14. package/plugins/serve/plugin.json +8 -0
  15. package/plugins/shell/index.ts +35 -1
  16. package/plugins/shell/shell.ts +5 -0
  17. package/plugins/task/index.ts +3 -1
  18. package/plugins/task/subagent.ts +49 -3
  19. package/plugins/tui/frontend.ts +73 -4
  20. package/plugins/tui/screen.ts +23 -0
  21. package/plugins/tui/tui.ts +176 -11
  22. package/plugins-dist/keys/index.js +1 -1
  23. package/plugins-dist/memory/index.js +7 -3
  24. package/plugins-dist/memory/memory.js +81 -34
  25. package/plugins-dist/models/index.js +62 -7
  26. package/plugins-dist/models/provider.js +47 -14
  27. package/plugins-dist/serve/index.js +238 -0
  28. package/plugins-dist/serve/plugin.json +14 -0
  29. package/plugins-dist/shell/index.js +33 -0
  30. package/plugins-dist/shell/shell.js +3 -0
  31. package/plugins-dist/task/index.js +3 -1
  32. package/plugins-dist/task/subagent.js +47 -2
  33. package/plugins-dist/tui/frontend.js +67 -3
  34. package/plugins-dist/tui/screen.js +3 -0
  35. package/plugins-dist/tui/tui.js +132 -9
  36. package/plugins-dist/tui/vendor.js +1388 -1088
  37. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  38. package/src/api.ts +1 -1
  39. package/src/session.ts +57 -5
package/src/api.ts CHANGED
@@ -5,7 +5,7 @@ import type { PluginLayer } from "@uncarved/jade-api";
5
5
  export * from "@uncarved/jade-api";
6
6
 
7
7
  /** 插件 API 的版本,插件在 plugin.json 的 api 字段里用 ^ 范围声明。和 @uncarved/jade-api 的版本一致 */
8
- export const API_VERSION = "1.5.0";
8
+ export const API_VERSION = "1.6.0";
9
9
 
10
10
  /** 来源层级,后面的层级更高(3.3) */
11
11
  export const LAYERS: readonly PluginLayer[] = ["bundled", "user", "project"];
package/src/session.ts CHANGED
@@ -35,6 +35,7 @@ import {
35
35
  type Session,
36
36
  type SessionContext,
37
37
  type SessionManager,
38
+ type StartedSession,
38
39
  type SessionTool,
39
40
  type SessionUI,
40
41
  type SpawnOptions,
@@ -441,6 +442,8 @@ class LiveSession implements Session {
441
442
  export class Sessions implements SessionManager {
442
443
  readonly #host: SessionHost;
443
444
  #current: LiveSession | undefined;
445
+ /** start 开的会话和交出去的句柄 */
446
+ #started = new Map<LiveSession, StartedSession>();
444
447
  #store: LogStore | undefined;
445
448
  /** 换会话串行进行 */
446
449
  #switching: Promise<unknown> = Promise.resolve();
@@ -469,15 +472,68 @@ export class Sessions implements SessionManager {
469
472
  return run;
470
473
  }
471
474
 
472
- /** 当前会话离开(detach,仍可恢复) */
475
+ /** 当前会话离开(detach,仍可恢复);start 开的会话一并离开 */
473
476
  async close(): Promise<void> {
474
477
  await this.#switching;
475
478
  const old = this.#current;
476
479
  this.#current = undefined;
477
480
  if (old) await this.#leave(old);
481
+ for (const h of [...this.#started.values()]) await h.close();
482
+ }
483
+
484
+ /** 和当前会话并存的会话(无头前端同时跑多个会话):不成为当前会话,各自 close */
485
+ start(opts: OpenOptions = {}): Promise<StartedSession> {
486
+ const run = this.#switching.then(() => this.#start(opts));
487
+ this.#switching = run.catch(() => {});
488
+ return run;
489
+ }
490
+
491
+ async #start(opts: OpenOptions): Promise<StartedSession> {
492
+ // 同一个会话的日志不能同时挂两个 Runtime
493
+ if (opts.resume !== undefined && (opts.resume === this.#current?.id || this.#isStarted(opts.resume))) {
494
+ throw new Error(`session ${opts.resume} is already open`);
495
+ }
496
+ const s = await this.#build(opts);
497
+ let closing: Promise<void> | undefined;
498
+ const handle: StartedSession = {
499
+ id: s.id,
500
+ get state() {
501
+ return s.state;
502
+ },
503
+ send: (input) => s.send(input),
504
+ idle: () => s.idle(),
505
+ recover: () => s.recover(),
506
+ abort: (reason) => s.abort(reason),
507
+ subscribe: (fn) => s.subscribe(fn),
508
+ history: () => s.history(),
509
+ tool: (name) => s.tool(name),
510
+ close: () =>
511
+ (closing ??= (async () => {
512
+ this.#started.delete(s);
513
+ await this.#leave(s);
514
+ })()),
515
+ };
516
+ this.#started.set(s, handle);
517
+ return handle;
518
+ }
519
+
520
+ #isStarted(id: string): boolean {
521
+ return [...this.#started.keys()].some((s) => s.id === id);
478
522
  }
479
523
 
480
524
  async #open(opts: OpenOptions): Promise<Session> {
525
+ // 同一个会话的日志不能同时挂两个 Runtime:start 开着的不能再成为当前会话
526
+ if (opts.resume !== undefined && this.#isStarted(opts.resume)) throw new Error(`session ${opts.resume} is already open`);
527
+ const s = await this.#build(opts);
528
+ // 提交:新会话成为当前会话,然后旧会话离开
529
+ const old = this.#current;
530
+ this.#current = s;
531
+ if (old) await this.#leave(old);
532
+ return s;
533
+ }
534
+
535
+ /** 建好一个会话(全部插件加入、Runtime 建好)。任何一步失败,新会话的作用域全部撤销 */
536
+ async #build(opts: OpenOptions): Promise<LiveSession> {
481
537
  const host = this.#host;
482
538
  const provider = host.service<Provider>("provider@1", host.core);
483
539
  if (!provider) throw new ServiceUnavailable("provider@1", "(none): no active plugin provides a model");
@@ -508,10 +564,6 @@ export class Sessions implements SessionManager {
508
564
  s.subscribe((n) => {
509
565
  if (n.type === "hook-error") host.report(`plugin ${n.info.plugin} failed at ${n.info.point}: ${n.info.error instanceof Error ? n.info.error.message : String(n.info.error)}`);
510
566
  });
511
- // 提交:新会话成为当前会话,然后旧会话离开
512
- const old = this.#current;
513
- this.#current = s;
514
- if (old) await this.#leave(old);
515
567
  return s;
516
568
  }
517
569