@chances-ai/client 24.0.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 (121) hide show
  1. package/dist/client-core/client.d.ts +145 -0
  2. package/dist/client-core/client.d.ts.map +1 -0
  3. package/dist/client-core/client.js +533 -0
  4. package/dist/client-core/client.js.map +1 -0
  5. package/dist/client-core/index.d.ts +16 -0
  6. package/dist/client-core/index.d.ts.map +1 -0
  7. package/dist/client-core/index.js +15 -0
  8. package/dist/client-core/index.js.map +1 -0
  9. package/dist/client-core/reverse-map.d.ts +66 -0
  10. package/dist/client-core/reverse-map.d.ts.map +1 -0
  11. package/dist/client-core/reverse-map.js +86 -0
  12. package/dist/client-core/reverse-map.js.map +1 -0
  13. package/dist/client-core/store.d.ts +144 -0
  14. package/dist/client-core/store.d.ts.map +1 -0
  15. package/dist/client-core/store.js +36 -0
  16. package/dist/client-core/store.js.map +1 -0
  17. package/dist/client-core/transport.d.ts +53 -0
  18. package/dist/client-core/transport.d.ts.map +1 -0
  19. package/dist/client-core/transport.js +103 -0
  20. package/dist/client-core/transport.js.map +1 -0
  21. package/dist/web-ui/approval-badge.d.ts +17 -0
  22. package/dist/web-ui/approval-badge.d.ts.map +1 -0
  23. package/dist/web-ui/approval-badge.js +24 -0
  24. package/dist/web-ui/approval-badge.js.map +1 -0
  25. package/dist/web-ui/button.d.ts +10 -0
  26. package/dist/web-ui/button.d.ts.map +1 -0
  27. package/dist/web-ui/button.js +16 -0
  28. package/dist/web-ui/button.js.map +1 -0
  29. package/dist/web-ui/code-view.d.ts +6 -0
  30. package/dist/web-ui/code-view.d.ts.map +1 -0
  31. package/dist/web-ui/code-view.js +14 -0
  32. package/dist/web-ui/code-view.js.map +1 -0
  33. package/dist/web-ui/code-viewer.d.ts +11 -0
  34. package/dist/web-ui/code-viewer.d.ts.map +1 -0
  35. package/dist/web-ui/code-viewer.js +55 -0
  36. package/dist/web-ui/code-viewer.js.map +1 -0
  37. package/dist/web-ui/commands.d.ts +23 -0
  38. package/dist/web-ui/commands.d.ts.map +1 -0
  39. package/dist/web-ui/commands.js +26 -0
  40. package/dist/web-ui/commands.js.map +1 -0
  41. package/dist/web-ui/control-panel.d.ts +9 -0
  42. package/dist/web-ui/control-panel.d.ts.map +1 -0
  43. package/dist/web-ui/control-panel.js +70 -0
  44. package/dist/web-ui/control-panel.js.map +1 -0
  45. package/dist/web-ui/diff-view.d.ts +8 -0
  46. package/dist/web-ui/diff-view.d.ts.map +1 -0
  47. package/dist/web-ui/diff-view.js +28 -0
  48. package/dist/web-ui/diff-view.js.map +1 -0
  49. package/dist/web-ui/environment-panel.d.ts +9 -0
  50. package/dist/web-ui/environment-panel.d.ts.map +1 -0
  51. package/dist/web-ui/environment-panel.js +38 -0
  52. package/dist/web-ui/environment-panel.js.map +1 -0
  53. package/dist/web-ui/file-tree.d.ts +9 -0
  54. package/dist/web-ui/file-tree.d.ts.map +1 -0
  55. package/dist/web-ui/file-tree.js +77 -0
  56. package/dist/web-ui/file-tree.js.map +1 -0
  57. package/dist/web-ui/help-view.d.ts +13 -0
  58. package/dist/web-ui/help-view.d.ts.map +1 -0
  59. package/dist/web-ui/help-view.js +15 -0
  60. package/dist/web-ui/help-view.js.map +1 -0
  61. package/dist/web-ui/helpers.d.ts +77 -0
  62. package/dist/web-ui/helpers.d.ts.map +1 -0
  63. package/dist/web-ui/helpers.js +176 -0
  64. package/dist/web-ui/helpers.js.map +1 -0
  65. package/dist/web-ui/hooks.d.ts +7 -0
  66. package/dist/web-ui/hooks.d.ts.map +1 -0
  67. package/dist/web-ui/hooks.js +26 -0
  68. package/dist/web-ui/hooks.js.map +1 -0
  69. package/dist/web-ui/index.d.ts +32 -0
  70. package/dist/web-ui/index.d.ts.map +1 -0
  71. package/dist/web-ui/index.js +38 -0
  72. package/dist/web-ui/index.js.map +1 -0
  73. package/dist/web-ui/markdown.d.ts +12 -0
  74. package/dist/web-ui/markdown.d.ts.map +1 -0
  75. package/dist/web-ui/markdown.js +55 -0
  76. package/dist/web-ui/markdown.js.map +1 -0
  77. package/dist/web-ui/model-picker.d.ts +9 -0
  78. package/dist/web-ui/model-picker.d.ts.map +1 -0
  79. package/dist/web-ui/model-picker.js +119 -0
  80. package/dist/web-ui/model-picker.js.map +1 -0
  81. package/dist/web-ui/permission.d.ts +7 -0
  82. package/dist/web-ui/permission.d.ts.map +1 -0
  83. package/dist/web-ui/permission.js +25 -0
  84. package/dist/web-ui/permission.js.map +1 -0
  85. package/dist/web-ui/primitives.d.ts +59 -0
  86. package/dist/web-ui/primitives.d.ts.map +1 -0
  87. package/dist/web-ui/primitives.js +38 -0
  88. package/dist/web-ui/primitives.js.map +1 -0
  89. package/dist/web-ui/progress.d.ts +28 -0
  90. package/dist/web-ui/progress.d.ts.map +1 -0
  91. package/dist/web-ui/progress.js +45 -0
  92. package/dist/web-ui/progress.js.map +1 -0
  93. package/dist/web-ui/prompt-input.d.ts +15 -0
  94. package/dist/web-ui/prompt-input.d.ts.map +1 -0
  95. package/dist/web-ui/prompt-input.js +129 -0
  96. package/dist/web-ui/prompt-input.js.map +1 -0
  97. package/dist/web-ui/question-view.d.ts +7 -0
  98. package/dist/web-ui/question-view.d.ts.map +1 -0
  99. package/dist/web-ui/question-view.js +60 -0
  100. package/dist/web-ui/question-view.js.map +1 -0
  101. package/dist/web-ui/segments.d.ts +8 -0
  102. package/dist/web-ui/segments.d.ts.map +1 -0
  103. package/dist/web-ui/segments.js +19 -0
  104. package/dist/web-ui/segments.js.map +1 -0
  105. package/dist/web-ui/session-sidebar.d.ts +15 -0
  106. package/dist/web-ui/session-sidebar.d.ts.map +1 -0
  107. package/dist/web-ui/session-sidebar.js +10 -0
  108. package/dist/web-ui/session-sidebar.js.map +1 -0
  109. package/dist/web-ui/status-bar.d.ts +10 -0
  110. package/dist/web-ui/status-bar.d.ts.map +1 -0
  111. package/dist/web-ui/status-bar.js +10 -0
  112. package/dist/web-ui/status-bar.js.map +1 -0
  113. package/dist/web-ui/theme-context.d.ts +13 -0
  114. package/dist/web-ui/theme-context.d.ts.map +1 -0
  115. package/dist/web-ui/theme-context.js +31 -0
  116. package/dist/web-ui/theme-context.js.map +1 -0
  117. package/dist/web-ui/transcript.d.ts +27 -0
  118. package/dist/web-ui/transcript.d.ts.map +1 -0
  119. package/dist/web-ui/transcript.js +56 -0
  120. package/dist/web-ui/transcript.js.map +1 -0
  121. package/package.json +56 -0
