@providerprotocol/ai 0.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 +84 -0
- package/dist/anthropic/index.d.ts +41 -0
- package/dist/anthropic/index.js +500 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/chunk-CUCRF5W6.js +136 -0
- package/dist/chunk-CUCRF5W6.js.map +1 -0
- package/dist/chunk-FTFX2VET.js +424 -0
- package/dist/chunk-FTFX2VET.js.map +1 -0
- package/dist/chunk-QUUX4G7U.js +117 -0
- package/dist/chunk-QUUX4G7U.js.map +1 -0
- package/dist/chunk-Y6Q7JCNP.js +39 -0
- package/dist/chunk-Y6Q7JCNP.js.map +1 -0
- package/dist/google/index.d.ts +69 -0
- package/dist/google/index.js +517 -0
- package/dist/google/index.js.map +1 -0
- package/dist/http/index.d.ts +61 -0
- package/dist/http/index.js +43 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index.d.ts +792 -0
- package/dist/index.js +898 -0
- package/dist/index.js.map +1 -0
- package/dist/openai/index.d.ts +204 -0
- package/dist/openai/index.js +1340 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/provider-CUJWjgNl.d.ts +192 -0
- package/dist/retry-I2661_rv.d.ts +118 -0
- package/package.json +88 -0
- package/src/anthropic/index.ts +3 -0
- package/src/core/image.ts +188 -0
- package/src/core/llm.ts +619 -0
- package/src/core/provider.ts +92 -0
- package/src/google/index.ts +3 -0
- package/src/http/errors.ts +112 -0
- package/src/http/fetch.ts +210 -0
- package/src/http/index.ts +31 -0
- package/src/http/keys.ts +136 -0
- package/src/http/retry.ts +205 -0
- package/src/http/sse.ts +136 -0
- package/src/index.ts +32 -0
- package/src/openai/index.ts +9 -0
- package/src/providers/anthropic/index.ts +17 -0
- package/src/providers/anthropic/llm.ts +196 -0
- package/src/providers/anthropic/transform.ts +452 -0
- package/src/providers/anthropic/types.ts +213 -0
- package/src/providers/google/index.ts +17 -0
- package/src/providers/google/llm.ts +203 -0
- package/src/providers/google/transform.ts +487 -0
- package/src/providers/google/types.ts +214 -0
- package/src/providers/openai/index.ts +151 -0
- package/src/providers/openai/llm.completions.ts +201 -0
- package/src/providers/openai/llm.responses.ts +211 -0
- package/src/providers/openai/transform.completions.ts +628 -0
- package/src/providers/openai/transform.responses.ts +718 -0
- package/src/providers/openai/types.ts +711 -0
- package/src/types/content.ts +133 -0
- package/src/types/errors.ts +85 -0
- package/src/types/index.ts +105 -0
- package/src/types/llm.ts +211 -0
- package/src/types/messages.ts +182 -0
- package/src/types/provider.ts +195 -0
- package/src/types/schema.ts +58 -0
- package/src/types/stream.ts +146 -0
- package/src/types/thread.ts +226 -0
- package/src/types/tool.ts +88 -0
- package/src/types/turn.ts +118 -0
- package/src/utils/id.ts +28 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Message, AssistantMessage } from './messages.ts';
|
|
2
|
+
import type { ToolExecution } from './tool.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Token usage information
|
|
6
|
+
*/
|
|
7
|
+
export interface TokenUsage {
|
|
8
|
+
/** Input tokens across all cycles */
|
|
9
|
+
inputTokens: number;
|
|
10
|
+
|
|
11
|
+
/** Output tokens across all cycles */
|
|
12
|
+
outputTokens: number;
|
|
13
|
+
|
|
14
|
+
/** Total tokens */
|
|
15
|
+
totalTokens: number;
|
|
16
|
+
|
|
17
|
+
/** Per-cycle breakdown (if available) */
|
|
18
|
+
cycles?: Array<{
|
|
19
|
+
inputTokens: number;
|
|
20
|
+
outputTokens: number;
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A Turn represents the complete result of one inference call,
|
|
26
|
+
* including all messages produced during tool execution loops.
|
|
27
|
+
*/
|
|
28
|
+
export interface Turn<TData = unknown> {
|
|
29
|
+
/**
|
|
30
|
+
* All messages produced during this inference, in chronological order.
|
|
31
|
+
* Types: UserMessage, AssistantMessage (may include toolCalls), ToolResultMessage
|
|
32
|
+
*/
|
|
33
|
+
readonly messages: Message[];
|
|
34
|
+
|
|
35
|
+
/** The final assistant response (convenience accessor) */
|
|
36
|
+
readonly response: AssistantMessage;
|
|
37
|
+
|
|
38
|
+
/** Tool executions that occurred during this turn */
|
|
39
|
+
readonly toolExecutions: ToolExecution[];
|
|
40
|
+
|
|
41
|
+
/** Aggregate token usage for the entire turn */
|
|
42
|
+
readonly usage: TokenUsage;
|
|
43
|
+
|
|
44
|
+
/** Total number of inference cycles (1 + number of tool rounds) */
|
|
45
|
+
readonly cycles: number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Structured output data (if structure was provided).
|
|
49
|
+
* Type is inferred from the schema when using TypeScript.
|
|
50
|
+
*/
|
|
51
|
+
readonly data?: TData;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create a Turn from accumulated data
|
|
56
|
+
*/
|
|
57
|
+
export function createTurn<TData = unknown>(
|
|
58
|
+
messages: Message[],
|
|
59
|
+
toolExecutions: ToolExecution[],
|
|
60
|
+
usage: TokenUsage,
|
|
61
|
+
cycles: number,
|
|
62
|
+
data?: TData
|
|
63
|
+
): Turn<TData> {
|
|
64
|
+
// Find the last assistant message as the response
|
|
65
|
+
const response = messages
|
|
66
|
+
.filter((m): m is AssistantMessage => m.type === 'assistant')
|
|
67
|
+
.pop();
|
|
68
|
+
|
|
69
|
+
if (!response) {
|
|
70
|
+
throw new Error('Turn must contain at least one assistant message');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
messages,
|
|
75
|
+
response,
|
|
76
|
+
toolExecutions,
|
|
77
|
+
usage,
|
|
78
|
+
cycles,
|
|
79
|
+
data,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create empty token usage
|
|
85
|
+
*/
|
|
86
|
+
export function emptyUsage(): TokenUsage {
|
|
87
|
+
return {
|
|
88
|
+
inputTokens: 0,
|
|
89
|
+
outputTokens: 0,
|
|
90
|
+
totalTokens: 0,
|
|
91
|
+
cycles: [],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Aggregate token usage from multiple cycles
|
|
97
|
+
*/
|
|
98
|
+
export function aggregateUsage(usages: TokenUsage[]): TokenUsage {
|
|
99
|
+
const cycles: TokenUsage['cycles'] = [];
|
|
100
|
+
let inputTokens = 0;
|
|
101
|
+
let outputTokens = 0;
|
|
102
|
+
|
|
103
|
+
for (const usage of usages) {
|
|
104
|
+
inputTokens += usage.inputTokens;
|
|
105
|
+
outputTokens += usage.outputTokens;
|
|
106
|
+
cycles.push({
|
|
107
|
+
inputTokens: usage.inputTokens,
|
|
108
|
+
outputTokens: usage.outputTokens,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
inputTokens,
|
|
114
|
+
outputTokens,
|
|
115
|
+
totalTokens: inputTokens + outputTokens,
|
|
116
|
+
cycles,
|
|
117
|
+
};
|
|
118
|
+
}
|
package/src/utils/id.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a unique ID
|
|
3
|
+
* Uses crypto.randomUUID if available, falls back to a simple implementation
|
|
4
|
+
*/
|
|
5
|
+
export function generateId(): string {
|
|
6
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
7
|
+
return crypto.randomUUID();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Fallback for environments without crypto.randomUUID
|
|
11
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
12
|
+
const r = (Math.random() * 16) | 0;
|
|
13
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
14
|
+
return v.toString(16);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Generate a short ID (for tool call IDs, etc.)
|
|
20
|
+
*/
|
|
21
|
+
export function generateShortId(prefix = ''): string {
|
|
22
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
23
|
+
let result = prefix;
|
|
24
|
+
for (let i = 0; i < 12; i++) {
|
|
25
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|