api-registry.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import type {
  2. Api,
  3. AssistantMessageEventStream,
  4. Context,
  5. Model,
  6. SimpleStreamOptions,
  7. StreamFunction,
  8. StreamOptions,
  9. } from "./types.ts";
  10. export type ApiStreamFunction = (
  11. model: Model<Api>,
  12. context: Context,
  13. options?: StreamOptions,
  14. ) => AssistantMessageEventStream;
  15. export type ApiStreamSimpleFunction = (
  16. model: Model<Api>,
  17. context: Context,
  18. options?: SimpleStreamOptions,
  19. ) => AssistantMessageEventStream;
  20. export interface ApiProvider<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> {
  21. api: TApi;
  22. stream: StreamFunction<TApi, TOptions>;
  23. streamSimple: StreamFunction<TApi, SimpleStreamOptions>;
  24. }
  25. interface ApiProviderInternal {
  26. api: Api;
  27. stream: ApiStreamFunction;
  28. streamSimple: ApiStreamSimpleFunction;
  29. }
  30. type RegisteredApiProvider = {
  31. provider: ApiProviderInternal;
  32. sourceId?: string;
  33. };
  34. const apiProviderRegistry = new Map<string, RegisteredApiProvider>();
  35. function wrapStream<TApi extends Api, TOptions extends StreamOptions>(
  36. api: TApi,
  37. stream: StreamFunction<TApi, TOptions>,
  38. ): ApiStreamFunction {
  39. return (model, context, options) => {
  40. if (model.api !== api) {
  41. throw new Error(`Mismatched api: ${model.api} expected ${api}`);
  42. }
  43. return stream(model as Model<TApi>, context, options as TOptions);
  44. };
  45. }
  46. function wrapStreamSimple<TApi extends Api>(
  47. api: TApi,
  48. streamSimple: StreamFunction<TApi, SimpleStreamOptions>,
  49. ): ApiStreamSimpleFunction {
  50. return (model, context, options) => {
  51. if (model.api !== api) {
  52. throw new Error(`Mismatched api: ${model.api} expected ${api}`);
  53. }
  54. return streamSimple(model as Model<TApi>, context, options);
  55. };
  56. }
  57. export function registerApiProvider<TApi extends Api, TOptions extends StreamOptions>(
  58. provider: ApiProvider<TApi, TOptions>,
  59. sourceId?: string,
  60. ): void {
  61. apiProviderRegistry.set(provider.api, {
  62. provider: {
  63. api: provider.api,
  64. stream: wrapStream(provider.api, provider.stream),
  65. streamSimple: wrapStreamSimple(provider.api, provider.streamSimple),
  66. },
  67. sourceId,
  68. });
  69. }
  70. export function getApiProvider(api: Api): ApiProviderInternal | undefined {
  71. return apiProviderRegistry.get(api)?.provider;
  72. }
  73. export function getApiProviders(): ApiProviderInternal[] {
  74. return Array.from(apiProviderRegistry.values(), (entry) => entry.provider);
  75. }
  76. export function unregisterApiProviders(sourceId: string): void {
  77. for (const [api, entry] of apiProviderRegistry.entries()) {
  78. if (entry.sourceId === sourceId) {
  79. apiProviderRegistry.delete(api);
  80. }
  81. }
  82. }
  83. export function clearApiProviders(): void {
  84. apiProviderRegistry.clear();
  85. }