@ebowwa/coder 0.7.65 → 0.7.68

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.
package/dist/index.js CHANGED
@@ -4876,6 +4876,69 @@ function convertToolsToOpenAIFormat(tools) {
4876
4876
  }
4877
4877
  }));
4878
4878
  }
4879
+ function convertMessagesToOpenAIFormat(messages) {
4880
+ const result = [];
4881
+ for (const msg of messages) {
4882
+ if (msg.role === "assistant") {
4883
+ const toolCalls = [];
4884
+ const textParts = [];
4885
+ for (const block of msg.content) {
4886
+ if (block.type === "text") {
4887
+ textParts.push(block.text);
4888
+ } else if (block.type === "tool_use") {
4889
+ toolCalls.push({
4890
+ id: block.id,
4891
+ type: "function",
4892
+ function: {
4893
+ name: block.name,
4894
+ arguments: JSON.stringify(block.input)
4895
+ }
4896
+ });
4897
+ } else if (block.type === "thinking" || block.type === "redacted_thinking") {}
4898
+ }
4899
+ const openAIMsg = {
4900
+ role: "assistant",
4901
+ content: textParts.join(`
4902
+ `) || null
4903
+ };
4904
+ if (toolCalls.length > 0) {
4905
+ openAIMsg.tool_calls = toolCalls;
4906
+ }
4907
+ result.push(openAIMsg);
4908
+ } else if (msg.role === "user") {
4909
+ const textParts = [];
4910
+ const toolResults = [];
4911
+ for (const block of msg.content) {
4912
+ if (block.type === "text") {
4913
+ textParts.push(block.text);
4914
+ } else if (block.type === "tool_result") {
4915
+ const contentStr = typeof block.content === "string" ? block.content : block.content.map((c) => c.type === "text" ? c.text : JSON.stringify(c)).join(`
4916
+ `);
4917
+ toolResults.push({
4918
+ tool_use_id: block.tool_use_id,
4919
+ content: contentStr,
4920
+ is_error: block.is_error
4921
+ });
4922
+ }
4923
+ }
4924
+ if (textParts.length > 0) {
4925
+ result.push({
4926
+ role: "user",
4927
+ content: textParts.join(`
4928
+ `)
4929
+ });
4930
+ }
4931
+ for (const tr of toolResults) {
4932
+ result.push({
4933
+ role: "tool",
4934
+ tool_call_id: tr.tool_use_id,
4935
+ content: tr.content
4936
+ });
4937
+ }
4938
+ }
4939
+ }
4940
+ return result;
4941
+ }
4879
4942
  function calculateCost2(model, usage) {
4880
4943
  return calculateCost(model, usage);
4881
4944
  }
@@ -5126,11 +5189,69 @@ async function executeStreamAttempt(request, headers, apiEndpoint, signal, model
5126
5189
  firstToken = false;
5127
5190
  }
5128
5191
  }
