Skip to content

Commit aa7e37a

Browse files
committed
fix(js): Export RessourcePartSchema through genkit-tools
1 parent fd5c1fd commit aa7e37a

File tree

8 files changed

+140
-22
lines changed

8 files changed

+140
-22
lines changed

genkit-tools/common/src/types/document.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const EmptyPartSchema = z.object({
2828
metadata: z.record(z.unknown()).optional(),
2929
custom: z.record(z.unknown()).optional(),
3030
reasoning: z.never().optional(),
31+
resource: z.never().optional(),
3132
});
3233

3334
/**
@@ -147,6 +148,20 @@ export const CustomPartSchema = EmptyPartSchema.extend({
147148
*/
148149
export type CustomPart = z.infer<typeof CustomPartSchema>;
149150

151+
/**
152+
* Zod schema of a resource part.
153+
*/
154+
export const ResourcePartSchema = EmptyPartSchema.extend({
155+
resource: z.object({
156+
uri: z.string(),
157+
}),
158+
});
159+
160+
/**
161+
* Resource part.
162+
*/
163+
export type ResourcePart = z.infer<typeof ResourcePartSchema>;
164+
150165
// Disclaimer: genkit/js/ai/document.ts defines the following schema, type pair
151166
// as PartSchema and Part, respectively. genkit-tools cannot retain those names
152167
// due to it clashing with similar schema in model.ts, and genkit-tools

