@ank1015/providers 0.0.1 → 0.0.3
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 +93 -383
- package/dist/agent/conversation.d.ts +97 -0
- package/dist/agent/conversation.d.ts.map +1 -0
- package/dist/agent/conversation.js +328 -0
- package/dist/agent/conversation.js.map +1 -0
- package/dist/agent/runner.d.ts +37 -0
- package/dist/agent/runner.d.ts.map +1 -0
- package/dist/agent/runner.js +169 -0
- package/dist/agent/runner.js.map +1 -0
- package/dist/agent/tools/calculate.d.ts +15 -0
- package/dist/agent/tools/calculate.d.ts.map +1 -0
- package/dist/agent/tools/calculate.js +23 -0
- package/dist/agent/tools/calculate.js.map +1 -0
- package/dist/agent/tools/get-current-time.d.ts +15 -0
- package/dist/agent/tools/get-current-time.d.ts.map +1 -0
- package/dist/agent/tools/get-current-time.js +38 -0
- package/dist/agent/tools/get-current-time.js.map +1 -0
- package/dist/agent/tools/index.d.ts +3 -0
- package/dist/agent/tools/index.d.ts.map +1 -0
- package/dist/agent/tools/index.js +3 -0
- package/dist/agent/tools/index.js.map +1 -0
- package/dist/agent/types.d.ts +53 -31
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/types.js +1 -2
- package/dist/agent/utils.d.ts +14 -0
- package/dist/agent/utils.d.ts.map +1 -0
- package/dist/agent/utils.js +59 -0
- package/dist/agent/utils.js.map +1 -0
- package/dist/index.d.ts +16 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -28
- package/dist/index.js.map +1 -1
- package/dist/llm.d.ts +15 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +92 -0
- package/dist/llm.js.map +1 -0
- package/dist/models.d.ts +8 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +25 -112
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +72 -227
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +30 -32
- package/dist/models.js.map +1 -1
- package/dist/providers/google/complete.d.ts +3 -0
- package/dist/providers/google/complete.d.ts.map +1 -0
- package/dist/providers/google/complete.js +53 -0
- package/dist/providers/google/complete.js.map +1 -0
- package/dist/providers/google/index.d.ts +6 -0
- package/dist/providers/google/index.d.ts.map +1 -0
- package/dist/providers/google/index.js +6 -0
- package/dist/providers/google/index.js.map +1 -0
- package/dist/providers/google/stream.d.ts +3 -0
- package/dist/providers/google/stream.d.ts.map +1 -0
- package/dist/providers/{google.js → google/stream.js} +67 -231
- package/dist/providers/google/stream.js.map +1 -0
- package/dist/providers/google/types.d.ts +8 -0
- package/dist/providers/google/types.d.ts.map +1 -0
- package/dist/providers/google/types.js +2 -0
- package/dist/providers/google/types.js.map +1 -0
- package/dist/providers/google/utils.d.ts +30 -0
- package/dist/providers/google/utils.d.ts.map +1 -0
- package/dist/providers/google/utils.js +354 -0
- package/dist/providers/google/utils.js.map +1 -0
- package/dist/providers/openai/complete.d.ts +3 -0
- package/dist/providers/openai/complete.d.ts.map +1 -0
- package/dist/providers/openai/complete.js +57 -0
- package/dist/providers/openai/complete.js.map +1 -0
- package/dist/providers/openai/index.d.ts +4 -0
- package/dist/providers/openai/index.d.ts.map +1 -0
- package/dist/providers/openai/index.js +4 -0
- package/dist/providers/openai/index.js.map +1 -0
- package/dist/providers/openai/stream.d.ts +3 -0
- package/dist/providers/openai/stream.d.ts.map +1 -0
- package/dist/providers/{openai.js → openai/stream.js} +74 -152
- package/dist/providers/openai/stream.js.map +1 -0
- package/dist/providers/openai/types.d.ts +8 -0
- package/dist/providers/openai/types.d.ts.map +1 -0
- package/dist/providers/openai/types.js +2 -0
- package/dist/providers/openai/types.js.map +1 -0
- package/dist/providers/openai/utils.d.ts +13 -0
- package/dist/providers/openai/utils.d.ts.map +1 -0
- package/dist/providers/openai/utils.js +285 -0
- package/dist/providers/openai/utils.js.map +1 -0
- package/dist/types.d.ts +95 -87
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -9
- package/dist/types.js.map +1 -1
- package/dist/utils/event-stream.d.ts +2 -2
- package/dist/utils/event-stream.d.ts.map +1 -1
- package/dist/utils/event-stream.js +2 -7
- package/dist/utils/event-stream.js.map +1 -1
- package/dist/utils/json-parse.js +3 -6
- package/dist/utils/json-parse.js.map +1 -1
- package/dist/utils/overflow.d.ts +51 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +106 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.js +1 -4
- package/dist/utils/sanitize-unicode.js.map +1 -1
- package/dist/utils/uuid.d.ts +6 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/dist/utils/uuid.js +9 -0
- package/dist/utils/uuid.js.map +1 -0
- package/dist/utils/validation.d.ts +10 -3
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +20 -12
- package/dist/utils/validation.js.map +1 -1
- package/package.json +47 -8
- package/biome.json +0 -43
- package/dist/agent/agent-loop.d.ts +0 -5
- package/dist/agent/agent-loop.d.ts.map +0 -1
- package/dist/agent/agent-loop.js +0 -219
- package/dist/agent/agent-loop.js.map +0 -1
- package/dist/providers/convert.d.ts +0 -6
- package/dist/providers/convert.d.ts.map +0 -1
- package/dist/providers/convert.js +0 -207
- package/dist/providers/convert.js.map +0 -1
- package/dist/providers/google.d.ts +0 -26
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/openai.d.ts +0 -17
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/stream.d.ts +0 -4
- package/dist/stream.d.ts.map +0 -1
- package/dist/stream.js +0 -40
- package/dist/stream.js.map +0 -1
- package/dist/test-google-agent-loop.d.ts +0 -2
- package/dist/test-google-agent-loop.d.ts.map +0 -1
- package/dist/test-google-agent-loop.js +0 -186
- package/dist/test-google-agent-loop.js.map +0 -1
- package/dist/test-google.d.ts +0 -2
- package/dist/test-google.d.ts.map +0 -1
- package/dist/test-google.js +0 -41
- package/dist/test-google.js.map +0 -1
- package/src/agent/agent-loop.ts +0 -275
- package/src/agent/types.ts +0 -80
- package/src/index.ts +0 -72
- package/src/models.generated.ts +0 -314
- package/src/models.ts +0 -45
- package/src/providers/convert.ts +0 -222
- package/src/providers/google.ts +0 -496
- package/src/providers/openai.ts +0 -437
- package/src/stream.ts +0 -60
- package/src/types.ts +0 -198
- package/src/utils/event-stream.ts +0 -60
- package/src/utils/json-parse.ts +0 -28
- package/src/utils/sanitize-unicode.ts +0 -25
- package/src/utils/validation.ts +0 -69
- package/test/core/agent-loop.test.ts +0 -958
- package/test/core/stream.test.ts +0 -409
- package/test/data/red-circle.png +0 -0
- package/test/data/superintelligentwill.pdf +0 -0
- package/test/edge-cases/general.test.ts +0 -565
- package/test/integration/e2e.test.ts +0 -530
- package/test/models/cost.test.ts +0 -499
- package/test/models/registry.test.ts +0 -298
- package/test/providers/convert.test.ts +0 -846
- package/test/providers/google-schema.test.ts +0 -666
- package/test/providers/google-stream.test.ts +0 -369
- package/test/providers/openai-stream.test.ts +0 -251
- package/test/utils/event-stream.test.ts +0 -289
- package/test/utils/json-parse.test.ts +0 -344
- package/test/utils/sanitize-unicode.test.ts +0 -329
- package/test/utils/validation.test.ts +0 -614
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -9
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GenerateContentConfig } from "@google/genai";
|
|
2
|
+
type Props = {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
};
|
|
6
|
+
export type GoogleProviderOptions = Omit<GenerateContentConfig, 'abortSignal' | 'systemPrompt'> & Props;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/providers/google/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAErD,KAAK,KAAK,GAAG;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,CAAA;AAED,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,qBAAqB,EAAE,aAAa,GAAG,cAAc,CAAC,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/providers/google/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Model, AssistantResponse, StopReason, Usage, Context, Tool } from "../../types.js";
|
|
2
|
+
import { GenerateContentResponse, GoogleGenAI, FinishReason, GenerateContentParameters, ContentListUnion } from "@google/genai";
|
|
3
|
+
import { GoogleProviderOptions } from "./types.js";
|
|
4
|
+
import type { TSchema } from "@sinclair/typebox";
|
|
5
|
+
export declare function createClient(model: Model<"google">, apiKey?: string): GoogleGenAI;
|
|
6
|
+
export declare function getResponseAssistantResponse(response: GenerateContentResponse): AssistantResponse;
|
|
7
|
+
export declare function getAssistantStopReason(response: GenerateContentResponse): StopReason;
|
|
8
|
+
export declare function getResponseUsage(response: GenerateContentResponse, model: Model<'google'>): Usage;
|
|
9
|
+
export declare function buildParams(model: Model<"google">, context: Context, options: GoogleProviderOptions): GenerateContentParameters;
|
|
10
|
+
export declare function buildGoogleMessages(model: Model<'google'>, context: Context): ContentListUnion;
|
|
11
|
+
/**
|
|
12
|
+
* JSON Schema type that can be primitives, objects, or arrays
|
|
13
|
+
* Covers the recursive nature of JSON Schema structures
|
|
14
|
+
*/
|
|
15
|
+
type JSONSchemaValue = TSchema | {
|
|
16
|
+
[key: string]: JSONSchemaValue;
|
|
17
|
+
} | JSONSchemaValue[] | string | number | boolean | null;
|
|
18
|
+
/**
|
|
19
|
+
* Transforms a JSON Schema to Google's supported subset.
|
|
20
|
+
* Main transformations:
|
|
21
|
+
* - Converts { "const": "value" } to { "enum": ["value"] }
|
|
22
|
+
* - Converts { "anyOf": [{ "const": "a" }, { "const": "b" }] } to { "enum": ["a", "b"] }
|
|
23
|
+
* - Recursively processes nested objects and arrays
|
|
24
|
+
*/
|
|
25
|
+
export declare function transformSchemaForGoogle(schema: JSONSchemaValue): JSONSchemaValue;
|
|
26
|
+
export declare function convertTools(tools: readonly Tool[]): any[];
|
|
27
|
+
export declare function mapStopReason(reason: FinishReason): StopReason;
|
|
28
|
+
export declare function getMockGoogleMessage(): GenerateContentResponse;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/providers/google/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAwB,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClH,OAAO,EAAyB,uBAAuB,EAAE,WAAW,EAAE,YAAY,EAAE,yBAAyB,EAAiB,gBAAgB,EAAQ,MAAM,eAAe,CAAC;AAE5K,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAGjD,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,WAAW,CAajF;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,uBAAuB,GAAG,iBAAiB,CAyDjG;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,uBAAuB,GAAG,UAAU,CAkBpF;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,uBAAuB,EAAE,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAajG;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,qBAAqB,6BA0CnG;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,GAAG,gBAAgB,CAuG9F;AAED;;;GAGG;AACH,KAAK,eAAe,GAAG,OAAO,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAA;CAAE,GAAG,eAAe,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAE3H;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAwEjF;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,GAAG,EAAE,CAU1D;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,UAAU,CAyB9D;AAED,wBAAgB,oBAAoB,IAAI,uBAAuB,CAQ9D"}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { GoogleGenAI, FinishReason } from "@google/genai";
|
|
2
|
+
import { calculateCost } from "../../models.js";
|
|
3
|
+
import { sanitizeSurrogates } from "../../utils/sanitize-unicode.js";
|
|
4
|
+
export function createClient(model, apiKey) {
|
|
5
|
+
if (!apiKey) {
|
|
6
|
+
if (!process.env.GEMINI_API_KEY) {
|
|
7
|
+
throw new Error("Gemini API key is required. Set GEMINI_API_KEY environment variable or pass it as an argument.");
|
|
8
|
+
}
|
|
9
|
+
apiKey = process.env.GEMINI_API_KEY;
|
|
10
|
+
}
|
|
11
|
+
return new GoogleGenAI({
|
|
12
|
+
apiKey,
|
|
13
|
+
httpOptions: model.headers ? { headers: model.headers } : undefined,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export function getResponseAssistantResponse(response) {
|
|
17
|
+
const assistantResponse = [];
|
|
18
|
+
// Process candidates
|
|
19
|
+
if (response.candidates) {
|
|
20
|
+
for (const candidate of response.candidates) {
|
|
21
|
+
if (candidate.content?.parts) {
|
|
22
|
+
for (const part of candidate.content.parts) {
|
|
23
|
+
// Handle text parts (thinking or regular text)
|
|
24
|
+
if (part.text !== undefined) {
|
|
25
|
+
const isThinking = part.thought === true;
|
|
26
|
+
if (isThinking) {
|
|
27
|
+
assistantResponse.push({
|
|
28
|
+
type: 'thinking',
|
|
29
|
+
thinkingText: part.text
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
assistantResponse.push({
|
|
34
|
+
type: 'response',
|
|
35
|
+
content: [{ type: 'text', content: part.text }]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (part.inlineData) {
|
|
40
|
+
const imageData = part.inlineData.data;
|
|
41
|
+
if (imageData) {
|
|
42
|
+
assistantResponse.push({
|
|
43
|
+
type: 'response',
|
|
44
|
+
content: [{
|
|
45
|
+
type: 'image',
|
|
46
|
+
data: imageData,
|
|
47
|
+
mimeType: part.inlineData.mimeType || 'image/png'
|
|
48
|
+
}]
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Handle function calls
|
|
53
|
+
if (part.functionCall) {
|
|
54
|
+
const toolCallId = part.functionCall.id ||
|
|
55
|
+
`${part.functionCall.name}_${Date.now()}_${Math.random()}`;
|
|
56
|
+
assistantResponse.push({
|
|
57
|
+
type: 'toolCall',
|
|
58
|
+
toolCallId: toolCallId,
|
|
59
|
+
name: part.functionCall.name || '',
|
|
60
|
+
arguments: part.functionCall.args || {}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return assistantResponse;
|
|
68
|
+
}
|
|
69
|
+
export function getAssistantStopReason(response) {
|
|
70
|
+
let finishReason;
|
|
71
|
+
if (response.candidates) {
|
|
72
|
+
for (const candidate of response.candidates) {
|
|
73
|
+
// Capture finish reason from first candidate
|
|
74
|
+
if (candidate.finishReason && !finishReason) {
|
|
75
|
+
finishReason = candidate.finishReason;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Map stop reason
|
|
80
|
+
let stopReason = 'stop';
|
|
81
|
+
if (finishReason) {
|
|
82
|
+
stopReason = mapStopReason(finishReason);
|
|
83
|
+
}
|
|
84
|
+
return stopReason;
|
|
85
|
+
}
|
|
86
|
+
export function getResponseUsage(response, model) {
|
|
87
|
+
// Extract usage information
|
|
88
|
+
const usage = {
|
|
89
|
+
input: response.usageMetadata?.promptTokenCount || 0,
|
|
90
|
+
output: (response.usageMetadata?.candidatesTokenCount || 0) +
|
|
91
|
+
(response.usageMetadata?.thoughtsTokenCount || 0),
|
|
92
|
+
cacheRead: response.usageMetadata?.cachedContentTokenCount || 0,
|
|
93
|
+
cacheWrite: 0,
|
|
94
|
+
totalTokens: response.usageMetadata?.totalTokenCount || 0,
|
|
95
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
96
|
+
};
|
|
97
|
+
calculateCost(model, usage);
|
|
98
|
+
return usage;
|
|
99
|
+
}
|
|
100
|
+
export function buildParams(model, context, options) {
|
|
101
|
+
const contents = buildGoogleMessages(model, context);
|
|
102
|
+
const { apiKey, signal, ...googleOptions } = options;
|
|
103
|
+
const config = {
|
|
104
|
+
...googleOptions
|
|
105
|
+
};
|
|
106
|
+
if (options?.signal) {
|
|
107
|
+
config.abortSignal = options.signal;
|
|
108
|
+
}
|
|
109
|
+
if (context.systemPrompt) {
|
|
110
|
+
config.systemInstruction = sanitizeSurrogates(context.systemPrompt);
|
|
111
|
+
}
|
|
112
|
+
const tools = [];
|
|
113
|
+
if (context.tools && model.tools.includes('function_calling')) {
|
|
114
|
+
const convertedTools = convertTools(context.tools);
|
|
115
|
+
for (const convertedTool of convertedTools) {
|
|
116
|
+
tools.push(convertedTool);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (googleOptions.tools) {
|
|
120
|
+
for (const optionTool of googleOptions.tools) {
|
|
121
|
+
tools.push(optionTool);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (tools.length > 0)
|
|
125
|
+
config.tools = tools;
|
|
126
|
+
const params = {
|
|
127
|
+
model: model.id,
|
|
128
|
+
contents,
|
|
129
|
+
config
|
|
130
|
+
};
|
|
131
|
+
return params;
|
|
132
|
+
}
|
|
133
|
+
export function buildGoogleMessages(model, context) {
|
|
134
|
+
const contents = [];
|
|
135
|
+
for (let i = 0; i < context.messages.length; i++) {
|
|
136
|
+
const message = context.messages[i];
|
|
137
|
+
if (message.role === 'user') {
|
|
138
|
+
const parts = [];
|
|
139
|
+
for (let p = 0; p < message.content.length; p++) {
|
|
140
|
+
const messageContent = message.content[p];
|
|
141
|
+
if (messageContent.type === 'text') {
|
|
142
|
+
parts.push({
|
|
143
|
+
text: sanitizeSurrogates(messageContent.content)
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (messageContent.type === 'image' && model.input.includes("image")) {
|
|
147
|
+
parts.push({
|
|
148
|
+
inlineData: {
|
|
149
|
+
mimeType: messageContent.mimeType,
|
|
150
|
+
data: messageContent.data
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (messageContent.type === 'file' && model.input.includes("file")) {
|
|
155
|
+
parts.push({
|
|
156
|
+
inlineData: {
|
|
157
|
+
mimeType: messageContent.mimeType,
|
|
158
|
+
data: messageContent.data
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
contents.push({
|
|
164
|
+
role: 'user',
|
|
165
|
+
parts
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
if (message.role === 'toolResult') {
|
|
169
|
+
const parts = [];
|
|
170
|
+
let textRes = '(see attached:)';
|
|
171
|
+
for (let p = 0; p < message.content.length; p++) {
|
|
172
|
+
const messageContent = message.content[p];
|
|
173
|
+
if (messageContent.type === 'text') {
|
|
174
|
+
textRes = messageContent.content;
|
|
175
|
+
}
|
|
176
|
+
if (messageContent.type === 'image' && model.input.includes("image")) {
|
|
177
|
+
parts.push({
|
|
178
|
+
inlineData: {
|
|
179
|
+
mimeType: messageContent.mimeType,
|
|
180
|
+
data: messageContent.data
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (messageContent.type === 'file' && model.input.includes("file")) {
|
|
185
|
+
parts.push({
|
|
186
|
+
inlineData: {
|
|
187
|
+
mimeType: messageContent.mimeType,
|
|
188
|
+
data: messageContent.data
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
contents.push({
|
|
194
|
+
role: 'user',
|
|
195
|
+
parts: [
|
|
196
|
+
{
|
|
197
|
+
functionResponse: {
|
|
198
|
+
id: message.toolCallId,
|
|
199
|
+
name: message.toolName,
|
|
200
|
+
parts,
|
|
201
|
+
response: {
|
|
202
|
+
result: sanitizeSurrogates(textRes),
|
|
203
|
+
isError: message.isError
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
]
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
if (message.role === 'assistant') {
|
|
211
|
+
if (message.model.api === 'google') {
|
|
212
|
+
const baseMessage = message;
|
|
213
|
+
if (baseMessage.message.candidates) {
|
|
214
|
+
for (let p = 0; p < baseMessage.message.candidates?.length; p++) {
|
|
215
|
+
const candidate = baseMessage.message.candidates[p];
|
|
216
|
+
if (candidate.content) {
|
|
217
|
+
contents.push(candidate.content);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// TODO Implement other provider conversions
|
|
223
|
+
else {
|
|
224
|
+
throw new Error(`Cannot convert ${message.model.api} assistant message to ${model.api} format. ` +
|
|
225
|
+
`Cross-provider conversion for ${message.model.api} → ${model.api} is not yet implemented.`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return contents;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Transforms a JSON Schema to Google's supported subset.
|
|
233
|
+
* Main transformations:
|
|
234
|
+
* - Converts { "const": "value" } to { "enum": ["value"] }
|
|
235
|
+
* - Converts { "anyOf": [{ "const": "a" }, { "const": "b" }] } to { "enum": ["a", "b"] }
|
|
236
|
+
* - Recursively processes nested objects and arrays
|
|
237
|
+
*/
|
|
238
|
+
export function transformSchemaForGoogle(schema) {
|
|
239
|
+
if (!schema || typeof schema !== 'object') {
|
|
240
|
+
return schema;
|
|
241
|
+
}
|
|
242
|
+
// Handle arrays
|
|
243
|
+
if (Array.isArray(schema)) {
|
|
244
|
+
return schema.map(transformSchemaForGoogle);
|
|
245
|
+
}
|
|
246
|
+
const transformed = {};
|
|
247
|
+
// Handle const keyword - convert to enum
|
|
248
|
+
if ('const' in schema) {
|
|
249
|
+
transformed.enum = [schema.const];
|
|
250
|
+
// Copy over other properties except const
|
|
251
|
+
for (const key in schema) {
|
|
252
|
+
if (key !== 'const') {
|
|
253
|
+
transformed[key] = schema[key];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return transformed;
|
|
257
|
+
}
|
|
258
|
+
// Handle anyOf with const values - convert to enum
|
|
259
|
+
if ('anyOf' in schema && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
260
|
+
const allConst = schema.anyOf.every((item) => item && typeof item === 'object' && 'const' in item);
|
|
261
|
+
if (allConst) {
|
|
262
|
+
// Extract all const values into a single enum
|
|
263
|
+
transformed.enum = schema.anyOf.map((item) => item.const);
|
|
264
|
+
// Copy over other properties from the parent schema
|
|
265
|
+
for (const key in schema) {
|
|
266
|
+
if (key !== 'anyOf') {
|
|
267
|
+
transformed[key] = schema[key];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Copy type and other properties from the first anyOf item if not already set
|
|
271
|
+
if (schema.anyOf.length > 0) {
|
|
272
|
+
const firstItem = schema.anyOf[0];
|
|
273
|
+
for (const key in firstItem) {
|
|
274
|
+
if (key !== 'const' && !(key in transformed)) {
|
|
275
|
+
transformed[key] = firstItem[key];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return transformed;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Recursively process all properties
|
|
283
|
+
for (const key in schema) {
|
|
284
|
+
if (key === 'properties' && typeof schema.properties === 'object') {
|
|
285
|
+
// Recursively transform each property
|
|
286
|
+
transformed.properties = {};
|
|
287
|
+
for (const propKey in schema.properties) {
|
|
288
|
+
transformed.properties[propKey] = transformSchemaForGoogle(schema.properties[propKey]);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else if (key === 'items' && schema.items) {
|
|
292
|
+
// Recursively transform array items schema
|
|
293
|
+
transformed.items = transformSchemaForGoogle(schema.items);
|
|
294
|
+
}
|
|
295
|
+
else if (key === 'anyOf' || key === 'oneOf' || key === 'allOf') {
|
|
296
|
+
// Recursively transform union/intersection schemas
|
|
297
|
+
transformed[key] = Array.isArray(schema[key])
|
|
298
|
+
? schema[key].map(transformSchemaForGoogle)
|
|
299
|
+
: transformSchemaForGoogle(schema[key]);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
// Copy other properties as-is
|
|
303
|
+
transformed[key] = schema[key];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return transformed;
|
|
307
|
+
}
|
|
308
|
+
export function convertTools(tools) {
|
|
309
|
+
return [
|
|
310
|
+
{
|
|
311
|
+
functionDeclarations: tools.map((tool) => ({
|
|
312
|
+
name: tool.name,
|
|
313
|
+
description: tool.description,
|
|
314
|
+
parameters: transformSchemaForGoogle(tool.parameters),
|
|
315
|
+
})),
|
|
316
|
+
},
|
|
317
|
+
];
|
|
318
|
+
}
|
|
319
|
+
export function mapStopReason(reason) {
|
|
320
|
+
switch (reason) {
|
|
321
|
+
case FinishReason.STOP:
|
|
322
|
+
return "stop";
|
|
323
|
+
case FinishReason.MAX_TOKENS:
|
|
324
|
+
return "length";
|
|
325
|
+
case FinishReason.BLOCKLIST:
|
|
326
|
+
case FinishReason.PROHIBITED_CONTENT:
|
|
327
|
+
case FinishReason.SPII:
|
|
328
|
+
case FinishReason.SAFETY:
|
|
329
|
+
case FinishReason.IMAGE_SAFETY:
|
|
330
|
+
case FinishReason.IMAGE_PROHIBITED_CONTENT:
|
|
331
|
+
case FinishReason.RECITATION:
|
|
332
|
+
case FinishReason.FINISH_REASON_UNSPECIFIED:
|
|
333
|
+
case FinishReason.OTHER:
|
|
334
|
+
case FinishReason.LANGUAGE:
|
|
335
|
+
case FinishReason.MALFORMED_FUNCTION_CALL:
|
|
336
|
+
case FinishReason.UNEXPECTED_TOOL_CALL:
|
|
337
|
+
case FinishReason.NO_IMAGE:
|
|
338
|
+
return "error";
|
|
339
|
+
default: {
|
|
340
|
+
const _exhaustive = reason;
|
|
341
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
export function getMockGoogleMessage() {
|
|
346
|
+
return {
|
|
347
|
+
text: '',
|
|
348
|
+
data: '',
|
|
349
|
+
functionCalls: [],
|
|
350
|
+
executableCode: '',
|
|
351
|
+
codeExecutionResult: ''
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/providers/google/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAkD,WAAW,EAAE,YAAY,EAAoE,MAAM,eAAe,CAAC;AAC5K,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAIrE,MAAM,UAAU,YAAY,CAAC,KAAsB,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AE;QACR,aAAa,EAAE,EAAE;QACjB,cAAc,EAAE,EAAE;QAClB,mBAAmB,EAAE,EAAE;KAC1B,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"complete.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/complete.ts"],"names":[],"mappings":"AAAA,OAAO,EAAG,gBAAgB,EAAsC,MAAM,gBAAgB,CAAA;AAMtF,eAAO,MAAM,cAAc,EAAC,gBAAgB,CAAC,QAAQ,CAiEpD,CAAA"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createClient, buildParams, getResponseAssistantResponse, getResponseUsage, mapStopReason } from "./utils.js";
|
|
2
|
+
export const completeOpenAI = async (model, context, options, id) => {
|
|
3
|
+
const startTimestamp = Date.now();
|
|
4
|
+
const client = createClient(model, options?.apiKey);
|
|
5
|
+
const params = buildParams(model, context, options);
|
|
6
|
+
try {
|
|
7
|
+
const response = await client.responses.create(params, { signal: options?.signal });
|
|
8
|
+
// Cache processed content for performance and consistency
|
|
9
|
+
const content = getResponseAssistantResponse(response);
|
|
10
|
+
const usage = getResponseUsage(response, model);
|
|
11
|
+
let stopReason = mapStopReason(response?.status);
|
|
12
|
+
const toolCallIndex = content.findIndex(c => c.type === 'toolCall');
|
|
13
|
+
if (toolCallIndex && toolCallIndex !== -1 && stopReason === 'stop') {
|
|
14
|
+
stopReason = 'toolUse';
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
role: "assistant",
|
|
18
|
+
message: response,
|
|
19
|
+
id,
|
|
20
|
+
api: model.api,
|
|
21
|
+
model,
|
|
22
|
+
timestamp: Date.now(),
|
|
23
|
+
duration: Date.now() - startTimestamp,
|
|
24
|
+
stopReason,
|
|
25
|
+
content,
|
|
26
|
+
usage
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
31
|
+
const isAborted = options.signal?.aborted;
|
|
32
|
+
const stopReason = isAborted ? "aborted" : "error";
|
|
33
|
+
// Return error response with empty content and zero usage
|
|
34
|
+
const emptyUsage = {
|
|
35
|
+
input: 0,
|
|
36
|
+
output: 0,
|
|
37
|
+
cacheRead: 0,
|
|
38
|
+
cacheWrite: 0,
|
|
39
|
+
totalTokens: 0,
|
|
40
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
role: "assistant",
|
|
44
|
+
message: {}, // Empty response object for error case
|
|
45
|
+
id,
|
|
46
|
+
api: model.api,
|
|
47
|
+
model,
|
|
48
|
+
errorMessage,
|
|
49
|
+
timestamp: Date.now(),
|
|
50
|
+
duration: Date.now() - startTimestamp,
|
|
51
|
+
stopReason,
|
|
52
|
+
content: [],
|
|
53
|
+
usage: emptyUsage
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=complete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"complete.js","sourceRoot":"","sources":["../../../src/providers/openai/complete.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAGtH,MAAM,CAAC,MAAM,cAAc,GAA8B,KAAK,EAC1D,KAAsB,EACtB,OAAgB,EAChB,OAA8B,EAC9B,EAAU,EACZ,EAAE;IACA,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAClC,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACpD,MAAM,MAAM,GAAG,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAEpD,IAAG,CAAC;QACA,MAAM,QAAQ,GAAa,MAAM,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,M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AAW,EAAE,CAAC;YACd,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;SACvE,CAAC;QAEF,OAAO;YACH,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,EAAc,EAAE,uCAAuC;YAChE,EAAE;YACF,GAAG,EAAE,KAAK,CAAC,GAAG;YACd,KAAK;YACL,YAAY;YACZ,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;YACrB,QAAQ,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,cAAc;YACrC,UAAU;YACV,OAAO,EAAE,EAAE;YACX,KAAK,EAAE,UAAU;SACpB,CAAC;IACN,CAAC;AACL,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/openai/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0I,cAAc,EAAe,MAAM,gBAAgB,CAAA;AAWpM,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,QAAQ,CAkVjD,CAAA"}
|