@ohos-ports/genkit 1.42.0-beta.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/LICENSE +203 -0
- package/README.md +434 -0
- package/lib/beta.d.mts +18 -0
- package/lib/beta.d.ts +18 -0
- package/lib/beta.js +88 -0
- package/lib/beta.js.map +1 -0
- package/lib/beta.mjs +64 -0
- package/lib/beta.mjs.map +1 -0
- package/lib/client/agent.d.mts +52 -0
- package/lib/client/agent.d.ts +52 -0
- package/lib/client/agent.js +90 -0
- package/lib/client/agent.js.map +1 -0
- package/lib/client/agent.mjs +68 -0
- package/lib/client/agent.mjs.map +1 -0
- package/lib/client/client.d.mts +80 -0
- package/lib/client/client.d.ts +80 -0
- package/lib/client/client.js +164 -0
- package/lib/client/client.js.map +1 -0
- package/lib/client/client.mjs +139 -0
- package/lib/client/client.mjs.map +1 -0
- package/lib/client/index.d.mts +6 -0
- package/lib/client/index.d.ts +6 -0
- package/lib/client/index.js +55 -0
- package/lib/client/index.js.map +1 -0
- package/lib/client/index.mjs +31 -0
- package/lib/client/index.mjs.map +1 -0
- package/lib/client/types.d.mts +17 -0
- package/lib/client/types.d.ts +17 -0
- package/lib/client/types.js +17 -0
- package/lib/client/types.js.map +1 -0
- package/lib/client/types.mjs +1 -0
- package/lib/client/types.mjs.map +1 -0
- package/lib/common.d.mts +3 -0
- package/lib/common.d.ts +3 -0
- package/lib/common.js +151 -0
- package/lib/common.js.map +1 -0
- package/lib/common.mjs +128 -0
- package/lib/common.mjs.map +1 -0
- package/lib/context.d.mts +1 -0
- package/lib/context.d.ts +1 -0
- package/lib/context.js +50 -0
- package/lib/context.js.map +1 -0
- package/lib/context.mjs +28 -0
- package/lib/context.mjs.map +1 -0
- package/lib/embedder.d.mts +1 -0
- package/lib/embedder.d.ts +1 -0
- package/lib/embedder.js +48 -0
- package/lib/embedder.js.map +1 -0
- package/lib/embedder.mjs +26 -0
- package/lib/embedder.mjs.map +1 -0
- package/lib/evaluator.d.mts +1 -0
- package/lib/evaluator.d.ts +1 -0
- package/lib/evaluator.js +60 -0
- package/lib/evaluator.js.map +1 -0
- package/lib/evaluator.mjs +38 -0
- package/lib/evaluator.mjs.map +1 -0
- package/lib/extract.d.mts +1 -0
- package/lib/extract.d.ts +1 -0
- package/lib/extract.js +48 -0
- package/lib/extract.js.map +1 -0
- package/lib/extract.mjs +23 -0
- package/lib/extract.mjs.map +1 -0
- package/lib/formats.d.mts +1 -0
- package/lib/formats.d.ts +1 -0
- package/lib/formats.js +34 -0
- package/lib/formats.js.map +1 -0
- package/lib/formats.mjs +18 -0
- package/lib/formats.mjs.map +1 -0
- package/lib/genkit-beta.d.mts +183 -0
- package/lib/genkit-beta.d.ts +183 -0
- package/lib/genkit-beta.js +178 -0
- package/lib/genkit-beta.js.map +1 -0
- package/lib/genkit-beta.mjs +159 -0
- package/lib/genkit-beta.mjs.map +1 -0
- package/lib/genkit.d.mts +10 -0
- package/lib/genkit.d.ts +10 -0
- package/lib/genkit.js +471 -0
- package/lib/genkit.js.map +1 -0
- package/lib/genkit.mjs +489 -0
- package/lib/genkit.mjs.map +1 -0
- package/lib/index-BSkuxdwl.d.mts +311 -0
- package/lib/index-BSkuxdwl.d.ts +311 -0
- package/lib/index.d.mts +10 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +51 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +24 -0
- package/lib/index.mjs.map +1 -0
- package/lib/logging.d.mts +1 -0
- package/lib/logging.d.ts +1 -0
- package/lib/logging.js +46 -0
- package/lib/logging.js.map +1 -0
- package/lib/logging.mjs +22 -0
- package/lib/logging.mjs.map +1 -0
- package/lib/middleware.d.mts +1 -0
- package/lib/middleware.d.ts +1 -0
- package/lib/middleware.js +56 -0
- package/lib/middleware.js.map +1 -0
- package/lib/middleware.mjs +34 -0
- package/lib/middleware.mjs.map +1 -0
- package/lib/model.d.mts +1 -0
- package/lib/model.d.ts +1 -0
- package/lib/model.js +92 -0
- package/lib/model.js.map +1 -0
- package/lib/model.mjs +70 -0
- package/lib/model.mjs.map +1 -0
- package/lib/plugin.d.mts +10 -0
- package/lib/plugin.d.ts +10 -0
- package/lib/plugin.js +143 -0
- package/lib/plugin.js.map +1 -0
- package/lib/plugin.mjs +113 -0
- package/lib/plugin.mjs.map +1 -0
- package/lib/registry.d.mts +1 -0
- package/lib/registry.d.ts +1 -0
- package/lib/registry.js +46 -0
- package/lib/registry.js.map +1 -0
- package/lib/registry.mjs +24 -0
- package/lib/registry.mjs.map +1 -0
- package/lib/reranker.d.mts +1 -0
- package/lib/reranker.d.ts +1 -0
- package/lib/reranker.js +54 -0
- package/lib/reranker.js.map +1 -0
- package/lib/reranker.mjs +32 -0
- package/lib/reranker.mjs.map +1 -0
- package/lib/retriever.d.mts +1 -0
- package/lib/retriever.d.ts +1 -0
- package/lib/retriever.js +58 -0
- package/lib/retriever.js.map +1 -0
- package/lib/retriever.mjs +36 -0
- package/lib/retriever.mjs.map +1 -0
- package/lib/schema.d.mts +1 -0
- package/lib/schema.d.ts +1 -0
- package/lib/schema.js +54 -0
- package/lib/schema.js.map +1 -0
- package/lib/schema.mjs +32 -0
- package/lib/schema.mjs.map +1 -0
- package/lib/testing.d.mts +1 -0
- package/lib/testing.d.ts +1 -0
- package/lib/testing.js +50 -0
- package/lib/testing.js.map +1 -0
- package/lib/testing.mjs +28 -0
- package/lib/testing.mjs.map +1 -0
- package/lib/tool.d.mts +1 -0
- package/lib/tool.d.ts +1 -0
- package/lib/tool.js +54 -0
- package/lib/tool.js.map +1 -0
- package/lib/tool.mjs +32 -0
- package/lib/tool.mjs.map +1 -0
- package/lib/tracing.d.mts +1 -0
- package/lib/tracing.d.ts +1 -0
- package/lib/tracing.js +82 -0
- package/lib/tracing.js.map +1 -0
- package/lib/tracing.mjs +60 -0
- package/lib/tracing.mjs.map +1 -0
- package/package.json +233 -0
- package/src/beta.ts +85 -0
- package/src/client/agent.ts +160 -0
- package/src/client/client.ts +237 -0
- package/src/client/index.ts +72 -0
- package/src/client/types.ts +41 -0
- package/src/common.ts +175 -0
- package/src/context.ts +37 -0
- package/src/embedder.ts +41 -0
- package/src/evaluator.ts +50 -0
- package/src/extract.ts +30 -0
- package/src/formats.ts +30 -0
- package/src/genkit-beta.ts +275 -0
- package/src/genkit.ts +818 -0
- package/src/index.ts +29 -0
- package/src/logging.ts +30 -0
- package/src/middleware.ts +38 -0
- package/src/model.ts +87 -0
- package/src/plugin.ts +172 -0
- package/src/registry.ts +36 -0
- package/src/reranker.ts +42 -0
- package/src/retriever.ts +55 -0
- package/src/schema.ts +40 -0
- package/src/testing.ts +47 -0
- package/src/tool.ts +40 -0
- package/src/tracing.ts +55 -0
- package/tests/agent_client_test.ts +497 -0
- package/tests/embed_test.ts +142 -0
- package/tests/evaluate_test.ts +52 -0
- package/tests/flow_test.ts +94 -0
- package/tests/formats_test.ts +168 -0
- package/tests/generate_test.ts +1631 -0
- package/tests/genkit_test.ts +33 -0
- package/tests/helpers.ts +176 -0
- package/tests/mock-model_test.ts +490 -0
- package/tests/plugins_test.ts +259 -0
- package/tests/prompts/badSchemaRef.prompt +9 -0
- package/tests/prompts/chat_preamble.prompt +5 -0
- package/tests/prompts/kitchensink.prompt +25 -0
- package/tests/prompts/output.prompt +11 -0
- package/tests/prompts/schemaRef.prompt +9 -0
- package/tests/prompts/sub/test.prompt +5 -0
- package/tests/prompts/test.prompt +5 -0
- package/tests/prompts/test.variant.prompt +8 -0
- package/tests/prompts/toolPrompt.prompt +6 -0
- package/tests/prompts_test.ts +1442 -0
- package/tests/session_test.ts +17 -0
- package/typedoc.json +22 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
createAgentAPI,
|
|
19
|
+
type AgentAPI,
|
|
20
|
+
type AgentTransport,
|
|
21
|
+
type SnapshotLookup,
|
|
22
|
+
} from '@genkit-ai/ai/agent-core';
|
|
23
|
+
|
|
24
|
+
import type {
|
|
25
|
+
AgentInit,
|
|
26
|
+
AgentInput,
|
|
27
|
+
AgentOutput,
|
|
28
|
+
AgentStreamChunk,
|
|
29
|
+
} from '@genkit-ai/ai';
|
|
30
|
+
import type { SessionSnapshot } from '@genkit-ai/ai/session';
|
|
31
|
+
import { runFlow, streamFlow } from './client.js';
|
|
32
|
+
|
|
33
|
+
// Re-export the transport-agnostic agent-client surface so existing imports
|
|
34
|
+
// from `genkit/beta/client` keep working.
|
|
35
|
+
export {
|
|
36
|
+
AgentError,
|
|
37
|
+
type AgentAPI,
|
|
38
|
+
type AgentChat,
|
|
39
|
+
type AgentChunk,
|
|
40
|
+
type AgentInterrupt,
|
|
41
|
+
type AgentResponse,
|
|
42
|
+
type AgentTurn,
|
|
43
|
+
type DetachedTask,
|
|
44
|
+
} from '@genkit-ai/ai/agent-core';
|
|
45
|
+
|
|
46
|
+
// Re-export the JSON Patch helper so apps can apply a chunk's `customPatch` to
|
|
47
|
+
// their own locally tracked copy of the agent's custom state.
|
|
48
|
+
export { applyPatch, type JsonPatch } from '@genkit-ai/ai/json-patch';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Options for {@link remoteAgent}.
|
|
52
|
+
*/
|
|
53
|
+
export interface RemoteAgentOptions {
|
|
54
|
+
/** Required. The agent endpoint. */
|
|
55
|
+
url: string;
|
|
56
|
+
/** Optional. Defaults to `${url}/getSnapshot`. */
|
|
57
|
+
getSnapshotUrl?: string;
|
|
58
|
+
/** Optional. Defaults to `${url}/abort`. */
|
|
59
|
+
abortUrl?: string;
|
|
60
|
+
/** Optional. Static headers, or a function called per request. */
|
|
61
|
+
headers?:
|
|
62
|
+
| Record<string, string>
|
|
63
|
+
| (() => Record<string, string> | Promise<Record<string, string>>);
|
|
64
|
+
/** Optional. Declares server- vs client-managed state; inferred otherwise. */
|
|
65
|
+
stateManagement?: 'server' | 'client';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// remoteAgent factory
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Creates a typed client for talking to a Genkit agent over HTTP.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { remoteAgent } from 'genkit/beta/client';
|
|
77
|
+
*
|
|
78
|
+
* const agent = remoteAgent<WeatherState>({
|
|
79
|
+
* url: '/api/weatherAgent',
|
|
80
|
+
* });
|
|
81
|
+
* const chat = agent.chat();
|
|
82
|
+
* const res = await chat.send('Weather in Tokyo?').response;
|
|
83
|
+
* console.log(res.text);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function remoteAgent<State = unknown>(
|
|
87
|
+
options: RemoteAgentOptions
|
|
88
|
+
): AgentAPI<State> {
|
|
89
|
+
const { url } = options;
|
|
90
|
+
const getSnapshotUrl = options.getSnapshotUrl ?? `${url}/getSnapshot`;
|
|
91
|
+
const abortUrl = options.abortUrl ?? `${url}/abort`;
|
|
92
|
+
|
|
93
|
+
const resolveHeaders = async (): Promise<
|
|
94
|
+
Record<string, string> | undefined
|
|
95
|
+
> => {
|
|
96
|
+
if (!options.headers) return undefined;
|
|
97
|
+
if (typeof options.headers === 'function') {
|
|
98
|
+
return options.headers();
|
|
99
|
+
}
|
|
100
|
+
return options.headers;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const transport: AgentTransport = {
|
|
104
|
+
stateManagement: options.stateManagement,
|
|
105
|
+
|
|
106
|
+
runTurn(
|
|
107
|
+
input: AgentInput,
|
|
108
|
+
init: AgentInit,
|
|
109
|
+
opts: { abortSignal: AbortSignal }
|
|
110
|
+
) {
|
|
111
|
+
// Kick off the request lazily so headers can be resolved asynchronously.
|
|
112
|
+
const started = (async () => {
|
|
113
|
+
const headers = await resolveHeaders();
|
|
114
|
+
return streamFlow<AgentOutput, AgentStreamChunk, AgentInit>({
|
|
115
|
+
url,
|
|
116
|
+
input,
|
|
117
|
+
init,
|
|
118
|
+
headers,
|
|
119
|
+
abortSignal: opts.abortSignal,
|
|
120
|
+
});
|
|
121
|
+
})();
|
|
122
|
+
|
|
123
|
+
const output = (async () => {
|
|
124
|
+
const { output } = await started;
|
|
125
|
+
return output;
|
|
126
|
+
})();
|
|
127
|
+
|
|
128
|
+
const stream = (async function* (): AsyncIterable<AgentStreamChunk> {
|
|
129
|
+
const { stream: rawStream } = await started;
|
|
130
|
+
yield* rawStream;
|
|
131
|
+
})();
|
|
132
|
+
|
|
133
|
+
return { stream, output };
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
async getSnapshot(lookup: SnapshotLookup) {
|
|
137
|
+
const headers = await resolveHeaders();
|
|
138
|
+
return runFlow<SessionSnapshot<State> | undefined>({
|
|
139
|
+
url: getSnapshotUrl,
|
|
140
|
+
input: lookup,
|
|
141
|
+
headers,
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
async abort(snapshotId: string) {
|
|
146
|
+
const headers = await resolveHeaders();
|
|
147
|
+
const result = await runFlow<{
|
|
148
|
+
snapshotId: string;
|
|
149
|
+
status?: SessionSnapshot['status'];
|
|
150
|
+
}>({
|
|
151
|
+
url: abortUrl,
|
|
152
|
+
input: { snapshotId },
|
|
153
|
+
headers,
|
|
154
|
+
});
|
|
155
|
+
return result?.status;
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return createAgentAPI<State>(transport);
|
|
160
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Channel, createTask } from '@genkit-ai/core/async';
|
|
18
|
+
|
|
19
|
+
const __flowStreamDelimiter = '\n\n';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Invoke and stream response from a deployed flow.
|
|
23
|
+
*
|
|
24
|
+
* For example:
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* import { streamFlow } from 'genkit/beta/client';
|
|
28
|
+
*
|
|
29
|
+
* const response = streamFlow({
|
|
30
|
+
* url: 'https://my-flow-deployed-url',
|
|
31
|
+
* input: 'foo',
|
|
32
|
+
* });
|
|
33
|
+
* for await (const chunk of response.stream) {
|
|
34
|
+
* console.log(chunk);
|
|
35
|
+
* }
|
|
36
|
+
* console.log(await response.output);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function streamFlow<O = any, S = any, Init = any>({
|
|
40
|
+
url,
|
|
41
|
+
input,
|
|
42
|
+
init,
|
|
43
|
+
streamId,
|
|
44
|
+
headers,
|
|
45
|
+
abortSignal,
|
|
46
|
+
}: {
|
|
47
|
+
/** URL of the deployed flow. */
|
|
48
|
+
url: string;
|
|
49
|
+
/** Flow input. */
|
|
50
|
+
input?: any;
|
|
51
|
+
/** Initialization data for the action. */
|
|
52
|
+
init?: Init;
|
|
53
|
+
/** Stream ID to connect to. */
|
|
54
|
+
streamId?: string;
|
|
55
|
+
/** A map of HTTP headers to be added to the HTTP call. */
|
|
56
|
+
headers?: Record<string, string>;
|
|
57
|
+
/** Abort signal to abort the request. */
|
|
58
|
+
abortSignal?: AbortSignal;
|
|
59
|
+
}): {
|
|
60
|
+
readonly output: Promise<O>;
|
|
61
|
+
readonly stream: AsyncIterable<S>;
|
|
62
|
+
readonly streamId: Promise<string | null>;
|
|
63
|
+
} {
|
|
64
|
+
const channel = new Channel<S>();
|
|
65
|
+
const streamIdTask = createTask<string | null>();
|
|
66
|
+
|
|
67
|
+
const operationPromise = __flowRunEnvelope({
|
|
68
|
+
url,
|
|
69
|
+
input,
|
|
70
|
+
init,
|
|
71
|
+
sendChunk: (c) => channel.send(c),
|
|
72
|
+
headers: streamId
|
|
73
|
+
? { ...headers, 'x-genkit-stream-id': streamId }
|
|
74
|
+
: headers,
|
|
75
|
+
abortSignal,
|
|
76
|
+
responseCallback: (response) => {
|
|
77
|
+
streamIdTask.resolve(response.headers.get('x-genkit-stream-id'));
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
operationPromise.then(
|
|
81
|
+
() => channel.close(),
|
|
82
|
+
(err) => channel.error(err)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
output: operationPromise,
|
|
87
|
+
stream: channel,
|
|
88
|
+
streamId: streamIdTask.promise,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function __flowRunEnvelope({
|
|
93
|
+
url,
|
|
94
|
+
input,
|
|
95
|
+
init,
|
|
96
|
+
sendChunk,
|
|
97
|
+
headers,
|
|
98
|
+
abortSignal,
|
|
99
|
+
responseCallback,
|
|
100
|
+
}: {
|
|
101
|
+
url: string;
|
|
102
|
+
input: any;
|
|
103
|
+
init?: any;
|
|
104
|
+
sendChunk: (chunk: any) => void;
|
|
105
|
+
headers?: Record<string, string>;
|
|
106
|
+
abortSignal?: AbortSignal;
|
|
107
|
+
responseCallback?: (response: Response) => void;
|
|
108
|
+
}) {
|
|
109
|
+
const response = await fetch(url, {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
data: input,
|
|
113
|
+
...(init !== undefined && { init }),
|
|
114
|
+
}),
|
|
115
|
+
headers: {
|
|
116
|
+
Accept: 'text/event-stream',
|
|
117
|
+
'Content-Type': 'application/json',
|
|
118
|
+
...headers,
|
|
119
|
+
},
|
|
120
|
+
signal: abortSignal,
|
|
121
|
+
});
|
|
122
|
+
if (responseCallback) {
|
|
123
|
+
responseCallback(response);
|
|
124
|
+
}
|
|
125
|
+
if (response.status === 204) {
|
|
126
|
+
throw new Error('NOT_FOUND: Stream not found.');
|
|
127
|
+
}
|
|
128
|
+
if (response.status !== 200) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Server returned: ${response.status}: ${await response.text()}`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (!response.body) {
|
|
134
|
+
throw new Error('Response body is empty');
|
|
135
|
+
}
|
|
136
|
+
var reader = response.body.getReader();
|
|
137
|
+
var decoder = new TextDecoder();
|
|
138
|
+
|
|
139
|
+
let buffer = '';
|
|
140
|
+
while (true) {
|
|
141
|
+
const result = await reader.read();
|
|
142
|
+
const decodedValue = decoder.decode(result.value);
|
|
143
|
+
if (decodedValue) {
|
|
144
|
+
buffer += decodedValue;
|
|
145
|
+
}
|
|
146
|
+
// If buffer includes the delimiter that means we are still receiving chunks.
|
|
147
|
+
while (buffer.includes(__flowStreamDelimiter)) {
|
|
148
|
+
const chunk = JSON.parse(
|
|
149
|
+
buffer
|
|
150
|
+
.substring(0, buffer.indexOf(__flowStreamDelimiter))
|
|
151
|
+
.substring('data: '.length)
|
|
152
|
+
);
|
|
153
|
+
if (chunk.hasOwnProperty('message')) {
|
|
154
|
+
sendChunk(chunk.message);
|
|
155
|
+
} else if (chunk.hasOwnProperty('result')) {
|
|
156
|
+
return chunk.result;
|
|
157
|
+
} else if (chunk.hasOwnProperty('error')) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`${chunk.error.status}: ${chunk.error.message}\n${chunk.error.details}`
|
|
160
|
+
);
|
|
161
|
+
} else {
|
|
162
|
+
throw new Error('unknown chunk format: ' + JSON.stringify(chunk));
|
|
163
|
+
}
|
|
164
|
+
buffer = buffer.substring(
|
|
165
|
+
buffer.indexOf(__flowStreamDelimiter) + __flowStreamDelimiter.length
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
throw new Error('stream did not terminate correctly');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Invoke a deployed flow over HTTP(s).
|
|
174
|
+
*
|
|
175
|
+
* For example:
|
|
176
|
+
*
|
|
177
|
+
* ```js
|
|
178
|
+
* import { runFlow } from 'genkit/beta/client';
|
|
179
|
+
*
|
|
180
|
+
* const response = await runFlow({
|
|
181
|
+
* url: 'https://my-flow-deployed-url',
|
|
182
|
+
* input: 'foo',
|
|
183
|
+
* });
|
|
184
|
+
* console.log(await response);
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export async function runFlow<O = any, Init = any>({
|
|
188
|
+
url,
|
|
189
|
+
input,
|
|
190
|
+
init,
|
|
191
|
+
headers,
|
|
192
|
+
abortSignal,
|
|
193
|
+
}: {
|
|
194
|
+
/** URL of the deployed flow. */
|
|
195
|
+
url: string;
|
|
196
|
+
/** Flow input. */
|
|
197
|
+
input?: any;
|
|
198
|
+
/** Initialization data for the action. */
|
|
199
|
+
init?: Init;
|
|
200
|
+
/** A map of HTTP headers to be added to the HTTP call. */
|
|
201
|
+
headers?: Record<string, string>;
|
|
202
|
+
/** Abort signal to abort the request. */
|
|
203
|
+
abortSignal?: AbortSignal;
|
|
204
|
+
}): Promise<O> {
|
|
205
|
+
const response = await fetch(url, {
|
|
206
|
+
method: 'POST',
|
|
207
|
+
body: JSON.stringify({
|
|
208
|
+
data: input,
|
|
209
|
+
...(init !== undefined && { init }),
|
|
210
|
+
}),
|
|
211
|
+
headers: {
|
|
212
|
+
'Content-Type': 'application/json',
|
|
213
|
+
...headers,
|
|
214
|
+
},
|
|
215
|
+
signal: abortSignal,
|
|
216
|
+
});
|
|
217
|
+
if (response.status !== 200) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`Server returned: ${response.status}: ${await response.text()}`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const wrappedResult = (await response.json()) as
|
|
223
|
+
| { result: O }
|
|
224
|
+
| { error: unknown };
|
|
225
|
+
if ('error' in wrappedResult) {
|
|
226
|
+
if (typeof wrappedResult.error === 'string') {
|
|
227
|
+
throw new Error(wrappedResult.error);
|
|
228
|
+
}
|
|
229
|
+
// TODO: The callable protocol defines an HttpError that has a JSON format of
|
|
230
|
+
// details?: string
|
|
231
|
+
// httpErrorCode: { canonicalName: string }
|
|
232
|
+
// message: string
|
|
233
|
+
// Should we create a new error class that parses this and exposes it as fields?
|
|
234
|
+
throw new Error(JSON.stringify(wrappedResult.error));
|
|
235
|
+
}
|
|
236
|
+
return wrappedResult.result;
|
|
237
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2024 Google LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A simple, browser-safe client library for remotely running/streaming deployed Genkit flows.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { runFlow, streamFlow } from 'genkit/beta/client';
|
|
24
|
+
*
|
|
25
|
+
* const response = await runFlow({
|
|
26
|
+
* url: 'https://my-flow-deployed-url',
|
|
27
|
+
* input: 'foo',
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* const response = streamFlow({
|
|
31
|
+
* url: 'https://my-flow-deployed-url',
|
|
32
|
+
* input: 'foo',
|
|
33
|
+
* });
|
|
34
|
+
* for await (const chunk of response.stream) {
|
|
35
|
+
* console.log(chunk);
|
|
36
|
+
* }
|
|
37
|
+
* console.log(await response.output);
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module beta/client
|
|
41
|
+
*/
|
|
42
|
+
export {
|
|
43
|
+
AgentError,
|
|
44
|
+
applyPatch,
|
|
45
|
+
remoteAgent,
|
|
46
|
+
type AgentAPI,
|
|
47
|
+
type AgentChat,
|
|
48
|
+
type AgentChunk,
|
|
49
|
+
type AgentInterrupt,
|
|
50
|
+
type AgentResponse,
|
|
51
|
+
type AgentTurn,
|
|
52
|
+
type DetachedTask,
|
|
53
|
+
type JsonPatch,
|
|
54
|
+
type RemoteAgentOptions,
|
|
55
|
+
} from './agent.js';
|
|
56
|
+
|
|
57
|
+
export { runFlow, streamFlow } from './client.js';
|
|
58
|
+
export type {
|
|
59
|
+
AgentFinishReason,
|
|
60
|
+
AgentInit,
|
|
61
|
+
AgentInput,
|
|
62
|
+
AgentOutput,
|
|
63
|
+
AgentStreamChunk,
|
|
64
|
+
Artifact,
|
|
65
|
+
GenerationUsage,
|
|
66
|
+
MessageData,
|
|
67
|
+
Part,
|
|
68
|
+
SessionSnapshot,
|
|
69
|
+
SessionState,
|
|
70
|
+
ToolRequestPart,
|
|
71
|
+
ToolResponsePart,
|
|
72
|
+
} from './types.js';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Type-only re-exports of the agent/generate contract types used by the agent
|
|
19
|
+
* client. Kept type-only so the browser-safe client pulls in no runtime code
|
|
20
|
+
* from `@genkit-ai/ai`.
|
|
21
|
+
*
|
|
22
|
+
* @module beta/client/types
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export type {
|
|
26
|
+
AgentInit,
|
|
27
|
+
AgentInput,
|
|
28
|
+
AgentOutput,
|
|
29
|
+
AgentStreamChunk,
|
|
30
|
+
GenerationUsage,
|
|
31
|
+
MessageData,
|
|
32
|
+
Part,
|
|
33
|
+
ToolRequestPart,
|
|
34
|
+
ToolResponsePart,
|
|
35
|
+
} from '@genkit-ai/ai';
|
|
36
|
+
export type {
|
|
37
|
+
AgentFinishReason,
|
|
38
|
+
Artifact,
|
|
39
|
+
SessionSnapshot,
|
|
40
|
+
SessionState,
|
|
41
|
+
} from '@genkit-ai/ai/session';
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Copyright 2024 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
BaseDataPointSchema,
|
|
20
|
+
Document,
|
|
21
|
+
DocumentDataSchema,
|
|
22
|
+
GENKIT_UI_METADATA,
|
|
23
|
+
GENKIT_UI_WIDGETS,
|
|
24
|
+
GenerateResponse,
|
|
25
|
+
GenerateResponseChunk,
|
|
26
|
+
GenerationBlockedError,
|
|
27
|
+
GenerationCommonConfigSchema,
|
|
28
|
+
GenerationResponseError,
|
|
29
|
+
LlmResponseSchema,
|
|
30
|
+
LlmStatsSchema,
|
|
31
|
+
Message,
|
|
32
|
+
MessageSchema,
|
|
33
|
+
MiddlewareDescSchema,
|
|
34
|
+
MiddlewareRefSchema,
|
|
35
|
+
ModelReferenceSchema,
|
|
36
|
+
ModelRequestSchema,
|
|
37
|
+
ModelResponseSchema,
|
|
38
|
+
PartSchema,
|
|
39
|
+
RoleSchema,
|
|
40
|
+
ToolCallSchema,
|
|
41
|
+
ToolInterruptError,
|
|
42
|
+
ToolSchema,
|
|
43
|
+
dynamicResource,
|
|
44
|
+
embedderActionMetadata,
|
|
45
|
+
embedderRef,
|
|
46
|
+
evaluatorRef,
|
|
47
|
+
generateMiddleware,
|
|
48
|
+
indexerRef,
|
|
49
|
+
modelActionMetadata,
|
|
50
|
+
modelRef,
|
|
51
|
+
rerankerRef,
|
|
52
|
+
resource,
|
|
53
|
+
respondTool,
|
|
54
|
+
restartTool,
|
|
55
|
+
retrieverRef,
|
|
56
|
+
type DocumentData,
|
|
57
|
+
type DynamicResourceAction,
|
|
58
|
+
type EmbedderAction,
|
|
59
|
+
type EmbedderArgument,
|
|
60
|
+
type EmbedderInfo,
|
|
61
|
+
type EmbedderParams,
|
|
62
|
+
type EmbedderReference,
|
|
63
|
+
type Embedding,
|
|
64
|
+
type EvalResponses,
|
|
65
|
+
type EvaluatorAction,
|
|
66
|
+
type EvaluatorInfo,
|
|
67
|
+
type EvaluatorParams,
|
|
68
|
+
type EvaluatorReference,
|
|
69
|
+
type ExecutablePrompt,
|
|
70
|
+
type GenerateMiddleware,
|
|
71
|
+
type GenerateMiddlewareDef,
|
|
72
|
+
type GenerateOptions,
|
|
73
|
+
type GenerateRequest,
|
|
74
|
+
type GenerateRequestData,
|
|
75
|
+
type GenerateResponseChunkData,
|
|
76
|
+
type GenerateResponseData,
|
|
77
|
+
type GenerateStreamOptions,
|
|
78
|
+
type GenerateStreamResponse,
|
|
79
|
+
type GenerationUsage,
|
|
80
|
+
type IndexerAction,
|
|
81
|
+
type IndexerArgument,
|
|
82
|
+
type IndexerInfo,
|
|
83
|
+
type IndexerParams,
|
|
84
|
+
type IndexerReference,
|
|
85
|
+
type InterruptConfig,
|
|
86
|
+
type LlmResponse,
|
|
87
|
+
type LlmStats,
|
|
88
|
+
type MediaPart,
|
|
89
|
+
type MessageData,
|
|
90
|
+
type MiddlewareDesc,
|
|
91
|
+
type MiddlewareRef,
|
|
92
|
+
type ModelArgument,
|
|
93
|
+
type ModelReference,
|
|
94
|
+
type ModelRequest,
|
|
95
|
+
type ModelResponseData,
|
|
96
|
+
type MultipartToolAction,
|
|
97
|
+
type OperationOptions,
|
|
98
|
+
type OutputOptions,
|
|
99
|
+
type Part,
|
|
100
|
+
type PromptAction,
|
|
101
|
+
type PromptConfig,
|
|
102
|
+
type PromptGenerateOptions,
|
|
103
|
+
type RankedDocument,
|
|
104
|
+
type RerankerAction,
|
|
105
|
+
type RerankerArgument,
|
|
106
|
+
type RerankerInfo,
|
|
107
|
+
type RerankerParams,
|
|
108
|
+
type RerankerReference,
|
|
109
|
+
type ResourceAction,
|
|
110
|
+
type ResumeOptions,
|
|
111
|
+
type RetrieverAction,
|
|
112
|
+
type RetrieverArgument,
|
|
113
|
+
type RetrieverInfo,
|
|
114
|
+
type RetrieverParams,
|
|
115
|
+
type RetrieverReference,
|
|
116
|
+
type Role,
|
|
117
|
+
type Tool,
|
|
118
|
+
type ToolAction,
|
|
119
|
+
type ToolArgument,
|
|
120
|
+
type ToolCall,
|
|
121
|
+
type ToolChoice,
|
|
122
|
+
type ToolConfig,
|
|
123
|
+
type ToolRequest,
|
|
124
|
+
type ToolRequestPart,
|
|
125
|
+
type ToolResponse,
|
|
126
|
+
type ToolResponsePart,
|
|
127
|
+
} from '@genkit-ai/ai';
|
|
128
|
+
export { dynamicTool, tool } from '@genkit-ai/ai/tool';
|
|
129
|
+
export {
|
|
130
|
+
GENKIT_CLIENT_HEADER,
|
|
131
|
+
GENKIT_VERSION,
|
|
132
|
+
GenkitError,
|
|
133
|
+
OperationSchema,
|
|
134
|
+
ReflectionServer,
|
|
135
|
+
StatusCodes,
|
|
136
|
+
StatusNameSchema,
|
|
137
|
+
StatusSchema,
|
|
138
|
+
UserFacingError,
|
|
139
|
+
annotateSchema,
|
|
140
|
+
defineJsonSchema,
|
|
141
|
+
defineSchema,
|
|
142
|
+
getClientHeader,
|
|
143
|
+
getCurrentEnv,
|
|
144
|
+
getGenkitRuntimeConfig,
|
|
145
|
+
getStreamingCallback,
|
|
146
|
+
isDevEnv,
|
|
147
|
+
resetGenkitRuntimeConfig,
|
|
148
|
+
runWithStreamingCallback,
|
|
149
|
+
setGenkitRuntimeConfig,
|
|
150
|
+
z,
|
|
151
|
+
type Action,
|
|
152
|
+
type ActionContext,
|
|
153
|
+
type ActionMetadata,
|
|
154
|
+
type DynamicActionProviderAction,
|
|
155
|
+
type ErrorResponseMetadata,
|
|
156
|
+
type Flow,
|
|
157
|
+
type FlowConfig,
|
|
158
|
+
type FlowFn,
|
|
159
|
+
type FlowSideChannel,
|
|
160
|
+
type GenkitRuntimeConfig,
|
|
161
|
+
type JSONSchema,
|
|
162
|
+
type JSONSchema7,
|
|
163
|
+
type Middleware,
|
|
164
|
+
type Operation,
|
|
165
|
+
type ReflectionServerOptions,
|
|
166
|
+
type RunActionResponse,
|
|
167
|
+
type Status,
|
|
168
|
+
type StatusName,
|
|
169
|
+
type StreamingCallback,
|
|
170
|
+
type StreamingResponse,
|
|
171
|
+
type TelemetryConfig,
|
|
172
|
+
} from '@genkit-ai/core';
|
|
173
|
+
import { initNodeFeatures } from '@genkit-ai/core/node';
|
|
174
|
+
|
|
175
|
+
initNodeFeatures();
|