genkit-tools/common/src/types/model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import {
2020
DocumentDataSchema,
2121
MediaPartSchema,
2222
ReasoningPartSchema,
23+
ResourcePartSchema,
2324
TextPartSchema,
2425
ToolRequestPartSchema,
2526
ToolResponsePartSchema,
2627
type CustomPart,
2728
type DataPart,
2829
type MediaPart,
30+
type ResourcePart,
2931
type TextPart,
3032
type ToolRequestPart,
3133
type ToolResponsePart,
@@ -34,12 +36,14 @@ export {
3436
CustomPartSchema,
3537
DataPartSchema,
3638
MediaPartSchema,
39+
ResourcePartSchema,
3740
TextPartSchema,
3841
ToolRequestPartSchema,
3942
ToolResponsePartSchema,
4043
type CustomPart,
4144
type DataPart,
4245
type MediaPart,
46+
type ResourcePart,
4347
type TextPart,
4448
type ToolRequestPart,
4549
type ToolResponsePart,
@@ -77,6 +81,7 @@ export const PartSchema = z.union([
7781
DataPartSchema,
7882
CustomPartSchema,
7983
ReasoningPartSchema,
84+
ResourcePartSchema,
8085
]);
8186

8287
/**

genkit-tools/genkit-schema.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
},
2828
"reasoning": {
2929
"not": {}
30+
},
31+
"resource": {
32+
"not": {}
3033
}
3134
},
3235
"required": [
@@ -59,6 +62,9 @@
5962
},
6063
"reasoning": {
6164
"$ref": "#/$defs/CustomPart/properties/reasoning"
65+
},
66+
"resource": {
67+
"$ref": "#/$defs/CustomPart/properties/resource"
6268
}
6369
},
6470
"additionalProperties": false
@@ -118,6 +124,9 @@
118124
},
119125
"reasoning": {
120126
"$ref": "#/$defs/CustomPart/properties/reasoning"
127+
},
128+
"resource": {
129+
"$ref": "#/$defs/CustomPart/properties/resource"
121130
}
122131
},
123132
"required": [
@@ -166,13 +175,61 @@
166175
},
167176
"reasoning": {
168177
"type": "string"
178+
},
179+
"resource": {
180+
"$ref": "#/$defs/CustomPart/properties/resource"
169181
}
170182
},
171183
"required": [
172184
"reasoning"
173185
],
174186
"additionalProperties": false
175187
},
188+
"ResourcePart": {
189+
"type": "object",
190+
"properties": {
191+
"text": {
192+
"$ref": "#/$defs/CustomPart/properties/text"
193+
},
194+
"media": {
195+
"$ref": "#/$defs/CustomPart/properties/media"
196+
},
197+
"toolRequest": {
198+
"$ref": "#/$defs/CustomPart/properties/toolRequest"
199+
},
200+
"toolResponse": {
201+
"$ref": "#/$defs/CustomPart/properties/toolResponse"
202+
},
203+
"data": {
204+
"$ref": "#/$defs/CustomPart/properties/data"
205+
},
206+
"metadata": {
207+
"$ref": "#/$defs/CustomPart/properties/metadata"
208+
},
209+
"custom": {
210+
"$ref": "#/$defs/DataPart/properties/custom"
211+
},
212+
"reasoning": {
213+
"$ref": "#/$defs/CustomPart/properties/reasoning"
214+
},
215+
"resource": {
216+
"type": "object",
217+
"properties": {
218+
"uri": {
219+
"type": "string"
220+
}
221+
},
222+
"required": [
223+
"uri"
224+
],
225+
"additionalProperties": false
226+
}
227+
},
228+
"required": [
229+
"resource"
230+
],
231+
"additionalProperties": false
232+
},
176233
"TextPart": {
177234
"type": "object",
178235
"properties": {
@@ -199,6 +256,9 @@
199256
},
200257
"reasoning": {
201258
"$ref": "#/$defs/CustomPart/properties/reasoning"
259+
},
260+
"resource": {
261+
"$ref": "#/$defs/CustomPart/properties/resource"
202262
}
203263
},
204264
"required": [
@@ -232,6 +292,9 @@
232292
},
233293
"reasoning": {
234294
"$ref": "#/$defs/CustomPart/properties/reasoning"
295+
},
296+
"resource": {
297+
"$ref": "#/$defs/CustomPart/properties/resource"
235298
}
236299
},
237300
"required": [
@@ -281,6 +344,9 @@
281344
},
282345
"reasoning": {
283346
"$ref": "#/$defs/CustomPart/properties/reasoning"
347+
},
348+
"resource": {
349+
"$ref": "#/$defs/CustomPart/properties/resource"
284350
}
285351
},
286352
"required": [
@@ -1138,6 +1204,9 @@
11381204
},
11391205
{
11401206
"$ref": "#/$defs/ReasoningPart"
1207+
},
1208+
{
1209+
"$ref": "#/$defs/ResourcePart"
11411210
}
11421211
]
11431212
},

js/testapps/flow-simple-ai/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ ai.defineResource(
12731273
description: 'provides my resource',
12741274
},
12751275
async (input) => {
1276-
return { content: [{ text: `resource ${input}` }] };
1276+
return { content: [{ text: `resource ${input.uri}` }] };
12771277
}
12781278
);
12791279

py/packages/genkit/src/genkit/core/typing.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CustomPart(BaseModel):
5656
metadata: dict[str, Any] | None = None
5757
custom: dict[str, Any]
5858
reasoning: Any | None = None
59+
resource: Any | None = None
5960

6061

6162
class Media(BaseModel):
@@ -66,6 +67,13 @@ class Media(BaseModel):
6667
url: str
6768

6869

70+
class Resource1(BaseModel):
71+
"""Model for resource1 data."""
72+
73+
model_config = ConfigDict(extra='forbid', populate_by_name=True)
74+
uri: str
75+
76+
6977
class ToolRequest(BaseModel):
7078
"""Model for toolrequest data."""
7179

@@ -505,6 +513,12 @@ class Reasoning(RootModel[Any]):
505513
root: Any
506514

507515

516+
class Resource(RootModel[Any]):
517+
"""Root model for resource."""
518+
519+
root: Any
520+
521+
508522
class Text(RootModel[Any]):
509523
"""Root model for text."""
510524

@@ -601,6 +615,7 @@ class DataPart(BaseModel):
601615
metadata: Metadata | None = None
602616
custom: dict[str, Any] | None = None
603617
reasoning: Reasoning | None = None
618+
resource: Resource | None = None
604619

605620

606621
class MediaPart(BaseModel):
@@ -615,6 +630,7 @@ class MediaPart(BaseModel):
615630
metadata: Metadata | None = None
616631
custom: Custom | None = None
617632
reasoning: Reasoning | None = None
633+
resource: Resource | None = None
618634

619635

620636
class ReasoningPart(BaseModel):
@@ -629,6 +645,22 @@ class ReasoningPart(BaseModel):
629645
metadata: Metadata | None = None
630646
custom: Custom | None = None
631647
reasoning: str
648+
resource: Resource | None = None
649+
650+
651+
class ResourcePart(BaseModel):
652+
"""Model for resourcepart data."""
653+
654+
model_config = ConfigDict(extra='forbid', populate_by_name=True)
655+
text: Text | None = None
656+
media: MediaModel | None = None
657+
tool_request: ToolRequestModel | None = Field(None, alias='toolRequest')
658+
tool_response: ToolResponseModel | None = Field(None, alias='toolResponse')
659+
data: Data | None = None
660+
metadata: Metadata | None = None
661+
custom: Custom | None = None
662+
reasoning: Reasoning | None = None
663+
resource: Resource1
632664

633665

634666
class TextPart(BaseModel):
@@ -643,6 +675,7 @@ class TextPart(BaseModel):
643675
metadata: Metadata | None = None
644676
custom: Custom | None = None
645677
reasoning: Reasoning | None = None
678+
resource: Resource | None = None
646679

647680

648681
class ToolRequestPart(BaseModel):
@@ -657,6 +690,7 @@ class ToolRequestPart(BaseModel):
657690
metadata: Metadata | None = None
658691
custom: Custom | None = None
659692
reasoning: Reasoning | None = None
693+
resource: Resource | None = None
660694

661695

662696
class ToolResponsePart(BaseModel):
@@ -671,6 +705,7 @@ class ToolResponsePart(BaseModel):
671705
metadata: Metadata | None = None
672706
custom: Custom | None = None
673707
reasoning: Reasoning | None = None
708+
resource: Resource | None = None
674709

675710

676711
class EmbedResponse(BaseModel):
@@ -719,11 +754,15 @@ class Resume(BaseModel):
719754

720755

721756
class Part(
722-
RootModel[TextPart | MediaPart | ToolRequestPart | ToolResponsePart | DataPart | CustomPart | ReasoningPart]
757+
RootModel[
758+
TextPart | MediaPart | ToolRequestPart | ToolResponsePart | DataPart | CustomPart | ReasoningPart | ResourcePart
759+
]
723760
):
724761
"""Root model for part."""
725762

726-
root: TextPart | MediaPart | ToolRequestPart | ToolResponsePart | DataPart | CustomPart | ReasoningPart
763+
root: (
764+
TextPart | MediaPart | ToolRequestPart | ToolResponsePart | DataPart | CustomPart | ReasoningPart | ResourcePart
765+
)
727766

728767

729768
class Link(BaseModel):

py/plugins/compat-oai/src/genkit/plugins/compat_oai/models/model_info.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,7 @@ def get_default_model_info(name: str) -> ModelInfo:
183183
supports=DEFAULT_SUPPORTS,
184184
)
185185

186+
186187
def get_default_openai_model_info(name: str) -> ModelInfo:
187188
"""Gets the default model info given a name."""
188-
return ModelInfo(
189-
label=f"OpenAI - {name}",
190-
supports={'multiturn': True}
191-
)
189+
return ModelInfo(label=f'OpenAI - {name}', supports={'multiturn': True})

py/plugins/compat-oai/src/genkit/plugins/compat_oai/openai_plugin.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
"""OpenAI OpenAI API Compatible Plugin for Genkit."""
19+
1920
from typing import Any, Callable
2021

2122
from openai import Client, OpenAI as OpenAIClient
@@ -44,13 +45,11 @@ def open_ai_name(name: str) -> str:
4445
"""
4546
return f'openai/{name}'
4647

48+
4749
def default_openai_metadata(name: str) -> dict[str, Any]:
4850
return {
49-
'model': {
50-
'label': f"OpenAI - {name}",
51-
'supports': {'multiturn': True}
52-
},
53-
}
51+
'model': {'label': f'OpenAI - {name}', 'supports': {'multiturn': True}},
52+
}
5453

5554

5655
class OpenAI(Plugin):
@@ -125,7 +124,6 @@ def resolve_action( # noqa: B027
125124
kind: ActionKind,
126125
name: str,
127126
) -> None:
128-
129127
if kind is not ActionKind.MODEL:
130128
return None
131129

@@ -166,8 +164,6 @@ def _define_openai_model(self, ai: GenkitRegistry, name: str) -> None:
166164
)
167165

168166

169-
170-
171167
def openai_model(name: str) -> str:
172168
"""Returns a string representing the OpenAI model name to use with Genkit.
173169

0 commit comments

Comments
 (0)