5192
+ if (choice?.delta?.tool_calls && Array.isArray(choice.delta.tool_calls)) {
5193
+ for (const toolCallDelta of choice.delta.tool_calls) {
5194
+ const index = toolCallDelta.index ?? 0;
5195
+ const toolCallId = toolCallDelta.id;
5196
+ if (toolCallId) {
5197
+ if (currentToolUseBlock) {
5198
+ try {
5199
+ currentToolUseBlock.input = JSON.parse(toolUseInput);
5200
+ } catch {
5201
+ currentToolUseBlock.input = {};
5202
+ }
5203
+ currentContent.push(currentToolUseBlock);
5204
+ callbacks.onToolUse?.({
5205
+ id: currentToolUseBlock.id,
5206
+ name: currentToolUseBlock.name,
5207
+ input: currentToolUseBlock.input
5208
+ });
5209
+ }
5210
+ currentToolUseBlock = {
5211
+ type: "tool_use",
5212
+ id: toolCallId,
5213
+ name: toolCallDelta.function?.name || "",
5214
+ input: {}
5215
+ };
5216
+ toolUseInput = "";
5217
+ if (firstToken) {
5218
+ ttft = Date.now() - startTime;
5219
+ firstToken = false;
5220
+ }
5221
+ }
5222
+ if (toolCallDelta.function?.arguments && currentToolUseBlock) {
5223
+ toolUseInput += toolCallDelta.function.arguments;
5224
+ }
5225
+ }
5226
+ }
5129
5227
  if (choice?.finish_reason) {
5130
5228
  if (currentTextBlock) {
5131
5229
  currentContent.push(currentTextBlock);
5132
5230
  currentTextBlock = null;
5133
5231
  }
5232
+ if (currentToolUseBlock) {
5233
+ try {
5234
+ currentToolUseBlock.input = JSON.parse(toolUseInput);
5235
+ } catch {
5236
+ currentToolUseBlock.input = {};
5237
+ }
5238
+ currentContent.push(currentToolUseBlock);
5239
+ callbacks.onToolUse?.({
5240
+ id: currentToolUseBlock.id,
5241
+ name: currentToolUseBlock.name,
5242
+ input: currentToolUseBlock.input
5243
+ });
5244
+ currentToolUseBlock = null;
5245
+ toolUseInput = "";
5246
+ }
5247
+ let stopReason = "end_turn";
5248
+ if (choice.finish_reason === "tool_calls" || choice.finish_reason === "function_call") {
5249
+ stopReason = "tool_use";
5250
+ } else if (choice.finish_reason === "length") {
5251
+ stopReason = "max_tokens";
5252
+ } else if (choice.finish_reason === "stop") {
5253
+ stopReason = "end_turn";
5254
+ }
5134
5255
  if (!message) {
5135
5256
  message = {
5136
5257
  id: `msg-${Date.now()}`,
@@ -5138,12 +5259,12 @@ async function executeStreamAttempt(request, headers, apiEndpoint, signal, model
5138
5259
  role: "assistant",
5139
5260
  content: currentContent,
5140
5261
  model,
5141
- stop_reason: choice.finish_reason === "stop" ? "end_turn" : choice.finish_reason === "length" ? "max_tokens" : "end_turn",
5262
+ stop_reason: stopReason,
5142
5263
  stop_sequence: null,
5143
5264
  usage: { input_tokens: 0, output_tokens: 0 }
5144
5265
  };
5145
5266
  } else {
5146
- message.stop_reason = choice.finish_reason === "stop" ? "end_turn" : choice.finish_reason === "length" ? "max_tokens" : "end_turn";
5267
+ message.stop_reason = stopReason;
5147
5268
  }
5148
5269
  }
5149
5270
  }
@@ -5211,20 +5332,6 @@ async function createMessageStream(messages, options) {
5211
5332
  signal
5212
5333
  } = options;
5213
5334
  const startTime = Date.now();
5214
- const cachedMessages = buildCachedMessages(messages, cacheConfig);
5215
- const cachedSystemPrompt = buildSystemPrompt(systemPrompt, cacheConfig);
5216
- const request = {
5217
- model,
5218
- max_tokens: maxTokens,
5219
- messages: cachedMessages.map((m) => ({
5220
- role: m.role,
5221
- content: m.content
5222
- })),
5223
- stream: true
5224
- };
5225
- if (cachedSystemPrompt) {
5226
- request.system = cachedSystemPrompt;
5227
- }
5228
5335
  const providerInfo = resolveProvider(model);
5229
5336
  let apiEndpoint;
5230
5337
  let headers;
@@ -5254,6 +5361,37 @@ async function createMessageStream(messages, options) {
5254
5361
  "anthropic-version": "2023-06-01"
5255
5362
  };
5256
5363
  }
