@0xmaxma/claude-gateway 2.0.7 → 2.0.8

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 (117) hide show
  1. package/README.md +1 -0
  2. package/config.template.json +5 -3
  3. package/dist/agent/runner.d.ts +14 -0
  4. package/dist/agent/runner.d.ts.map +1 -1
  5. package/dist/agent/runner.js +80 -19
  6. package/dist/agent/runner.js.map +1 -1
  7. package/dist/api/router.d.ts.map +1 -1
  8. package/dist/api/router.js +116 -5
  9. package/dist/api/router.js.map +1 -1
  10. package/dist/api/voice-settings-router.d.ts.map +1 -1
  11. package/dist/api/voice-settings-router.js +2 -1
  12. package/dist/api/voice-settings-router.js.map +1 -1
  13. package/dist/build-provenance.json +4 -4
  14. package/dist/history/db.d.ts +4 -0
  15. package/dist/history/db.d.ts.map +1 -1
  16. package/dist/history/db.js +33 -5
  17. package/dist/history/db.js.map +1 -1
  18. package/dist/history/types.d.ts +1 -0
  19. package/dist/history/types.d.ts.map +1 -1
  20. package/dist/orchestration/channel-speech.d.ts +1 -1
  21. package/dist/orchestration/channel-speech.d.ts.map +1 -1
  22. package/dist/orchestration/channel-speech.js +2 -2
  23. package/dist/orchestration/channel-speech.js.map +1 -1
  24. package/dist/orchestration/config.d.ts +13 -0
  25. package/dist/orchestration/config.d.ts.map +1 -1
  26. package/dist/orchestration/config.js +6 -0
  27. package/dist/orchestration/config.js.map +1 -1
  28. package/dist/orchestration/dashboard-provider-wait.d.ts +9 -0
  29. package/dist/orchestration/dashboard-provider-wait.d.ts.map +1 -0
  30. package/dist/orchestration/dashboard-provider-wait.js +26 -0
  31. package/dist/orchestration/dashboard-provider-wait.js.map +1 -0
  32. package/dist/orchestration/dashboard-reader-worker.js +13 -6
  33. package/dist/orchestration/dashboard-reader-worker.js.map +1 -1
  34. package/dist/orchestration/delivery.d.ts +2 -1
  35. package/dist/orchestration/delivery.d.ts.map +1 -1
  36. package/dist/orchestration/delivery.js +39 -5
  37. package/dist/orchestration/delivery.js.map +1 -1
  38. package/dist/orchestration/file-delivery.d.ts +1 -1
  39. package/dist/orchestration/file-delivery.d.ts.map +1 -1
  40. package/dist/orchestration/file-delivery.js +8 -1
  41. package/dist/orchestration/file-delivery.js.map +1 -1
  42. package/dist/orchestration/gateway-config.js +1 -1
  43. package/dist/orchestration/gateway-config.js.map +1 -1
  44. package/dist/orchestration/line-quota.d.ts +57 -0
  45. package/dist/orchestration/line-quota.d.ts.map +1 -0
  46. package/dist/orchestration/line-quota.js +191 -0
  47. package/dist/orchestration/line-quota.js.map +1 -0
  48. package/dist/orchestration/process-turn.d.ts +1 -0
  49. package/dist/orchestration/process-turn.d.ts.map +1 -1
  50. package/dist/orchestration/process-turn.js +26 -6
  51. package/dist/orchestration/process-turn.js.map +1 -1
  52. package/dist/orchestration/provider-admission.d.ts +63 -0
  53. package/dist/orchestration/provider-admission.d.ts.map +1 -0
  54. package/dist/orchestration/provider-admission.js +175 -0
  55. package/dist/orchestration/provider-admission.js.map +1 -0
  56. package/dist/orchestration/provider-error-metadata.d.ts +9 -0
  57. package/dist/orchestration/provider-error-metadata.d.ts.map +1 -0
  58. package/dist/orchestration/provider-error-metadata.js +28 -0
  59. package/dist/orchestration/provider-error-metadata.js.map +1 -0
  60. package/dist/orchestration/provider-scope.d.ts +28 -0
  61. package/dist/orchestration/provider-scope.d.ts.map +1 -0
  62. package/dist/orchestration/provider-scope.js +137 -0
  63. package/dist/orchestration/provider-scope.js.map +1 -0
  64. package/dist/orchestration/runtime.d.ts +13 -0
  65. package/dist/orchestration/runtime.d.ts.map +1 -1
  66. package/dist/orchestration/runtime.js +217 -24
  67. package/dist/orchestration/runtime.js.map +1 -1
  68. package/dist/orchestration/store.d.ts +6 -0
  69. package/dist/orchestration/store.d.ts.map +1 -1
  70. package/dist/orchestration/store.js +51 -10
  71. package/dist/orchestration/store.js.map +1 -1
  72. package/dist/orchestration/tasks/driver.d.ts +5 -2
  73. package/dist/orchestration/tasks/driver.d.ts.map +1 -1
  74. package/dist/orchestration/tasks/driver.js +19 -5
  75. package/dist/orchestration/tasks/driver.js.map +1 -1
  76. package/dist/orchestration/tasks/scheduler.d.ts +12 -2
  77. package/dist/orchestration/tasks/scheduler.d.ts.map +1 -1
  78. package/dist/orchestration/tasks/scheduler.js +77 -7
  79. package/dist/orchestration/tasks/scheduler.js.map +1 -1
  80. package/dist/orchestration/tasks/service.d.ts +1 -1
  81. package/dist/orchestration/tasks/service.d.ts.map +1 -1
  82. package/dist/orchestration/tasks/service.js +11 -2
  83. package/dist/orchestration/tasks/service.js.map +1 -1
  84. package/dist/orchestration/types.d.ts +3 -0
  85. package/dist/orchestration/types.d.ts.map +1 -1
  86. package/dist/orchestration/types.js.map +1 -1
  87. package/dist/session/codex-process.d.ts +6 -0
  88. package/dist/session/codex-process.d.ts.map +1 -1
  89. package/dist/session/codex-process.js +63 -5
  90. package/dist/session/codex-process.js.map +1 -1
  91. package/dist/session/codex-runtime.d.ts.map +1 -1
  92. package/dist/session/codex-runtime.js +9 -1
  93. package/dist/session/codex-runtime.js.map +1 -1
  94. package/dist/share/share-store.d.ts +5 -11
  95. package/dist/share/share-store.d.ts.map +1 -1
  96. package/dist/share/share-store.js +33 -5
  97. package/dist/share/share-store.js.map +1 -1
  98. package/dist/ui/dashboard-client.d.ts.map +1 -1
  99. package/dist/ui/dashboard-client.js +7 -6
  100. package/dist/ui/dashboard-client.js.map +1 -1
  101. package/dist/ui/dashboard-presentation.d.ts +2 -0
  102. package/dist/ui/dashboard-presentation.d.ts.map +1 -1
  103. package/dist/ui/dashboard-presentation.js +15 -1
  104. package/dist/ui/dashboard-presentation.js.map +1 -1
  105. package/dist/ui/token-report.d.ts +6 -0
  106. package/dist/ui/token-report.d.ts.map +1 -1
  107. package/dist/ui/token-report.js +1 -1
  108. package/dist/ui/token-report.js.map +1 -1
  109. package/dist/voice/providers/paxalabs-live-stt.d.ts +29 -0
  110. package/dist/voice/providers/paxalabs-live-stt.d.ts.map +1 -0
  111. package/dist/voice/providers/paxalabs-live-stt.js +137 -0
  112. package/dist/voice/providers/paxalabs-live-stt.js.map +1 -0
  113. package/dist/voice/providers/registry.d.ts.map +1 -1
  114. package/dist/voice/providers/registry.js +2 -1
  115. package/dist/voice/providers/registry.js.map +1 -1
  116. package/mcp/tools/telegram/receiver-server.ts +4 -2
  117. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/api/router.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAW,QAAQ,EAAE,MAAM,SAAS,CAAC;AAQpD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,EAAe,WAAW,EAAe,MAAM,UAAU,CAAC;AAoFtF,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAExD;AAMD,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAEtD;AAuED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,QAAQ,GAAG,IAAI,CA4CjD;AAmUD,wBAAgB,eAAe,CAC7B,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACtC,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACtC,OAAO,EAAE,MAAM,EAAE,EACjB,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,WAAW,EAAE,GAAG,SAAS,CAAC,GACzD,MAAM,CAizIR"}
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/api/router.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAW,QAAQ,EAAE,MAAM,SAAS,CAAC;AAQpD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,EAAe,WAAW,EAAe,MAAM,UAAU,CAAC;AAoFtF,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAExD;AAMD,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAEtD;AAuED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,QAAQ,GAAG,IAAI,CA4CjD;AAmUD,wBAAgB,eAAe,CAC7B,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACtC,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACtC,OAAO,EAAE,MAAM,EAAE,EACjB,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,WAAW,EAAE,GAAG,SAAS,CAAC,GACzD,MAAM,CAi4IR"}
@@ -602,7 +602,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
602
602
  * Send a message to an agent and receive its response synchronously.
