@mupt-ai/dari-gateway 1.0.312 → 1.0.314
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/README.md +276 -2
- package/dist/context/routing_context.js +4 -1
- package/dist/http/handler.d.ts +2 -1
- package/dist/http/handler.js +12 -2
- package/dist/ingress/anthropic_messages.js +15 -3
- package/dist/ingress/chat_completions.js +6 -1
- package/dist/ingress/codex.js +2 -2
- package/dist/ingress/models.js +46 -2
- package/dist/interaction/types.d.ts +1 -0
- package/dist/link/assets.d.ts +14 -0
- package/dist/link/assets.js +49 -0
- package/dist/link/budget.d.ts +27 -0
- package/dist/link/budget.js +62 -0
- package/dist/link/client.d.ts +382 -0
- package/dist/link/client.js +578 -0
- package/dist/link/endpoints.d.ts +23 -0
- package/dist/link/endpoints.js +1615 -0
- package/dist/link/errors.d.ts +30 -0
- package/dist/link/errors.js +120 -0
- package/dist/link/font.d.ts +1 -0
- package/dist/link/font.js +6 -0
- package/dist/link/grant_access.d.ts +7 -0
- package/dist/link/grant_access.js +99 -0
- package/dist/link/grants.d.ts +151 -0
- package/dist/link/grants.js +275 -0
- package/dist/link/in_flight.d.ts +4 -0
- package/dist/link/in_flight.js +43 -0
- package/dist/link/key_providers.d.ts +6 -0
- package/dist/link/key_providers.js +21 -0
- package/dist/link/self_hosted.d.ts +3 -0
- package/dist/link/self_hosted.js +166 -0
- package/dist/link/service.d.ts +85 -0
- package/dist/link/service.js +187 -0
- package/dist/link/sqlite_store.d.ts +74 -0
- package/dist/link/sqlite_store.js +381 -0
- package/dist/models/canonical_models.json +19 -0
- package/dist/models/gateways.d.ts +2 -0
- package/dist/models/gateways.js +2 -0
- package/dist/models/subscriptions.d.ts +8 -1
- package/dist/models/subscriptions.js +13 -2
- package/dist/platform/platform.d.ts +298 -1
- package/dist/protocol/input/router_model.d.ts +4 -0
- package/dist/protocol/input/router_model.js +31 -1
- package/dist/providers/execution.js +24 -2
- package/dist/providers/pi_options.js +57 -1
- package/dist/providers/provider_call.d.ts +4 -0
- package/dist/providers/provider_call.js +4 -1
- package/dist/providers/stream_finalization.d.ts +1 -0
- package/dist/runtime/explicit_model.js +16 -4
- package/dist/runtime/harness_identity/extract.js +20 -36
- package/dist/runtime/prepare_routed_completion.js +5 -1
- package/dist/runtime/routed_runtime.js +8 -0
- package/dist/runtime/runtime_reporter.d.ts +1 -0
- package/dist/runtime/runtime_reporter.js +1 -0
- package/dist/self_host/config.d.ts +9 -0
- package/dist/self_host/config.js +11 -1
- package/dist/self_host/main.d.ts +10 -3
- package/dist/self_host/main.js +19 -4
- package/dist/self_host/platform.d.ts +2 -2
- package/dist/self_host/platform.js +36 -7
- package/dist/self_host/resolve.d.ts +8 -0
- package/dist/self_host/resolve.js +130 -15
- package/dist/self_host/secret_box.d.ts +23 -0
- package/dist/self_host/secret_box.js +105 -0
- package/dist/self_host/stub_gateway.d.ts +60 -0
- package/dist/self_host/stub_gateway.js +397 -0
- package/dist/self_host/stub_main.d.ts +2 -0
- package/dist/self_host/stub_main.js +69 -0
- package/dist/self_host/subscription_test_gateway.d.ts +12 -9
- package/dist/self_host/subscription_test_gateway.js +25 -91
- package/dist/self_host/subscriptions/http.js +3 -2
- package/dist/self_host/subscriptions/index.d.ts +2 -2
- package/dist/self_host/subscriptions/index.js +2 -2
- package/dist/self_host/subscriptions/store.d.ts +41 -16
- package/dist/self_host/subscriptions/store.js +136 -32
- package/dist/telemetry/report.js +18 -0
- package/dist/types.d.ts +18 -0
- package/package.json +4 -3
- package/src/context/routing_context.ts +3 -1
- package/src/http/handler.ts +14 -2
- package/src/ingress/anthropic_messages.ts +19 -4
- package/src/ingress/chat_completions.ts +6 -1
- package/src/ingress/codex.ts +2 -2
- package/src/ingress/models.ts +48 -2
- package/src/interaction/types.ts +4 -0
- package/src/link/assets.ts +56 -0
- package/src/link/budget.ts +75 -0
- package/src/link/client.ts +793 -0
- package/src/link/endpoints.ts +1834 -0
- package/src/link/errors.ts +157 -0
- package/src/link/font.ts +9 -0
- package/src/link/grant_access.ts +116 -0
- package/src/link/grants.ts +436 -0
- package/src/link/in_flight.ts +46 -0
- package/src/link/key_providers.ts +25 -0
- package/src/link/self_hosted.ts +186 -0
- package/src/link/service.ts +281 -0
- package/src/link/sqlite_store.ts +512 -0
- package/src/models/canonical_models.json +19 -0
- package/src/models/gateways.ts +3 -0
- package/src/models/subscriptions.ts +15 -1
- package/src/platform/platform.ts +324 -1
- package/src/protocol/input/router_model.ts +36 -1
- package/src/providers/execution.ts +24 -2
- package/src/providers/pi_options.ts +57 -1
- package/src/providers/provider_call.ts +15 -1
- package/src/providers/stream_finalization.ts +1 -0
- package/src/runtime/explicit_model.ts +21 -8
- package/src/runtime/harness_identity/extract.ts +21 -60
- package/src/runtime/prepare_routed_completion.ts +6 -1
- package/src/runtime/routed_runtime.ts +8 -0
- package/src/runtime/runtime_reporter.ts +3 -0
- package/src/self_host/config.ts +24 -1
- package/src/self_host/main.ts +21 -4
- package/src/self_host/platform.ts +40 -8
- package/src/self_host/resolve.ts +157 -15
- package/src/self_host/secret_box.ts +112 -0
- package/src/self_host/stub_gateway.ts +497 -0
- package/src/self_host/stub_main.ts +72 -0
- package/src/self_host/subscription_test_gateway.ts +26 -95
- package/src/self_host/subscriptions/http.ts +3 -2
- package/src/self_host/subscriptions/index.ts +3 -0
- package/src/self_host/subscriptions/store.ts +167 -36
- package/src/telemetry/report.ts +17 -1
- package/src/types.ts +26 -0
- package/dist/self_host/subscriptions/test_support.d.ts +0 -15
- package/dist/self_host/subscriptions/test_support.js +0 -34
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/* What the Dari Link approval page draws: Dari's mark, each provider's mark,
|
|
2
|
+
and the line icons. Provider marks are the monochrome ones from
|
|
3
|
+
@lobehub/icons (MIT), each the content of a 24x24 SVG that takes the text
|
|
4
|
+
color; the names they stand for are their owners' trademarks. */
|
|
5
|
+
/** Dari's round mark. */
|
|
6
|
+
export const DARI_MARK = `<svg class="mark" viewBox="0 0 1024 1024" aria-label="Dari" role="img"><circle cx="512" cy="512" r="462" fill="#000"/><g fill="#fff"><path d="M227.174 643.267C309.13 501.04 353.3 382.247 461.711 379.747c100.278 6.927 172.975 162.686 217.376 263.52h110.942c-81.304-117.797-196.325-301.555-326.906-303.36-166.044-4.954-236.495 312.426-235.949 303.36"/><path d="M657.933 552.765h177.78V487.33l-224.079 18.712 44.162 15.727zM327.444 446.849h14.02v89.346h-14.02z"/><path d="M368.884 403.285h13.92v129.377h-13.92z"/><path d="M413.178 382.234h13.886v144.712h-13.886zm48.935-4.033h13.886v144.712h-13.886z"/><path d="m525.783 410.11.44.31v-.31zm-13.447.421v118.442h13.887V423.498a300 300 0 0 0-13.887-12.967"/><path d="m593.563 507.768-329.946 33.841-85.683 11.157h444.87c-9.563-15.205-19.29-30.327-29.241-44.998"/></g></svg>`;
|
|
7
|
+
// Each provider's mark.
|
|
8
|
+
const MARKS = {
|
|
9
|
+
anthropic: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M13.827 3.52h3.603L24 20h-3.603l-6.57-16.48zm-7.258 0h3.767L16.906 20h-3.674l-1.343-3.461H5.017l-1.344 3.46H0L6.57 3.522zm4.132 9.959L8.453 7.687 6.205 13.48H10.7z"></path></svg>`,
|
|
10
|
+
claude: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M4.709 15.955l4.72-2.647.08-.23-.08-.128H9.2l-.79-.048-2.698-.073-2.339-.097-2.266-.122-.571-.121L0 11.784l.055-.352.48-.321.686.06 1.52.103 2.278.158 1.652.097 2.449.255h.389l.055-.157-.134-.098-.103-.097-2.358-1.596-2.552-1.688-1.336-.972-.724-.491-.364-.462-.158-1.008.656-.722.881.06.225.061.893.686 1.908 1.476 2.491 1.833.365.304.145-.103.019-.073-.164-.274-1.355-2.446-1.446-2.49-.644-1.032-.17-.619a2.97 2.97 0 01-.104-.729L6.283.134 6.696 0l.996.134.42.364.62 1.414 1.002 2.229 1.555 3.03.456.898.243.832.091.255h.158V9.01l.128-1.706.237-2.095.23-2.695.08-.76.376-.91.747-.492.584.28.48.685-.067.444-.286 1.851-.559 2.903-.364 1.942h.212l.243-.242.985-1.306 1.652-2.064.73-.82.85-.904.547-.431h1.033l.76 1.129-.34 1.166-1.064 1.347-.881 1.142-1.264 1.7-.79 1.36.073.11.188-.02 2.856-.606 1.543-.28 1.841-.315.833.388.091.395-.328.807-1.969.486-2.309.462-3.439.813-.042.03.049.061 1.549.146.662.036h1.622l3.02.225.79.522.474.638-.079.485-1.215.62-1.64-.389-3.829-.91-1.312-.329h-.182v.11l1.093 1.068 2.006 1.81 2.509 2.33.127.578-.322.455-.34-.049-2.205-1.657-.851-.747-1.926-1.62h-.128v.17l.444.649 2.345 3.521.122 1.08-.17.353-.608.213-.668-.122-1.374-1.925-1.415-2.167-1.143-1.943-.14.08-.674 7.254-.316.37-.729.28-.607-.461-.322-.747.322-1.476.389-1.924.315-1.53.286-1.9.17-.632-.012-.042-.14.018-1.434 1.967-2.18 2.945-1.726 1.845-.414.164-.717-.37.067-.662.401-.589 2.388-3.036 1.44-1.882.93-1.086-.006-.158h-.055L4.132 18.56l-1.13.146-.487-.456.061-.746.231-.243 1.908-1.312-.006.006z"></path></svg>`,
|
|
11
|
+
meta: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M6.897 4c1.915 0 3.516.932 5.43 3.376l.282-.373c.19-.246.383-.484.58-.71l.313-.35C14.588 4.788 15.792 4 17.225 4c1.273 0 2.469.557 3.491 1.516l.218.213c1.73 1.765 2.917 4.71 3.053 8.026l.011.392.002.25c0 1.501-.28 2.759-.818 3.7l-.14.23-.108.153c-.301.42-.664.758-1.086 1.009l-.265.142-.087.04a3.493 3.493 0 01-.302.118 4.117 4.117 0 01-1.33.208c-.524 0-.996-.067-1.438-.215-.614-.204-1.163-.56-1.726-1.116l-.227-.235c-.753-.812-1.534-1.976-2.493-3.586l-1.43-2.41-.544-.895-1.766 3.13-.343.592C7.597 19.156 6.227 20 4.356 20c-1.21 0-2.205-.42-2.936-1.182l-.168-.184c-.484-.573-.837-1.311-1.043-2.189l-.067-.32a8.69 8.69 0 01-.136-1.288L0 14.468c.002-.745.06-1.49.174-2.23l.1-.573c.298-1.53.828-2.958 1.536-4.157l.209-.34c1.177-1.83 2.789-3.053 4.615-3.16L6.897 4zm-.033 2.615l-.201.01c-.83.083-1.606.673-2.252 1.577l-.138.199-.01.018c-.67 1.017-1.185 2.378-1.456 3.845l-.004.022a12.591 12.591 0 00-.207 2.254l.002.188c.004.18.017.36.04.54l.043.291c.092.503.257.908.486 1.208l.117.137c.303.323.698.492 1.17.492 1.1 0 1.796-.676 3.696-3.641l2.175-3.4.454-.701-.139-.198C9.11 7.3 8.084 6.616 6.864 6.616zm10.196-.552l-.176.007c-.635.048-1.223.359-1.82.933l-.196.198c-.439.462-.887 1.064-1.367 1.807l.266.398c.18.274.362.56.55.858l.293.475 1.396 2.335.695 1.114c.583.926 1.03 1.6 1.408 2.082l.213.262c.282.326.529.54.777.673l.102.05c.227.1.457.138.718.138.176.002.35-.023.518-.073.338-.104.61-.32.813-.637l.095-.163.077-.162c.194-.459.29-1.06.29-1.785l-.006-.449c-.08-2.871-.938-5.372-2.2-6.798l-.176-.189c-.67-.683-1.444-1.074-2.27-1.074z"></path></svg>`,
|
|
12
|
+
moonshot: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M1.052 16.916l9.539 2.552a21.007 21.007 0 00.06 2.033l5.956 1.593a11.997 11.997 0 01-5.586.865l-.18-.016-.044-.004-.084-.009-.094-.01a11.605 11.605 0 01-.157-.02l-.107-.014-.11-.016a11.962 11.962 0 01-.32-.051l-.042-.008-.075-.013-.107-.02-.07-.015-.093-.019-.075-.016-.095-.02-.097-.023-.094-.022-.068-.017-.088-.022-.09-.024-.095-.025-.082-.023-.109-.03-.062-.02-.084-.025-.093-.028-.105-.034-.058-.019-.08-.026-.09-.031-.066-.024a6.293 6.293 0 01-.044-.015l-.068-.025-.101-.037-.057-.022-.08-.03-.087-.035-.088-.035-.079-.032-.095-.04-.063-.028-.063-.027a5.655 5.655 0 01-.041-.018l-.066-.03-.103-.047-.052-.024-.096-.046-.062-.03-.084-.04-.086-.044-.093-.047-.052-.027-.103-.055-.057-.03-.058-.032a6.49 6.49 0 01-.046-.026l-.094-.053-.06-.034-.051-.03-.072-.041-.082-.05-.093-.056-.052-.032-.084-.053-.061-.039-.079-.05-.07-.047-.053-.035a7.785 7.785 0 01-.054-.036l-.044-.03-.044-.03a6.066 6.066 0 01-.04-.028l-.057-.04-.076-.054-.069-.05-.074-.054-.056-.042-.076-.057-.076-.059-.086-.067-.045-.035-.064-.052-.074-.06-.089-.073-.046-.039-.046-.039a7.516 7.516 0 01-.043-.037l-.045-.04-.061-.053-.07-.062-.068-.06-.062-.058-.067-.062-.053-.05-.088-.084a13.28 13.28 0 01-.099-.097l-.029-.028-.041-.042-.069-.07-.05-.051-.05-.053a6.457 6.457 0 01-.168-.179l-.08-.088-.062-.07-.071-.08-.042-.049-.053-.062-.058-.068-.046-.056a7.175 7.175 0 01-.027-.033l-.045-.055-.066-.082-.041-.052-.05-.064-.02-.025a11.99 11.99 0 01-1.44-2.402zm-1.02-5.794l11.353 3.037a20.468 20.468 0 00-.469 2.011l10.817 2.894a12.076 12.076 0 01-1.845 2.005L.657 15.923l-.016-.046-.035-.104a11.965 11.965 0 01-.05-.153l-.007-.023a11.896 11.896 0 01-.207-.741l-.03-.126-.018-.08-.021-.097-.018-.081-.018-.09-.017-.084-.018-.094c-.026-.141-.05-.283-.071-.426l-.017-.118-.011-.083-.013-.102a12.01 12.01 0 01-.019-.161l-.005-.047a12.12 12.12 0 01-.034-2.145zm1.593-5.15l11.948 3.196c-.368.605-.705 1.231-1.01 1.875l11.295 3.022c-.142.82-.368 1.612-.668 2.365l-11.55-3.09L.124 10.26l.015-.1.008-.049.01-.067.015-.087.018-.098c.026-.148.056-.295.088-.442l.028-.124.02-.085.024-.097c.022-.09.045-.18.07-.268l.028-.102.023-.083.03-.1.025-.082.03-.096.026-.082.031-.095a11.896 11.896 0 011.01-2.232zm4.442-4.4L17.352 4.59a20.77 20.77 0 00-1.688 1.721l7.823 2.093c.267.852.442 1.744.513 2.665L2.106 5.213l.045-.065.027-.04.04-.055.046-.065.055-.076.054-.072.064-.086.05-.065.057-.073.055-.07.06-.074.055-.069.065-.077.054-.066.066-.077.053-.06.072-.082.053-.06.067-.074.054-.058.073-.078.058-.06.063-.067.168-.17.1-.098.059-.056.076-.071a12.084 12.084 0 012.272-1.677zM12.017 0h.097l.082.001.069.001.054.002.068.002.046.001.076.003.047.002.06.003.054.002.087.005.105.007.144.011.088.007.044.004.077.008.082.008.047.005.102.012.05.006.108.014.081.01.042.006.065.01.207.032.07.012.065.011.14.026.092.018.11.022.046.01.075.016.041.01L14.7.3l.042.01.065.015.049.012.071.017.096.024.112.03.113.03.113.032.05.015.07.02.078.024.073.023.05.016.05.016.076.025.099.033.102.036.048.017.064.023.093.034.11.041.116.045.1.04.047.02.06.024.041.018.063.026.04.018.057.025.11.048.1.046.074.035.075.036.06.028.092.046.091.045.102.052.053.028.049.026.046.024.06.033.041.022.052.029.088.05.106.06.087.051.057.034.053.032.096.059.088.055.098.062.036.024.064.041.084.056.04.027.062.042.062.043.023.017c.054.037.108.075.161.114l.083.06.065.048.056.043.086.065.082.064.04.03.05.041.086.069.079.065.085.071c.712.6 1.353 1.283 1.909 2.031L7.222.994l.062-.027.065-.028.081-.034.086-.035c.113-.045.227-.09.341-.131l.096-.035.093-.033.084-.03.096-.031c.087-.03.176-.058.264-.085l.091-.027.086-.025.102-.03.085-.023.1-.026L9.04.37l.09-.023.091-.022.095-.022.09-.02.098-.021.091-.02.095-.018.092-.018.1-.018.091-.016.098-.017.092-.014.097-.015.092-.013.102-.013.091-.012.105-.012.09-.01.105-.01c.093-.01.186-.018.28-.024l.106-.008.09-.005.11-.006.093-.004.1-.004.097-.002.099-.002.197-.002z"></path></svg>`,
|
|
13
|
+
openai: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M9.205 8.658v-2.26c0-.19.072-.333.238-.428l4.543-2.616c.619-.357 1.356-.523 2.117-.523 2.854 0 4.662 2.212 4.662 4.566 0 .167 0 .357-.024.547l-4.71-2.759a.797.797 0 00-.856 0l-5.97 3.473zm10.609 8.8V12.06c0-.333-.143-.57-.429-.737l-5.97-3.473 1.95-1.118a.433.433 0 01.476 0l4.543 2.617c1.309.76 2.189 2.378 2.189 3.948 0 1.808-1.07 3.473-2.76 4.163zM7.802 12.703l-1.95-1.142c-.167-.095-.239-.238-.239-.428V5.899c0-2.545 1.95-4.472 4.591-4.472 1 0 1.927.333 2.712.928L8.23 5.067c-.285.166-.428.404-.428.737v6.898zM12 15.128l-2.795-1.57v-3.33L12 8.658l2.795 1.57v3.33L12 15.128zm1.796 7.23c-1 0-1.927-.332-2.712-.927l4.686-2.712c.285-.166.428-.404.428-.737v-6.898l1.974 1.142c.167.095.238.238.238.428v5.233c0 2.545-1.974 4.472-4.614 4.472zm-5.637-5.303l-4.544-2.617c-1.308-.761-2.188-2.378-2.188-3.948A4.482 4.482 0 014.21 6.327v5.423c0 .333.143.571.428.738l5.947 3.449-1.95 1.118a.432.432 0 01-.476 0zm-.262 3.9c-2.688 0-4.662-2.021-4.662-4.519 0-.19.024-.38.047-.57l4.686 2.71c.286.167.571.167.856 0l5.97-3.448v2.26c0 .19-.07.333-.237.428l-4.543 2.616c-.619.357-1.356.523-2.117.523zm5.899 2.83a5.947 5.947 0 005.827-4.756C22.287 18.339 24 15.84 24 13.296c0-1.665-.713-3.282-1.998-4.448.119-.5.19-.999.19-1.498 0-3.401-2.759-5.947-5.946-5.947-.642 0-1.26.095-1.88.31A5.962 5.962 0 0010.205 0a5.947 5.947 0 00-5.827 4.757C1.713 5.447 0 7.945 0 10.49c0 1.666.713 3.283 1.998 4.448-.119.5-.19 1-.19 1.499 0 3.401 2.759 5.946 5.946 5.946.642 0 1.26-.095 1.88-.309a5.96 5.96 0 004.162 1.713z"></path></svg>`,
|
|
14
|
+
opencode: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M16 6H8v12h8V6zm4 16H4V2h16v20z"></path></svg>`,
|
|
15
|
+
openrouter: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M18.654 3.87a5.087 5.087 0 110 10.174L23.7 19.09c.64.641.187 1.737-.72 1.737H8.48a8.479 8.479 0 010-16.958h10.175zM8.479 7.26a5.087 5.087 0 100 10.176 5.087 5.087 0 000-10.175z"></path></svg>`,
|
|
16
|
+
vercel: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M12 0l12 20.785H0L12 0z"></path></svg>`,
|
|
17
|
+
xai: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M6.469 8.776L16.512 23h-4.464L2.005 8.776H6.47zm-.004 7.9l2.233 3.164L6.467 23H2l4.465-6.324zM22 2.582V23h-3.659V7.764L22 2.582zM22 1l-9.952 14.095-2.233-3.163L17.533 1H22z"></path></svg>`,
|
|
18
|
+
};
|
|
19
|
+
// The mark for each plan kind, key provider, and gateway.
|
|
20
|
+
const MARK_FOR = {
|
|
21
|
+
anthropic_claude_code: "claude",
|
|
22
|
+
openai_codex: "openai",
|
|
23
|
+
meta_muse: "meta",
|
|
24
|
+
opencode_go: "opencode",
|
|
25
|
+
anthropic: "anthropic",
|
|
26
|
+
openai: "openai",
|
|
27
|
+
meta: "meta",
|
|
28
|
+
moonshotai: "moonshot",
|
|
29
|
+
xai: "xai",
|
|
30
|
+
openrouter: "openrouter",
|
|
31
|
+
"vercel-ai-gateway": "vercel",
|
|
32
|
+
};
|
|
33
|
+
/** The mark of a plan kind (`openai_codex`), key provider (`anthropic`), or gateway (`openrouter`); undefined when it has none. */
|
|
34
|
+
export function linkLogo(id) {
|
|
35
|
+
const mark = MARK_FOR[id];
|
|
36
|
+
return mark === undefined ? undefined : MARKS[mark];
|
|
37
|
+
}
|
|
38
|
+
const icon = (paths) => `<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${paths}</svg>`;
|
|
39
|
+
// Lucide's line icons, as the Dari app draws them.
|
|
40
|
+
export const ICONS = {
|
|
41
|
+
back: icon(`<path d="m12 19-7-7 7-7"/><path d="M19 12H5"/>`),
|
|
42
|
+
close: icon(`<path d="M18 6 6 18"/><path d="m6 6 12 12"/>`),
|
|
43
|
+
external: icon(`<path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/>`),
|
|
44
|
+
plus: icon(`<path d="M5 12h14"/><path d="M12 5v14"/>`),
|
|
45
|
+
trash: icon(`<path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>`),
|
|
46
|
+
check: icon(`<path d="M20 6 9 17l-5-5"/>`),
|
|
47
|
+
zap: icon(`<path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/>`),
|
|
48
|
+
shield: icon(`<path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/><path d="m9 12 2 2 4-4"/>`),
|
|
49
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { LinkBudget } from "../platform/platform.js";
|
|
2
|
+
/** The windows a budget can cap: five hours and one week. */
|
|
3
|
+
export declare const LINK_BUDGET_WINDOW_MINUTES: readonly [300, 10080];
|
|
4
|
+
/** Tokens one of a grant's requests used, and when it was received. */
|
|
5
|
+
export type BudgetUse = {
|
|
6
|
+
at: Date;
|
|
7
|
+
tokens: number;
|
|
8
|
+
};
|
|
9
|
+
/** A budget window as it stands for a grant; `resetsAt` is set only at the cap. */
|
|
10
|
+
export type BudgetStanding = {
|
|
11
|
+
windowMinutes: number;
|
|
12
|
+
maxTokens: number;
|
|
13
|
+
usedTokens: number;
|
|
14
|
+
resetsAt: Date | null;
|
|
15
|
+
};
|
|
16
|
+
/** Each budget window as it stands, from the grant's uses. */
|
|
17
|
+
export declare function budgetStandings(budget: readonly LinkBudget[], uses: readonly BudgetUse[], now: Date): BudgetStanding[];
|
|
18
|
+
/** Undefined while the grant is within its budget; otherwise when it is back. */
|
|
19
|
+
export declare function overBudgetUntil(standings: readonly BudgetStanding[]): Date | undefined;
|
|
20
|
+
/** How far back a budget's windows reach, in milliseconds; 0 for none. */
|
|
21
|
+
export declare function budgetLookbackMs(budget: readonly LinkBudget[]): number;
|
|
22
|
+
/** A budget from a request: five-hour and weekly windows, each a positive whole token cap. */
|
|
23
|
+
export declare function validBudget(value: unknown): LinkBudget[] | null;
|
|
24
|
+
export declare function budgetBody(budget: readonly LinkBudget[]): {
|
|
25
|
+
window_minutes: number;
|
|
26
|
+
max_tokens: number;
|
|
27
|
+
}[];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/* A Dari Link grant's budget: the most tokens its app may use in each rolling
|
|
2
|
+
window, across all of the grant's plans and API keys together.
|
|
3
|
+
|
|
4
|
+
A grant's tokens are what Dari recorded for its requests (child tokens
|
|
5
|
+
included): input + output + cache read + cache write, the same total the
|
|
6
|
+
Connected Apps page shows. A window's used tokens are those of requests
|
|
7
|
+
received in its last `windowMinutes`. Each request is checked before it is
|
|
8
|
+
served, so one in flight may take a window past its cap.
|
|
9
|
+
|
|
10
|
+
A window at its cap is back once enough of its oldest requests roll off to
|
|
11
|
+
bring it under the cap again: `resetsAt` is when that happens. */
|
|
12
|
+
/** The windows a budget can cap: five hours and one week. */
|
|
13
|
+
export const LINK_BUDGET_WINDOW_MINUTES = [300, 10080];
|
|
14
|
+
/** Each budget window as it stands, from the grant's uses. */
|
|
15
|
+
export function budgetStandings(budget, uses, now) {
|
|
16
|
+
return budget.map((entry) => {
|
|
17
|
+
const windowMs = entry.windowMinutes * 60_000;
|
|
18
|
+
const live = uses
|
|
19
|
+
.filter((use) => use.at.getTime() > now.getTime() - windowMs)
|
|
20
|
+
.sort((a, b) => a.at.getTime() - b.at.getTime());
|
|
21
|
+
const usedTokens = live.reduce((sum, use) => sum + use.tokens, 0);
|
|
22
|
+
let resetsAt = null;
|
|
23
|
+
let remaining = usedTokens;
|
|
24
|
+
for (const use of live) {
|
|
25
|
+
if (remaining < entry.maxTokens)
|
|
26
|
+
break;
|
|
27
|
+
remaining -= use.tokens;
|
|
28
|
+
resetsAt = new Date(use.at.getTime() + windowMs);
|
|
29
|
+
}
|
|
30
|
+
return { windowMinutes: entry.windowMinutes, maxTokens: entry.maxTokens, usedTokens, resetsAt };
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** Undefined while the grant is within its budget; otherwise when it is back. */
|
|
34
|
+
export function overBudgetUntil(standings) {
|
|
35
|
+
const resets = standings.flatMap((standing) => standing.resetsAt === null ? [] : [standing.resetsAt.getTime()]);
|
|
36
|
+
return resets.length === 0 ? undefined : new Date(Math.max(...resets));
|
|
37
|
+
}
|
|
38
|
+
/** How far back a budget's windows reach, in milliseconds; 0 for none. */
|
|
39
|
+
export function budgetLookbackMs(budget) {
|
|
40
|
+
return Math.max(0, ...budget.map((entry) => entry.windowMinutes)) * 60_000;
|
|
41
|
+
}
|
|
42
|
+
/** A budget from a request: five-hour and weekly windows, each a positive whole token cap. */
|
|
43
|
+
export function validBudget(value) {
|
|
44
|
+
if (!Array.isArray(value))
|
|
45
|
+
return null;
|
|
46
|
+
const budget = [];
|
|
47
|
+
for (const entry of value) {
|
|
48
|
+
if (typeof entry !== "object" || entry === null)
|
|
49
|
+
return null;
|
|
50
|
+
const { window_minutes: minutes, max_tokens: tokens } = entry;
|
|
51
|
+
if (!LINK_BUDGET_WINDOW_MINUTES.includes(minutes)
|
|
52
|
+
|| typeof tokens !== "number" || !Number.isSafeInteger(tokens) || tokens <= 0
|
|
53
|
+
|| budget.some((other) => other.windowMinutes === minutes)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
budget.push({ windowMinutes: minutes, maxTokens: tokens });
|
|
57
|
+
}
|
|
58
|
+
return budget.sort((a, b) => a.windowMinutes - b.windowMinutes);
|
|
59
|
+
}
|
|
60
|
+
export function budgetBody(budget) {
|
|
61
|
+
return budget.map((entry) => ({ window_minutes: entry.windowMinutes, max_tokens: entry.maxTokens }));
|
|
62
|
+
}
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small Dari Link client for apps: register, send the user to approve,
|
|
3
|
+
* exchange the code, and call the gateway with tokens that refresh
|
|
4
|
+
* themselves. It uses only `fetch` and Web Crypto, so it runs in browsers,
|
|
5
|
+
* Node, Bun, and Deno.
|
|
6
|
+
*
|
|
7
|
+
* In a browser, one button does the approval: `DariLink.open` opens Dari in a
|
|
8
|
+
* popup, the user signs in, picks plans, and approves, and the popup closes.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { DariLink } from "@mupt-ai/dari-gateway/link/client";
|
|
12
|
+
*
|
|
13
|
+
* button.onclick = async () => {
|
|
14
|
+
* const { code, codeVerifier } = await DariLink.open({ gatewayUrl, clientId, redirectUri });
|
|
15
|
+
* await fetch("/api/dari/exchange", { method: "POST", body: JSON.stringify({ code, codeVerifier }) });
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Without a popup, send the user to the approval URL yourself:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { authorizeUrl, exchange, linkFetch } from "@mupt-ai/dari-gateway/link/client";
|
|
23
|
+
*
|
|
24
|
+
* const { url, codeVerifier, state } = await authorizeUrl({ gatewayUrl, clientId, redirectUri });
|
|
25
|
+
* // Keep codeVerifier and state with the user's session, send them to `url`;
|
|
26
|
+
* // on the redirect back, check `state`, then:
|
|
27
|
+
* await db.save(user, await exchange({ gatewayUrl, clientId, redirectUri, code, codeVerifier }));
|
|
28
|
+
* const gatewayFetch = linkFetch({
|
|
29
|
+
* gatewayUrl,
|
|
30
|
+
* clientId,
|
|
31
|
+
* loadTokens: () => db.load(user),
|
|
32
|
+
* saveTokens: (next) => db.save(user, next),
|
|
33
|
+
* });
|
|
34
|
+
* await gatewayFetch("/v1/chat/completions", {
|
|
35
|
+
* method: "POST",
|
|
36
|
+
* headers: { "content-type": "application/json" },
|
|
37
|
+
* body: JSON.stringify({ model: "dari/routing", messages }),
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* A server keeps a secret: register it as a confidential client
|
|
42
|
+
* (`registerClient({ confidential: true })`, or Settings → Dari Link Apps on
|
|
43
|
+
* hosted Dari) and pass `clientSecret` to every
|
|
44
|
+
* call. Its refresh token then never rotates, so any number of processes can
|
|
45
|
+
* refresh the same grant. A public client's refresh token rotates on every
|
|
46
|
+
* use; `linkFetch` reads the tokens from your storage on each call, so
|
|
47
|
+
* processes that share that storage share the rotation.
|
|
48
|
+
*/
|
|
49
|
+
export type LinkPlan = {
|
|
50
|
+
id: string;
|
|
51
|
+
kind: string;
|
|
52
|
+
title: string;
|
|
53
|
+
provider?: string;
|
|
54
|
+
model_prefix?: string;
|
|
55
|
+
plan: string;
|
|
56
|
+
label: string;
|
|
57
|
+
account_email: string | null;
|
|
58
|
+
serves: string;
|
|
59
|
+
unavailable_until: string | null;
|
|
60
|
+
usage: Array<{
|
|
61
|
+
window_minutes: number;
|
|
62
|
+
utilization: number;
|
|
63
|
+
resets_at: string;
|
|
64
|
+
}>;
|
|
65
|
+
state: "ready" | "plan_limit";
|
|
66
|
+
available_at: string | null;
|
|
67
|
+
models: LinkModel[];
|
|
68
|
+
};
|
|
69
|
+
export type LinkModel = {
|
|
70
|
+
id: string;
|
|
71
|
+
name: string;
|
|
72
|
+
};
|
|
73
|
+
export type LinkBudgetWindow = {
|
|
74
|
+
window_minutes: number;
|
|
75
|
+
max_tokens: number;
|
|
76
|
+
used_tokens: number;
|
|
77
|
+
resets_at: string | null;
|
|
78
|
+
};
|
|
79
|
+
export type LinkTokenSet = {
|
|
80
|
+
accessToken: string;
|
|
81
|
+
refreshToken: string;
|
|
82
|
+
expiresAt: number;
|
|
83
|
+
grantId: string | null;
|
|
84
|
+
scope: string;
|
|
85
|
+
budget: LinkBudgetWindow[];
|
|
86
|
+
state: "ready" | "budget_limit";
|
|
87
|
+
availableAt: string | null;
|
|
88
|
+
plans: LinkPlan[];
|
|
89
|
+
models: LinkModel[];
|
|
90
|
+
manageUrl: string;
|
|
91
|
+
routes: boolean;
|
|
92
|
+
};
|
|
93
|
+
export type LinkChildToken = {
|
|
94
|
+
accessToken: string;
|
|
95
|
+
expiresAt: number;
|
|
96
|
+
models: Array<{
|
|
97
|
+
model: string;
|
|
98
|
+
thinking: string | null;
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* A refused Dari Link call. `code` is the most specific error code: an OAuth
|
|
103
|
+
* error such as `invalid_grant` (the grant is gone: the user must link
|
|
104
|
+
* again), `refresh_race`, or a gateway code such as `grant_revoked`. `detail`
|
|
105
|
+
* holds what came with it, such as `revoked_by` or `request_id`.
|
|
106
|
+
*/
|
|
107
|
+
export declare class LinkError extends Error {
|
|
108
|
+
readonly code: string;
|
|
109
|
+
readonly status: number;
|
|
110
|
+
readonly detail: Record<string, unknown>;
|
|
111
|
+
constructor(code: string, message: string, status: number, detail?: Record<string, unknown>);
|
|
112
|
+
}
|
|
113
|
+
type Fetch = typeof fetch;
|
|
114
|
+
type Gateway = {
|
|
115
|
+
gatewayUrl: string;
|
|
116
|
+
fetch?: Fetch;
|
|
117
|
+
};
|
|
118
|
+
type Client = Gateway & {
|
|
119
|
+
clientId: string;
|
|
120
|
+
clientSecret?: string;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Register an app openly (RFC 7591). A public client gets a client id; with
|
|
124
|
+
* `confidential`, a server also gets its client secret, returned this once:
|
|
125
|
+
* keep it in your secret manager.
|
|
126
|
+
*/
|
|
127
|
+
export declare function registerClient(args: Gateway & {
|
|
128
|
+
clientName: string;
|
|
129
|
+
redirectUris: string[];
|
|
130
|
+
confidential?: boolean;
|
|
131
|
+
}): Promise<{
|
|
132
|
+
clientId: string;
|
|
133
|
+
clientSecret?: string;
|
|
134
|
+
}>;
|
|
135
|
+
/**
|
|
136
|
+
* The approval URL to send the user to, with a fresh PKCE verifier and state.
|
|
137
|
+
* `clients` names the clients the app runs, such as `claude_code`, so the
|
|
138
|
+
* user can grant plans that serve only those. `plans` limits the page to
|
|
139
|
+
* those plan kinds, such as `openai_codex`. `purposes` says what the app does
|
|
140
|
+
* with each audience's plans, shown beside them. `grantId` (update mode)
|
|
141
|
+
* opens the page on that existing grant: the user changes its plans in place,
|
|
142
|
+
* its id and tokens stay, and the new code redeems for more tokens of it.
|
|
143
|
+
*/
|
|
144
|
+
export declare function authorizeUrl(args: AuthorizeOptions): Promise<{
|
|
145
|
+
url: string;
|
|
146
|
+
codeVerifier: string;
|
|
147
|
+
state: string;
|
|
148
|
+
}>;
|
|
149
|
+
export type AuthorizeOptions = {
|
|
150
|
+
gatewayUrl: string;
|
|
151
|
+
clientId: string;
|
|
152
|
+
redirectUri: string;
|
|
153
|
+
state?: string;
|
|
154
|
+
clients?: string[];
|
|
155
|
+
plans?: string[];
|
|
156
|
+
keyProviders?: string[];
|
|
157
|
+
purposes?: {
|
|
158
|
+
any_app?: string;
|
|
159
|
+
claude_code?: string;
|
|
160
|
+
muse_code?: string;
|
|
161
|
+
};
|
|
162
|
+
grantId?: string;
|
|
163
|
+
responseMode?: "query" | "web_message";
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* A fresh PKCE pair and state, made on your server: keep `codeVerifier` (and
|
|
167
|
+
* `state`) with the user's session, and hand the browser only `codeChallenge`
|
|
168
|
+
* and `state` for `DariLink.open`. The verifier then never reaches the page,
|
|
169
|
+
* and the page needs no Web Crypto.
|
|
170
|
+
*/
|
|
171
|
+
export declare function createAuthorization(state?: string): Promise<{
|
|
172
|
+
codeVerifier: string;
|
|
173
|
+
codeChallenge: string;
|
|
174
|
+
state: string;
|
|
175
|
+
}>;
|
|
176
|
+
type LinkPopup = {
|
|
177
|
+
closed: boolean;
|
|
178
|
+
close(): void;
|
|
179
|
+
location: {
|
|
180
|
+
href: string;
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
type LinkMessage = {
|
|
184
|
+
source: unknown;
|
|
185
|
+
origin: string;
|
|
186
|
+
data: unknown;
|
|
187
|
+
};
|
|
188
|
+
type LinkChannel = {
|
|
189
|
+
postMessage(data: unknown): void;
|
|
190
|
+
close(): void;
|
|
191
|
+
onmessage: ((event: {
|
|
192
|
+
data: unknown;
|
|
193
|
+
}) => void) | null;
|
|
194
|
+
};
|
|
195
|
+
export type LinkBrowser = {
|
|
196
|
+
open(url: string, target: string, features: string): LinkPopup | null;
|
|
197
|
+
addEventListener(type: "message", listener: (event: LinkMessage) => void): void;
|
|
198
|
+
removeEventListener(type: "message", listener: (event: LinkMessage) => void): void;
|
|
199
|
+
BroadcastChannel?: new (name: string) => LinkChannel;
|
|
200
|
+
location?: {
|
|
201
|
+
search: string;
|
|
202
|
+
};
|
|
203
|
+
close?(): void;
|
|
204
|
+
isSecureContext?: boolean;
|
|
205
|
+
screenX?: number;
|
|
206
|
+
screenY?: number;
|
|
207
|
+
outerWidth?: number;
|
|
208
|
+
outerHeight?: number;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* The approval step the user reached in the popup: `connect_subscription`
|
|
212
|
+
* (no plan to share yet) or `choose_plans`. A user who denies leaves at
|
|
213
|
+
* `denied`.
|
|
214
|
+
*/
|
|
215
|
+
export type LinkStep = "connect_subscription" | "choose_plans" | "denied";
|
|
216
|
+
/** Why the popup ended without a code, and the step the user left at (null before any). */
|
|
217
|
+
export type LinkExit = {
|
|
218
|
+
error: string;
|
|
219
|
+
step: LinkStep | null;
|
|
220
|
+
};
|
|
221
|
+
export type OpenLinkOptions = AuthorizeOptions & {
|
|
222
|
+
codeChallenge?: string;
|
|
223
|
+
fetch?: Fetch;
|
|
224
|
+
window?: LinkBrowser;
|
|
225
|
+
onEvent?: (step: LinkStep) => void;
|
|
226
|
+
onExit?: (exit: LinkExit) => void;
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Open Dari's approval page in a popup and resolve with the code once the
|
|
230
|
+
* user approves. Call it from a click handler, so the browser allows the
|
|
231
|
+
* popup. Send `code` and `codeVerifier` to your server to `exchange`.
|
|
232
|
+
*
|
|
233
|
+
* The options may be a function returning them (a promise is fine): it is
|
|
234
|
+
* awaited only after the popup opens, so fetching a client id or state from
|
|
235
|
+
* your server first does not get the popup blocked.
|
|
236
|
+
*
|
|
237
|
+
* Rejects with a `LinkError`: `popup_blocked`, `popup_closed` when the user
|
|
238
|
+
* closes it, or the OAuth error the page returned, such as `access_denied`.
|
|
239
|
+
* The result is accepted only from the popup and from the gateway's approval
|
|
240
|
+
* origin, and only with the state this call made.
|
|
241
|
+
*
|
|
242
|
+
* A popup that signed the user in along the way can lose its link to this
|
|
243
|
+
* window; the approval then lands on your redirect URI inside the popup. Call
|
|
244
|
+
* `DariLink.callback()` on that page (or load `/v1/link/callback.js` there)
|
|
245
|
+
* and this call still resolves.
|
|
246
|
+
*/
|
|
247
|
+
export declare function openLink(options: OpenLinkOptions | (() => OpenLinkOptions | Promise<OpenLinkOptions>), browserOverride?: LinkBrowser): Promise<{
|
|
248
|
+
code: string;
|
|
249
|
+
state: string;
|
|
250
|
+
codeVerifier: string | null;
|
|
251
|
+
}>;
|
|
252
|
+
/**
|
|
253
|
+
* On your redirect URI's page: hand an approval that arrived there in the
|
|
254
|
+
* popup to the window that called `DariLink.open`, and close the popup.
|
|
255
|
+
* Returns false when the page holds no Dari Link result.
|
|
256
|
+
*/
|
|
257
|
+
export declare function completeLink(args?: {
|
|
258
|
+
window?: LinkBrowser;
|
|
259
|
+
}): boolean;
|
|
260
|
+
/** The Dari mark: a white turtle on a black disc, square, scalable. */
|
|
261
|
+
export declare const DARI_MARK_SVG: string;
|
|
262
|
+
type ButtonHost = {
|
|
263
|
+
innerHTML: string;
|
|
264
|
+
querySelector(selector: string): {
|
|
265
|
+
addEventListener(type: "click", listener: () => void): void;
|
|
266
|
+
} | null;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Render the standard "Connect with Dari" button into `container` and call
|
|
270
|
+
* `onClick` (which should call `DariLink.open`) when it is pressed. `dark`
|
|
271
|
+
* (the default) is for light pages: black button, white text; `light` is for
|
|
272
|
+
* dark pages. The button is 40px tall with the mark at 20px; see the README's
|
|
273
|
+
* button guidelines.
|
|
274
|
+
*/
|
|
275
|
+
export declare function renderButton(container: ButtonHost, options: {
|
|
276
|
+
onClick: () => void;
|
|
277
|
+
theme?: "dark" | "light";
|
|
278
|
+
label?: string;
|
|
279
|
+
}): void;
|
|
280
|
+
/**
|
|
281
|
+
* `DariLink.open(...)` is the one call a "Connect with Dari" button makes;
|
|
282
|
+
* `DariLink.callback()` goes on the redirect URI's page; `DariLink.renderButton`
|
|
283
|
+
* draws the button.
|
|
284
|
+
*/
|
|
285
|
+
export declare const DariLink: {
|
|
286
|
+
open: typeof openLink;
|
|
287
|
+
callback: typeof completeLink;
|
|
288
|
+
renderButton: typeof renderButton;
|
|
289
|
+
};
|
|
290
|
+
/** Redeem the code from the approval redirect for the grant's first tokens. */
|
|
291
|
+
export declare function exchange(args: Client & {
|
|
292
|
+
redirectUri: string;
|
|
293
|
+
code: string;
|
|
294
|
+
codeVerifier: string;
|
|
295
|
+
}): Promise<LinkTokenSet>;
|
|
296
|
+
/**
|
|
297
|
+
* A new access token. A public client's refresh token rotates: the one passed
|
|
298
|
+
* in stops working. A confidential client's stays the same.
|
|
299
|
+
*/
|
|
300
|
+
export declare function refresh(args: Client & {
|
|
301
|
+
refreshToken: string;
|
|
302
|
+
}): Promise<LinkTokenSet>;
|
|
303
|
+
/**
|
|
304
|
+
* Mint a child token for a sandbox or worker, from the grant's access token
|
|
305
|
+
* or from your token storage (refreshed as `linkFetch` does): it serves only
|
|
306
|
+
* its models (each at one thinking level when pinned) for up to twelve
|
|
307
|
+
* hours, cannot refresh, mint, or read the grant, and dies with the grant.
|
|
308
|
+
*/
|
|
309
|
+
export declare function childToken(args: Client & ({
|
|
310
|
+
accessToken: string;
|
|
311
|
+
} | TokenStorage) & {
|
|
312
|
+
models: Array<{
|
|
313
|
+
model: string;
|
|
314
|
+
thinking?: string;
|
|
315
|
+
}>;
|
|
316
|
+
expiresIn?: number;
|
|
317
|
+
tags?: Record<string, string>;
|
|
318
|
+
now?: () => number;
|
|
319
|
+
}): Promise<LinkChildToken>;
|
|
320
|
+
/** End the grant a token belongs to, as when the user disconnects in the app. */
|
|
321
|
+
export declare function revoke(args: Client & {
|
|
322
|
+
token: string;
|
|
323
|
+
}): Promise<void>;
|
|
324
|
+
/**
|
|
325
|
+
* A fetch for the gateway on one grant: paths resolve against `gatewayUrl`
|
|
326
|
+
* and the access token is attached. Your storage is the source of truth: the
|
|
327
|
+
* tokens are read with `loadTokens` on every call, refreshed shortly before
|
|
328
|
+
* they expire and once after a 401 for an expired token, and every new token
|
|
329
|
+
* set reaches `saveTokens` before use. Concurrent calls in a process share
|
|
330
|
+
* one refresh. A grant that is gone throws LinkError with code
|
|
331
|
+
* `invalid_grant` from the refresh, and a revoked access token's 401
|
|
332
|
+
* (`grant_revoked`) is returned as it is: either way the user is unlinked and
|
|
333
|
+
* must link again. With no tokens stored it throws `not_linked`.
|
|
334
|
+
*/
|
|
335
|
+
export declare function linkFetch(args: Client & TokenStorage & {
|
|
336
|
+
now?: () => number;
|
|
337
|
+
}): (input: string, init?: RequestInit) => Promise<Response>;
|
|
338
|
+
export type LinkUsageTotals = {
|
|
339
|
+
requests: number;
|
|
340
|
+
errors: number;
|
|
341
|
+
input_tokens: number;
|
|
342
|
+
output_tokens: number;
|
|
343
|
+
cache_read_tokens: number;
|
|
344
|
+
cache_write_tokens: number;
|
|
345
|
+
};
|
|
346
|
+
export type LinkUsage = LinkUsageTotals & {
|
|
347
|
+
complete_through: string | null;
|
|
348
|
+
by_model: Array<LinkUsageTotals & {
|
|
349
|
+
model: string;
|
|
350
|
+
thinking: string | null;
|
|
351
|
+
}>;
|
|
352
|
+
by_tag?: Array<LinkUsageTotals & {
|
|
353
|
+
value: string | null;
|
|
354
|
+
}>;
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* What the grant's requests used, child tokens' included: only requests
|
|
358
|
+
* carrying every given tag (X-Dari-Tags or a child token's tags), received
|
|
359
|
+
* in [since, until), and with `groupByTag` also totalled per value of that
|
|
360
|
+
* tag. Pass the `linkFetch` your app already uses as `fetcher` so this shares
|
|
361
|
+
* its refresh; otherwise pass the client and token storage.
|
|
362
|
+
*/
|
|
363
|
+
export declare function usage(args: ({
|
|
364
|
+
fetcher: (input: string, init?: RequestInit) => Promise<Response>;
|
|
365
|
+
} | (Client & TokenStorage & {
|
|
366
|
+
now?: () => number;
|
|
367
|
+
})) & {
|
|
368
|
+
tags?: Record<string, string>;
|
|
369
|
+
since?: Date;
|
|
370
|
+
until?: Date;
|
|
371
|
+
groupByTag?: string;
|
|
372
|
+
}): Promise<LinkUsage>;
|
|
373
|
+
/**
|
|
374
|
+
* Where an app keeps one grant's tokens. `loadTokens` returns null when the
|
|
375
|
+
* user has not linked (or unlinked): calls then throw LinkError `not_linked`
|
|
376
|
+
* without reaching the gateway.
|
|
377
|
+
*/
|
|
378
|
+
export type TokenStorage = {
|
|
379
|
+
loadTokens: () => LinkTokenSet | null | Promise<LinkTokenSet | null>;
|
|
380
|
+
saveTokens: (tokens: LinkTokenSet) => void | Promise<void>;
|
|
381
|
+
};
|
|
382
|
+
export {};
|