-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtypes.ts
More file actions
135 lines (129 loc) · 5 KB
/
types.ts
File metadata and controls
135 lines (129 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { puterClients } from '../clients';
import type { IExtensionClientInstances } from '../clients/types';
import type { puterServices } from '../services';
import type { IExtensionServiceInstances } from '../services/types';
import type { puterStores } from '../stores';
import type { IExtensionStoreInstances } from '../stores/types';
import type { DriverConcurrentConfig, DriverRateLimitConfig } from './meta';
import type { IConfig, LayerInstances, WithCostsReporting } from '../types';
/**
* Extension-augmentable driver registry. Extensions add their own driver
* instance types via TypeScript declaration merging:
*
* declare module '@heyputer/backend/drivers/types' {
* interface IExtensionDriverInstances {
* myDriver: MyDriver;
* }
* }
*
* Augmentations flow into `this.drivers` (PuterController) and into the
* `extension.import('driver')` proxy.
*/
export interface IExtensionDriverInstances {
/**
* Open index signature so reads of extension-only driver keys return
* `unknown` instead of a type error. Concrete declaration-merged keys
* override this for that name.
*/
[key: string]: unknown;
}
export type IPuterDriver<T extends WithCostsReporting = WithCostsReporting> =
new (
config: IConfig,
clients: LayerInstances<typeof puterClients> &
IExtensionClientInstances,
stores: LayerInstances<typeof puterStores> & IExtensionStoreInstances,
services: LayerInstances<typeof puterServices> &
IExtensionServiceInstances,
) => T;
/**
* Base class for v2 drivers.
*
* A driver implements a named interface (e.g., `puter-chat-completion`) and
* exposes methods that match the interface contract. Multiple drivers can
* implement the same interface (e.g., `openai-completion` and `claude` both
* implement `puter-chat-completion`).
*
* **Two ways to declare a driver:**
*
* 1. Decorator:
* ```ts
* @Driver('puter-chat-completion', { name: 'openai', default: true })
* class OpenAIChat extends PuterDriver { ... }
* ```
*
* 2. Imperative (no decorator):
* ```ts
* class OpenAIChat extends PuterDriver {
* readonly driverInterface = 'puter-chat-completion';
* readonly driverName = 'openai';
* readonly isDefault = true;
* }
* ```
*/
export const PuterDriver = class PuterDriver implements WithCostsReporting {
/** The interface this driver implements. Set by `@Driver` or override. */
declare readonly driverInterface?: string;
/** Unique name within its interface. Set by `@Driver` or override. */
declare readonly driverName?: string;
/** When true, this is the default driver for its interface. */
declare readonly isDefault?: boolean;
/**
* Rate-limit policy applied to RPC calls into this driver. Set by
* `@Driver({ rateLimit: ... })` or declared imperatively. See
* `DriverRateLimitConfig` in `./meta` for the shape.
*/
declare readonly rateLimit?: DriverRateLimitConfig;
/**
* Concurrent in-flight policy applied to RPC calls into this driver.
* Set by `@Driver({ concurrent: ... })` or declared imperatively.
* See `DriverConcurrentConfig` in `./meta` for the shape.
*/
declare readonly concurrent?: DriverConcurrentConfig;
constructor(
protected config: IConfig,
protected clients: LayerInstances<typeof puterClients> &
IExtensionClientInstances,
protected stores: LayerInstances<typeof puterStores> &
IExtensionStoreInstances,
protected services: LayerInstances<typeof puterServices> &
IExtensionServiceInstances,
) {}
public onServerStart() {
return;
}
public onServerPrepareShutdown() {
return;
}
public onServerShutdown() {
return;
}
public getReportedCosts(): // eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>[] // eslint-disable-next-line @typescript-eslint/no-explicit-any
| Promise<Record<string, any>[]> {
return [];
}
} satisfies IPuterDriver<WithCostsReporting>;
export type IPuterDriverRegistry = Record<
string,
| IPuterDriver<WithCostsReporting>
| (InstanceType<IPuterDriver<WithCostsReporting>> & Record<string, unknown>)
>;