@best-agent/cli 0.0.2-beta.8 → 0.0.2-beta.9

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 (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -181
  3. package/dist/launcher.js +32 -0
  4. package/package.json +15 -27
  5. package/dist/best-agent-native-bridge.d.ts +0 -389
  6. package/dist/best-agent-native-bridge.js +0 -4645
  7. package/dist/cli-storage.d.ts +0 -5
  8. package/dist/cli-storage.js +0 -7
  9. package/dist/dimsdk/ask-user.d.ts +0 -21
  10. package/dist/dimsdk/ask-user.js +0 -131
  11. package/dist/dimsdk/cli/version.d.ts +0 -6
  12. package/dist/dimsdk/cli/version.js +0 -36
  13. package/dist/dimsdk/compat/archships/dim-agent/runtime-host.d.ts +0 -49
  14. package/dist/dimsdk/compat/archships/dim-agent/runtime-host.js +0 -215
  15. package/dist/dimsdk/compat/runtime-host.d.ts +0 -2
  16. package/dist/dimsdk/compat/runtime-host.js +0 -2
  17. package/dist/dimsdk/core/debug-log.d.ts +0 -8
  18. package/dist/dimsdk/core/debug-log.js +0 -100
  19. package/dist/dimsdk/core/env.d.ts +0 -26
  20. package/dist/dimsdk/core/env.js +0 -130
  21. package/dist/dimsdk/core/markdown/sanitize.d.ts +0 -2
  22. package/dist/dimsdk/core/markdown/sanitize.js +0 -43
  23. package/dist/dimsdk/core/markdown/wrap.d.ts +0 -9
  24. package/dist/dimsdk/core/markdown/wrap.js +0 -139
  25. package/dist/dimsdk/core/markdown.d.ts +0 -18
  26. package/dist/dimsdk/core/markdown.js +0 -62
  27. package/dist/dimsdk/core/model.d.ts +0 -273
  28. package/dist/dimsdk/core/model.js +0 -16
  29. package/dist/dimsdk/core/observability/perf-log.d.ts +0 -18
  30. package/dist/dimsdk/core/observability/perf-log.js +0 -236
  31. package/dist/dimsdk/core/store/agent-event-mappers.d.ts +0 -26
  32. package/dist/dimsdk/core/store/agent-event-mappers.js +0 -109
  33. package/dist/dimsdk/core/store/message-mutations.d.ts +0 -105
  34. package/dist/dimsdk/core/store/message-mutations.js +0 -1528
  35. package/dist/dimsdk/core/store/semantic-readers.d.ts +0 -2
  36. package/dist/dimsdk/core/store/semantic-readers.js +0 -4
  37. package/dist/dimsdk/core/store/transcript-projector.d.ts +0 -164
  38. package/dist/dimsdk/core/store/transcript-projector.js +0 -580
  39. package/dist/dimsdk/core/tool-metadata.d.ts +0 -12
  40. package/dist/dimsdk/core/tool-metadata.js +0 -70
  41. package/dist/dimsdk/core/transcript-part-id.d.ts +0 -6
  42. package/dist/dimsdk/core/transcript-part-id.js +0 -22
  43. package/dist/dimsdk/i18n/index.d.ts +0 -18
  44. package/dist/dimsdk/i18n/index.js +0 -141
  45. package/dist/dimsdk/i18n/locales/en.d.ts +0 -988
  46. package/dist/dimsdk/i18n/locales/en.js +0 -987
  47. package/dist/dimsdk/i18n/locales/index.d.ts +0 -1976
  48. package/dist/dimsdk/i18n/locales/index.js +0 -8
  49. package/dist/dimsdk/i18n/locales/zh-CN.d.ts +0 -988
  50. package/dist/dimsdk/i18n/locales/zh-CN.js +0 -987
  51. package/dist/dimsdk/i18n/types.d.ts +0 -13
  52. package/dist/dimsdk/i18n/types.js +0 -2
  53. package/dist/dimsdk/shared/value-normalizers.d.ts +0 -7
  54. package/dist/dimsdk/shared/value-normalizers.js +0 -48
  55. package/dist/dimsdk-tui.bundle-size.json +0 -169
  56. package/dist/dimsdk-tui.bundle.js +0 -330756
  57. package/dist/goatchain-adapter.d.ts +0 -41
  58. package/dist/goatchain-adapter.js +0 -499
  59. package/dist/index.d.ts +0 -3
  60. package/dist/index.js +0 -1405
  61. package/dist/native-runtime-bridge.d.ts +0 -387
  62. package/dist/native-runtime-bridge.js +0 -4624
  63. package/dist/provider-config-env.d.ts +0 -7
  64. package/dist/provider-config-env.js +0 -286
  65. package/dist/provider-model-limit-index.generated.d.ts +0 -3
  66. package/dist/provider-model-limit-index.generated.js +0 -7818
  67. package/dist/provider-model-limits.d.ts +0 -8
  68. package/dist/provider-model-limits.js +0 -43
  69. package/dist/runtime.d.ts +0 -47
  70. package/dist/runtime.js +0 -516
@@ -1,41 +0,0 @@
1
- import type { AgentRuntime } from "@best-agent/engine";
2
- import { type ThreadDetail, type ThreadId, type ThreadMode } from "@best-agent/protocol";
3
- import { type BridgeMessage, createBestAgentNativeBridge, type BestAgentSessionsApi } from "./best-agent-native-bridge.js";
4
- type ACPMessage = Readonly<{
5
- content?: unknown;
6
- metadata?: Record<string, unknown>;
7
- reasoning_content?: string;
8
- role: "assistant" | "tool" | "user";
9
- tool_call_id?: string;
10
- tool_calls?: ReadonlyArray<{
11
- function: {
12
- arguments: string;
13
- name: string;
14
- };
15
- id: string;
16
- type: "function";
17
- }>;
18
- }>;
19
- type NativeBridge = ReturnType<typeof createBestAgentNativeBridge>;
20
- type NativeAgent = NativeBridge["agent"];
21
- type GoatChainBridgeAgent = NativeAgent & Readonly<{
22
- receiveMessage: (messages: readonly unknown[], metadata?: Record<string, unknown>) => AsyncIterable<ACPMessage>;
23
- }>;
24
- export type GoatChainSessionsApi = BestAgentSessionsApi;
25
- export declare function resolveInitialGoatChainThread(runtime: AgentRuntime, options: Readonly<{
26
- latest?: boolean;
27
- resumeThreadId?: string;
28
- threadMode?: ThreadMode;
29
- }>): Promise<ThreadDetail>;
30
- export declare function createGoatChainBridge(options: Readonly<{
31
- agentProfile?: string;
32
- runtime: AgentRuntime;
33
- threadMode?: ThreadMode;
34
- }>): Readonly<{
35
- agent: GoatChainBridgeAgent;
36
- attachStore: (store: unknown) => void;
37
- hydrateThread: (threadId: ThreadId) => Promise<BridgeMessage[]>;
38
- sessions: GoatChainSessionsApi;
39
- }>;
40
- export {};
41
- //# sourceMappingURL=goatchain-adapter.d.ts.map
@@ -1,499 +0,0 @@
1
- import { extractPromptText, normalizePromptContentParts, } from "@best-agent/protocol";
2
- import { createBestAgentNativeBridge, resolveInitialBestAgentThread, } from "./best-agent-native-bridge.js";
3
- const DEFAULT_ASSISTANT_TEXT_STREAM_ID = "__default_assistant_text__";
4
- function createAsyncQueue(opts) {
5
- const state = {
6
- closed: false,
7
- pending: [],
8
- waiters: [],
9
- };
10
- let closedNotified = false;
11
- const notifyClosed = () => {
12
- if (closedNotified)
13
- return;
14
- closedNotified = true;
15
- opts?.onClose?.();
16
- };
17
- return {
18
- close() {
19
- if (state.closed)
20
- return;
21
- state.closed = true;
22
- while (state.waiters.length > 0)
23
- state.waiters.shift()?.({ done: true, value: undefined });
24
- notifyClosed();
25
- },
26
- push(value) {
27
- if (state.closed)
28
- return;
29
- const waiter = state.waiters.shift();
30
- if (waiter) {
31
- waiter({ done: false, value });
32
- return;
33
- }
34
- state.pending.push(value);
35
- },
36
- [Symbol.asyncIterator]() {
37
- return {
38
- next() {
39
- if (state.pending.length > 0) {
40
- return Promise.resolve({
41
- done: false,
42
- value: state.pending.shift(),
43
- });
44
- }
45
- if (state.closed)
46
- return Promise.resolve({ done: true, value: undefined });
47
- return new Promise((resolve) => {
48
- state.waiters.push(resolve);
49
- });
50
- },
51
- return() {
52
- if (!state.closed) {
53
- state.closed = true;
54
- while (state.waiters.length > 0)
55
- state.waiters.shift()?.({ done: true, value: undefined });
56
- }
57
- notifyClosed();
58
- return Promise.resolve({ done: true, value: undefined });
59
- },
60
- };
61
- },
62
- };
63
- }
64
- function normalizeText(value) {
65
- return String(value ?? "").trim();
66
- }
67
- function asRecord(value) {
68
- if (!value || typeof value !== "object" || Array.isArray(value))
69
- return {};
70
- return value;
71
- }
72
- function serializeUnknown(value) {
73
- if (typeof value === "string")
74
- return value;
75
- try {
76
- return JSON.stringify(value);
77
- }
78
- catch {
79
- return String(value);
80
- }
81
- }
82
- function makeDoneMessage(input) {
83
- return {
84
- content: "",
85
- metadata: {
86
- done: true,
87
- ...(input.error ? { error: input.error } : {}),
88
- stopReason: input.stopReason,
89
- },
90
- role: "assistant",
91
- };
92
- }
93
- function makeAssistantStreamMessage(content, originalEventType, metadata = {}) {
94
- return {
95
- content,
96
- metadata: {
97
- ...metadata,
98
- originalEventType,
99
- },
100
- role: "assistant",
101
- };
102
- }
103
- function extractLatestUserPrompt(messages) {
104
- for (let index = messages.length - 1; index >= 0; index -= 1) {
105
- const message = asRecord(messages[index]);
106
- if (normalizeText(message.role) !== "user")
107
- continue;
108
- const prompt = extractPromptText(message.content);
109
- if (prompt)
110
- return prompt;
111
- }
112
- return "";
113
- }
114
- function extractLatestUserPromptContent(messages) {
115
- for (let index = messages.length - 1; index >= 0; index -= 1) {
116
- const message = asRecord(messages[index]);
117
- if (normalizeText(message.role) !== "user")
118
- continue;
119
- const content = normalizePromptContentParts(message.content);
120
- if (content?.length)
121
- return content;
122
- }
123
- return undefined;
124
- }
125
- function readAbortSignal(metadata) {
126
- const signal = metadata.signal ?? metadata.abortSignal;
127
- return signal instanceof AbortSignal ? signal : undefined;
128
- }
129
- function serializeToolArguments(input) {
130
- if (Object.keys(input).length === 1 && typeof input._raw === "string") {
131
- return input._raw;
132
- }
133
- try {
134
- return JSON.stringify(input);
135
- }
136
- catch {
137
- return "";
138
- }
139
- }
140
- function toolOutputFromEvent(output) {
141
- const record = asRecord(output);
142
- const structuredContent = asRecord(record.structuredContent);
143
- const value = "output" in record ? record.output : output;
144
- return {
145
- content: serializeUnknown(value),
146
- ...(Object.keys(structuredContent).length > 0 ? { structuredContent } : {}),
147
- };
148
- }
149
- function errorMessage(error) {
150
- if (error instanceof Error)
151
- return error.message;
152
- return normalizeText(error) || "Unknown error";
153
- }
154
- async function openOrEnsureSession(agent, sessionId) {
155
- const openSession = agent.openSession;
156
- if (typeof openSession === "function") {
157
- const existing = await openSession(sessionId);
158
- if (existing)
159
- return existing;
160
- }
161
- const ensureSession = agent.ensureSession;
162
- if (typeof ensureSession === "function")
163
- return await ensureSession({ sessionId });
164
- return null;
165
- }
166
- export async function resolveInitialGoatChainThread(runtime, options) {
167
- return await resolveInitialBestAgentThread(runtime, options);
168
- }
169
- export function createGoatChainBridge(options) {
170
- const bridge = createBestAgentNativeBridge(options);
171
- const pendingActions = new Map();
172
- const receiveMessageStream = async function* (messages, rawMetadata = {}) {
173
- const metadata = rawMetadata;
174
- const sessionId = normalizeText(metadata.session_id);
175
- if (!sessionId) {
176
- yield makeDoneMessage({
177
- error: "Missing session_id for GoatChain chat.",
178
- stopReason: "failed",
179
- });
180
- return;
181
- }
182
- const session = await openOrEnsureSession(bridge.agent, sessionId);
183
- if (!session) {
184
- yield makeDoneMessage({
185
- error: "Session bridge is unavailable.",
186
- stopReason: "failed",
187
- });
188
- return;
189
- }
190
- const pending = pendingActions.get(sessionId);
191
- const signal = readAbortSignal(metadata);
192
- let stream;
193
- if (pending) {
194
- if (pending.kind === "tool_approval") {
195
- const decision = pending.toolCallId
196
- ? metadata.toolContext?.approval?.decisions?.[pending.toolCallId]
197
- : undefined;
198
- if (!decision) {
199
- yield makeDoneMessage({
200
- error: `Missing approval decision for ${pending.toolCallId ?? pending.actionId}.`,
201
- stopReason: "failed",
202
- });
203
- return;
204
- }
205
- stream = session.resume({
206
- actionId: pending.actionId,
207
- alwaysAllowForConversation: metadata.toolContext?.approval?.alwaysAllowForConversation,
208
- approved: decision.approved,
209
- kind: "tool_approval",
210
- ...(decision.reason ? { reason: decision.reason } : {}),
211
- ...(pending.toolCallId ? { toolCallId: pending.toolCallId } : {}),
212
- }, { signal });
213
- }
214
- else {
215
- const answers = pending.toolCallId
216
- ? metadata.toolContext?.askUser?.answers?.[pending.toolCallId]
217
- : undefined;
218
- const rejectedReason = pending.toolCallId
219
- ? metadata.toolContext?.askUser?.rejected?.[pending.toolCallId]
220
- : undefined;
221
- if (!answers && !rejectedReason) {
222
- yield makeDoneMessage({
223
- error: `Missing ask-user answers for ${pending.toolCallId ?? pending.actionId}.`,
224
- stopReason: "failed",
225
- });
226
- return;
227
- }
228
- stream = session.resume({
229
- actionId: pending.actionId,
230
- answers: { ...answers },
231
- kind: "ask_user",
232
- ...(pending.toolCallId ? { toolCallId: pending.toolCallId } : {}),
233
- ...(rejectedReason ? { reason: rejectedReason, rejected: true } : {}),
234
- }, { signal });
235
- }
236
- }
237
- else {
238
- const prompt = extractLatestUserPrompt(messages);
239
- const promptContent = extractLatestUserPromptContent(messages);
240
- const attach = metadata.resume === true;
241
- if (!prompt && !promptContent?.length && !attach) {
242
- yield makeDoneMessage({
243
- error: "Missing user prompt.",
244
- stopReason: "failed",
245
- });
246
- return;
247
- }
248
- stream = session.run({
249
- content: promptContent?.length ? promptContent : prompt,
250
- ...(attach ? { metadata: { resume: true } } : {}),
251
- }, { signal });
252
- }
253
- const streamedAssistantTextByStreamId = new Map();
254
- const readStreamedAssistantText = () => [...streamedAssistantTextByStreamId.values()].join("");
255
- const setAssistantStreamText = (streamId, content) => {
256
- streamedAssistantTextByStreamId.set(streamId || DEFAULT_ASSISTANT_TEXT_STREAM_ID, content);
257
- };
258
- try {
259
- for await (const event of stream) {
260
- const record = asRecord(event);
261
- const eventType = normalizeText(record.type);
262
- if (eventType === "assistant.message") {
263
- const message = asRecord(record.message);
264
- const content = normalizeText(message.content);
265
- const thinking = normalizeText(message.thinking);
266
- if (thinking) {
267
- yield {
268
- ...(content ? { content } : {}),
269
- reasoning_content: thinking,
270
- role: "assistant",
271
- };
272
- if (!content)
273
- continue;
274
- }
275
- if (content) {
276
- if (readStreamedAssistantText() && content === readStreamedAssistantText()) {
277
- continue;
278
- }
279
- streamedAssistantTextByStreamId.clear();
280
- setAssistantStreamText(undefined, content);
281
- yield makeAssistantStreamMessage(content, "text_end", {
282
- settled: true,
283
- });
284
- }
285
- continue;
286
- }
287
- if (eventType === "assistant.delta") {
288
- const delta = String(record.delta ?? "");
289
- if (delta) {
290
- const streamId = normalizeText(record.streamId);
291
- const resolvedStreamId = streamId || DEFAULT_ASSISTANT_TEXT_STREAM_ID;
292
- streamedAssistantTextByStreamId.set(resolvedStreamId, `${streamedAssistantTextByStreamId.get(resolvedStreamId) ?? ""}${delta}`);
293
- yield makeAssistantStreamMessage(delta, "text_delta", streamId ? { streamId } : {});
294
- }
295
- continue;
296
- }
297
- if (eventType === "assistant.reset") {
298
- const streamId = normalizeText(record.streamId);
299
- if (streamId) {
300
- if (streamedAssistantTextByStreamId.has(streamId)) {
301
- streamedAssistantTextByStreamId.set(streamId, "");
302
- }
303
- }
304
- else {
305
- streamedAssistantTextByStreamId.clear();
306
- }
307
- yield {
308
- metadata: {
309
- originalEventType: "text_reset",
310
- ...(streamId ? { streamId } : {}),
311
- },
312
- role: "assistant",
313
- };
314
- continue;
315
- }
316
- if (eventType === "thinking.started") {
317
- yield {
318
- metadata: { originalEventType: "thinking_start" },
319
- role: "assistant",
320
- };
321
- continue;
322
- }
323
- if (eventType === "thinking.delta") {
324
- const delta = String(record.delta ?? "");
325
- yield {
326
- metadata: { originalEventType: "thinking_delta" },
327
- reasoning_content: delta,
328
- role: "assistant",
329
- };
330
- continue;
331
- }
332
- if (eventType === "thinking.finished") {
333
- yield {
334
- metadata: { originalEventType: "thinking_end" },
335
- reasoning_content: normalizeText(record.content),
336
- role: "assistant",
337
- };
338
- continue;
339
- }
340
- if (eventType === "tool.started") {
341
- if (normalizeText(record.subagentId)) {
342
- continue;
343
- }
344
- const toolName = normalizeText(record.toolName) || "tool";
345
- const toolCallId = normalizeText(record.toolCallId) || "tool";
346
- const toolInput = asRecord(record.input);
347
- yield {
348
- metadata: record.originalEventType
349
- ? { originalEventType: record.originalEventType }
350
- : undefined,
351
- role: "assistant",
352
- tool_calls: [
353
- {
354
- function: {
355
- arguments: serializeToolArguments(toolInput),
356
- name: toolName,
357
- },
358
- id: toolCallId,
359
- type: "function",
360
- },
361
- ],
362
- };
363
- continue;
364
- }
365
- if (eventType === "tool.finished") {
366
- if (normalizeText(record.subagentId)) {
367
- continue;
368
- }
369
- const toolCallId = normalizeText(record.toolCallId) || "tool";
370
- const output = toolOutputFromEvent(record.output);
371
- yield {
372
- content: output.content,
373
- metadata: output.structuredContent
374
- ? { structuredContent: output.structuredContent }
375
- : undefined,
376
- role: "tool",
377
- tool_call_id: toolCallId,
378
- };
379
- continue;
380
- }
381
- if (eventType === "usage.updated") {
382
- yield {
383
- metadata: {
384
- ...(record.lastRequestBudget ? { lastRequestBudget: record.lastRequestBudget } : {}),
385
- originalEventType: "usage_refresh",
386
- ...(record.usage ? { usage: record.usage } : {}),
387
- },
388
- role: "assistant",
389
- };
390
- continue;
391
- }
392
- if (eventType === "action.required") {
393
- const kind = normalizeText(record.kind);
394
- if (kind === "tool_approval") {
395
- const toolCallId = normalizeText(record.toolCallId);
396
- pendingActions.set(sessionId, {
397
- actionId: normalizeText(record.actionId),
398
- kind: "tool_approval",
399
- toolCallId: toolCallId || undefined,
400
- toolName: normalizeText(record.toolName) || "tool",
401
- });
402
- yield {
403
- content: normalizeText(record.reason) || "Approval requested.",
404
- metadata: {
405
- approval_requested: true,
406
- available_scopes: Array.isArray(record.availableScopes)
407
- ? record.availableScopes
408
- : undefined,
409
- permission: record.permission,
410
- requires_action: true,
411
- risk_level: record.riskLevel,
412
- tool_call_id: toolCallId,
413
- tool_name: record.toolName,
414
- },
415
- role: "assistant",
416
- };
417
- yield makeDoneMessage({ stopReason: "approval_required" });
418
- return;
419
- }
420
- if (kind === "ask_user") {
421
- const toolCallId = normalizeText(record.toolCallId);
422
- pendingActions.set(sessionId, {
423
- actionId: normalizeText(record.actionId),
424
- kind: "ask_user",
425
- questions: record.questions ?? [],
426
- toolCallId: toolCallId || undefined,
427
- toolName: normalizeText(record.toolName) || "ask-user",
428
- });
429
- yield {
430
- content: "",
431
- metadata: {
432
- kind: "ask_user",
433
- questions: record.questions,
434
- requires_action: true,
435
- tool_call_id: toolCallId,
436
- tool_name: record.toolName,
437
- },
438
- role: "assistant",
439
- };
440
- yield makeDoneMessage({ stopReason: "action_required" });
441
- return;
442
- }
443
- continue;
444
- }
445
- if (eventType === "run.finished") {
446
- pendingActions.delete(sessionId);
447
- yield makeDoneMessage({
448
- stopReason: record.status === "interrupted" ? "interrupted" : "completed",
449
- });
450
- return;
451
- }
452
- if (eventType === "run.error") {
453
- pendingActions.delete(sessionId);
454
- yield makeDoneMessage({
455
- error: errorMessage(record.error),
456
- stopReason: "failed",
457
- });
458
- return;
459
- }
460
- }
461
- }
462
- catch (error) {
463
- pendingActions.delete(sessionId);
464
- yield makeDoneMessage({
465
- error: errorMessage(error),
466
- stopReason: "failed",
467
- });
468
- }
469
- };
470
- const agent = {
471
- ...bridge.agent,
472
- receiveMessage(messages, rawMetadata = {}) {
473
- const source = receiveMessageStream(messages, rawMetadata);
474
- const queue = createAsyncQueue({
475
- onClose: () => {
476
- void source.return?.();
477
- },
478
- });
479
- void (async () => {
480
- try {
481
- for await (const message of source)
482
- queue.push(message);
483
- }
484
- finally {
485
- queue.close();
486
- }
487
- })();
488
- return queue;
489
- },
490
- };
491
- return {
492
- ...bridge,
493
- attachStore(store) {
494
- bridge.attachStore(store);
495
- },
496
- agent,
497
- };
498
- }
499
- //# sourceMappingURL=goatchain-adapter.js.map
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=index.d.ts.map