Skip to content

Commit bcd46cd

Browse files
committed
refactor(js): update type refs to use the @genkit-ai/shared package
1 parent 19bc11b commit bcd46cd

34 files changed

+225
-875
lines changed

js/ai/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"license": "Apache-2.0",
2929
"dependencies": {
3030
"@genkit-ai/core": "workspace:*",
31+
"@genkit-ai/shared": "workspace:*",
3132
"@opentelemetry/api": "^1.9.0",
3233
"@types/node": "^20.11.19",
3334
"colorette": "^2.0.20",

js/ai/src/check-operation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { GenkitError, Operation } from '@genkit-ai/core';
17+
import { GenkitError } from '@genkit-ai/core';
1818
import { Registry } from '@genkit-ai/core/registry';
19+
import { type Operation } from '@genkit-ai/shared';
1920

2021
export async function checkOperation<T = unknown>(
2122
registry: Registry,

js/ai/src/document.ts

Lines changed: 2 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -14,164 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { z } from '@genkit-ai/core';
18-
import type { Embedding } from './embedder';
19-
20-
const EmptyPartSchema = z.object({
21-
text: z.never().optional(),
22-
media: z.never().optional(),
23-
toolRequest: z.never().optional(),
24-
toolResponse: z.never().optional(),
25-
data: z.unknown().optional(),
26-
metadata: z.record(z.unknown()).optional(),
27-
custom: z.record(z.unknown()).optional(),
28-
reasoning: z.never().optional(),
29-
resource: z.never().optional(),
30-
});
31-
32-
/**
33-
* Zod schema for a text part.
34-
*/
35-
export const TextPartSchema = EmptyPartSchema.extend({
36-
/** The text of the message. */
37-
text: z.string(),
38-
});
39-
40-
/**
41-
* Zod schema for a reasoning part.
42-
*/
43-
export const ReasoningPartSchema = EmptyPartSchema.extend({
44-
/** The reasoning text of the message. */
45-
reasoning: z.string(),
46-
});
47-
48-
/**
49-
* Text part.
50-
*/
51-
export type TextPart = z.infer<typeof TextPartSchema>;
52-
53-
/**
54-
* Zod schema of media.
55-
*/
56-
export const MediaSchema = z.object({
57-
/** The media content type. Inferred from data uri if not provided. */
58-
contentType: z.string().optional(),
59-
/** A `data:` or `https:` uri containing the media content. */
60-
url: z.string(),
61-
});
62-
63-
/**
64-
* Zod schema of a media part.
65-
*/
66-
export const MediaPartSchema = EmptyPartSchema.extend({
67-
media: MediaSchema,
68-
});
69-
70-
/**
71-
* Media part.
72-
*/
73-
export type MediaPart = z.infer<typeof MediaPartSchema>;
74-
75-
/**
76-
* Zod schema of a tool request.
77-
*/
78-
export const ToolRequestSchema = z.object({
79-
/** The call id or reference for a specific request. */
80-
ref: z.string().optional(),
81-
/** The name of the tool to call. */
82-
name: z.string(),
83-
/** The input parameters for the tool, usually a JSON object. */
84-
input: z.unknown().optional(),
85-
});
86-
export type ToolRequest = z.infer<typeof ToolRequestSchema>;
87-
88-
/**
89-
* Zod schema of a tool request part.
90-
*/
91-
export const ToolRequestPartSchema = EmptyPartSchema.extend({
92-
/** A request for a tool to be executed, usually provided by a model. */
93-
toolRequest: ToolRequestSchema,
94-
});
95-
96-
/**
97-
* Tool part.
98-
*/
99-
export type ToolRequestPart = z.infer<typeof ToolRequestPartSchema>;
100-
101-
/**
102-
* Zod schema of a tool response.
103-
*/
104-
export const ToolResponseSchema = z.object({
105-
/** The call id or reference for a specific request. */
106-
ref: z.string().optional(),
107-
/** The name of the tool. */
108-
name: z.string(),
109-
/** The output data returned from the tool, usually a JSON object. */
110-
output: z.unknown().optional(),
111-
});
112-
export type ToolResponse = z.infer<typeof ToolResponseSchema>;
113-
114-
/**
115-
* Zod schema of a tool response part.
116-
*/
117-
export const ToolResponsePartSchema = EmptyPartSchema.extend({
118-
/** A provided response to a tool call. */
119-
toolResponse: ToolResponseSchema,
120-
});
121-
122-
/**
123-
* Tool response part.
124-
*/
125-
export type ToolResponsePart = z.infer<typeof ToolResponsePartSchema>;
126-
127-
/**
128-
* Zod schema of a data part.
129-
*/
130-
export const DataPartSchema = EmptyPartSchema.extend({
131-
data: z.unknown(),
132-
});
133-
134-
/**
135-
* Data part.
136-
*/
137-
export type DataPart = z.infer<typeof DataPartSchema>;
138-
139-
/**
140-
* Zod schema of a custom part.
141-
*/
142-
export const CustomPartSchema = EmptyPartSchema.extend({
143-
custom: z.record(z.any()),
144-
});
145-
146-
/**
147-
* Custom part.
148-
*/
149-
export type CustomPart = z.infer<typeof CustomPartSchema>;
150-
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-
165-
export const PartSchema = z.union([TextPartSchema, MediaPartSchema]);
166-
export type Part = z.infer<typeof PartSchema>;
167-
168-
// We need both metadata and embedMetadata because they can
169-
// contain the same fields (e.g. video start/stop) with different values.
170-
export const DocumentDataSchema = z.object({
171-
content: z.array(PartSchema),
172-
metadata: z.record(z.string(), z.any()).optional(),
173-
});
174-
export type DocumentData = z.infer<typeof DocumentDataSchema>;
17+
import type { DocumentData, DocumentPart, Embedding } from '@genkit-ai/shared';
17518

17619
function deepCopy<T>(value: T): T {
17720
if (value === undefined) {
@@ -185,7 +28,7 @@ function deepCopy<T>(value: T): T {
18528
* retrieved. Each document can contain multiple parts (for example text and an image)
18629
*/
18730
export class Document implements DocumentData {
188-
content: Part[];
31+
content: DocumentPart[];
18932
metadata?: Record<string, any>;
19033

19134
constructor(data: DocumentData) {

js/ai/src/embedder.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ import {
2222
} from '@genkit-ai/core';
2323
import type { Registry } from '@genkit-ai/core/registry';
2424
import { toJsonSchema } from '@genkit-ai/core/schema';
25-
import { Document, DocumentDataSchema, type DocumentData } from './document.js';
26-
27-
/**
28-
* A batch (array) of embeddings.
29-
*/
30-
export type EmbeddingBatch = { embedding: number[] }[];
31-
32-
/**
33-
* EmbeddingSchema includes the embedding and also metadata so you know
34-
* which of multiple embeddings corresponds to which part of a document.
35-
*/
36-
export const EmbeddingSchema = z.object({
37-
embedding: z.array(z.number()),
38-
metadata: z.record(z.string(), z.unknown()).optional(),
39-
});
40-
export type Embedding = z.infer<typeof EmbeddingSchema>;
25+
import {
26+
EmbedRequestSchema,
27+
EmbedResponse,
28+
EmbedResponseSchema,
29+
EmbeddingSchema,
30+
type DocumentData,
31+
type Embedding,
32+
type EmbeddingBatch,
33+
} from '@genkit-ai/shared';
34+
import { Document } from './document.js';
35+
export {
36+
EmbedRequestSchema,
37+
EmbeddingSchema,
38+
type Embedding,
39+
type EmbeddingBatch,
40+
};
4141

4242
/**
4343
* A function used for embedder definition, encapsulates embedder implementation.
@@ -47,23 +47,6 @@ export type EmbedderFn<EmbedderOptions extends z.ZodTypeAny> = (
4747
embedderOpts?: z.infer<EmbedderOptions>
4848
) => Promise<EmbedResponse>;
4949

50-
/**
51-
* Zod schema of an embed request.
52-
*/
53-
const EmbedRequestSchema = z.object({
54-
input: z.array(DocumentDataSchema),
55-
options: z.any().optional(),
56-
});
57-
58-
/**
59-
* Zod schema of an embed response.
60-
*/
61-
const EmbedResponseSchema = z.object({
62-
embeddings: z.array(EmbeddingSchema),
63-
// TODO: stats, etc.
64-
});
65-
type EmbedResponse = z.infer<typeof EmbedResponseSchema>;
66-
6750
/**
6851
* Embedder action -- a subtype of {@link Action} with input/output types for embedders.
6952
*/

js/ai/src/evaluator.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,33 @@ import { logger } from '@genkit-ai/core/logging';
1919
import type { Registry } from '@genkit-ai/core/registry';
2020
import { toJsonSchema } from '@genkit-ai/core/schema';
2121
import { SPAN_TYPE_ATTR, runInNewSpan } from '@genkit-ai/core/tracing';
22+
import {
23+
BaseDataPointSchema,
24+
BaseEvalDataPoint,
25+
BaseEvalDataPointSchema,
26+
EvalStatusEnumSchema,
27+
ScoreSchema,
28+
} from '@genkit-ai/shared';
2229
import { randomUUID } from 'crypto';
2330

31+
export {
32+
BaseDataPointSchema,
33+
BaseEvalDataPointSchema,
34+
EvalStatusEnumSchema,
35+
ScoreSchema,
36+
type BaseEvalDataPoint,
37+
};
38+
2439
export const ATTR_PREFIX = 'genkit';
2540
export const SPAN_STATE_ATTR = ATTR_PREFIX + ':state';
2641

27-
export const BaseDataPointSchema = z.object({
28-
input: z.unknown(),
29-
output: z.unknown().optional(),
30-
context: z.array(z.unknown()).optional(),
31-
reference: z.unknown().optional(),
32-
testCaseId: z.string().optional(),
33-
traceIds: z.array(z.string()).optional(),
34-
});
35-
36-
// DataPoint that is to be used for actions. This needs testCaseId to be present.
37-
export const BaseEvalDataPointSchema = BaseDataPointSchema.extend({
38-
testCaseId: z.string(),
39-
});
40-
export type BaseEvalDataPoint = z.infer<typeof BaseEvalDataPointSchema>;
41-
42-
const EvalStatusEnumSchema = z.enum(['UNKNOWN', 'PASS', 'FAIL']);
43-
4442
/** Enum that indicates if an evaluation has passed or failed */
4543
export enum EvalStatusEnum {
4644
UNKNOWN = 'UNKNOWN',
4745
PASS = 'PASS',
4846
FAIL = 'FAIL',
4947
}
5048

51-
export const ScoreSchema = z.object({
52-
id: z
53-
.string()
54-
.describe(
55-
'Optional ID to differentiate different scores if applying in a single evaluation'
56-
)
57-
.optional(),
58-
score: z.union([z.number(), z.string(), z.boolean()]).optional(),
59-
status: EvalStatusEnumSchema.optional(),
60-
error: z.string().optional(),
61-
details: z
62-
.object({
63-
reasoning: z.string().optional(),
64-
})
65-
.passthrough()
66-
.optional(),
67-
});
68-
6949
// Update genkit-tools/src/utils/evals.ts if you change this value
7050
export const EVALUATOR_METADATA_KEY_DISPLAY_NAME = 'evaluatorDisplayName';
7151
export const EVALUATOR_METADATA_KEY_DEFINITION = 'evaluatorDefinition';

js/ai/src/formats/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
import type { JSONSchema } from '@genkit-ai/core';
1818
import type { Registry } from '@genkit-ai/core/registry';
19+
import type { MessageData } from '@genkit-ai/shared';
1920
import type { OutputOptions } from '../generate.js';
20-
import type { MessageData, TextPart } from '../model.js';
21+
import type { TextPart } from '../model.js';
2122
import { arrayFormatter } from './array.js';
2223
import { enumFormatter } from './enum.js';
2324
import { jsonFormatter } from './json.js';

js/ai/src/formats/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
import type { JSONSchema } from '@genkit-ai/core';
18+
import type { ModelRequest } from '@genkit-ai/shared';
1819
import type { GenerateResponseChunk } from '../generate.js';
1920
import type { Message } from '../message.js';
20-
import type { ModelRequest } from '../model.js';
2121

2222
export type OutputContentTypes = 'application/json' | 'text/plain';
2323

js/ai/src/generate.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
GenkitError,
2020
isAction,
2121
isDetachedAction,
22-
Operation,
2322
runWithContext,
2423
sentinelNoopStreamingCallback,
2524
type Action,
@@ -30,7 +29,18 @@ import {
3029
import { Channel } from '@genkit-ai/core/async';
3130
import { Registry } from '@genkit-ai/core/registry';
3231
import { toJsonSchema } from '@genkit-ai/core/schema';
33-
import type { DocumentData } from './document.js';
32+
import type {
33+
DocumentData,
34+
GenerateActionOptions,
35+
GenerateRequest,
36+
GenerateResponseChunkData,
37+
GenerateResponseData,
38+
GenerationCommonConfigSchema,
39+
MessageData,
40+
Operation,
41+
Part,
42+
} from '@genkit-ai/shared';
43+
import type {} from './document.js';
3444
import {
3545
injectInstructions,
3646
resolveFormat,
@@ -44,16 +54,9 @@ import { GenerateResponseChunk } from './generate/chunk.js';
4454
import { GenerateResponse } from './generate/response.js';
4555
import { Message } from './message.js';
4656
import {
47-
GenerateResponseChunkData,
48-
GenerateResponseData,
4957
resolveModel,
50-
type GenerateActionOptions,
51-
type GenerateRequest,
52-
type GenerationCommonConfigSchema,
53-
type MessageData,
5458
type ModelArgument,
5559
type ModelMiddleware,
56-
type Part,
5760
type ToolRequestPart,
5861
type ToolResponsePart,
5962
} from './model.js';

0 commit comments

Comments
 (0)