5364
+ const cachedMessages = buildCachedMessages(messages, cacheConfig);
5365
+ const cachedSystemPrompt = buildSystemPrompt(systemPrompt, cacheConfig);
5366
+ let requestMessages;
5367
+ if (apiFormat === "openai") {
5368
+ const openAIMessages = convertMessagesToOpenAIFormat(cachedMessages);
5369
+ if (cachedSystemPrompt) {
5370
+ const systemText = typeof cachedSystemPrompt === "string" ? cachedSystemPrompt : cachedSystemPrompt.map((b) => b.text).join(`
5371
+
5372
+ `);
5373
+ requestMessages = [
5374
+ { role: "system", content: systemText },
5375
+ ...openAIMessages
5376
+ ];
5377
+ } else {
5378
+ requestMessages = openAIMessages;
5379
+ }
5380
+ } else {
5381
+ requestMessages = cachedMessages.map((m) => ({
5382
+ role: m.role,
5383
+ content: m.content
5384
+ }));
5385
+ }
5386
+ const request = {
5387
+ model,
5388
+ max_tokens: maxTokens,
5389
+ messages: requestMessages,
5390
+ stream: true
5391
+ };
5392
+ if (cachedSystemPrompt && apiFormat === "anthropic") {
5393
+ request.system = cachedSystemPrompt;
5394
+ }
5257
5395
  if (tools && tools.length > 0) {
5258
5396
  if (apiFormat === "openai") {
5259
5397
  request.tools = convertToolsToOpenAIFormat(tools);
@@ -33333,13 +33471,20 @@ var init_input_handler = __esm(() => {
33333
33471
  isShiftDown(event) {
33334
33472
  return event.code === "down" || event.code === "Down";
33335
33473
  },
33474
+ isSpace(event) {
33475
+ return event.code === " " || event.code === "Space";
33476
+ },
33336
33477
  isPrintable(event) {
33337
33478
  if (event.is_special)
33338
33479
  return false;
33339
33480
  const code = event.code;
33481
+ if (code === " " || code === "Space")
33482
+ return true;
33340
33483
  return code.length === 1 && !event.ctrl;
33341
33484
  },
33342
33485
  getChar(event) {
33486
+ if (event.code === "Space")
33487
+ return " ";
33343
33488
  return event.code;
33344
33489
  }
33345
33490
  };
@@ -34224,6 +34369,84 @@ var init_stream_highlighter = __esm(() => {
34224
34369
  };
34225
34370
  });
34226
34371
 
34372
+ // packages/src/interfaces/ui/terminal/cli/interactive/scroll-handler.ts
34373
+ function handleScrollEvent(event, currentOffset, totalMessages, config = {}) {
34374
+ const { pageScrollAmount, lineScrollAmount } = { ...DEFAULT_SCROLL_CONFIG, ...config };
34375
+ const maxScroll = Math.max(0, Math.max(totalMessages - 1, 10));
34376
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34377
+ console.error("[ScrollHandler] Event:", {
34378
+ code: event.code,
34379
+ shift: event.shift,
34380
+ ctrl: event.ctrl,
34381
+ alt: event.alt,
34382
+ is_special: event.is_special
34383
+ });
34384
+ }
34385
+ if (KeyEvents.isPageUp(event)) {
34386
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34387
+ console.error("[ScrollHandler] PageUp detected, scrolling up");
34388
+ }
34389
+ return { handled: true, newOffset: Math.min(currentOffset + pageScrollAmount, maxScroll) };
34390
+ }
34391
+ if (KeyEvents.isPageDown(event)) {
34392
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34393
+ console.error("[ScrollHandler] PageDown detected, scrolling down");
34394
+ }
34395
+ return { handled: true, newOffset: Math.max(0, currentOffset - pageScrollAmount) };
34396
+ }
34397
+ if (KeyEvents.isHome(event)) {
34398
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34399
+ console.error("[ScrollHandler] Home detected, resetting to bottom");
34400
+ }
34401
+ return { handled: true, newOffset: 0 };
34402
+ }
34403
+ if (KeyEvents.isUp(event) && event.alt) {
34404
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34405
+ console.error("[ScrollHandler] Alt+Up detected, scrolling up");
34406
+ }
34407
+ return { handled: true, newOffset: Math.min(currentOffset + lineScrollAmount, maxScroll) };
34408
+ }
34409
+ if (KeyEvents.isDown(event) && event.alt) {
34410
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34411
+ console.error("[ScrollHandler] Alt+Down detected, scrolling down");
34412
+ }
34413
+ return { handled: true, newOffset: Math.max(0, currentOffset - lineScrollAmount) };
34414
+ }
34415
+ if (KeyEvents.isUp(event) && event.ctrl) {
34416
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34417
+ console.error("[ScrollHandler] Ctrl+Up detected, scrolling up");
34418
+ }
34419
+ return { handled: true, newOffset: Math.min(currentOffset + lineScrollAmount, maxScroll) };
34420
+ }
34421
+ if (KeyEvents.isDown(event) && event.ctrl) {
34422
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34423
+ console.error("[ScrollHandler] Ctrl+Down detected, scrolling down");
34424
+ }
34425
+ return { handled: true, newOffset: Math.max(0, currentOffset - lineScrollAmount) };
34426
+ }
34427
+ if (KeyEvents.isUp(event) && event.shift) {
34428
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34429
+ console.error("[ScrollHandler] Shift+Up detected, scrolling up");
34430
+ }
34431
+ return { handled: true, newOffset: Math.min(currentOffset + lineScrollAmount, maxScroll) };
34432
+ }
34433
+ if (KeyEvents.isDown(event) && event.shift) {
34434
+ if (process.env.CODER_DEBUG_SCROLL === "1") {
34435
+ console.error("[ScrollHandler] Shift+Down detected, scrolling down");
34436
+ }
34437
+ return { handled: true, newOffset: Math.max(0, currentOffset - lineScrollAmount) };
34438
+ }
34439
+ return { handled: false, newOffset: currentOffset };
34440
+ }
34441
+ var DEFAULT_SCROLL_CONFIG;
34442
+ var init_scroll_handler = __esm(() => {
34443
+ init_input_handler();
34444
+ DEFAULT_SCROLL_CONFIG = {
34445
+ pageScrollAmount: 3,
34446
+ lineScrollAmount: 1
34447
+ };
34448
+ });
34449
+
34227
34450
  // packages/src/interfaces/ui/terminal/cli/interactive/interactive-runner.ts
