@chatcops/core 0.3.2 → 0.5.0
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/dist/agent/chat.d.ts +13 -0
- package/dist/agent/chat.d.ts.map +1 -0
- package/dist/agent/chat.js +42 -0
- package/dist/agent/chat.js.map +1 -0
- package/dist/agent/errors.d.ts +14 -0
- package/dist/agent/errors.d.ts.map +1 -0
- package/dist/agent/errors.js +24 -0
- package/dist/agent/errors.js.map +1 -0
- package/dist/agent/index.d.ts +5 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +5 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/run-turn.d.ts +68 -0
- package/dist/agent/run-turn.d.ts.map +1 -0
- package/dist/agent/run-turn.js +344 -0
- package/dist/agent/run-turn.js.map +1 -0
- package/dist/agent/tool-registry.d.ts +28 -0
- package/dist/agent/tool-registry.d.ts.map +1 -0
- package/dist/agent/tool-registry.js +69 -0
- package/dist/agent/tool-registry.js.map +1 -0
- package/dist/analytics/index.d.ts +1 -0
- package/dist/analytics/index.d.ts.map +1 -1
- package/dist/analytics/index.js +1 -0
- package/dist/analytics/index.js.map +1 -1
- package/dist/analytics/sink.d.ts +24 -0
- package/dist/analytics/sink.d.ts.map +1 -0
- package/dist/analytics/sink.js +106 -0
- package/dist/analytics/sink.js.map +1 -0
- package/dist/conversation/index.d.ts +1 -0
- package/dist/conversation/index.d.ts.map +1 -1
- package/dist/conversation/index.js.map +1 -1
- package/dist/conversation/manager.d.ts +62 -6
- package/dist/conversation/manager.d.ts.map +1 -1
- package/dist/conversation/manager.js +109 -30
- package/dist/conversation/manager.js.map +1 -1
- package/dist/conversation/store.d.ts +6 -5
- package/dist/conversation/store.d.ts.map +1 -1
- package/dist/conversation/store.js +4 -0
- package/dist/conversation/store.js.map +1 -1
- package/dist/index.d.ts +18 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/dist/limits/budget.d.ts +84 -0
- package/dist/limits/budget.d.ts.map +1 -0
- package/dist/limits/budget.js +114 -0
- package/dist/limits/budget.js.map +1 -0
- package/dist/limits/rate-limit.d.ts +53 -0
- package/dist/limits/rate-limit.d.ts.map +1 -0
- package/dist/limits/rate-limit.js +68 -0
- package/dist/limits/rate-limit.js.map +1 -0
- package/dist/parts.d.ts +9 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +17 -0
- package/dist/parts.js.map +1 -0
- package/dist/protocol/ai-sdk.d.ts +27 -0
- package/dist/protocol/ai-sdk.d.ts.map +1 -0
- package/dist/protocol/ai-sdk.js +127 -0
- package/dist/protocol/ai-sdk.js.map +1 -0
- package/dist/protocol/errors.d.ts +10 -0
- package/dist/protocol/errors.d.ts.map +1 -0
- package/dist/protocol/errors.js +42 -0
- package/dist/protocol/errors.js.map +1 -0
- package/dist/protocol/index.d.ts +9 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +6 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/legacy.d.ts +18 -0
- package/dist/protocol/legacy.d.ts.map +1 -0
- package/dist/protocol/legacy.js +41 -0
- package/dist/protocol/legacy.js.map +1 -0
- package/dist/protocol/sse.d.ts +10 -0
- package/dist/protocol/sse.d.ts.map +1 -0
- package/dist/protocol/sse.js +15 -0
- package/dist/protocol/sse.js.map +1 -0
- package/dist/protocol/v1.d.ts +140 -0
- package/dist/protocol/v1.d.ts.map +1 -0
- package/dist/protocol/v1.js +15 -0
- package/dist/protocol/v1.js.map +1 -0
- package/dist/providers/base.d.ts +17 -15
- package/dist/providers/base.d.ts.map +1 -1
- package/dist/providers/base.js +43 -31
- package/dist/providers/base.js.map +1 -1
- package/dist/providers/claude.d.ts +40 -2
- package/dist/providers/claude.d.ts.map +1 -1
- package/dist/providers/claude.js +250 -113
- package/dist/providers/claude.js.map +1 -1
- package/dist/providers/gemini.d.ts +23 -3
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +245 -102
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/history.d.ts +52 -0
- package/dist/providers/history.d.ts.map +1 -0
- package/dist/providers/history.js +212 -0
- package/dist/providers/history.js.map +1 -0
- package/dist/providers/index.d.ts +5 -5
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +5 -4
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/openai.d.ts +37 -3
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +246 -140
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/registry.d.ts +7 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +12 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/schema.d.ts +16 -0
- package/dist/providers/schema.d.ts.map +1 -0
- package/dist/providers/schema.js +80 -0
- package/dist/providers/schema.js.map +1 -0
- package/dist/providers/tool-execution.d.ts +10 -0
- package/dist/providers/tool-execution.d.ts.map +1 -1
- package/dist/providers/tool-execution.js +38 -12
- package/dist/providers/tool-execution.js.map +1 -1
- package/dist/providers/types.d.ts +71 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +2 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/storage/adapt.d.ts +15 -0
- package/dist/storage/adapt.d.ts.map +1 -0
- package/dist/storage/adapt.js +83 -0
- package/dist/storage/adapt.js.map +1 -0
- package/dist/storage/errors.d.ts +18 -0
- package/dist/storage/errors.d.ts.map +1 -0
- package/dist/storage/errors.js +28 -0
- package/dist/storage/errors.js.map +1 -0
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +4 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/memory.d.ts +24 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +265 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/types.d.ts +118 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +2 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/testing/conformance.d.ts +29 -0
- package/dist/testing/conformance.d.ts.map +1 -0
- package/dist/testing/conformance.js +335 -0
- package/dist/testing/conformance.js.map +1 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +5 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/provider.d.ts +45 -0
- package/dist/testing/provider.d.ts.map +1 -0
- package/dist/testing/provider.js +82 -0
- package/dist/testing/provider.js.map +1 -0
- package/dist/tools/base.d.ts +14 -4
- package/dist/tools/base.d.ts.map +1 -1
- package/dist/tools/base.js +4 -2
- package/dist/tools/base.js.map +1 -1
- package/dist/tools/lead-capture.d.ts +3 -1
- package/dist/tools/lead-capture.d.ts.map +1 -1
- package/dist/tools/lead-capture.js +6 -1
- package/dist/tools/lead-capture.js.map +1 -1
- package/dist/types.d.ts +168 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +53 -5
package/dist/providers/gemini.js
CHANGED
|
@@ -1,122 +1,265 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { FinishReason as GeminiFinishReason, GoogleGenAI, ThinkingLevel, } from '@google/genai';
|
|
2
|
+
import { chatSyncViaTurn, chatViaTurn } from '../agent/chat.js';
|
|
3
|
+
import { joinSystem } from '../agent/run-turn.js';
|
|
4
|
+
import { DEFAULT_MAX_TOKENS, toGeminiTools } from './base.js';
|
|
5
|
+
import { normalizeTurns } from './history.js';
|
|
6
|
+
const DEFAULT_MODEL = 'gemini-3.5-flash-lite';
|
|
7
|
+
const DEFAULT_EFFORT = 'low';
|
|
6
8
|
function createToolCallId() {
|
|
7
|
-
|
|
8
|
-
return `gemini_tool_call_${toolCallSequence}`;
|
|
9
|
+
return `gemini_call_${crypto.randomUUID()}`;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
const THINKING_LEVELS = {
|
|
12
|
+
low: ThinkingLevel.LOW,
|
|
13
|
+
medium: ThinkingLevel.MEDIUM,
|
|
14
|
+
high: ThinkingLevel.HIGH,
|
|
15
|
+
xhigh: ThinkingLevel.HIGH,
|
|
16
|
+
max: ThinkingLevel.HIGH,
|
|
17
|
+
};
|
|
18
|
+
/** `thinkingLevel` is a Gemini 3 setting; older models use a token budget instead. */
|
|
19
|
+
export function geminiSupportsThinkingLevel(model) {
|
|
20
|
+
const major = Number(model.match(/gemini-(\d+)/)?.[1] ?? 0);
|
|
21
|
+
return major >= 3;
|
|
17
22
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
function geminiMetadata(part) {
|
|
24
|
+
return 'providerMetadata' in part ? part.providerMetadata?.gemini : undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Gemini 3 wants a thought signature on the first function call of each
|
|
28
|
+
* step. Calls that came from another provider have none, and Google
|
|
29
|
+
* documents this value for that case.
|
|
30
|
+
*/
|
|
31
|
+
const PLACEHOLDER_SIGNATURE = 'skip_thought_signature_validator';
|
|
32
|
+
/**
|
|
33
|
+
* Stored history as Gemini contents. An assistant message with tool calls
|
|
34
|
+
* becomes a model turn with its `functionCall` parts, then a user turn with
|
|
35
|
+
* the `functionResponse` parts, for each step. Thought signatures go back on
|
|
36
|
+
* the parts they came with.
|
|
37
|
+
*/
|
|
38
|
+
export function toGeminiStepContents(messages, model = DEFAULT_MODEL) {
|
|
39
|
+
const contents = [];
|
|
40
|
+
const needsSignature = geminiSupportsThinkingLevel(model);
|
|
41
|
+
const synthetic = new Set();
|
|
42
|
+
const push = (role, parts) => {
|
|
43
|
+
if (parts.length === 0)
|
|
44
|
+
return;
|
|
45
|
+
const last = contents.at(-1);
|
|
46
|
+
if (last?.role === role)
|
|
47
|
+
last.parts = [...(last.parts ?? []), ...parts];
|
|
48
|
+
else
|
|
49
|
+
contents.push({ role, parts });
|
|
50
|
+
};
|
|
51
|
+
for (const turn of normalizeTurns(messages)) {
|
|
52
|
+
let role = turn.role === 'assistant' ? 'model' : 'user';
|
|
53
|
+
let run = [];
|
|
54
|
+
let signedCall = false;
|
|
55
|
+
for (const part of turn.parts) {
|
|
56
|
+
const partRole = part.type === 'tool-result' ? 'user' : turn.role === 'assistant' ? 'model' : 'user';
|
|
57
|
+
if (partRole !== role) {
|
|
58
|
+
push(role, run);
|
|
59
|
+
run = [];
|
|
60
|
+
role = partRole;
|
|
61
|
+
signedCall = false;
|
|
62
|
+
}
|
|
63
|
+
const meta = geminiMetadata(part);
|
|
64
|
+
const signature = meta?.thoughtSignature ? { thoughtSignature: meta.thoughtSignature } : {};
|
|
65
|
+
switch (part.type) {
|
|
66
|
+
case 'text':
|
|
67
|
+
run.push({ text: part.text, ...signature });
|
|
68
|
+
break;
|
|
69
|
+
case 'reasoning':
|
|
70
|
+
// Thoughts from another provider mean nothing to Gemini.
|
|
71
|
+
if (meta)
|
|
72
|
+
run.push({ text: part.text, thought: true, ...signature });
|
|
73
|
+
break;
|
|
74
|
+
case 'tool-call': {
|
|
75
|
+
if (meta?.syntheticId)
|
|
76
|
+
synthetic.add(part.toolCallId);
|
|
77
|
+
const args = typeof part.input === 'object' && part.input !== null && !Array.isArray(part.input)
|
|
78
|
+
? part.input
|
|
79
|
+
: {};
|
|
80
|
+
const placeholder = needsSignature && !signedCall && !meta?.thoughtSignature
|
|
81
|
+
? { thoughtSignature: PLACEHOLDER_SIGNATURE }
|
|
82
|
+
: {};
|
|
83
|
+
signedCall = true;
|
|
84
|
+
run.push({
|
|
85
|
+
functionCall: { ...(meta?.syntheticId ? {} : { id: part.toolCallId }), name: part.toolName, args },
|
|
86
|
+
...signature,
|
|
87
|
+
...placeholder,
|
|
88
|
+
});
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case 'tool-result':
|
|
92
|
+
run.push({
|
|
93
|
+
functionResponse: {
|
|
94
|
+
...(synthetic.has(part.toolCallId) ? {} : { id: part.toolCallId }),
|
|
95
|
+
name: part.toolName,
|
|
96
|
+
response: part.output ?? { success: false, message: part.error },
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
push(role, run);
|
|
103
|
+
}
|
|
104
|
+
if (contents.length === 0) {
|
|
105
|
+
throw new Error('Cannot send a request with no user message.');
|
|
22
106
|
}
|
|
23
|
-
return
|
|
107
|
+
return contents;
|
|
108
|
+
}
|
|
109
|
+
const CONTENT_FILTERED = new Set([
|
|
110
|
+
GeminiFinishReason.SAFETY,
|
|
111
|
+
GeminiFinishReason.RECITATION,
|
|
112
|
+
GeminiFinishReason.BLOCKLIST,
|
|
113
|
+
GeminiFinishReason.PROHIBITED_CONTENT,
|
|
114
|
+
GeminiFinishReason.SPII,
|
|
115
|
+
GeminiFinishReason.IMAGE_SAFETY,
|
|
116
|
+
GeminiFinishReason.IMAGE_PROHIBITED_CONTENT,
|
|
117
|
+
]);
|
|
118
|
+
function finishReason(reason, blocked, hasToolCalls) {
|
|
119
|
+
if (blocked || (reason && CONTENT_FILTERED.has(reason)))
|
|
120
|
+
return 'content-filter';
|
|
121
|
+
if (reason === GeminiFinishReason.MAX_TOKENS)
|
|
122
|
+
return 'length';
|
|
123
|
+
if (reason === GeminiFinishReason.MALFORMED_FUNCTION_CALL || reason === GeminiFinishReason.UNEXPECTED_TOOL_CALL)
|
|
124
|
+
return 'error';
|
|
125
|
+
if (hasToolCalls)
|
|
126
|
+
return 'tool-calls';
|
|
127
|
+
return 'stop';
|
|
128
|
+
}
|
|
129
|
+
/** Thinking tokens are billed as output, so they count as output here. */
|
|
130
|
+
function toUsage(usage) {
|
|
131
|
+
const cached = usage?.cachedContentTokenCount ?? 0;
|
|
132
|
+
return {
|
|
133
|
+
inputTokens: usage?.promptTokenCount ?? 0,
|
|
134
|
+
outputTokens: (usage?.candidatesTokenCount ?? 0) + (usage?.thoughtsTokenCount ?? 0),
|
|
135
|
+
...(cached ? { cacheReadTokens: cached } : {}),
|
|
136
|
+
};
|
|
24
137
|
}
|
|
25
138
|
export class GeminiProvider {
|
|
26
139
|
name = 'gemini';
|
|
27
|
-
|
|
140
|
+
ai;
|
|
28
141
|
model;
|
|
142
|
+
effort;
|
|
29
143
|
constructor(config) {
|
|
30
|
-
|
|
144
|
+
if (!config.apiKey?.trim())
|
|
145
|
+
throw new Error('gemini requires apiKey');
|
|
146
|
+
this.ai = new GoogleGenAI({
|
|
147
|
+
apiKey: config.apiKey,
|
|
148
|
+
...(config.baseURL || config.fetch
|
|
149
|
+
? {
|
|
150
|
+
httpOptions: {
|
|
151
|
+
...(config.baseURL ? { baseUrl: config.baseURL } : {}),
|
|
152
|
+
...(config.fetch ? { fetch: config.fetch } : {}),
|
|
153
|
+
},
|
|
154
|
+
}
|
|
155
|
+
: {}),
|
|
156
|
+
});
|
|
31
157
|
this.model = config.model ?? DEFAULT_MODEL;
|
|
158
|
+
this.effort = config.effort ?? DEFAULT_EFFORT;
|
|
32
159
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
160
|
+
config(params) {
|
|
161
|
+
return {
|
|
162
|
+
systemInstruction: joinSystem(params.systemPrompt, params.systemContext),
|
|
163
|
+
maxOutputTokens: params.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
164
|
+
...(params.temperature != null ? { temperature: params.temperature } : {}),
|
|
37
165
|
...(params.tools?.length ? { tools: toGeminiTools(params.tools) } : {}),
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
...(params.temperature != null ? { temperature: params.temperature } : {}),
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
let toolRounds = 0;
|
|
47
|
-
let request = getLastMessage(params);
|
|
48
|
-
while (true) {
|
|
49
|
-
const result = await chat.sendMessageStream(request);
|
|
50
|
-
for await (const chunk of result.stream) {
|
|
51
|
-
const text = chunk.text();
|
|
52
|
-
if (text) {
|
|
53
|
-
yield text;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const response = await result.response;
|
|
57
|
-
const functionCalls = response.functionCalls() ?? [];
|
|
58
|
-
if (functionCalls.length === 0) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (toolRounds >= MAX_TOOL_ROUNDS) {
|
|
62
|
-
throw new Error(`Exceeded maximum tool rounds (${MAX_TOOL_ROUNDS}).`);
|
|
63
|
-
}
|
|
64
|
-
toolRounds += 1;
|
|
65
|
-
request = await Promise.all(functionCalls.map(async (call) => {
|
|
66
|
-
const result = await executeToolCall(params, {
|
|
67
|
-
id: createToolCallId(),
|
|
68
|
-
name: call.name,
|
|
69
|
-
input: call.args,
|
|
70
|
-
});
|
|
71
|
-
return {
|
|
72
|
-
functionResponse: {
|
|
73
|
-
name: call.name,
|
|
74
|
-
response: serializeToolResult(result),
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
}));
|
|
78
|
-
}
|
|
166
|
+
...(geminiSupportsThinkingLevel(this.model)
|
|
167
|
+
? { thinkingConfig: { thinkingLevel: THINKING_LEVELS[this.effort] } }
|
|
168
|
+
: {}),
|
|
169
|
+
...(params.signal ? { abortSignal: params.signal } : {}),
|
|
170
|
+
};
|
|
79
171
|
}
|
|
80
|
-
|
|
81
|
-
|
|
172
|
+
/** One model request: streams text and thoughts, then reports tool calls, usage and why it stopped. */
|
|
173
|
+
async *step(params) {
|
|
174
|
+
params.signal?.throwIfAborted();
|
|
175
|
+
const stream = await this.ai.models.generateContentStream({
|
|
82
176
|
model: this.model,
|
|
83
|
-
|
|
84
|
-
|
|
177
|
+
contents: toGeminiStepContents(params.messages, this.model),
|
|
178
|
+
config: this.config(params),
|
|
85
179
|
});
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
180
|
+
const parts = [];
|
|
181
|
+
const calls = [];
|
|
182
|
+
// Text and thoughts arrive in pieces. A signature ends the part it's on.
|
|
183
|
+
let current = null;
|
|
184
|
+
let reason;
|
|
185
|
+
let blocked = false;
|
|
186
|
+
let usage;
|
|
187
|
+
for await (const chunk of stream) {
|
|
188
|
+
if (chunk.usageMetadata)
|
|
189
|
+
usage = chunk.usageMetadata;
|
|
190
|
+
if (chunk.promptFeedback?.blockReason)
|
|
191
|
+
blocked = true;
|
|
192
|
+
const candidate = chunk.candidates?.[0];
|
|
193
|
+
if (candidate?.finishReason)
|
|
194
|
+
reason = candidate.finishReason;
|
|
195
|
+
for (const part of candidate?.content?.parts ?? []) {
|
|
196
|
+
const signature = part.thoughtSignature;
|
|
197
|
+
if (part.functionCall) {
|
|
198
|
+
current = null;
|
|
199
|
+
const call = part.functionCall;
|
|
200
|
+
const syntheticId = !call.id;
|
|
201
|
+
const meta = {
|
|
202
|
+
...(signature ? { thoughtSignature: signature } : {}),
|
|
203
|
+
...(syntheticId ? { syntheticId: true } : {}),
|
|
204
|
+
};
|
|
205
|
+
const toolCall = {
|
|
206
|
+
type: 'tool-call',
|
|
207
|
+
toolCallId: call.id ?? createToolCallId(),
|
|
208
|
+
toolName: call.name ?? '',
|
|
209
|
+
input: call.args ?? {},
|
|
210
|
+
executor: 'server',
|
|
211
|
+
state: 'pending',
|
|
212
|
+
...(Object.keys(meta).length ? { providerMetadata: { gemini: { ...meta } } } : {}),
|
|
213
|
+
};
|
|
214
|
+
parts.push(toolCall);
|
|
215
|
+
calls.push(toolCall);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (typeof part.text === 'string') {
|
|
219
|
+
const kind = part.thought ? 'reasoning' : 'text';
|
|
220
|
+
if (!current || current.type !== kind) {
|
|
221
|
+
const next = kind === 'text' ? { type: 'text', text: '' } : { type: 'reasoning', text: '' };
|
|
222
|
+
parts.push(next);
|
|
223
|
+
current = next;
|
|
224
|
+
}
|
|
225
|
+
current.text += part.text;
|
|
226
|
+
if (part.text)
|
|
227
|
+
yield { type: kind === 'text' ? 'text-delta' : 'reasoning-delta', delta: part.text };
|
|
228
|
+
}
|
|
229
|
+
if (signature) {
|
|
230
|
+
const target = current ?? parts.at(-1);
|
|
231
|
+
if (target && (target.type === 'text' || target.type === 'reasoning')) {
|
|
232
|
+
target.providerMetadata = { gemini: { thoughtSignature: signature } };
|
|
233
|
+
}
|
|
234
|
+
current = null;
|
|
235
|
+
}
|
|
104
236
|
}
|
|
105
|
-
toolRounds += 1;
|
|
106
|
-
request = await Promise.all(functionCalls.map(async (call) => {
|
|
107
|
-
const toolResult = await executeToolCall(params, {
|
|
108
|
-
id: createToolCallId(),
|
|
109
|
-
name: call.name,
|
|
110
|
-
input: call.args,
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
functionResponse: {
|
|
114
|
-
name: call.name,
|
|
115
|
-
response: serializeToolResult(toolResult),
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
}));
|
|
119
237
|
}
|
|
238
|
+
params.signal?.throwIfAborted();
|
|
239
|
+
for (const call of calls) {
|
|
240
|
+
yield {
|
|
241
|
+
type: 'tool-call',
|
|
242
|
+
call: { id: call.toolCallId, name: call.toolName, input: call.input },
|
|
243
|
+
...(call.toolName ? {} : { inputError: 'The model sent a function call without a name.' }),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
yield {
|
|
247
|
+
type: 'finish',
|
|
248
|
+
reason: finishReason(reason, blocked, calls.length > 0),
|
|
249
|
+
usage: toUsage(usage),
|
|
250
|
+
parts: parts.filter((part) => part.type !== 'text' || part.text !== '' || part.providerMetadata),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* A turn with tools run through `params.toolExecutor`, as text.
|
|
255
|
+
* @deprecated Use `runTurn()`, which also returns the reply's parts, status and usage.
|
|
256
|
+
*/
|
|
257
|
+
chat(params) {
|
|
258
|
+
return chatViaTurn(this, params);
|
|
259
|
+
}
|
|
260
|
+
/** @deprecated Use `runTurn()`. */
|
|
261
|
+
chatSync(params) {
|
|
262
|
+
return chatSyncViaTurn(this, params);
|
|
120
263
|
}
|
|
121
264
|
}
|
|
122
265
|
//# sourceMappingURL=gemini.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gemini.js","sourceRoot":"","sources":["../../src/providers/gemini.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"gemini.js","sourceRoot":"","sources":["../../src/providers/gemini.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,IAAI,kBAAkB,EAClC,WAAW,EACX,aAAa,GAKd,MAAM,eAAe,CAAC;AAavB,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAiB,MAAM,cAAc,CAAC;AAG7D,MAAM,aAAa,GAAG,uBAAuB,CAAC;AAC9C,MAAM,cAAc,GAAmB,KAAK,CAAC;AAE7C,SAAS,gBAAgB;IACvB,OAAO,eAAe,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC;AAC9C,CAAC;AAED,MAAM,eAAe,GAA0C;IAC7D,GAAG,EAAE,aAAa,CAAC,GAAG;IACtB,MAAM,EAAE,aAAa,CAAC,MAAM;IAC5B,IAAI,EAAE,aAAa,CAAC,IAAI;IACxB,KAAK,EAAE,aAAa,CAAC,IAAI;IACzB,GAAG,EAAE,aAAa,CAAC,IAAI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EAAE,CAAC;oBACxE,CAAC;oBACD,OAAO,GAAG,IAAI,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;QACD,MAAM,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;QAEhC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM;gBACJ,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,KAAgC,EAAE;gBAChG,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,gDAAgD,EAAE,CAAC;aAC3F,CAAC;QACJ,CAAC;QAED,MAAM;YACJ,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;YACvD,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;YACrB,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,EAAE,IAAI,IAAI,CAAC,gBAAgB,CAAC;SACjG,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,MAA0B;QAC7B,OAAO,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,mCAAmC;IACnC,QAAQ,CAAC,MAA0B;QACjC,OAAO,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ChatMessage, ReasoningPart, TextPart, ToolCallPart, ToolResultPart } from '../types.js';
|
|
2
|
+
export interface NormalizedMessage {
|
|
3
|
+
role: 'user' | 'assistant';
|
|
4
|
+
content: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Turns stored messages into a history every provider accepts: it starts with
|
|
8
|
+
* a user turn and alternates between user and assistant.
|
|
9
|
+
*
|
|
10
|
+
* - System messages and empty messages are dropped.
|
|
11
|
+
* - Leading assistant turns are dropped (history trimming can leave one first).
|
|
12
|
+
* - Consecutive turns with the same role are merged with a blank line, for
|
|
13
|
+
* example a user message saved before a provider call that then failed.
|
|
14
|
+
*/
|
|
15
|
+
export declare function normalizeHistory(messages: ChatMessage[]): NormalizedMessage[];
|
|
16
|
+
/** The parts a model sees again on the next request. */
|
|
17
|
+
export type TurnPart = TextPart | ReasoningPart | ToolCallPart | ToolResultPart;
|
|
18
|
+
export interface NormalizedTurn {
|
|
19
|
+
role: 'user' | 'assistant';
|
|
20
|
+
parts: TurnPart[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Like `normalizeHistory`, but keeps message parts, so tool calls, their
|
|
24
|
+
* results and reasoning signatures from earlier steps go back to the model.
|
|
25
|
+
*
|
|
26
|
+
* - System messages, empty messages and leading assistant turns are dropped.
|
|
27
|
+
* - Sources, captured leads and handoff notes are dropped; they're for people.
|
|
28
|
+
* - Turns with the same role in a row are merged, and text that meets across
|
|
29
|
+
* the join is combined with a blank line, as `normalizeHistory` does.
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalizeTurns(messages: ChatMessage[]): NormalizedTurn[];
|
|
32
|
+
/**
|
|
33
|
+
* A rough token count for budgeting, on the high side: about 3.5 characters
|
|
34
|
+
* per token for English, plus a few tokens of per-message overhead. Real
|
|
35
|
+
* counts differ by model; this only has to keep a long conversation from
|
|
36
|
+
* growing without bound. Messages with parts are counted by their parts, so
|
|
37
|
+
* tool inputs and results count too.
|
|
38
|
+
*/
|
|
39
|
+
export declare function estimateTokens(message: Pick<ChatMessage, 'content' | 'parts'>): number;
|
|
40
|
+
/** History budget used when nothing else is configured. */
|
|
41
|
+
export declare const DEFAULT_MAX_HISTORY_TOKENS = 32000;
|
|
42
|
+
/**
|
|
43
|
+
* Keeps a conversation within a token budget before it goes to the model.
|
|
44
|
+
* The first user message stays (it usually says what the visitor came for),
|
|
45
|
+
* then as many of the newest messages as fit. The result never starts with
|
|
46
|
+
* an assistant turn. System messages are left out, since the system prompt
|
|
47
|
+
* is sent separately.
|
|
48
|
+
*
|
|
49
|
+
* Nothing stored changes; this only shapes what the model sees.
|
|
50
|
+
*/
|
|
51
|
+
export declare function trimHistory(messages: ChatMessage[], maxTokens?: number): ChatMessage[];
|
|
52
|
+
//# sourceMappingURL=history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.d.ts","sourceRoot":"","sources":["../../src/providers/history.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,WAAW,EAEX,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,cAAc,EACf,MAAM,aAAa,CAAC;AAErB,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,iBAAiB,EAAE,CAiB7E;AAED,wDAAwD;AACxD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,aAAa,GAAG,YAAY,GAAG,cAAc,CAAC;AAEhF,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB;AA6ED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,cAAc,EAAE,CA0BxE;AAyBD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE,SAAS,GAAG,OAAO,CAAC,GAAG,MAAM,CAKtF;AAED,2DAA2D;AAC3D,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAEjD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,WAAW,EAAE,EACvB,SAAS,GAAE,MAAmC,GAC7C,WAAW,EAAE,CA2Bf"}
|