@meetopenbot/claude-code 0.1.8 → 0.1.10

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 (2) hide show
  1. package/dist/index.js +107 -48
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -19371,18 +19371,25 @@ function uiWidget(args) {
19371
19371
 
19372
19372
  // credits-auth.ts
19373
19373
  var INTEGRATIONS_TOKEN_HEADER = "x-openbot-integrations-token";
19374
+ var CREDITS_API_KEY_PLACEHOLDER = "openbot-credits";
19375
+ var resolveCreditsAuthConfig = () => {
19376
+ const baseUrl = process.env.OPENBOT_INTEGRATIONS_BASE_URL?.trim();
19377
+ const token = process.env.OPENBOT_INTEGRATIONS_TOKEN?.trim();
19378
+ if (!baseUrl || !token) return void 0;
19379
+ return { baseUrl: baseUrl.replace(/\/$/, ""), token };
19380
+ };
19374
19381
  var buildCreditsAnthropicEnv = () => {
19375
- const integrationsBaseUrl = process.env.OPENBOT_INTEGRATIONS_BASE_URL?.trim();
19376
- const integrationsToken = process.env.OPENBOT_INTEGRATIONS_TOKEN?.trim();
19377
- if (!integrationsBaseUrl || !integrationsToken) return void 0;
19378
- const env = {
19379
- ANTHROPIC_BASE_URL: `${integrationsBaseUrl.replace(/\/$/, "")}/anthropic/v1`,
19380
- ANTHROPIC_CUSTOM_HEADERS: `${INTEGRATIONS_TOKEN_HEADER}: ${integrationsToken}`
19382
+ const config = resolveCreditsAuthConfig();
19383
+ if (!config) return void 0;
19384
+ const apiKey = config.token || CREDITS_API_KEY_PLACEHOLDER;
19385
+ return {
19386
+ ANTHROPIC_BASE_URL: `${config.baseUrl}/anthropic/v1`,
19387
+ ANTHROPIC_CUSTOM_HEADERS: `${INTEGRATIONS_TOKEN_HEADER}: ${config.token}`,
19388
+ ANTHROPIC_API_KEY: apiKey,
19389
+ // Prevent the CLI from preferring stored OAuth over gateway routing.
19390
+ CLAUDE_CODE_OAUTH_TOKEN: "",
19391
+ ANTHROPIC_AUTH_TOKEN: ""
19381
19392
  };
19382
- if (!process.env.ANTHROPIC_API_KEY?.trim()) {
19383
- env.ANTHROPIC_API_KEY = "openbot-credits";
19384
- }
19385
- return env;
19386
19393
  };
19387
19394
 
19388
19395
  // runtime.ts
@@ -19424,12 +19431,27 @@ var isCreditsErrorMessage = (message) => {
19424
19431
  const lower = message.toLowerCase();
19425
19432
  return lower.includes("insufficient_credits") || lower.includes("insufficient credits") || lower.includes("402");
19426
19433
  };
19434
+ var isIntegrationsProviderError = (message) => {
19435
+ const lower = message.toLowerCase();
19436
+ return lower.includes("provider api key not configured") || lower.includes("503") && lower.includes("provider");
19437
+ };
19438
+ var CREDITS_NOT_CONFIGURED_MESSAGE = "OpenBot Credits is not configured on this runtime. The cloud host must set OPENBOT_INTEGRATIONS_BASE_URL and OPENBOT_INTEGRATIONS_TOKEN (try redeploying the workspace).";
19439
+ var CREDITS_PROVIDER_UNAVAILABLE_MESSAGE = "OpenBot Credits could not reach Anthropic \u2014 the platform provider API key is not configured yet. Try again later or switch this agent to BYOK mode.";
19440
+ var CREDITS_AUTH_FAILED_MESSAGE = "Claude could not authenticate via OpenBot Credits. Check your workspace credit balance in settings, or switch this agent to BYOK mode.";
19427
19441
  var buildSdkEnv = (authMode) => {
19428
19442
  if (authMode !== "credits") return void 0;
19429
19443
  const creditsEnv = buildCreditsAnthropicEnv();
19430
19444
  if (!creditsEnv) return void 0;
19431
19445
  return { ...process.env, ...creditsEnv };
19432
19446
  };
19447
+ var creditsErrorMessage = (message) => {
19448
+ if (isIntegrationsProviderError(message)) return CREDITS_PROVIDER_UNAVAILABLE_MESSAGE;
19449
+ if (isCreditsErrorMessage(message)) {
19450
+ return "Insufficient workspace credits. Add credits in workspace settings or switch this agent to BYOK mode.";
19451
+ }
19452
+ if (isAuthErrorMessage(message)) return CREDITS_AUTH_FAILED_MESSAGE;
19453
+ return void 0;
19454
+ };
19433
19455
  var buildApiKeyWidget = (agentId, threadId, reason) => uiWidget({
19434
19456
  agentId,
19435
19457
  threadId,
@@ -19687,6 +19709,14 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19687
19709
  workingDir
19688
19710
  };
19689
19711
  const sdkEnv = buildSdkEnv(authMode);
19712
+ if (authMode === "credits" && !resolveCreditsAuthConfig()) {
19713
+ yield agentOutput({
19714
+ agentId: context.state.agentId,
19715
+ threadId,
19716
+ content: CREDITS_NOT_CONFIGURED_MESSAGE
19717
+ });
19718
+ return;
19719
+ }
19690
19720
  const sdkOptions = {
19691
19721
  model,
19692
19722
  permissionMode,
@@ -19700,12 +19730,34 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19700
19730
  ...allowedTools ? { allowedTools } : {},
19701
19731
  ...sdkEnv ? { env: sdkEnv } : {}
19702
19732
  };
19733
+ let creditsFailureYielded = false;
19703
19734
  try {
19704
19735
  let lastSessionId = resumeId;
19705
19736
  let authWidgetYielded = false;
19706
19737
  const emittedToolCallIds = /* @__PURE__ */ new Set();
19707
19738
  const emittedToolResultIds = /* @__PURE__ */ new Set();
19708
19739
  const toolTitleByUseId = /* @__PURE__ */ new Map();
19740
+ const emitAssistantText = function* (text) {
19741
+ if (!text) return;
19742
+ if (authMode === "credits") {
19743
+ const creditsMessage = creditsErrorMessage(text);
19744
+ if (creditsMessage) {
19745
+ if (creditsFailureYielded) return;
19746
+ creditsFailureYielded = true;
19747
+ yield agentOutput({
19748
+ agentId: context.state.agentId,
19749
+ threadId,
19750
+ content: creditsMessage
19751
+ });
19752
+ return;
19753
+ }
19754
+ }
19755
+ yield agentOutput({
19756
+ agentId: context.state.agentId,
19757
+ threadId,
19758
+ content: text
19759
+ });
19760
+ };
19709
19761
  for await (const message of QA$({ prompt: userContent, options: sdkOptions })) {
19710
19762
  if ("session_id" in message && typeof message.session_id === "string") {
19711
19763
  lastSessionId = message.session_id;
@@ -19716,7 +19768,7 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19716
19768
  yield agentOutput({
19717
19769
  agentId: context.state.agentId,
19718
19770
  threadId,
19719
- content: "Claude could not authenticate via OpenBot Credits. Check your workspace credit balance in settings, or switch this agent to BYOK mode."
19771
+ content: creditsErrorMessage(message.error) ?? CREDITS_AUTH_FAILED_MESSAGE
19720
19772
  });
19721
19773
  } else {
19722
19774
  yield buildApiKeyWidget(context.state.agentId, threadId, message.error);
@@ -19739,11 +19791,7 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19739
19791
  const joined = textParts.join("\n");
19740
19792
  textParts.length = 0;
19741
19793
  if (joined.length > 0) {
19742
- yield agentOutput({
19743
- agentId: context.state.agentId,
19744
- threadId,
19745
- content: joined
19746
- });
19794
+ yield* emitAssistantText(joined);
19747
19795
  }
19748
19796
  }
19749
19797
  if (!emittedToolCallIds.has(tool.toolUseId)) {
@@ -19776,14 +19824,7 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19776
19824
  }
19777
19825
  }
19778
19826
  if (textParts.length > 0) {
19779
- const joined = textParts.join("\n");
19780
- if (joined.length > 0) {
19781
- yield agentOutput({
19782
- agentId: context.state.agentId,
19783
- threadId,
19784
- content: joined
19785
- });
19786
- }
19827
+ yield* emitAssistantText(textParts.join("\n"));
19787
19828
  }
19788
19829
  }
19789
19830
  }
@@ -19822,22 +19863,32 @@ var claudeCodeRuntime = (options = {}) => (builder) => {
19822
19863
  if (message.type === "result" && message.subtype !== "success") {
19823
19864
  const subtype = message.subtype;
19824
19865
  const resultText = "result" in message && typeof message.result === "string" ? message.result : "";
19825
- if (authMode === "credits" && (isCreditsErrorMessage(subtype) || isCreditsErrorMessage(resultText))) {
19826
- yield agentOutput({
19827
- agentId: context.state.agentId,
19828
- threadId,
19829
- content: "Insufficient workspace credits. Add credits in workspace settings or switch this agent to BYOK mode."
19830
- });
19831
- return;
19866
+ const errorText = resultText || subtype;
19867
+ if (authMode === "credits") {
19868
+ const creditsMessage = creditsErrorMessage(errorText);
19869
+ if (creditsMessage) {
19870
+ if (!creditsFailureYielded) {
19871
+ creditsFailureYielded = true;
19872
+ yield agentOutput({
19873
+ agentId: context.state.agentId,
19874
+ threadId,
19875
+ content: creditsMessage
19876
+ });
19877
+ }
19878
+ continue;
19879
+ }
19832
19880
  }
19833
19881
  if (!authWidgetYielded && (isAuthErrorMessage(subtype) || isAuthErrorMessage(resultText))) {
19834
19882
  authWidgetYielded = true;
19835
19883
  if (authMode === "credits") {
19836
- yield agentOutput({
19837
- agentId: context.state.agentId,
19838
- threadId,
19839
- content: "Claude could not authenticate via OpenBot Credits. Check your workspace credit balance in settings, or switch this agent to BYOK mode."
19840
- });
19884
+ if (!creditsFailureYielded) {
19885
+ creditsFailureYielded = true;
19886
+ yield agentOutput({
19887
+ agentId: context.state.agentId,
19888
+ threadId,
19889
+ content: creditsErrorMessage(errorText) ?? CREDITS_AUTH_FAILED_MESSAGE
19890
+ });
19891
+ }
19841
19892
  } else {
19842
19893
  yield buildApiKeyWidget(context.state.agentId, threadId, subtype);
19843
19894
  }
@@ -19857,21 +19908,29 @@ ${details}` : `[claude-code] run ended with error: ${subtype}`
19857
19908
  }
19858
19909
  } catch (error) {
19859
19910
  const errorMessage = formatClaudeCodeError(error, launchContext, stderrChunks);
19860
- if (authMode === "credits" && isCreditsErrorMessage(errorMessage)) {
19861
- yield agentOutput({
19862
- agentId: context.state.agentId,
19863
- threadId,
19864
- content: "Insufficient workspace credits. Add credits in workspace settings or switch this agent to BYOK mode."
19865
- });
19866
- return;
19911
+ if (authMode === "credits") {
19912
+ const creditsMessage = creditsErrorMessage(errorMessage);
19913
+ if (creditsMessage) {
19914
+ if (!creditsFailureYielded) {
19915
+ yield agentOutput({
19916
+ agentId: context.state.agentId,
19917
+ threadId,
19918
+ content: creditsMessage
19919
+ });
19920
+ }
19921
+ return;
19922
+ }
19867
19923
  }
19868
19924
  if (isAuthErrorMessage(errorMessage)) {
19869
19925
  if (authMode === "credits") {
19870
- yield agentOutput({
19871
- agentId: context.state.agentId,
19872
- threadId,
19873
- content: "Claude could not authenticate via OpenBot Credits. Check your workspace credit balance in settings, or switch this agent to BYOK mode."
19874
- });
19926
+ if (!creditsFailureYielded) {
19927
+ creditsFailureYielded = true;
19928
+ yield agentOutput({
19929
+ agentId: context.state.agentId,
19930
+ threadId,
19931
+ content: creditsErrorMessage(errorMessage) ?? CREDITS_AUTH_FAILED_MESSAGE
19932
+ });
19933
+ }
19875
19934
  } else {
19876
19935
  yield buildApiKeyWidget(context.state.agentId, threadId, errorMessage);
19877
19936
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meetopenbot/claude-code",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "description": "Claude Code plugin for OpenBot",
6
6
  "main": "./dist/index.js",