34228
34451
  import process10 from "process";
34229
34452
  function createInitialState() {
@@ -34374,6 +34597,23 @@ class InteractiveRunner {
34374
34597
  this.state = { ...this.state, inputValue: "", cursorPos: 0 };
34375
34598
  return true;
34376
34599
  }
34600
+ if (process10.env.CODER_DEBUG_SCROLL === "1") {
34601
+ console.error("[InteractiveRunner] Key event:", {
34602
+ code: event.code,
34603
+ shift: event.shift,
34604
+ ctrl: event.ctrl,
34605
+ alt: event.alt,
34606
+ is_special: event.is_special
34607
+ });
34608
+ }
34609
+ const scrollResult = handleScrollEvent(event, this.state.scrollOffset, this.messageStore.messages.length);
34610
+ if (process10.env.CODER_DEBUG_SCROLL === "1") {
34611
+ console.error("[InteractiveRunner] Scroll result:", scrollResult);
34612
+ }
34613
+ if (scrollResult.handled) {
34614
+ this.state = { ...this.state, scrollOffset: scrollResult.newOffset };
34615
+ return true;
34616
+ }
34377
34617
  if (KeyEvents.isUp(event)) {
34378
34618
  return this._handleHistoryUp();
34379
34619
  }
@@ -34671,18 +34911,19 @@ class InteractiveRunner {
34671
34911
  })) : [];
34672
34912
  return {
34673
34913
  messages: renderMessages,
34674
- inputValue,
34675
- cursorPos,
34676
- statusText,
34677
- isLoading,
34678
- streamingText,
34914
+ input_value: inputValue,
34915
+ cursor_pos: cursorPos,
34916
+ status_text: statusText,
34917
+ is_loading: isLoading,
34918
+ streaming_text: streamingText,
34679
34919
  model: this.props.model,
34680
- showHelp: helpMode,
34681
- helpText,
34682
- searchMode: sessionSelectMode,
34683
- searchQuery: "",
34684
- searchResults,
34685
- searchSelected: 0
34920
+ show_help: helpMode,
34921
+ help_text: helpText,
34922
+ search_mode: sessionSelectMode,
34923
+ search_query: "",
34924
+ search_results: searchResults,
34925
+ search_selected: 0,
34926
+ scroll_offset: this.state.scrollOffset
34686
34927
  };
34687
34928
  }
