@better-window-ai/types 1.0.1
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/LICENSE +21 -0
- package/README.md +170 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# @better-window-ai/types
|
|
2
|
+
|
|
3
|
+
TypeScript type definitions for the `window.ai` API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @better-window-ai/types
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### In TypeScript Projects
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import type { WindowAI, AiModelInfo } from "@better-window-ai/types";
|
|
17
|
+
|
|
18
|
+
// Check if AI is available
|
|
19
|
+
const available = await window.ai.available();
|
|
20
|
+
if (available) {
|
|
21
|
+
// Get current model
|
|
22
|
+
const model = await window.ai.getCurrentModel();
|
|
23
|
+
console.log(model?.name);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Global Types
|
|
28
|
+
|
|
29
|
+
The package automatically extends the global `Window` interface, so you can use `window.ai` with full type support:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// No import needed - types are global
|
|
33
|
+
const available = await window.ai.available();
|
|
34
|
+
const models = await window.ai.getModels();
|
|
35
|
+
const current = await window.ai.getCurrentModel();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API Reference
|
|
39
|
+
|
|
40
|
+
### `window.ai.available()`
|
|
41
|
+
|
|
42
|
+
Check if AI functionality is available.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const isAvailable: boolean = await window.ai.available();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `window.ai.getModels()`
|
|
49
|
+
|
|
50
|
+
Get all available model configurations.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const models: AiModelInfo[] = await window.ai.getModels();
|
|
54
|
+
// [{ id: "...", name: "GPT-4", model: "gpt-4o" }]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `window.ai.getCurrentModel()`
|
|
58
|
+
|
|
59
|
+
Get the currently active model configuration.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const model: AiModelInfo | null = await window.ai.getCurrentModel();
|
|
63
|
+
// { id: "...", name: "GPT-4", model: "gpt-4o" } or null
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `window.ai.generateText(body)`
|
|
67
|
+
|
|
68
|
+
Generate text using AI. Accepts OpenAI SDK compatible parameters (without `model` and `stream`) and returns OpenAI SDK compatible response.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import type OpenAI from "openai";
|
|
72
|
+
|
|
73
|
+
const response = await window.ai.generateText({
|
|
74
|
+
messages: [
|
|
75
|
+
{ role: "system", content: "You are a helpful assistant" },
|
|
76
|
+
{ role: "user", content: "Hello, AI!" },
|
|
77
|
+
],
|
|
78
|
+
temperature: 0.7,
|
|
79
|
+
max_tokens: 1000,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
console.log(response.choices[0].message.content);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Note:** The `model` and `stream` parameters are omitted - the extension uses the configured model and non-streaming mode.
|
|
86
|
+
|
|
87
|
+
See [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat/create) for full parameter documentation.
|
|
88
|
+
|
|
89
|
+
## Type Definitions
|
|
90
|
+
|
|
91
|
+
### `AiModelInfo`
|
|
92
|
+
|
|
93
|
+
Model configuration information.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
interface AiModelInfo {
|
|
97
|
+
id: string;
|
|
98
|
+
name: string;
|
|
99
|
+
model: string;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### `WindowAI`
|
|
104
|
+
|
|
105
|
+
The main window.ai interface.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
interface WindowAI {
|
|
109
|
+
available(): Promise<boolean>;
|
|
110
|
+
getModels(): Promise<AiModelInfo[]>;
|
|
111
|
+
getCurrentModel(): Promise<AiModelInfo | null>;
|
|
112
|
+
generateText(
|
|
113
|
+
body: Omit<
|
|
114
|
+
OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming,
|
|
115
|
+
"stream" | "model"
|
|
116
|
+
>
|
|
117
|
+
): Promise<OpenAI.Chat.Completions.ChatCompletion>;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Examples
|
|
122
|
+
|
|
123
|
+
### Basic Text Generation
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const response = await window.ai.generateText({
|
|
127
|
+
messages: [{ role: "user", content: "Explain TypeScript" }],
|
|
128
|
+
});
|
|
129
|
+
console.log(response.choices[0].message.content);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### With System Prompt
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
const response = await window.ai.generateText({
|
|
136
|
+
messages: [
|
|
137
|
+
{ role: "system", content: "You are a poet" },
|
|
138
|
+
{ role: "user", content: "Write a haiku about coding" },
|
|
139
|
+
],
|
|
140
|
+
temperature: 0.8,
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Checking Availability
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
if (await window.ai.available()) {
|
|
148
|
+
const models = await window.ai.getModels();
|
|
149
|
+
console.log("Available models:", models);
|
|
150
|
+
} else {
|
|
151
|
+
console.log("Please configure AI models in the extension first");
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Get Current Model
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
const current = await window.ai.getCurrentModel();
|
|
159
|
+
if (current) {
|
|
160
|
+
console.log(`Using model: ${current.name} (${current.model})`);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Browser Extension Integration
|
|
165
|
+
|
|
166
|
+
This package is designed for use with browser extensions that inject the `window.ai` API. Make sure the extension is installed and configured before using these APIs.
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type OpenAI from "openai";
|
|
2
|
+
/**
|
|
3
|
+
* 模型信息
|
|
4
|
+
*/
|
|
5
|
+
export interface AiModelInfo {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
model: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* window.ai API 类型定义
|
|
12
|
+
*/
|
|
13
|
+
export interface WindowAI {
|
|
14
|
+
/**
|
|
15
|
+
* 检查 AI 功能是否可用
|
|
16
|
+
* @returns 如果有可用的激活配置则返回 true
|
|
17
|
+
*/
|
|
18
|
+
available(): Promise<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* 获取所有可用的模型配置
|
|
21
|
+
* @returns 模型配置列表
|
|
22
|
+
*/
|
|
23
|
+
getModels(): Promise<AiModelInfo[]>;
|
|
24
|
+
/**
|
|
25
|
+
* 获取当前激活的模型配置
|
|
26
|
+
* @returns 当前模型配置,如果没有则返回 null
|
|
27
|
+
*/
|
|
28
|
+
getCurrentModel(): Promise<AiModelInfo | null>;
|
|
29
|
+
/**
|
|
30
|
+
* 调用 AI 生成文本
|
|
31
|
+
* @param prompt 用户输入的提示词
|
|
32
|
+
* @param options 可选配置
|
|
33
|
+
* @returns 生成的文本内容或 function call 结果
|
|
34
|
+
*/
|
|
35
|
+
generateText(body: Omit<OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming, "stream" | "model">): Promise<OpenAI.Chat.Completions.ChatCompletion>;
|
|
36
|
+
}
|
|
37
|
+
declare global {
|
|
38
|
+
interface Window {
|
|
39
|
+
ai: WindowAI;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9B;;;OAGG;IACH,SAAS,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAEpC;;;OAGG;IACH,eAAe,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IAE/C;;;;;OAKG;IACH,YAAY,CACV,IAAI,EAAE,IAAI,CACR,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,sCAAsC,EAC9D,QAAQ,GAAG,OAAO,CACnB,GACA,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;CACpD;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,EAAE,EAAE,QAAQ,CAAC;KACd;CACF"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@better-window-ai/types",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "TypeScript type definitions for window.ai API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"window.ai",
|
|
17
|
+
"ai",
|
|
18
|
+
"openai",
|
|
19
|
+
"typescript",
|
|
20
|
+
"types",
|
|
21
|
+
"browser-extension",
|
|
22
|
+
"function-calling"
|
|
23
|
+
],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/kit-kack/better-window-ai.git",
|
|
29
|
+
"directory": "packages/window-ai-types"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
}
|
|
34
|
+
}
|