603
603
  * Body: { message: string, chat_id: string, session_id?: string }
604
604
  */
605
- router.post('/v1/agents/:agentId/messages', auth, async (req, res) => {
605
+ router.post(['/v1/agents/:agentId/messages', '/v1/agents/:agentId/messages/accept'], auth, async (req, res) => {
606
606
  const { agentId } = req.params;
607
607
  const apiKey = req.apiKey;
608
608
  if (!(0, auth_1.canAccessAgent)(apiKey, agentId)) {
@@ -615,7 +615,14 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
615
615
  return;
616
616
  }
617
617
  const body = req.body;
618
- const { message, chat_id, session_id, stream, timeout_ms, media_files, model: requestModel, store_user_message, image_params, video_params } = body;
618
+ const { message, chat_id, session_id, stream, timeout_ms, media_files, model: requestModel, store_user_message, image_params, video_params, client_message_id } = body;
619
+ const accept_only = req.path.endsWith('/messages/accept') ? true : body.accept_only;
620
+ if ((accept_only !== undefined && typeof accept_only !== 'boolean') ||
621
+ (client_message_id !== undefined && (typeof client_message_id !== 'string' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(client_message_id)))) {
622
+ res.status(400).json({ error: 'Invalid accept_only or client_message_id' });
623
+ return;
624
+ }
625
+ const clientMessageId = client_message_id;
619
626
  if (message !== undefined && typeof message !== 'string') {
620
627
  res.status(400).json({ error: 'message must be a string if provided' });
621
628
  return;
@@ -795,6 +802,25 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
795
802
  const stopReply = runner.apiTaskStopReply?.(sessionId, (0, identity_1.apiPrincipal)(apiKey), trimmedMessage);
796
803
  const commandMessage = stopReply ?? trimmedMessage;
797
804
  const isBuiltinCommand = !!commandMessage && runner_1.AgentRunner.isApiBuiltinCommand(commandMessage);
805
+ if (accept_only === true) {
806
+ if (stream || isBuiltinCommand || skipUserMessage) {
807
+ res.status(400).json({ error: 'Queued admission requires a stored message and cannot stream or execute a command' });
808
+ return;
809
+ }
810
+ try {
811
+ const inputId = await runner.acceptApiMessage(sessionId, chatIdStr, trimmedMessage, {
812
+ timeoutMs, allowTools: agentConfigs.get(agentId)?.allow_tools ?? !!apiKey.allow_tools, mediaFiles: validatedMediaFiles, model: modelStr,
813
+ imageParams: validatedImageParams, videoParams: validatedVideoParams,
814
+ requestId, principalId: (0, identity_1.apiPrincipal)(apiKey), clientMessageId,
815
+ });
816
+ res.status(202).json({ status: 'accepted', input_id: inputId, session_id: sessionId, client_message_id: clientMessageId });
817
+ }
818
+ catch (error) {
819
+ const code = error.code;
820
+ res.status(code === 'NOT_SUPPORTED' || code === 'IDEMPOTENCY_CONFLICT' ? 409 : code === 'QUEUE_FULL' ? 429 : code === 'ACCESS_DENIED' ? 403 : 503).json({ error: error.message, code });
821
+ }
822
+ return;
823
+ }
798
824
  if (stream) {
799
825
  // SSE streaming mode
800
826
  let onClientDisconnect;
@@ -806,7 +832,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
806
832
  if (isBuiltinCommand) {
807
833
  openSseStream(res);
808
834
  try {
809
- const { responseText } = await runner.executeApiCommand(sessionId, chatIdStr, commandMessage, { skipPersist: skipUserMessage, model: modelStr, principalId: (0, identity_1.apiPrincipal)(apiKey), displayCommand: trimmedMessage });
835
+ const { responseText } = await runner.executeApiCommand(sessionId, chatIdStr, commandMessage, { skipPersist: skipUserMessage, model: modelStr, principalId: (0, identity_1.apiPrincipal)(apiKey), displayCommand: trimmedMessage, clientMessageId });
810
836
  sseCallbacks.onChunk({ type: 'text_delta', text: responseText });
811
837
  sseCallbacks.onDone(responseText, []);
812
838
  }
@@ -823,7 +849,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
823
849
  openSseStream(res);
824
850
  const agentCfg = agentConfigs.get(agentId);
825
851
  const allowTools = agentCfg.allow_tools ?? !!apiKey.allow_tools;
826
- onClientDisconnect = await runner.sendApiMessageStream(sessionId, chatIdStr, trimmedMessage, sseCallbacks, { timeoutMs, allowTools, mediaFiles: validatedMediaFiles, model: modelStr, skipUserMessage, imageParams: validatedImageParams, videoParams: validatedVideoParams, requestId, principalId: (0, identity_1.apiPrincipal)(apiKey) });
852
+ onClientDisconnect = await runner.sendApiMessageStream(sessionId, chatIdStr, trimmedMessage, sseCallbacks, { timeoutMs, allowTools, mediaFiles: validatedMediaFiles, model: modelStr, skipUserMessage, imageParams: validatedImageParams, videoParams: validatedVideoParams, requestId, principalId: (0, identity_1.apiPrincipal)(apiKey), clientMessageId });
827
853
  // Client disconnect — detaches this connection's sink. The turn keeps
828
854
  // running and keeps buffering, so the client can resume it on a new
829
855
  // connection via GET …/sessions/:sessionId/stream?after_seq= (#421).
@@ -855,7 +881,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
855
881
  let attachments = [];
856
882
  if (isBuiltinCommand) {
857
883
  // Built-in command — answer locally, return the same JSON shape as a normal reply.
858
- ({ responseText } = await runner.executeApiCommand(sessionId, chatIdStr, commandMessage, { skipPersist: skipUserMessage, principalId: (0, identity_1.apiPrincipal)(apiKey), displayCommand: trimmedMessage }));
884
+ ({ responseText } = await runner.executeApiCommand(sessionId, chatIdStr, commandMessage, { skipPersist: skipUserMessage, principalId: (0, identity_1.apiPrincipal)(apiKey), displayCommand: trimmedMessage, clientMessageId }));
859
885
  }
860
886
  else {
861
887
  const agentCfgSync = agentConfigs.get(agentId);
@@ -870,6 +896,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
870
896
  videoParams: validatedVideoParams,
871
897
  requestId,
872
898
  principalId: (0, identity_1.apiPrincipal)(apiKey),
899
+ clientMessageId,
873
900
  }));
874
901
  }
875
902
  const syncResult = {
@@ -940,6 +967,7 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
940
967
  model: cfg.claude?.model ?? null,
941
968
  allow_tools: cfg.allow_tools ?? false,
942
969
  orchestration_enabled: cfg.orchestration?.enabled === true,
970
+ chat_input_stream: cfg.orchestration?.enabled === true && (cfg.orchestration.channels ?? ['api']).includes('api'),
943
971
  connectors: cfg.connectors ?? {},
944
972
  avatarUrl: cfg.avatar ? `/api/v1/agents/${id}/avatar` : null,
945
973
  telegram_connected: !!cfg.telegram?.botToken,
@@ -4976,6 +5004,89 @@ function createApiRouter(agentRunners, agentConfigs, apiKeys, configPath, models
4976
5004
  * is keyed by session id, and a client resuming after a reload may well have
4977
5005
  * nothing but the session id left.
4978
5006
  */
5007
+ router.get('/v1/agents/:agentId/sessions/:sessionId/messages/stream', auth, async (req, res) => {
5008
+ const { agentId, sessionId } = req.params;
5009
+ const key = req.apiKey, runner = agentRunners.get(agentId);
5010
+ if (!(0, auth_1.canAccessAgent)(key, agentId) || !runner || !isValidSessionId(sessionId)) {
5011
+ res.status(403).end();
5012
+ return;
5013
+ }
5014
+ const after = req.query.after_id;
5015
+ let cursor = after === undefined ? 0 : Number(after);
5016
+ if ((after !== undefined && (typeof after !== 'string' || !/^\d+$/.test(after))) || !Number.isSafeInteger(cursor) || cursor < 0) {
5017
+ res.status(400).json({ error: 'Invalid message cursor' });
5018
+ return;
5019
+ }
5020
+ let unsubscribe, heartbeat;
5021
+ let closed = false, pumping = false, dirty = false;
5022
+ const close = () => { closed = true; unsubscribe?.(); clearInterval(heartbeat); };
5023
+ res.on('close', close);
5024
+ try {
5025
+ await runner.authorizeVoiceSession(sessionId, (0, identity_1.apiPrincipal)(key));
5026
+ if (closed)
5027
+ return;
5028
+ const history = runner.getHistoryDb();
5029
+ res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache, no-transform', 'X-Accel-Buffering': 'no' });
5030
+ res.flushHeaders();
5031
+ const pump = async () => {
5032
+ dirty = true;
5033
+ if (pumping || closed)
5034
+ return;
5035
+ pumping = true;
5036
+ try {
5037
+ while (dirty && !closed) {
5038
+ dirty = false;
5039
+ await runner.authorizeVoiceSession(sessionId, (0, identity_1.apiPrincipal)(key));
5040
+ if (closed)
5041
+ break;
5042
+ const rows = history.getUserMessagesAfter(sessionId, cursor);
5043
+ for (const message of rows) {
5044
+ if (res.writableLength > 1024 * 1024) {
5045
+ close();
5046
+ res.end();
5047
+ break;
5048
+ }
5049
+ res.write(`id: ${message.id}\ndata: ${JSON.stringify(message)}\n\n`);
5050
+ cursor = message.id;
5051
+ }
5052
+ if (rows.length === 100) {
5053
+ dirty = true;
5054
+ await new Promise(resolve => setImmediate(resolve));
5055
+ }
5056
+ }
5057
+ }
5058
+ catch {
5059
+ close();
5060
+ res.end();
5061
+ }
5062
+ finally {
5063
+ pumping = false;
5064
+ }
5065
+ };
5066
+ // Subscribe before replay: commits made while authorization awaits are
5067
+ // covered by the replay cursor, including notifications during a drain.
5068
+ unsubscribe = history.subscribeMessages(changedSession => { if (changedSession === sessionId)
5069
+ void pump(); });
5070
+ heartbeat = setInterval(() => {
5071
+ if (closed)
5072
+ return;
5073
+ if (res.writableLength > 1024 * 1024) {
5074
+ close();
5075
+ res.end();
5076
+ return;
5077
+ }
5078
+ res.write(': heartbeat\n\n');
5079
+ void pump(); // Also covers a writer in another process.
5080
+ }, 15000);
5081
+ await pump();
5082
+ }
5083
+ catch {
5084
+ close();
5085
+ if (!res.headersSent)
5086
+ res.status(403);
5087
+ res.end();
5088
+ }
5089
+ });
4979
5090
  router.get('/v1/agents/:agentId/sessions/:sessionId/activity/stream', auth, async (req, res) => {
4980
5091
  const { agentId, sessionId } = req.params;
4981
5092
  const key = req.apiKey, runner = agentRunners.get(agentId);