34688
34929
  _getHelpText(section) {
@@ -34784,6 +35025,7 @@ var init_interactive_runner = __esm(() => {
34784
35025
  init_native();
34785
35026
  init_spinner_frames();
34786
35027
  init_input_handler();
35028
+ init_scroll_handler();
34787
35029
  init_types4();
34788
35030
  });
34789
35031
 
@@ -34813,10 +35055,10 @@ init_cognitive_security();
34813
35055
  init_source();
34814
35056
  import process9 from "process";
34815
35057
 
34816
- // ../../node_modules/.bun/cli-cursor@5.0.0/node_modules/cli-cursor/index.js
35058
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/index.js
34817
35059
  import process5 from "process";
34818
35060
 
34819
- // ../../node_modules/.bun/restore-cursor@5.1.0/node_modules/restore-cursor/index.js
35061
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/node_modules/restore-cursor/index.js
34820
35062
  import process4 from "process";
34821
35063
 
34822
35064
  // ../../node_modules/.bun/mimic-function@5.0.1/node_modules/mimic-function/index.js
@@ -34865,7 +35107,7 @@ function mimicFunction(to, from, { ignoreNonConfigurable = false } = {}) {
34865
35107
  return to;
34866
35108
  }
34867
35109
 
34868
- // ../../node_modules/.bun/onetime@7.0.0/node_modules/onetime/index.js
35110
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/node_modules/restore-cursor/node_modules/onetime/index.js
34869
35111
  var calledFunctions = new WeakMap;
34870
35112
  var onetime = (function_, options = {}) => {
34871
35113
  if (typeof function_ !== "function") {
@@ -34896,7 +35138,7 @@ onetime.callCount = (function_) => {
34896
35138
  };
34897
35139
  var onetime_default = onetime;
34898
35140
 
34899
- // ../../node_modules/.bun/signal-exit@4.1.0/node_modules/signal-exit/dist/mjs/signals.js
35141
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/node_modules/restore-cursor/node_modules/signal-exit/dist/mjs/signals.js
34900
35142
  var signals = [];
34901
35143
  signals.push("SIGHUP", "SIGINT", "SIGTERM");
34902
35144
  if (process.platform !== "win32") {
@@ -34906,7 +35148,7 @@ if (process.platform === "linux") {
34906
35148
  signals.push("SIGIO", "SIGPOLL", "SIGPWR", "SIGSTKFLT");
34907
35149
  }
34908
35150
 
34909
- // ../../node_modules/.bun/signal-exit@4.1.0/node_modules/signal-exit/dist/mjs/index.js
35151
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/node_modules/restore-cursor/node_modules/signal-exit/dist/mjs/index.js
34910
35152
  var processOk = (process3) => !!process3 && typeof process3 === "object" && typeof process3.removeListener === "function" && typeof process3.emit === "function" && typeof process3.reallyExit === "function" && typeof process3.listeners === "function" && typeof process3.kill === "function" && typeof process3.pid === "number" && typeof process3.on === "function";
34911
35153
  var kExitEmitter = Symbol.for("signal-exit emitter");
34912
35154
  var global = globalThis;
@@ -35104,7 +35346,7 @@ var {
35104
35346
  unload
35105
35347
  } = signalExitWrap(processOk(process3) ? new SignalExit(process3) : new SignalExitFallback);
35106
35348
 
35107
- // ../../node_modules/.bun/restore-cursor@5.1.0/node_modules/restore-cursor/index.js
35349
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/node_modules/restore-cursor/index.js
35108
35350
  var terminal = process4.stderr.isTTY ? process4.stderr : process4.stdout.isTTY ? process4.stdout : undefined;
35109
35351
  var restoreCursor = terminal ? onetime_default(() => {
35110
35352
  onExit(() => {
@@ -35113,7 +35355,7 @@ var restoreCursor = terminal ? onetime_default(() => {
35113
35355
  }) : () => {};
35114
35356
  var restore_cursor_default = restoreCursor;
35115
35357
 
35116
- // ../../node_modules/.bun/cli-cursor@5.0.0/node_modules/cli-cursor/index.js
35358
+ // ../../node_modules/.bun/ora@8.2.0/node_modules/ora/node_modules/cli-cursor/index.js
35117
35359
  var isHidden = false;
35118
35360
  var cliCursor = {};
35119
35361
  cliCursor.show = (writableStream = process5.stderr) => {
@@ -36166,3 +36408,6 @@ export {
36166
36408
  AskUserQuestionTool,
36167
36409
  AnalyzeImageTool
36168
36410
  };
36411
+
36412
+ //# debugId=0DF7A0DEA7CF8E8D64756E2164756E21
36413
+ //# sourceMappingURL=index.js.map