@@ -0,0 +1,145 @@
1
+ /**
2
+ * (v17 M1 / v19 M2 / v20 M3) `RpcClient` — the pure-TS ACP client state machine.
3
+ * It owns the transport, a browser-safe `LocalEventBus`, the reused
4
+ * `ChatViewModel` (transcript), and a vanilla-zustand control store. Inbound ACP
5
+ * `session/update` notifications are reverse-mapped to `AppEvent`s on the bus
6
+ * that drives the ChatViewModel; the `session/prompt` response, the
7
+ * `session/request_permission` request, and the chances extension event/responses
8
+ * drive the control store. Outbound commands are ACP JSON-RPC. No DOM/React/Ink
9
+ * here — that is web-ui's job.
10
+ *
11
+ * **Self-rolled ACP, not the SDK runtime (docs/6.4a §3).** The relay fans one
12
+ * session out to many sockets, so the SDK's point-to-point `ClientSideConnection`
13
+ * (which owns JSON-RPC id correlation for one socket) doesn't fit. We frame ACP
14
+ * ourselves on the same transport seam, keeping the M2 reconnect machinery: ids
15
+ * are prefixed per client so a fan-out response can't cross-talk between tabs
16
+ * (`ownsId`); a re-received `session/request_permission` for an already-pending
17
+ * id is idempotent; permission answers are ACP responses to the agent's request.
18
+ *
19
+ * Handshake: on the first (or a cold-reloaded) `relay_welcome`, the client sends
20
+ * ACP `initialize` then `session/new` to learn its capabilities/models +
21
+ * sessionId; a RESUMING reconnect skips the handshake (it already has them) and
22
+ * just replays the gap. The wire is real ACP — a plain ACP client/Zed speaks the
23
+ * same `initialize`/`session/*` it does (proven by the c6 SDK-interop test).
24
+ */
25
+ import { ChatViewModel, type ChatViewModelOptions } from "@chances-ai/ui-core";
26
+ import type { ApprovalMode } from "@chances-ai/runtime";
27
+ import { type ClientStore, type GitDiffResult, type GitStatusResult, type ListFilesResult, type QuestionAnswer, type ReadFileResult } from "./store.js";
28
+ import type { TransportFactory } from "./transport.js";
29
+ /** Schedule `run` after `delayMs`; returns a cancel fn. Injected so reconnect
30
+ * timing is deterministic in tests (no real timers). */
31
+ export type ReconnectScheduler = (delayMs: number, run: () => void) => () => void;
32
+ export interface ReconnectOptions {
33
+ schedule?: ReconnectScheduler;
34
+ baseDelayMs?: number;
35
+ maxDelayMs?: number;
36
+ staleAfterMs?: number;
37
+ }
38
+ export interface RpcClientOptions {
39
+ scheduler?: ChatViewModelOptions["scheduler"];
40
+ home?: string;
41
+ reconnect?: ReconnectOptions;
42
+ /** Per-client id prefix so two tabs' command ids never collide on the fan-out
43
+ * response stream. Defaults to a short random token. */
44
+ clientId?: string;
45
+ }
46
+ export declare class RpcClient {
47
+ readonly view: ChatViewModel;
48
+ readonly store: ClientStore;
49
+ private readonly bus;
50
+ private readonly factory;
51
+ private readonly handlers;
52
+ private transport;
53
+ private seq;
54
+ private readonly clientId;
55
+ /** In-flight request/response waiters (initialize/session.new/ping/set_model). */
56
+ private readonly waiters;
57
+ private userClosed;
58
+ private transportDown;
59
+ private reconnectAttempt;
60
+ private reconnectBudgetMs;
61
+ private cancelReconnect;
62
+ private readonly schedule;
63
+ private readonly baseDelayMs;
64
+ private readonly maxDelayMs;
65
+ private readonly staleAfterMs;
66
+ constructor(factory: TransportFactory, opts?: RpcClientOptions);
67
+ private connect;
68
+ /** Submit a prompt (ACP `session/prompt`). Echoes a user line + optimistically
69
+ * marks busy; a rejected response (AGENT_BUSY) clears it. */
70
+ prompt(text: string): void;
71
+ /** Cancel the running turn (ACP `session/cancel` notification). */
72
+ abort(): void;
73
+ /** Claim the controller lease — a handoff (no effect when the lease is disabled,
74
+ * since everyone already drives). The relay broadcasts the new holder/epoch as
75
+ * a `_chances/unstable/control` notification that recomputes `isController`. */
76
+ takeControl(): void;
77
+ /** Switch model (chances extension). Resolves with the ack result. */
78
+ setModel(selection: {
79
+ provider?: string;
80
+ model?: string;
81
+ }): Promise<unknown>;
82
+ /** (v22) Set the session approval mode (chances extension `set_approval`), the
83
+ * GUI twin of the TUI Shift+Tab. Allowed mid-turn (applies to the next
84
+ * permission check). Optimistically updates the store, then reconciles with
85
+ * the ack (the engine echoes the mode it actually applied). */
86
+ setApprovalMode(mode: ApprovalMode): Promise<unknown>;
87
+ /** (v22) Advance the approval mode one step in the Shift+Tab cycle. */
88
+ cycleApprovalMode(): Promise<unknown>;
89
+ /** Answer the open permission ask — an ACP RESPONSE to the agent's
90
+ * `session/request_permission` request (id = the ask id). No-op when none. */
91
+ answerPermission(allow: boolean, remember?: boolean): void;
92
+ /** (v22) Answer the open `ask_user_question` — an ACP RESPONSE to the agent's
93
+ * `_chances/unstable/request_question` request (id = the ask id). `declined`
94
+ * cancels. No-op when none pending. */
95
+ answerQuestion(answer: QuestionAnswer): void;
96
+ /** Liveness probe (chances extension `_chances/unstable/ping`). */
97
+ ping(): Promise<unknown>;
98
+ /** List the immediate children of `dir` (one level, lazy). The file tree calls
99
+ * this per-expand; results stay component-local (not in the store). */
100
+ listFiles(dir: string): Promise<ListFilesResult>;
101
+ /** Read a workspace file (capped + binary-guarded) and set it as the open file
102
+ * in the store so the code viewer renders it. */
103
+ openFileInViewer(path: string): Promise<ReadFileResult>;
104
+ /** Refresh `git_status` into the store (shared by the environment panel + the
105
+ * file tree change badges). */
106
+ refreshGitStatus(): Promise<GitStatusResult>;
107
+ /** `git diff [path]` — the code viewer fetches this on demand (component-local). */
108
+ gitDiff(path?: string): Promise<GitDiffResult>;
109
+ /** Shared request/response helper for the read-only queries. */
110
+ private query;
111
+ /** Close the transport for good — a USER close, so NO reconnect follows. */
112
+ close(): void;
113
+ private handleMessage;
114
+ /** A `relay_welcome`: adopt the snapshot, clear backoff, cold-reload if stale,
115
+ * and (re)handshake if this is a fresh/cold session. */
116
+ private onWelcome;
117
+ private doHandshake;
118
+ private coldReload;
119
+ private onSessionUpdate;
120
+ private onPermissionRequest;
121
+ /** (v22) An incoming `_chances/unstable/request_question` — park it for the UI.
122
+ * Idempotent on re-send (M2), mirroring the permission ask. */
123
+ private onQuestionRequest;
124
+ private onExtEvent;
125
+ /** A `_chances/unstable/control` notification (a lease handoff / state change):
126
+ * recompute whether THIS client may drive. */
127
+ private onControlState;
128
+ /** Derive lease UI state: this client may drive when the lease is OFF (everyone
129
+ * drives) OR it holds the lease (`holder === clientId`). */
130
+ private leaseStateFrom;
131
+ private onResponse;
132
+ /** The `session/prompt` response folds the turn end (chances `result` frame):
133
+ * clear busy + any pending permission, record stopReason/usage, commit the VM. */
134
+ private onTurnEnd;
135
+ private onTransportDown;
136
+ private scheduleReconnect;
137
+ private toTerminal;
138
+ private rejectWaiters;
139
+ private sessionId;
140
+ private nextId;
141
+ private ownsId;
142
+ private sendRaw;
143
+ private expectResponse;
144
+ }
145
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client-core/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,aAAa,EAAiB,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAM9F,OAAO,KAAK,EAAE,YAAY,EAAwB,MAAM,qBAAqB,CAAC;AAM9E,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,cAAc,EAEnB,KAAK,cAAc,EACpB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAa,gBAAgB,EAAqB,MAAM,gBAAgB,CAAC;AAErF;yDACyD;AACzD,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;AAElF,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,WA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