@stage-labs/metro 0.1.0-beta.308 → 0.1.0-beta.309
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/package.json +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +3 -9
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +11 -4
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +7 -6
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +1 -6
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/claude-logins.ts +53 -29
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/claude-store.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +40 -62
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/delivered.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effective.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/fallback.ts +18 -19
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +36 -64
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +99 -213
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +80 -283
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-import.ts +125 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-lists.ts +0 -17
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-v2.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +0 -79
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/org-models.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +4 -44
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage-refresh.ts +37 -57
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +0 -13
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +3 -4
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/runner.json +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +0 -219
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +0 -119
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +0 -160
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +0 -46
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +0 -150
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +0 -139
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +0 -236
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +0 -208
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +0 -157
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +0 -82
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter-login.ts +0 -192
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter-signin.ts +0 -64
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +0 -36
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
2
|
-
import { log } from '@metro-labs/core/log';
|
|
3
|
-
import { GeminiAuthError, type GeminiTokens } from './gemini-auth.js';
|
|
4
|
-
import { API, clientHeaders, clientMetadata, setupBases, type Bases } from './gemini-client.js';
|
|
5
|
-
import { nonEmpty } from './text.js';
|
|
6
|
-
|
|
7
|
-
const FREE_TIER = 'free-tier';
|
|
8
|
-
const PROJECT_RE = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/;
|
|
9
|
-
const POLL_MS = 5000;
|
|
10
|
-
const POLL_MAX = 24;
|
|
11
|
-
|
|
12
|
-
export interface Onboarded {
|
|
13
|
-
project: string;
|
|
14
|
-
tier: string | null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export function parseGeminiProject(value: unknown): string | null {
|
|
19
|
-
const project = typeof value === 'string' ? value.trim() : '';
|
|
20
|
-
if (project === '') return null;
|
|
21
|
-
if (!PROJECT_RE.test(project)) throw new GeminiAuthError('that is not a Google Cloud project id: 6 to 30 lowercase letters, digits and dashes, starting with a letter');
|
|
22
|
-
return project;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
class Retryable extends Error {}
|
|
26
|
-
|
|
27
|
-
async function callOne(base: string, method: string, token: string, body: unknown, fetchImpl: typeof fetch): Promise<Record<string, unknown>> {
|
|
28
|
-
let res: Response;
|
|
29
|
-
try {
|
|
30
|
-
res = await fetchImpl(`${base}/${API}:${method}`, {
|
|
31
|
-
method: 'POST',
|
|
32
|
-
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json', ...clientHeaders() },
|
|
33
|
-
body: JSON.stringify(body),
|
|
34
|
-
signal: AbortSignal.timeout(30_000),
|
|
35
|
-
});
|
|
36
|
-
} catch (err) {
|
|
37
|
-
throw new Retryable(`could not reach Google Code Assist: ${err instanceof Error ? err.message : String(err)}`);
|
|
38
|
-
}
|
|
39
|
-
const answer: unknown = await res.json().catch(() => null);
|
|
40
|
-
if (!res.ok) {
|
|
41
|
-
const detail = (isRecord(answer) && isRecord(answer.error) ? nonEmpty(answer.error.message) : null) ?? `Google Code Assist answered ${String(res.status)} on ${method}`;
|
|
42
|
-
throw res.status >= 500 ? new Retryable(detail) : new GeminiAuthError(detail);
|
|
43
|
-
}
|
|
44
|
-
return isRecord(answer) ? answer : {};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function call(bases: string[], method: string, token: string, body: unknown, fetchImpl: typeof fetch): Promise<Record<string, unknown>> {
|
|
48
|
-
let last = 'no Code Assist endpoint to call';
|
|
49
|
-
for (const base of bases) {
|
|
50
|
-
try {
|
|
51
|
-
return await callOne(base, method, token, body, fetchImpl);
|
|
52
|
-
} catch (err) {
|
|
53
|
-
if (!(err instanceof Retryable)) throw err;
|
|
54
|
-
last = err.message;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
throw new GeminiAuthError(last);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const tierOf = (load: Record<string, unknown>): { id: string | null; name: string | null } => {
|
|
61
|
-
const tier = isRecord(load.paidTier) ? load.paidTier : isRecord(load.currentTier) ? load.currentTier : null;
|
|
62
|
-
return { id: tier === null ? null : nonEmpty(tier.id), name: tier === null ? null : nonEmpty(tier.name) };
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
async function waitOperation(base: string, token: string, first: Record<string, unknown>, fetchImpl: typeof fetch): Promise<Record<string, unknown>> {
|
|
66
|
-
let op = first;
|
|
67
|
-
for (let i = 0; op.done !== true && i < POLL_MAX; i += 1) {
|
|
68
|
-
const name = nonEmpty(op.name);
|
|
69
|
-
if (name === null) break;
|
|
70
|
-
await new Promise((r) => setTimeout(r, POLL_MS));
|
|
71
|
-
const res = await fetchImpl(`${base}/${API}/${name}`, { headers: { authorization: `Bearer ${token}`, ...clientHeaders() }, signal: AbortSignal.timeout(30_000) });
|
|
72
|
-
const answer: unknown = await res.json().catch(() => null);
|
|
73
|
-
op = isRecord(answer) ? answer : {};
|
|
74
|
-
}
|
|
75
|
-
return op;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const REASONS: Record<string, string> = {
|
|
79
|
-
DASHER_USER: 'this is a Google Workspace account; the individual tier needs a personal Google account',
|
|
80
|
-
NON_USER_ACCOUNT: 'this is not a user account',
|
|
81
|
-
RESTRICTED_AGE: 'the account does not meet the age requirement',
|
|
82
|
-
UNSUPPORTED_LOCATION: 'Gemini Code Assist for individuals is not offered where this account is located',
|
|
83
|
-
UNKNOWN_LOCATION: 'Google could not tell where this account is located',
|
|
84
|
-
RESTRICTED_NETWORK: 'the network this box is on is restricted',
|
|
85
|
-
INELIGIBLE_ACCOUNT: 'Google marks this account as not eligible',
|
|
86
|
-
UNSUPPORTED_CLIENT: 'Google no longer serves the client metro presents; nothing on this box can fix that',
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
function refuseIneligible(load: Record<string, unknown>): void {
|
|
90
|
-
if (isRecord(load.currentTier) || !Array.isArray(load.ineligibleTiers)) return;
|
|
91
|
-
const tiers = load.ineligibleTiers.filter(isRecord);
|
|
92
|
-
if (tiers.length === 0) return;
|
|
93
|
-
const validation = tiers.find((t) => t.reasonCode === 'VALIDATION_REQUIRED' && nonEmpty(t.validationUrl) !== null);
|
|
94
|
-
if (validation !== undefined) throw new GeminiAuthError(`Google asks you to validate the account first: open ${nonEmpty(validation.validationUrl) ?? ''} and then connect again`);
|
|
95
|
-
const said = tiers.map((t) => `${nonEmpty(t.reasonMessage) ?? 'not eligible'} [${nonEmpty(t.reasonCode) ?? 'UNKNOWN'}: ${REASONS[nonEmpty(t.reasonCode) ?? ''] ?? 'no known reason'}]`);
|
|
96
|
-
throw new GeminiAuthError(said.join('; '));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const NO_PROJECT = 'this account holds a Code Assist licence but Google named no project for it: enter the id of the Google Cloud project that carries the licence and connect again';
|
|
100
|
-
|
|
101
|
-
function tierToOnboard(load: Record<string, unknown>): { id: string; name: string | null } {
|
|
102
|
-
const allowed = Array.isArray(load.allowedTiers) ? load.allowedTiers.filter(isRecord) : [];
|
|
103
|
-
const chosen = allowed.find((t) => t.isDefault === true);
|
|
104
|
-
return { id: nonEmpty(chosen?.id) ?? FREE_TIER, name: nonEmpty(chosen?.name) };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const summary = (load: Record<string, unknown>): Record<string, unknown> => ({
|
|
108
|
-
current: isRecord(load.currentTier) ? nonEmpty(load.currentTier.id) : null,
|
|
109
|
-
paid: isRecord(load.paidTier) ? nonEmpty(load.paidTier.id) : null,
|
|
110
|
-
allowed: Array.isArray(load.allowedTiers) ? load.allowedTiers.filter(isRecord).map((t) => `${nonEmpty(t.id) ?? '?'}${t.isDefault === true ? '*' : ''}`) : [],
|
|
111
|
-
ineligible: Array.isArray(load.ineligibleTiers) ? load.ineligibleTiers.filter(isRecord).map((t) => `${nonEmpty(t.tierId) ?? '?'}:${nonEmpty(t.reasonCode) ?? '?'}`) : [],
|
|
112
|
-
project: nonEmpty(load.cloudaicompanionProject),
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
function onboardBody(load: Record<string, unknown>, project: string | null): { body: Record<string, unknown>; name: string | null } {
|
|
116
|
-
const wanted = tierToOnboard(load);
|
|
117
|
-
const paid = wanted.id !== FREE_TIER && project !== null;
|
|
118
|
-
return { body: { tierId: wanted.id, metadata: clientMetadata(paid ? project : null) }, name: wanted.name ?? wanted.id };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const NO_ASSIGNMENT = 'Google did not assign a Code Assist project to this account; enter the id of the Google Cloud project that carries your licence and connect again';
|
|
122
|
-
|
|
123
|
-
const withProject = (project: string | null): Record<string, unknown> => (project === null ? {} : { cloudaicompanionProject: project });
|
|
124
|
-
|
|
125
|
-
const assignedProject = (op: Record<string, unknown>): string | null => {
|
|
126
|
-
const response = isRecord(op.response) ? op.response : {};
|
|
127
|
-
return isRecord(response.cloudaicompanionProject) ? nonEmpty(response.cloudaicompanionProject.id) : null;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
async function onboardNew(load: Record<string, unknown>, tokens: GeminiTokens, project: string | null, bases: string[], fetchImpl: typeof fetch): Promise<Onboarded> {
|
|
131
|
-
const { body, name } = onboardBody(load, project);
|
|
132
|
-
const first = await call(bases, 'onboardUser', tokens.accessToken, body, fetchImpl);
|
|
133
|
-
const op = await waitOperation(bases[0] ?? '', tokens.accessToken, first, fetchImpl);
|
|
134
|
-
const assigned = assignedProject(op) ?? project;
|
|
135
|
-
if (assigned === null) throw new GeminiAuthError(NO_ASSIGNMENT);
|
|
136
|
-
return { project: assigned, tier: name };
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export async function onboard(tokens: GeminiTokens, project: string | null = null, base?: Bases, fetchImpl: typeof fetch = fetch): Promise<Onboarded> {
|
|
140
|
-
const bases = setupBases(base);
|
|
141
|
-
const asked = { metadata: clientMetadata(project), mode: 1, ...withProject(project) };
|
|
142
|
-
const load = await call(bases, 'loadCodeAssist', tokens.accessToken, asked, fetchImpl);
|
|
143
|
-
log.info(summary(load), 'gemini: Code Assist answered loadCodeAssist');
|
|
144
|
-
refuseIneligible(load);
|
|
145
|
-
if (!isRecord(load.currentTier)) return onboardNew(load, tokens, project, [...bases].reverse(), fetchImpl);
|
|
146
|
-
const known = nonEmpty(load.cloudaicompanionProject) ?? project;
|
|
147
|
-
if (known === null) throw new GeminiAuthError(NO_PROJECT);
|
|
148
|
-
const tier = tierOf(load);
|
|
149
|
-
return { project: known, tier: tier.name ?? tier.id };
|
|
150
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
2
|
-
import { errorFrame } from './forward.js';
|
|
3
|
-
import { frame, messageEnd, messageStart, type Usage } from './frames.js';
|
|
4
|
-
import { encodeSignature, newCallId, rememberSignature } from './gemini-translate.js';
|
|
5
|
-
import { stringOf } from '@metro-labs/http/api-http';
|
|
6
|
-
|
|
7
|
-
type Item = Record<string, unknown>;
|
|
8
|
-
|
|
9
|
-
interface Open {
|
|
10
|
-
index: number;
|
|
11
|
-
kind: 'text' | 'thinking';
|
|
12
|
-
signature: string | null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const num = (value: unknown): number => (typeof value === 'number' ? value : 0);
|
|
16
|
-
|
|
17
|
-
const STOP_OF: Record<string, string> = { STOP: 'end_turn', MAX_TOKENS: 'max_tokens' };
|
|
18
|
-
const REFUSALS = new Set(['SAFETY', 'RECITATION', 'BLOCKLIST', 'PROHIBITED_CONTENT', 'SPII', 'MALFORMED_FUNCTION_CALL']);
|
|
19
|
-
|
|
20
|
-
function usageOf(meta: Item): Usage {
|
|
21
|
-
const cached = num(meta.cachedContentTokenCount);
|
|
22
|
-
return {
|
|
23
|
-
input_tokens: Math.max(0, num(meta.promptTokenCount) - cached),
|
|
24
|
-
output_tokens: num(meta.candidatesTokenCount) + num(meta.thoughtsTokenCount),
|
|
25
|
-
cache_read_input_tokens: cached,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class GeminiStreamTranslator {
|
|
30
|
-
private next = 0;
|
|
31
|
-
private open: Open | null = null;
|
|
32
|
-
private started = false;
|
|
33
|
-
private done = false;
|
|
34
|
-
private toolCalls = 0;
|
|
35
|
-
private usage: Usage = { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0 };
|
|
36
|
-
private stop = 'end_turn';
|
|
37
|
-
private finishedBy: string | null = null;
|
|
38
|
-
private refusal: string | null = null;
|
|
39
|
-
private readonly model: string;
|
|
40
|
-
private readonly restore: (name: string) => string;
|
|
41
|
-
|
|
42
|
-
constructor(model: string, restore: (name: string) => string = (name) => name) {
|
|
43
|
-
this.model = model;
|
|
44
|
-
this.restore = restore;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get finished(): boolean {
|
|
48
|
-
return this.done;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private start(): string {
|
|
52
|
-
if (this.started) return '';
|
|
53
|
-
this.started = true;
|
|
54
|
-
return messageStart('msg_gemini', this.model);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private closeOpen(): string {
|
|
58
|
-
const block = this.open;
|
|
59
|
-
if (block === null) return '';
|
|
60
|
-
this.open = null;
|
|
61
|
-
const signature = block.kind === 'thinking' ? frame('content_block_delta', { type: 'content_block_delta', index: block.index, delta: { type: 'signature_delta', signature: encodeSignature(block.signature ?? '') } }) : '';
|
|
62
|
-
return signature + frame('content_block_stop', { type: 'content_block_stop', index: block.index });
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private ensure(kind: Open['kind']): string {
|
|
66
|
-
if (this.open?.kind === kind) return '';
|
|
67
|
-
const closed = this.closeOpen();
|
|
68
|
-
this.open = { index: this.next, kind, signature: null };
|
|
69
|
-
this.next += 1;
|
|
70
|
-
const content = kind === 'text' ? { type: 'text', text: '' } : { type: 'thinking', thinking: '' };
|
|
71
|
-
return closed + frame('content_block_start', { type: 'content_block_start', index: this.open.index, content_block: content });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private textPart(part: Item): string {
|
|
75
|
-
const kind = part.thought === true ? 'thinking' : 'text';
|
|
76
|
-
const text = stringOf(part.text);
|
|
77
|
-
let out = this.ensure(kind);
|
|
78
|
-
const block = this.open;
|
|
79
|
-
if (block !== null && stringOf(part.thoughtSignature) !== '') block.signature = stringOf(part.thoughtSignature);
|
|
80
|
-
if (text === '') return out;
|
|
81
|
-
const delta = kind === 'text' ? { type: 'text_delta', text } : { type: 'thinking_delta', thinking: text };
|
|
82
|
-
out += frame('content_block_delta', { type: 'content_block_delta', index: block?.index ?? 0, delta });
|
|
83
|
-
return out;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private callPart(call: Item, signature: string): string {
|
|
87
|
-
this.toolCalls += 1;
|
|
88
|
-
const id = stringOf(call.id) || newCallId();
|
|
89
|
-
if (signature !== '') rememberSignature(id, signature);
|
|
90
|
-
const index = this.next;
|
|
91
|
-
this.next += 1;
|
|
92
|
-
return (
|
|
93
|
-
this.closeOpen() +
|
|
94
|
-
frame('content_block_start', { type: 'content_block_start', index, content_block: { type: 'tool_use', id, name: this.restore(stringOf(call.name)), input: {} } }) +
|
|
95
|
-
frame('content_block_delta', { type: 'content_block_delta', index, delta: { type: 'input_json_delta', partial_json: JSON.stringify(isRecord(call.args) ? call.args : {}) } }) +
|
|
96
|
-
frame('content_block_stop', { type: 'content_block_stop', index })
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private part(part: Item): string {
|
|
101
|
-
if (isRecord(part.functionCall)) return this.callPart(part.functionCall, stringOf(part.thoughtSignature));
|
|
102
|
-
if (typeof part.text === 'string') return this.textPart(part);
|
|
103
|
-
return '';
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private candidate(candidate: Item): string {
|
|
107
|
-
const content = isRecord(candidate.content) ? candidate.content : {};
|
|
108
|
-
const parts = Array.isArray(content.parts) ? content.parts.filter(isRecord) : [];
|
|
109
|
-
const out = parts.map((part) => this.part(part)).join('');
|
|
110
|
-
const finish = stringOf(candidate.finishReason);
|
|
111
|
-
if (finish !== '') this.finishedBy = finish;
|
|
112
|
-
const stop = STOP_OF[finish];
|
|
113
|
-
if (stop !== undefined) this.stop = stop;
|
|
114
|
-
else if (REFUSALS.has(finish)) this.refusal = `Gemini stopped the answer (${finish})`;
|
|
115
|
-
return out;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
push(data: Item): string {
|
|
119
|
-
if (this.done) return '';
|
|
120
|
-
const response = isRecord(data.response) ? data.response : data;
|
|
121
|
-
const out = this.start();
|
|
122
|
-
if (isRecord(response.usageMetadata)) this.usage = usageOf(response.usageMetadata);
|
|
123
|
-
const candidate = Array.isArray(response.candidates) ? response.candidates.find(isRecord) : undefined;
|
|
124
|
-
if (candidate !== undefined) return out + this.candidate(candidate);
|
|
125
|
-
const feedback = isRecord(response.promptFeedback) ? stringOf(response.promptFeedback.blockReason) : '';
|
|
126
|
-
if (feedback !== '') this.refusal = `Gemini blocked the prompt (${feedback})`;
|
|
127
|
-
return out;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
close(error?: string): string {
|
|
131
|
-
if (this.done) return '';
|
|
132
|
-
this.done = true;
|
|
133
|
-
const out = this.start() + this.closeOpen();
|
|
134
|
-
const unfinished = this.finishedBy === null && this.refusal === null ? 'Gemini ended the stream before completing the response' : null;
|
|
135
|
-
const failure = error ?? this.refusal ?? unfinished;
|
|
136
|
-
if (failure !== null) return out + errorFrame('api_error', failure);
|
|
137
|
-
return out + messageEnd(this.toolCalls > 0 ? 'tool_use' : this.stop, this.usage);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
-
import { ToolNames } from './codex-translate.js';
|
|
4
|
-
import { CLIENT_NAME, requestId, SYSTEM_PREFIX } from './gemini-client.js';
|
|
5
|
-
import { cappedEffort, effortToApply } from './effort.js';
|
|
6
|
-
import { inlineImage, resultImages, resultText, type InlineImage } from './text.js';
|
|
7
|
-
import { stringOf } from '@metro-labs/http/api-http';
|
|
8
|
-
|
|
9
|
-
type Item = Record<string, unknown>;
|
|
10
|
-
|
|
11
|
-
const SIGNATURE_PREFIX = 'metro-gemini:';
|
|
12
|
-
export const SKIP_SIGNATURE = 'skip_thought_signature_validator';
|
|
13
|
-
const SCHEMA_KEEP = new Set([
|
|
14
|
-
'type',
|
|
15
|
-
'description',
|
|
16
|
-
'nullable',
|
|
17
|
-
'enum',
|
|
18
|
-
'required',
|
|
19
|
-
'minItems',
|
|
20
|
-
'maxItems',
|
|
21
|
-
'minProperties',
|
|
22
|
-
'maxProperties',
|
|
23
|
-
'minLength',
|
|
24
|
-
'maxLength',
|
|
25
|
-
'pattern',
|
|
26
|
-
'minimum',
|
|
27
|
-
'maximum',
|
|
28
|
-
'propertyOrdering',
|
|
29
|
-
]);
|
|
30
|
-
const SCHEMA_LISTS = new Set(['anyOf', 'oneOf']);
|
|
31
|
-
const SIGNATURES_MAX = 2000;
|
|
32
|
-
const MAX_OUTPUT_TOKENS = 16384;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const callSignatures = new Map<string, string>();
|
|
36
|
-
|
|
37
|
-
export function rememberSignature(id: string, signature: string): void {
|
|
38
|
-
callSignatures.set(id, signature);
|
|
39
|
-
if (callSignatures.size > SIGNATURES_MAX) {
|
|
40
|
-
const first = callSignatures.keys().next().value;
|
|
41
|
-
if (first !== undefined) callSignatures.delete(first);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const newCallId = (): string => `toolu_gm_${randomBytes(12).toString('base64url')}`;
|
|
46
|
-
|
|
47
|
-
export const encodeSignature = (signature: string): string => `${SIGNATURE_PREFIX}${Buffer.from(signature).toString('base64url')}`;
|
|
48
|
-
|
|
49
|
-
export function decodeSignature(signature: unknown): string | null {
|
|
50
|
-
if (typeof signature !== 'string' || !signature.startsWith(SIGNATURE_PREFIX)) return null;
|
|
51
|
-
const raw = Buffer.from(signature.slice(SIGNATURE_PREFIX.length), 'base64url').toString('utf8');
|
|
52
|
-
return raw === '' ? null : raw;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function systemText(system: unknown): string {
|
|
56
|
-
if (typeof system === 'string') return system.trim();
|
|
57
|
-
if (!Array.isArray(system)) return '';
|
|
58
|
-
return system
|
|
59
|
-
.filter(isRecord)
|
|
60
|
-
.map((block) => stringOf(block.text))
|
|
61
|
-
.filter((t) => t !== '')
|
|
62
|
-
.join('\n\n')
|
|
63
|
-
.trim();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const imagePart = (image: InlineImage): Item => ({ inlineData: { mimeType: image.mediaType, data: image.data } });
|
|
67
|
-
|
|
68
|
-
const GEMINI_IMAGES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/heic', 'image/heif']);
|
|
69
|
-
const geminiSees = (mediaType: string): boolean => GEMINI_IMAGES.has(mediaType);
|
|
70
|
-
|
|
71
|
-
function toolParts(block: Item, id: string, name: string): Item[] {
|
|
72
|
-
const images = resultImages(block, geminiSees);
|
|
73
|
-
const text = resultText(block, geminiSees);
|
|
74
|
-
const result = text === '' && images.length > 0 ? `Binary content provided (${String(images.length)} item(s)).` : text;
|
|
75
|
-
return [{ functionResponse: { id, name, response: { result } } }, ...images.map(imagePart)];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function userPart(block: Item): Item | null {
|
|
79
|
-
if (block.type === 'text') return { text: stringOf(block.text) };
|
|
80
|
-
const image = inlineImage(block);
|
|
81
|
-
return image === null ? null : imagePart(image);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function userParts(content: unknown, calls: Map<string, string>): Item[] {
|
|
85
|
-
if (typeof content === 'string') return content === '' ? [] : [{ text: content }];
|
|
86
|
-
if (!Array.isArray(content)) return [];
|
|
87
|
-
const out: Item[] = [];
|
|
88
|
-
for (const block of content.filter(isRecord)) {
|
|
89
|
-
if (block.type === 'tool_result') {
|
|
90
|
-
const id = stringOf(block.tool_use_id);
|
|
91
|
-
out.push(...toolParts(block, id, calls.get(id) ?? 'tool'));
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
const part = userPart(block);
|
|
95
|
-
if (part !== null) out.push(part);
|
|
96
|
-
}
|
|
97
|
-
return out;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function callPart(block: Item, names: ToolNames, calls: Map<string, string>): Item {
|
|
101
|
-
const id = stringOf(block.id);
|
|
102
|
-
const name = names.alias(stringOf(block.name));
|
|
103
|
-
calls.set(id, name);
|
|
104
|
-
return { functionCall: { id, name, args: isRecord(block.input) ? block.input : {} }, thoughtSignature: callSignatures.get(id) ?? SKIP_SIGNATURE };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function assistantPart(block: Item, names: ToolNames, calls: Map<string, string>): Item | null {
|
|
108
|
-
if (block.type === 'text') return { text: stringOf(block.text) };
|
|
109
|
-
if (block.type === 'tool_use') return callPart(block, names, calls);
|
|
110
|
-
if (block.type !== 'thinking') return null;
|
|
111
|
-
const signature = decodeSignature(block.signature);
|
|
112
|
-
return signature === null ? null : { text: stringOf(block.thinking) || ' ', thought: true, thoughtSignature: signature };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function assistantParts(content: unknown, names: ToolNames, calls: Map<string, string>): Item[] {
|
|
116
|
-
if (typeof content === 'string') return content === '' ? [] : [{ text: content }];
|
|
117
|
-
if (!Array.isArray(content)) return [];
|
|
118
|
-
const out: Item[] = [];
|
|
119
|
-
for (const block of content.filter(isRecord)) {
|
|
120
|
-
const part = assistantPart(block, names, calls);
|
|
121
|
-
if (part !== null) out.push(part);
|
|
122
|
-
}
|
|
123
|
-
return out;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function appendTurn(out: Item[], role: string, parts: Item[]): void {
|
|
127
|
-
if (parts.length === 0) return;
|
|
128
|
-
const last = out.at(-1);
|
|
129
|
-
const held = last?.role === role && Array.isArray(last.parts) ? (last.parts as Item[]) : null;
|
|
130
|
-
if (held !== null) held.push(...parts);
|
|
131
|
-
else out.push({ role, parts });
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function contentsOf(messages: unknown, names = new ToolNames()): Item[] {
|
|
135
|
-
const out: Item[] = [];
|
|
136
|
-
if (!Array.isArray(messages)) return out;
|
|
137
|
-
const calls = new Map<string, string>();
|
|
138
|
-
for (const message of messages.filter(isRecord)) {
|
|
139
|
-
const model = message.role === 'assistant';
|
|
140
|
-
appendTurn(out, model ? 'model' : 'user', model ? assistantParts(message.content, names, calls) : userParts(message.content, calls));
|
|
141
|
-
}
|
|
142
|
-
return out;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function schemaType(value: unknown, out: Item): void {
|
|
146
|
-
if (!Array.isArray(value)) {
|
|
147
|
-
out.type = value;
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
const kinds = value.filter((k) => k !== 'null');
|
|
151
|
-
if (kinds.length !== value.length) out.nullable = true;
|
|
152
|
-
if (kinds.length > 0) out.type = kinds[0];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function schemaField(key: string, value: unknown, out: Item): void {
|
|
156
|
-
if (key === 'type') schemaType(value, out);
|
|
157
|
-
else if (key === 'const') out.enum = [value];
|
|
158
|
-
else if (key === 'items') out.items = cleanSchema(value);
|
|
159
|
-
else if (key === 'properties' && isRecord(value)) out.properties = Object.fromEntries(Object.entries(value).map(([name, v]) => [name, cleanSchema(v)]));
|
|
160
|
-
else if (SCHEMA_LISTS.has(key) && Array.isArray(value)) out.anyOf = value.map(cleanSchema);
|
|
161
|
-
else if (SCHEMA_KEEP.has(key)) out[key] = value;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function cleanSchema(schema: unknown): Item {
|
|
165
|
-
if (!isRecord(schema)) return {};
|
|
166
|
-
const out: Item = {};
|
|
167
|
-
for (const [key, value] of Object.entries(schema)) schemaField(key, value, out);
|
|
168
|
-
if (out.nullable === true && Array.isArray(out.enum)) out.enum = out.enum.filter((value) => value !== null);
|
|
169
|
-
return out;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function toolDeclarations(tools: unknown, names = new ToolNames()): Item[] {
|
|
173
|
-
if (!Array.isArray(tools)) return [];
|
|
174
|
-
return tools
|
|
175
|
-
.filter(isRecord)
|
|
176
|
-
.filter((tool) => typeof tool.name === 'string' && (tool.type === undefined || tool.type === 'custom'))
|
|
177
|
-
.map((tool) => ({
|
|
178
|
-
name: names.alias(stringOf(tool.name)),
|
|
179
|
-
description: stringOf(tool.description),
|
|
180
|
-
parameters: cleanSchema(isRecord(tool.input_schema) ? tool.input_schema : { type: 'object', properties: {} }),
|
|
181
|
-
}));
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function toolConfig(choice: unknown, names: ToolNames): Item | null {
|
|
185
|
-
if (!isRecord(choice)) return null;
|
|
186
|
-
if (choice.type === 'any') return { functionCallingConfig: { mode: 'ANY' } };
|
|
187
|
-
if (choice.type === 'none') return { functionCallingConfig: { mode: 'NONE' } };
|
|
188
|
-
if (choice.type === 'tool') return { functionCallingConfig: { mode: 'ANY', allowedFunctionNames: [names.alias(stringOf(choice.name))] } };
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function generationConfig(body: Item): Item {
|
|
193
|
-
const out: Item = {};
|
|
194
|
-
if (typeof body.max_tokens === 'number') out.maxOutputTokens = Math.min(body.max_tokens, MAX_OUTPUT_TOKENS);
|
|
195
|
-
if (typeof body.temperature === 'number') out.temperature = body.temperature;
|
|
196
|
-
if (typeof body.top_p === 'number') out.topP = body.top_p;
|
|
197
|
-
if (isRecord(body.thinking) && body.thinking.type === 'enabled') out.thinkingConfig = { includeThoughts: true };
|
|
198
|
-
const effort = effortToApply(body);
|
|
199
|
-
if (effort !== null) out.thinkingLevel = cappedEffort(effort);
|
|
200
|
-
return out;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export interface GeminiRequest {
|
|
204
|
-
model: string;
|
|
205
|
-
project: string;
|
|
206
|
-
userAgent: string;
|
|
207
|
-
requestType: string;
|
|
208
|
-
requestId: string;
|
|
209
|
-
request: Item;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const systemParts = (system: string): Item[] => [
|
|
213
|
-
{ text: SYSTEM_PREFIX },
|
|
214
|
-
{ text: `Please ignore the following [ignore]${SYSTEM_PREFIX}[/ignore]` },
|
|
215
|
-
...(system === '' ? [] : [{ text: system }]),
|
|
216
|
-
];
|
|
217
|
-
|
|
218
|
-
export function toGeminiRequest(body: Item, model: string, project: string, promptId: string, names = new ToolNames()): GeminiRequest {
|
|
219
|
-
const declarations = toolDeclarations(body.tools, names);
|
|
220
|
-
const config = toolConfig(body.tool_choice, names);
|
|
221
|
-
return {
|
|
222
|
-
model,
|
|
223
|
-
project,
|
|
224
|
-
userAgent: CLIENT_NAME,
|
|
225
|
-
requestType: 'agent',
|
|
226
|
-
requestId: requestId(),
|
|
227
|
-
request: {
|
|
228
|
-
contents: contentsOf(body.messages, names),
|
|
229
|
-
systemInstruction: { role: 'user', parts: systemParts(systemText(body.system)) },
|
|
230
|
-
...(declarations.length === 0 ? {} : { tools: [{ functionDeclarations: declarations }] }),
|
|
231
|
-
...(config === null ? {} : { toolConfig: config }),
|
|
232
|
-
generationConfig: generationConfig(body),
|
|
233
|
-
sessionId: promptId,
|
|
234
|
-
},
|
|
235
|
-
};
|
|
236
|
-
}
|