@effect-uai/google 0.10.0 → 0.11.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/Gemini.d.mts +3 -2
- package/dist/Gemini.d.mts.map +1 -1
- package/dist/Gemini.mjs +18 -6
- package/dist/Gemini.mjs.map +1 -1
- package/dist/GeminiEmbedding.mjs +1 -1
- package/dist/GeminiEmbedding.mjs.map +1 -1
- package/dist/GeminiSynthesizer.d.mts +4 -14
- package/dist/GeminiSynthesizer.d.mts.map +1 -1
- package/dist/GeminiSynthesizer.mjs +2 -2
- package/dist/GeminiSynthesizer.mjs.map +1 -1
- package/dist/GeminiSynthesizer.test.mjs +1 -1
- package/dist/GeminiTools.d.mts +42 -0
- package/dist/GeminiTools.d.mts.map +1 -0
- package/dist/GeminiTools.mjs +64 -0
- package/dist/GeminiTools.mjs.map +1 -0
- package/dist/GoogleDeepResearch.d.mts +70 -0
- package/dist/GoogleDeepResearch.d.mts.map +1 -0
- package/dist/GoogleDeepResearch.mjs +269 -0
- package/dist/GoogleDeepResearch.mjs.map +1 -0
- package/dist/LyriaGenerator.mjs +1 -1
- package/dist/LyriaGenerator.mjs.map +1 -1
- package/dist/LyriaGenerator.test.mjs +1 -1
- package/dist/codec.d.mts +16 -4
- package/dist/codec.d.mts.map +1 -1
- package/dist/codec.mjs +5 -6
- package/dist/codec.mjs.map +1 -1
- package/dist/{dist-DMsg8YLb.mjs → dist-CWoAuwS9.mjs} +7 -7
- package/dist/{dist-DMsg8YLb.mjs.map → dist-CWoAuwS9.mjs.map} +1 -1
- package/dist/geminiSpeechCodec.test.mjs +1 -1
- package/dist/geminiTools.test.d.mts +1 -0
- package/dist/geminiTools.test.mjs +80 -0
- package/dist/geminiTools.test.mjs.map +1 -0
- package/dist/index.d.mts +4 -2
- package/dist/index.mjs +3 -1
- package/dist/models.d.mts +11 -1
- package/dist/models.d.mts.map +1 -1
- package/package.json +8 -4
- package/src/Gemini.ts +21 -3
- package/src/GeminiEmbedding.ts +1 -1
- package/src/GeminiSynthesizer.ts +4 -2
- package/src/GeminiTools.ts +95 -0
- package/src/GoogleDeepResearch.ts +446 -0
- package/src/LyriaGenerator.ts +1 -1
- package/src/codec.ts +25 -11
- package/src/geminiTools.test.ts +88 -0
- package/src/index.ts +2 -0
- package/src/models.ts +15 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Match, Redacted, Ref, Schema, Stream } from "effect"
|
|
2
|
+
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
|
|
3
|
+
import * as AiError from "@effect-uai/core/AiError"
|
|
4
|
+
import {
|
|
5
|
+
DeepResearch,
|
|
6
|
+
type DeepResearchService,
|
|
7
|
+
type DeepResearchServiceShape,
|
|
8
|
+
fromJob,
|
|
9
|
+
type ResearchJobOps,
|
|
10
|
+
} from "@effect-uai/core/DeepResearch"
|
|
11
|
+
import * as Items from "@effect-uai/core/Items"
|
|
12
|
+
import * as Job from "@effect-uai/core/Job"
|
|
13
|
+
import type { ResearchJobRef, ResearchRequest, ResearchState } from "@effect-uai/core/Research"
|
|
14
|
+
import * as SSE from "@effect-uai/core/SSE"
|
|
15
|
+
import { type Turn, TurnEvent } from "@effect-uai/core/Turn"
|
|
16
|
+
import type { GoogleDeepResearchModel } from "./models.js"
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Public types
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Gemini-typed research request. Narrows `model` to the deep-research agent ids.
|
|
24
|
+
* Provider-portable code uses the generic `DeepResearch` tag instead.
|
|
25
|
+
*/
|
|
26
|
+
export type GoogleResearchRequest = Omit<ResearchRequest, "model"> & {
|
|
27
|
+
readonly model?: GoogleDeepResearchModel
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type GoogleDeepResearchService = DeepResearchServiceShape<GoogleResearchRequest>
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Provider-typed tag for Gemini deep research over the Interactions API
|
|
34
|
+
* (`POST /v1beta/interactions`, `agent: deep-research-*`). Yield this for the
|
|
35
|
+
* agent-id knob; yield the generic `DeepResearch` tag for portable code.
|
|
36
|
+
*
|
|
37
|
+
* `research` / `submit` / `collect` drive a background job by polling;
|
|
38
|
+
* `researchStream` consumes the real Interactions SSE (`step.delta` text /
|
|
39
|
+
* thought, terminating in `interaction.completed`). Preview surface: the SSE
|
|
40
|
+
* event shapes and the completed-interaction citation shape are decoded
|
|
41
|
+
* defensively and best confirmed against a live response.
|
|
42
|
+
*/
|
|
43
|
+
export class GoogleDeepResearch extends Context.Service<
|
|
44
|
+
GoogleDeepResearch,
|
|
45
|
+
GoogleDeepResearchService
|
|
46
|
+
>()("@betalyra/effect-uai/providers/google/GoogleDeepResearch") {}
|
|
47
|
+
|
|
48
|
+
export type Config = {
|
|
49
|
+
readonly apiKey: Redacted.Redacted
|
|
50
|
+
readonly baseUrl?: string
|
|
51
|
+
/** Poll cadence / overall timeout for the derived poll loops. */
|
|
52
|
+
readonly job?: Job.JobConfig
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// Wire codec
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
const WireSubmit = Schema.Struct({ id: Schema.String })
|
|
60
|
+
const decodeSubmit = Schema.decodeUnknownEffect(WireSubmit)
|
|
61
|
+
|
|
62
|
+
// A completed interaction's text lives at `outputs[].text` (SDK `outputs`) or,
|
|
63
|
+
// in some doc examples, `steps[].content[].text`. Decode both defensively and
|
|
64
|
+
// extract from whichever is populated. Citations are undocumented on this
|
|
65
|
+
// preview surface, so none are mapped yet (the report text carries inline
|
|
66
|
+
// `[n]` markers); enrich once a live response confirms the shape.
|
|
67
|
+
const WireTextPart = Schema.Struct({
|
|
68
|
+
type: Schema.optional(Schema.NullOr(Schema.String)),
|
|
69
|
+
text: Schema.optional(Schema.NullOr(Schema.String)),
|
|
70
|
+
})
|
|
71
|
+
const WireStep = Schema.Struct({
|
|
72
|
+
type: Schema.optional(Schema.NullOr(Schema.String)),
|
|
73
|
+
text: Schema.optional(Schema.NullOr(Schema.String)),
|
|
74
|
+
content: Schema.optional(Schema.NullOr(Schema.Array(WireTextPart))),
|
|
75
|
+
})
|
|
76
|
+
export const WireInteraction = Schema.Struct({
|
|
77
|
+
id: Schema.optional(Schema.NullOr(Schema.String)),
|
|
78
|
+
status: Schema.optional(Schema.NullOr(Schema.String)),
|
|
79
|
+
outputs: Schema.optional(Schema.NullOr(Schema.Array(WireTextPart))),
|
|
80
|
+
steps: Schema.optional(Schema.NullOr(Schema.Array(WireStep))),
|
|
81
|
+
error: Schema.optional(
|
|
82
|
+
Schema.NullOr(
|
|
83
|
+
Schema.Struct({
|
|
84
|
+
message: Schema.optional(Schema.NullOr(Schema.String)),
|
|
85
|
+
code: Schema.optional(Schema.NullOr(Schema.String)),
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
),
|
|
89
|
+
})
|
|
90
|
+
export type WireInteraction = typeof WireInteraction.Type
|
|
91
|
+
const decodeInteraction = Schema.decodeUnknownEffect(WireInteraction)
|
|
92
|
+
|
|
93
|
+
const textOfParts = (parts: ReadonlyArray<typeof WireTextPart.Type> | null | undefined): string =>
|
|
94
|
+
(parts ?? [])
|
|
95
|
+
.filter((p) => (p.type ?? "text") === "text" && p.text != null)
|
|
96
|
+
.map((p) => p.text)
|
|
97
|
+
.join("")
|
|
98
|
+
|
|
99
|
+
const reportText = (wire: WireInteraction): string => {
|
|
100
|
+
const fromOutputs = textOfParts(wire.outputs)
|
|
101
|
+
if (fromOutputs.length > 0) return fromOutputs
|
|
102
|
+
const lastStep = (wire.steps ?? []).at(-1)
|
|
103
|
+
const fromStep = textOfParts(lastStep?.content)
|
|
104
|
+
return fromStep.length > 0 ? fromStep : (lastStep?.text ?? "")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const turnFromInteraction = (wire: WireInteraction): Turn => ({
|
|
108
|
+
items: [
|
|
109
|
+
{
|
|
110
|
+
type: "message",
|
|
111
|
+
role: "assistant",
|
|
112
|
+
content: [{ type: "output_text", text: reportText(wire) }],
|
|
113
|
+
providerData: wire,
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
usage: {},
|
|
117
|
+
stop_reason: "stop",
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// Map the interaction status onto the generic `JobState`.
|
|
121
|
+
export const jobStateOf = (wire: WireInteraction): ResearchState =>
|
|
122
|
+
Match.value(wire.status ?? "").pipe(
|
|
123
|
+
Match.whenOr("queued", "pending", (): ResearchState => ({ _tag: "Pending" })),
|
|
124
|
+
Match.whenOr(
|
|
125
|
+
"in_progress",
|
|
126
|
+
"running",
|
|
127
|
+
"processing",
|
|
128
|
+
(): ResearchState => ({ _tag: "Running" }),
|
|
129
|
+
),
|
|
130
|
+
Match.whenOr(
|
|
131
|
+
"completed",
|
|
132
|
+
"succeeded",
|
|
133
|
+
(): ResearchState => ({ _tag: "Succeeded", result: turnFromInteraction(wire) }),
|
|
134
|
+
),
|
|
135
|
+
Match.whenOr(
|
|
136
|
+
"failed",
|
|
137
|
+
"error",
|
|
138
|
+
"cancelled",
|
|
139
|
+
(): ResearchState => ({
|
|
140
|
+
_tag: "Failed",
|
|
141
|
+
...(wire.error?.message != null && { reason: wire.error.message }),
|
|
142
|
+
raw: wire,
|
|
143
|
+
}),
|
|
144
|
+
),
|
|
145
|
+
Match.orElse((): ResearchState => ({ _tag: "Running" })),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// HTTP
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
const baseUrl = (cfg: Config): string =>
|
|
153
|
+
cfg.baseUrl ?? "https://generativelanguage.googleapis.com/v1beta"
|
|
154
|
+
|
|
155
|
+
const authHeader = (cfg: Config) =>
|
|
156
|
+
HttpClientRequest.setHeader("x-goog-api-key", Redacted.value(cfg.apiKey))
|
|
157
|
+
|
|
158
|
+
const transportFailure = (cause: unknown): AiError.AiError =>
|
|
159
|
+
new AiError.Unavailable({ provider: "google", raw: cause })
|
|
160
|
+
|
|
161
|
+
const httpStatusError = (status: number, body: string): AiError.AiError => {
|
|
162
|
+
const provider = "google"
|
|
163
|
+
const raw = body
|
|
164
|
+
if (status === 429) return new AiError.RateLimited({ provider, raw })
|
|
165
|
+
if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw })
|
|
166
|
+
if (status === 401) return new AiError.AuthFailed({ provider, subtype: "auth", raw })
|
|
167
|
+
if (status === 403) return new AiError.AuthFailed({ provider, subtype: "permission", raw })
|
|
168
|
+
if (status === 402) return new AiError.AuthFailed({ provider, subtype: "billing", raw })
|
|
169
|
+
if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw })
|
|
170
|
+
if (status >= 500) return new AiError.Unavailable({ provider, status, raw })
|
|
171
|
+
return new AiError.InvalidRequest({ provider, raw })
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Deep research takes one question. Concatenate the user turns' text into the
|
|
175
|
+
// Interactions `input` string.
|
|
176
|
+
const inputText = (history: ReadonlyArray<Items.HistoryItem>): string =>
|
|
177
|
+
history
|
|
178
|
+
.filter(Items.isMessage)
|
|
179
|
+
.filter((m) => m.role === "user")
|
|
180
|
+
.flatMap((m) => m.content)
|
|
181
|
+
.filter(Items.isInputText)
|
|
182
|
+
.map((b) => b.text)
|
|
183
|
+
.join("\n")
|
|
184
|
+
|
|
185
|
+
const submitBody = (request: GoogleResearchRequest): Record<string, unknown> => ({
|
|
186
|
+
agent: request.model ?? "deep-research-preview-04-2026",
|
|
187
|
+
input: inputText(request.history),
|
|
188
|
+
background: true,
|
|
189
|
+
store: true,
|
|
190
|
+
agent_config: { type: "deep-research", thinking_summaries: "auto" },
|
|
191
|
+
tools: [{ type: "google_search" }],
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
const submitJob = (
|
|
195
|
+
cfg: Config,
|
|
196
|
+
request: GoogleResearchRequest,
|
|
197
|
+
): Effect.Effect<ResearchJobRef, AiError.AiError, HttpClient.HttpClient> =>
|
|
198
|
+
Effect.gen(function* () {
|
|
199
|
+
const client = yield* HttpClient.HttpClient
|
|
200
|
+
const httpRequest = HttpClientRequest.post(`${baseUrl(cfg)}/interactions`).pipe(
|
|
201
|
+
authHeader(cfg),
|
|
202
|
+
HttpClientRequest.bodyJsonUnsafe(submitBody(request)),
|
|
203
|
+
)
|
|
204
|
+
const response = yield* client.execute(httpRequest).pipe(Effect.mapError(transportFailure))
|
|
205
|
+
if (response.status >= 400) {
|
|
206
|
+
const text = yield* response.text.pipe(Effect.orElseSucceed(() => ""))
|
|
207
|
+
return yield* httpStatusError(response.status, text)
|
|
208
|
+
}
|
|
209
|
+
const json = yield* response.json.pipe(Effect.mapError(transportFailure))
|
|
210
|
+
const wire = yield* decodeSubmit(json).pipe(Effect.mapError(transportFailure))
|
|
211
|
+
return Job.jobRef<Turn>("google", wire.id)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
const pollJob = (
|
|
215
|
+
cfg: Config,
|
|
216
|
+
ref: ResearchJobRef,
|
|
217
|
+
): Effect.Effect<ResearchState, AiError.AiError, HttpClient.HttpClient> =>
|
|
218
|
+
Effect.gen(function* () {
|
|
219
|
+
const client = yield* HttpClient.HttpClient
|
|
220
|
+
const httpRequest = HttpClientRequest.get(`${baseUrl(cfg)}/interactions/${ref.id}`).pipe(
|
|
221
|
+
authHeader(cfg),
|
|
222
|
+
)
|
|
223
|
+
const response = yield* client.execute(httpRequest).pipe(Effect.mapError(transportFailure))
|
|
224
|
+
if (response.status >= 400) {
|
|
225
|
+
const text = yield* response.text.pipe(Effect.orElseSucceed(() => ""))
|
|
226
|
+
return yield* httpStatusError(response.status, text)
|
|
227
|
+
}
|
|
228
|
+
const json = yield* response.json.pipe(Effect.mapError(transportFailure))
|
|
229
|
+
const wire = yield* decodeInteraction(json).pipe(Effect.mapError(transportFailure))
|
|
230
|
+
return jobStateOf(wire)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
// No verified cancel endpoint on the preview Interactions surface.
|
|
234
|
+
const unsupported = (capability: string): AiError.Unsupported =>
|
|
235
|
+
new AiError.Unsupported({
|
|
236
|
+
provider: "google",
|
|
237
|
+
capability,
|
|
238
|
+
reason: "Gemini deep research (preview) exposes no verified cancel endpoint.",
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
// Streaming (Interactions SSE)
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
// The Interactions stream emits `step.delta` events in two shapes: the report
|
|
246
|
+
// body arrives as `delta: { type: "text", text }`, while a thinking summary
|
|
247
|
+
// arrives as `delta: { type: "thought_summary", content: { text } }` (and a
|
|
248
|
+
// content-free `thought_signature` we skip). Terminates in `interaction.completed`
|
|
249
|
+
// / `interaction.error`, then an OpenAI-style `[DONE]` sentinel we drop upstream.
|
|
250
|
+
const WireDelta = Schema.Struct({
|
|
251
|
+
type: Schema.optional(Schema.NullOr(Schema.String)),
|
|
252
|
+
text: Schema.optional(Schema.NullOr(Schema.String)),
|
|
253
|
+
content: Schema.optional(
|
|
254
|
+
Schema.NullOr(
|
|
255
|
+
Schema.Struct({
|
|
256
|
+
type: Schema.optional(Schema.NullOr(Schema.String)),
|
|
257
|
+
text: Schema.optional(Schema.NullOr(Schema.String)),
|
|
258
|
+
}),
|
|
259
|
+
),
|
|
260
|
+
),
|
|
261
|
+
})
|
|
262
|
+
const WireStreamEvent = Schema.Struct({
|
|
263
|
+
event_type: Schema.optional(Schema.NullOr(Schema.String)),
|
|
264
|
+
delta: Schema.optional(Schema.NullOr(WireDelta)),
|
|
265
|
+
interaction: Schema.optional(Schema.NullOr(WireInteraction)),
|
|
266
|
+
error: Schema.optional(
|
|
267
|
+
Schema.NullOr(Schema.Struct({ message: Schema.optional(Schema.NullOr(Schema.String)) })),
|
|
268
|
+
),
|
|
269
|
+
})
|
|
270
|
+
type WireStreamEvent = typeof WireStreamEvent.Type
|
|
271
|
+
|
|
272
|
+
const parseJsonUnknown = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown))
|
|
273
|
+
const decodeStreamEvent = Schema.decodeUnknownEffect(WireStreamEvent)
|
|
274
|
+
|
|
275
|
+
// Prefer the completed interaction's decoded text; when the terminal event is
|
|
276
|
+
// bare (or its text decodes empty) fall back to the accumulated stream deltas,
|
|
277
|
+
// so `TurnComplete.turn` always carries the report that streamed.
|
|
278
|
+
const completedTurn = (wire: WireInteraction | null | undefined, streamed: string): Turn =>
|
|
279
|
+
wire != null && reportText(wire).length > 0
|
|
280
|
+
? turnFromInteraction(wire)
|
|
281
|
+
: {
|
|
282
|
+
items: [
|
|
283
|
+
{
|
|
284
|
+
type: "message",
|
|
285
|
+
role: "assistant",
|
|
286
|
+
content: [{ type: "output_text", text: streamed }],
|
|
287
|
+
...(wire != null && { providerData: wire }),
|
|
288
|
+
},
|
|
289
|
+
],
|
|
290
|
+
usage: {},
|
|
291
|
+
stop_reason: "stop",
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Map one wire event onto `TurnEvent`s, appending text deltas to `acc` (the
|
|
295
|
+
// `completedTurn` fallback) and lifting `interaction.error` to a typed failure.
|
|
296
|
+
const streamEventToTurnEvents = (
|
|
297
|
+
acc: Ref.Ref<string>,
|
|
298
|
+
ev: WireStreamEvent,
|
|
299
|
+
): Effect.Effect<ReadonlyArray<TurnEvent>, AiError.AiError> => {
|
|
300
|
+
if (ev.event_type === "interaction.error") {
|
|
301
|
+
return Effect.fail(
|
|
302
|
+
new AiError.GenerationFailed({
|
|
303
|
+
provider: "google",
|
|
304
|
+
...(ev.error?.message != null && { message: ev.error.message }),
|
|
305
|
+
raw: ev,
|
|
306
|
+
}),
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
if (ev.event_type === "step.delta") {
|
|
310
|
+
const delta = ev.delta
|
|
311
|
+
// Report body: `delta.text`. Accumulate it so the terminal `TurnComplete`
|
|
312
|
+
// carries the report even when `interaction.completed` arrives text-free.
|
|
313
|
+
if (delta?.text != null) {
|
|
314
|
+
const text = delta.text
|
|
315
|
+
return Effect.as(
|
|
316
|
+
Ref.update(acc, (streamed) => streamed + text),
|
|
317
|
+
[TurnEvent.TextDelta({ text })],
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
// Thinking summary: nested under `delta.content.text`. Surfaced as reasoning,
|
|
321
|
+
// not accumulated into the report.
|
|
322
|
+
if (delta?.content?.text != null) {
|
|
323
|
+
return Effect.succeed([
|
|
324
|
+
TurnEvent.ReasoningDelta({ text: delta.content.text, kind: "summary" }),
|
|
325
|
+
])
|
|
326
|
+
}
|
|
327
|
+
return Effect.succeed([])
|
|
328
|
+
}
|
|
329
|
+
if (ev.event_type === "interaction.completed") {
|
|
330
|
+
return Effect.map(Ref.get(acc), (streamed) => [
|
|
331
|
+
TurnEvent.TurnComplete({ turn: completedTurn(ev.interaction, streamed) }),
|
|
332
|
+
])
|
|
333
|
+
}
|
|
334
|
+
return Effect.succeed([])
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// A streaming interaction create (`?stream=true`): consume the SSE directly,
|
|
338
|
+
// mapping each event onto `TurnEvent`s via {@link streamEventToTurnEvents}.
|
|
339
|
+
const streamCreate = (
|
|
340
|
+
cfg: Config,
|
|
341
|
+
request: GoogleResearchRequest,
|
|
342
|
+
): Stream.Stream<TurnEvent, AiError.AiError, HttpClient.HttpClient> =>
|
|
343
|
+
Stream.unwrap(
|
|
344
|
+
Effect.gen(function* () {
|
|
345
|
+
const client = yield* HttpClient.HttpClient
|
|
346
|
+
const acc = yield* Ref.make("")
|
|
347
|
+
const httpRequest = HttpClientRequest.post(`${baseUrl(cfg)}/interactions?stream=true`).pipe(
|
|
348
|
+
authHeader(cfg),
|
|
349
|
+
HttpClientRequest.bodyJsonUnsafe({ ...submitBody(request), stream: true }),
|
|
350
|
+
HttpClientRequest.accept("text/event-stream"),
|
|
351
|
+
)
|
|
352
|
+
const response = yield* client.execute(httpRequest).pipe(Effect.mapError(transportFailure))
|
|
353
|
+
// Preview surface: log the response status + content-type so an
|
|
354
|
+
// unexpected `application/json` (i.e. `?stream=true` not triggering SSE)
|
|
355
|
+
// is visible at LOG_LEVEL=Debug rather than presenting as a silent hang.
|
|
356
|
+
yield* Effect.logDebug("google interactions stream response", {
|
|
357
|
+
status: response.status,
|
|
358
|
+
contentType: response.headers["content-type"],
|
|
359
|
+
})
|
|
360
|
+
if (response.status >= 400) {
|
|
361
|
+
const text = yield* response.text.pipe(Effect.orElseSucceed(() => ""))
|
|
362
|
+
return Stream.fail(httpStatusError(response.status, text))
|
|
363
|
+
}
|
|
364
|
+
return response.stream.pipe(
|
|
365
|
+
Stream.mapError(transportFailure),
|
|
366
|
+
SSE.fromBytes,
|
|
367
|
+
// Log each raw frame before decode: the ground truth for the (unverified)
|
|
368
|
+
// event_type / delta shapes this codec keys on.
|
|
369
|
+
Stream.tap((ev) =>
|
|
370
|
+
Effect.logDebug("google interactions sse frame", { event: ev.event, data: ev.data }),
|
|
371
|
+
),
|
|
372
|
+
// The stream ends with an OpenAI-style `[DONE]` sentinel that is not JSON;
|
|
373
|
+
// drop it (and any blank frame) before the JSON decode.
|
|
374
|
+
Stream.filter((ev) => ev.event !== "done" && ev.data.trim() !== "[DONE]"),
|
|
375
|
+
Stream.mapEffect((ev) =>
|
|
376
|
+
parseJsonUnknown(ev.data).pipe(
|
|
377
|
+
Effect.flatMap(decodeStreamEvent),
|
|
378
|
+
Effect.mapError(transportFailure),
|
|
379
|
+
Effect.flatMap((wire) => streamEventToTurnEvents(acc, wire)),
|
|
380
|
+
),
|
|
381
|
+
),
|
|
382
|
+
Stream.flatMap(Stream.fromIterable),
|
|
383
|
+
)
|
|
384
|
+
}),
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
// ---------------------------------------------------------------------------
|
|
388
|
+
// Constructors
|
|
389
|
+
// ---------------------------------------------------------------------------
|
|
390
|
+
|
|
391
|
+
const jobOps = (
|
|
392
|
+
cfg: Config,
|
|
393
|
+
client: HttpClient.HttpClient,
|
|
394
|
+
): ResearchJobOps<GoogleResearchRequest> => {
|
|
395
|
+
const withClient = <A>(e: Effect.Effect<A, AiError.AiError, HttpClient.HttpClient>) =>
|
|
396
|
+
Effect.provideService(e, HttpClient.HttpClient, client)
|
|
397
|
+
return {
|
|
398
|
+
submit: (request) => withClient(submitJob(cfg, request)),
|
|
399
|
+
poll: (ref) => withClient(pollJob(cfg, ref)),
|
|
400
|
+
cancel: () => Effect.fail(unsupported("cancel")),
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// fromJob's default surface, with `researchStream` overridden to the real
|
|
405
|
+
// Interactions SSE (its submit-then-attach default only synthesizes progress).
|
|
406
|
+
const buildService = <Req extends ResearchRequest>(
|
|
407
|
+
cfg: Config,
|
|
408
|
+
client: HttpClient.HttpClient,
|
|
409
|
+
ops: ResearchJobOps<Req>,
|
|
410
|
+
toGoogle: (request: Req) => GoogleResearchRequest,
|
|
411
|
+
): DeepResearchServiceShape<Req> => ({
|
|
412
|
+
...fromJob(ops, cfg.job),
|
|
413
|
+
researchStream: (request) =>
|
|
414
|
+
Stream.provideService(streamCreate(cfg, toGoogle(request)), HttpClient.HttpClient, client),
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
/** Build a `GoogleDeepResearchService`. For Layer setup, prefer {@link layer}. */
|
|
418
|
+
export const make = (
|
|
419
|
+
cfg: Config,
|
|
420
|
+
): Effect.Effect<GoogleDeepResearchService, never, HttpClient.HttpClient> =>
|
|
421
|
+
Effect.map(HttpClient.HttpClient, (client) =>
|
|
422
|
+
buildService(cfg, client, jobOps(cfg, client), (request) => request),
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Layer registering the provider-typed `GoogleDeepResearch` tag and the generic
|
|
427
|
+
* `DeepResearch` tag. No capability markers: the surface (poll, collect,
|
|
428
|
+
* synthesized stream) is universal.
|
|
429
|
+
*/
|
|
430
|
+
export const layer = (
|
|
431
|
+
cfg: Config,
|
|
432
|
+
): Layer.Layer<GoogleDeepResearch | DeepResearch, never, HttpClient.HttpClient> => {
|
|
433
|
+
const typed = Layer.effect(GoogleDeepResearch, make(cfg))
|
|
434
|
+
const generic = Layer.effect(
|
|
435
|
+
DeepResearch,
|
|
436
|
+
Effect.map(HttpClient.HttpClient, (client): DeepResearchService => {
|
|
437
|
+
const ops = jobOps(cfg, client)
|
|
438
|
+
const genericOps: ResearchJobOps<ResearchRequest> = {
|
|
439
|
+
...ops,
|
|
440
|
+
submit: (request) => ops.submit(request as GoogleResearchRequest),
|
|
441
|
+
}
|
|
442
|
+
return buildService(cfg, client, genericOps, (request) => request as GoogleResearchRequest)
|
|
443
|
+
}),
|
|
444
|
+
)
|
|
445
|
+
return Layer.merge(typed, generic)
|
|
446
|
+
}
|
package/src/LyriaGenerator.ts
CHANGED
|
@@ -371,7 +371,7 @@ const streamGenerationFromUnsupported = <E, R>(
|
|
|
371
371
|
export const make = (
|
|
372
372
|
cfg: Config,
|
|
373
373
|
): Effect.Effect<LyriaGeneratorService, never, HttpClient.HttpClient> =>
|
|
374
|
-
Effect.map(HttpClient.HttpClient
|
|
374
|
+
Effect.map(HttpClient.HttpClient, (client) => ({
|
|
375
375
|
generate: (request) =>
|
|
376
376
|
generateImpl(cfg)(request).pipe(Effect.provideService(HttpClient.HttpClient, client)),
|
|
377
377
|
streamGeneration: (request) =>
|
package/src/codec.ts
CHANGED
|
@@ -101,14 +101,24 @@ type RequestSystemInstruction = {
|
|
|
101
101
|
readonly parts: ReadonlyArray<{ readonly text: string }>
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Gemini's tool declaration. A `tools[]` entry is either a function-declaration
|
|
106
|
+
* block (translated from `ToolDescriptor`s) or one of the native hosted-tool
|
|
107
|
+
* entries. The native entries are empty objects keyed camelCase per the REST
|
|
108
|
+
* v1beta `Tool` object, and sit as siblings of the declarations in the same
|
|
109
|
+
* `tools` array.
|
|
110
|
+
*/
|
|
111
|
+
export type RequestTool =
|
|
112
|
+
| {
|
|
113
|
+
readonly functionDeclarations: ReadonlyArray<{
|
|
114
|
+
readonly name: string
|
|
115
|
+
readonly description: string
|
|
116
|
+
readonly parameters: Record<string, unknown>
|
|
117
|
+
}>
|
|
118
|
+
}
|
|
119
|
+
| { readonly googleSearch: {} }
|
|
120
|
+
| { readonly urlContext: {} }
|
|
121
|
+
| { readonly codeExecution: {} }
|
|
112
122
|
|
|
113
123
|
type RequestToolConfig = {
|
|
114
124
|
readonly functionCallingConfig: {
|
|
@@ -402,11 +412,13 @@ export const buildRequestBody = (
|
|
|
402
412
|
history: ReadonlyArray<HistoryItem>,
|
|
403
413
|
generationConfig: Option.Option<GenerationConfig>,
|
|
404
414
|
tools: ReadonlyArray<ToolDescriptor> = [],
|
|
415
|
+
providerToolEntries: ReadonlyArray<RequestTool> = [],
|
|
405
416
|
toolChoice: CommonRequest["toolChoice"] = undefined,
|
|
406
417
|
): RequestBody => {
|
|
407
418
|
const systemTexts = pipe(allMessages(history), Arr.filterMap(systemMessageText))
|
|
408
419
|
const contents = pipe(history, Arr.filterMap(itemToContent(history)))
|
|
409
|
-
const
|
|
420
|
+
const functionTools = toolDescriptorsToTools(tools)
|
|
421
|
+
const requestTools = [...functionTools, ...providerToolEntries]
|
|
410
422
|
return {
|
|
411
423
|
contents,
|
|
412
424
|
...(systemTexts.length > 0 && {
|
|
@@ -416,8 +428,10 @@ export const buildRequestBody = (
|
|
|
416
428
|
onNone: () => ({}),
|
|
417
429
|
onSome: (cfg) => ({ generationConfig: cfg }),
|
|
418
430
|
}),
|
|
419
|
-
...(requestTools.length > 0 && {
|
|
420
|
-
|
|
431
|
+
...(requestTools.length > 0 && { tools: requestTools }),
|
|
432
|
+
// Only force a function-calling mode when there are function declarations;
|
|
433
|
+
// a grounding-only request must keep the API default.
|
|
434
|
+
...(functionTools.length > 0 && {
|
|
421
435
|
toolConfig: { functionCallingConfig: toolChoiceToFunctionCallingConfig(toolChoice) },
|
|
422
436
|
}),
|
|
423
437
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Effect, Option, Result, Schema } from "effect"
|
|
2
|
+
import * as Tool from "@effect-uai/core/Tool"
|
|
3
|
+
import { descriptorsOf, providerToolsOf } from "@effect-uai/core/Tool"
|
|
4
|
+
import { describe, expect, it } from "vitest"
|
|
5
|
+
import { WireChunk, buildRequestBody } from "./codec.js"
|
|
6
|
+
import { codeExecutionTool, googleSearchTool, renderProviderTools } from "./GeminiTools.js"
|
|
7
|
+
|
|
8
|
+
const localTool = Tool.make({
|
|
9
|
+
name: "get_weather",
|
|
10
|
+
description: "Look up the weather.",
|
|
11
|
+
inputSchema: Tool.fromEffectSchema(Schema.Struct({ city: Schema.String })),
|
|
12
|
+
run: ({ city }) => Effect.succeed(`sunny in ${city}`),
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const build = (toolkit: Record<string, Tool.AnyTool>) => {
|
|
16
|
+
const entries = Result.getOrThrow(renderProviderTools(providerToolsOf(toolkit)))
|
|
17
|
+
return buildRequestBody([], Option.none(), descriptorsOf(toolkit), entries)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("Gemini provider tools", () => {
|
|
21
|
+
it("renders google_search to the native googleSearch entry", () => {
|
|
22
|
+
const body = build({ google_search: googleSearchTool })
|
|
23
|
+
expect(body.tools).toEqual([{ googleSearch: {} }])
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("renders a mixed toolkit to both a functionDeclarations entry and the native entry", () => {
|
|
27
|
+
const body = build({ get_weather: localTool, code_execution: codeExecutionTool })
|
|
28
|
+
expect(body.tools).toEqual([
|
|
29
|
+
{
|
|
30
|
+
functionDeclarations: [
|
|
31
|
+
expect.objectContaining({ name: "get_weather", description: "Look up the weather." }),
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
{ codeExecution: {} },
|
|
35
|
+
])
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("omits toolConfig for a grounding-only request (no forced function-calling mode)", () => {
|
|
39
|
+
const body = build({ google_search: googleSearchTool })
|
|
40
|
+
expect(body.toolConfig).toBeUndefined()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("sets toolConfig when a function declaration is present", () => {
|
|
44
|
+
const body = build({ get_weather: localTool, google_search: googleSearchTool })
|
|
45
|
+
expect(body.toolConfig).toEqual({ functionCallingConfig: { mode: "AUTO" } })
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("fails on a foreign provider tool", () => {
|
|
49
|
+
const foreign = Tool.provider({
|
|
50
|
+
name: "web_search",
|
|
51
|
+
description: "Anthropic web search.",
|
|
52
|
+
inputSchema: Tool.noInput,
|
|
53
|
+
provider: "anthropic",
|
|
54
|
+
config: { type: "web_search_20250305" },
|
|
55
|
+
})
|
|
56
|
+
const result = renderProviderTools(providerToolsOf({ web_search: foreign }))
|
|
57
|
+
expect(Result.isFailure(result)).toBe(true)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it("fails on an unrecognized config kind", () => {
|
|
61
|
+
const bogus = Tool.provider({
|
|
62
|
+
name: "mystery",
|
|
63
|
+
description: "Unknown Gemini tool.",
|
|
64
|
+
inputSchema: Tool.noInput,
|
|
65
|
+
provider: "gemini",
|
|
66
|
+
config: { kind: "telepathy" },
|
|
67
|
+
})
|
|
68
|
+
const result = renderProviderTools(providerToolsOf({ mystery: bogus }))
|
|
69
|
+
expect(Result.isFailure(result)).toBe(true)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it("decodes a grounded chunk without throwing (groundingMetadata is dropped)", () => {
|
|
73
|
+
const grounded = {
|
|
74
|
+
candidates: [
|
|
75
|
+
{
|
|
76
|
+
content: { role: "model", parts: [{ text: "Grounded answer." }] },
|
|
77
|
+
groundingMetadata: {
|
|
78
|
+
webSearchQueries: ["effect ts"],
|
|
79
|
+
groundingChunks: [{ web: { uri: "https://example.com", title: "Example" } }],
|
|
80
|
+
},
|
|
81
|
+
finishReason: "STOP",
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
}
|
|
85
|
+
const chunk = Schema.decodeUnknownSync(WireChunk)(grounded)
|
|
86
|
+
expect(chunk.candidates?.[0]?.content?.parts?.[0]).toEqual({ text: "Grounded answer." })
|
|
87
|
+
})
|
|
88
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * as Gemini from "./Gemini.js"
|
|
2
|
+
export * as GeminiTools from "./GeminiTools.js"
|
|
2
3
|
export * as GeminiEmbedding from "./GeminiEmbedding.js"
|
|
3
4
|
export * as GeminiSynthesizer from "./GeminiSynthesizer.js"
|
|
5
|
+
export * as GoogleDeepResearch from "./GoogleDeepResearch.js"
|
|
4
6
|
export * as LyriaGenerator from "./LyriaGenerator.js"
|
|
5
7
|
export * as codec from "./codec.js"
|
|
6
8
|
export * from "./models.js"
|
package/src/models.ts
CHANGED
|
@@ -17,6 +17,21 @@ export type GoogleModel =
|
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
18
18
|
| (string & {})
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Deep-research agent identifiers for the Interactions API (`POST
|
|
22
|
+
* /v1beta/interactions`). `-preview` is the fast tier, `-max-` the
|
|
23
|
+
* comprehensive one. Preview surface: verify against live docs before relying on
|
|
24
|
+
* exact ids. The `(string & {})` tail accepts any string so a renamed agent
|
|
25
|
+
* works without an SDK update.
|
|
26
|
+
*
|
|
27
|
+
* Reference: https://ai.google.dev/gemini-api/docs/interactions/deep-research
|
|
28
|
+
*/
|
|
29
|
+
export type GoogleDeepResearchModel =
|
|
30
|
+
| "deep-research-preview-04-2026"
|
|
31
|
+
| "deep-research-max-preview-04-2026"
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
33
|
+
| (string & {})
|
|
34
|
+
|
|
20
35
|
/**
|
|
21
36
|
* Known Gemini embedding model identifiers.
|
|
22
37
|
*
|