@rudderjs/ai 0.0.1
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 +21 -0
- package/README.md +461 -0
- package/boost/guidelines.md +150 -0
- package/dist/agent.d.ts +74 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +1070 -0
- package/dist/agent.js.map +1 -0
- package/dist/attachment.d.ts +35 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +121 -0
- package/dist/attachment.js.map +1 -0
- package/dist/audio.d.ts +33 -0
- package/dist/audio.d.ts.map +1 -0
- package/dist/audio.js +76 -0
- package/dist/audio.js.map +1 -0
- package/dist/cached-embedding.d.ts +14 -0
- package/dist/cached-embedding.d.ts.map +1 -0
- package/dist/cached-embedding.js +44 -0
- package/dist/cached-embedding.js.map +1 -0
- package/dist/conversation.d.ts +16 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +53 -0
- package/dist/conversation.js.map +1 -0
- package/dist/facade.d.ts +53 -0
- package/dist/facade.d.ts.map +1 -0
- package/dist/facade.js +100 -0
- package/dist/facade.js.map +1 -0
- package/dist/fake.d.ts +55 -0
- package/dist/fake.d.ts.map +1 -0
- package/dist/fake.js +172 -0
- package/dist/fake.js.map +1 -0
- package/dist/image.d.ts +27 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +90 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +18 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +72 -0
- package/dist/middleware.js.map +1 -0
- package/dist/output.d.ts +22 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +55 -0
- package/dist/output.js.map +1 -0
- package/dist/provider-tools.d.ts +60 -0
- package/dist/provider-tools.d.ts.map +1 -0
- package/dist/provider-tools.js +133 -0
- package/dist/provider-tools.js.map +1 -0
- package/dist/provider.d.ts +12 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +94 -0
- package/dist/provider.js.map +1 -0
- package/dist/providers/anthropic.d.ts +12 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +221 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/azure.d.ts +13 -0
- package/dist/providers/azure.d.ts.map +1 -0
- package/dist/providers/azure.js +15 -0
- package/dist/providers/azure.js.map +1 -0
- package/dist/providers/deepseek.d.ts +12 -0
- package/dist/providers/deepseek.d.ts.map +1 -0
- package/dist/providers/deepseek.js +15 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/google.d.ts +13 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +293 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/groq.d.ts +12 -0
- package/dist/providers/groq.d.ts.map +1 -0
- package/dist/providers/groq.js +15 -0
- package/dist/providers/groq.js.map +1 -0
- package/dist/providers/mistral.d.ts +13 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +46 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/ollama.d.ts +11 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +15 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +26 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +374 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/xai.d.ts +12 -0
- package/dist/providers/xai.d.ts.map +1 -0
- package/dist/providers/xai.js +15 -0
- package/dist/providers/xai.js.map +1 -0
- package/dist/queue-job.d.ts +35 -0
- package/dist/queue-job.d.ts.map +1 -0
- package/dist/queue-job.js +82 -0
- package/dist/queue-job.js.map +1 -0
- package/dist/registry.d.ts +25 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +54 -0
- package/dist/registry.js.map +1 -0
- package/dist/tool.d.ts +157 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +134 -0
- package/dist/tool.js.map +1 -0
- package/dist/transcription.d.ts +28 -0
- package/dist/transcription.d.ts.map +1 -0
- package/dist/transcription.js +63 -0
- package/dist/transcription.js.map +1 -0
- package/dist/types.d.ts +439 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vercel-protocol.d.ts +18 -0
- package/dist/vercel-protocol.d.ts.map +1 -0
- package/dist/vercel-protocol.js +75 -0
- package/dist/vercel-protocol.js.map +1 -0
- package/dist/zod-to-json-schema.d.ts +8 -0
- package/dist/zod-to-json-schema.d.ts.map +1 -0
- package/dist/zod-to-json-schema.js +86 -0
- package/dist/zod-to-json-schema.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Queued AI prompt builder.
|
|
3
|
+
* Created via `agent.queue('prompt')`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* await agent.queue('Analyze this report')
|
|
7
|
+
* .onQueue('ai')
|
|
8
|
+
* .then(response => console.log(response.text))
|
|
9
|
+
* .catch(err => console.error(err))
|
|
10
|
+
* .send()
|
|
11
|
+
*/
|
|
12
|
+
export class QueuedPromptBuilder {
|
|
13
|
+
agentRef;
|
|
14
|
+
input;
|
|
15
|
+
options;
|
|
16
|
+
_queue = 'default';
|
|
17
|
+
_delay = 0;
|
|
18
|
+
_thenFn;
|
|
19
|
+
_catchFn;
|
|
20
|
+
constructor(agentRef, input, options) {
|
|
21
|
+
this.agentRef = agentRef;
|
|
22
|
+
this.input = input;
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
/** Set the queue name */
|
|
26
|
+
onQueue(name) {
|
|
27
|
+
this._queue = name;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
/** Set a delay before the job runs */
|
|
31
|
+
delay(ms) {
|
|
32
|
+
this._delay = ms;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
/** Callback when the prompt succeeds */
|
|
36
|
+
then(fn) {
|
|
37
|
+
this._thenFn = fn;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/** Callback when the prompt fails */
|
|
41
|
+
catch(fn) {
|
|
42
|
+
this._catchFn = fn;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/** Dispatch the job to the queue */
|
|
46
|
+
async send() {
|
|
47
|
+
// Lazy import @rudderjs/queue — uses dispatch(fn) for queued closures
|
|
48
|
+
let dispatchFn;
|
|
49
|
+
try {
|
|
50
|
+
const specifier = '@rudderjs/queue';
|
|
51
|
+
const mod = await import(/* @vite-ignore */ specifier);
|
|
52
|
+
dispatchFn = mod['dispatch'];
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new Error('[RudderJS AI] @rudderjs/queue is required for agent.queue(). Install it: pnpm add @rudderjs/queue');
|
|
56
|
+
}
|
|
57
|
+
const agentRef = this.agentRef;
|
|
58
|
+
const input = this.input;
|
|
59
|
+
const promptOptions = this.options;
|
|
60
|
+
const thenFn = this._thenFn;
|
|
61
|
+
const catchFn = this._catchFn;
|
|
62
|
+
await dispatchFn(async () => {
|
|
63
|
+
try {
|
|
64
|
+
const response = await agentRef.prompt(input, promptOptions);
|
|
65
|
+
if (thenFn)
|
|
66
|
+
await thenFn(response);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (catchFn) {
|
|
70
|
+
await catchFn(error);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, {
|
|
77
|
+
queue: this._queue,
|
|
78
|
+
delay: this._delay,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=queue-job.js.map
|
|
@@ -0,0 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,IAAI,CAAC,QAAQ,CAAA;QAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;QACxB,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAA;QAClC,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAA;QAC3B,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAA;QAE7B,MAAM,UAAU,CAAC,KAAK,IAAI,EAAE;YAC1B,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAA;gBAC5D,IAAI,MAAM;oBAAE,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAA;YACpC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,OAAO,EAAE,CAAC;oBACZ,MAAM,OAAO,CAAC,KAAK,CAAC,CAAA;gBACtB,CAAC;qBAAM,CAAC;oBACN,MAAM,KAAK,CAAA;gBACb,CAAC;YACH,CAAC;QACH,CAAC,EAAE;YACD,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,KAAK,EAAE,IAAI,CAAC,MAAM;SACnB,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AiModelConfig, ProviderFactory, ProviderAdapter } from './types.js';
|
|
2
|
+
export declare class AiRegistry {
|
|
3
|
+
private static readonly factories;
|
|
4
|
+
private static _default;
|
|
5
|
+
private static _models;
|
|
6
|
+
/** Register a provider factory */
|
|
7
|
+
static register(factory: ProviderFactory): void;
|
|
8
|
+
/** Get a registered provider factory by name */
|
|
9
|
+
static getFactory(name: string): ProviderFactory;
|
|
10
|
+
/** Set the default provider/model string */
|
|
11
|
+
static setDefault(modelString: string): void;
|
|
12
|
+
/** Get the default provider/model string */
|
|
13
|
+
static getDefault(): string;
|
|
14
|
+
/** Parse 'provider/model' string into [providerName, modelId] */
|
|
15
|
+
static parseModelString(modelString: string): [string, string];
|
|
16
|
+
/** Resolve a provider/model string to a ProviderAdapter */
|
|
17
|
+
static resolve(modelString: string): ProviderAdapter;
|
|
18
|
+
/** Set available models for user selection */
|
|
19
|
+
static setModels(models: AiModelConfig[]): void;
|
|
20
|
+
/** Get available models */
|
|
21
|
+
static getModels(): AiModelConfig[];
|
|
22
|
+
/** @internal — reset for testing */
|
|
23
|
+
static reset(): void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjF,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAqC;IACtE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAsB;IAC7C,OAAO,CAAC,MAAM,CAAC,OAAO,CAAsB;IAE5C,kCAAkC;IAClC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAI/C,gDAAgD;IAChD,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAMhD,4CAA4C;IAC5C,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAI5C,4CAA4C;IAC5C,MAAM,CAAC,UAAU,IAAI,MAAM;IAK3B,iEAAiE;IACjE,MAAM,CAAC,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IAM9D,2DAA2D;IAC3D,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,eAAe;IAMpD,8CAA8C;IAC9C,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI;IAI/C,2BAA2B;IAC3B,MAAM,CAAC,SAAS,IAAI,aAAa,EAAE;IAInC,oCAAoC;IACpC,MAAM,CAAC,KAAK,IAAI,IAAI;CAKrB"}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export class AiRegistry {
|
|
2
|
+
static factories = new Map();
|
|
3
|
+
static _default = null;
|
|
4
|
+
static _models = [];
|
|
5
|
+
/** Register a provider factory */
|
|
6
|
+
static register(factory) {
|
|
7
|
+
this.factories.set(factory.name, factory);
|
|
8
|
+
}
|
|
9
|
+
/** Get a registered provider factory by name */
|
|
10
|
+
static getFactory(name) {
|
|
11
|
+
const f = this.factories.get(name);
|
|
12
|
+
if (!f)
|
|
13
|
+
throw new Error(`[RudderJS AI] Unknown AI provider "${name}". Register it first.`);
|
|
14
|
+
return f;
|
|
15
|
+
}
|
|
16
|
+
/** Set the default provider/model string */
|
|
17
|
+
static setDefault(modelString) {
|
|
18
|
+
this._default = modelString;
|
|
19
|
+
}
|
|
20
|
+
/** Get the default provider/model string */
|
|
21
|
+
static getDefault() {
|
|
22
|
+
if (!this._default)
|
|
23
|
+
throw new Error('[RudderJS AI] No default model set. Add ai() to providers with a config.');
|
|
24
|
+
return this._default;
|
|
25
|
+
}
|
|
26
|
+
/** Parse 'provider/model' string into [providerName, modelId] */
|
|
27
|
+
static parseModelString(modelString) {
|
|
28
|
+
const slash = modelString.indexOf('/');
|
|
29
|
+
if (slash === -1)
|
|
30
|
+
throw new Error(`[RudderJS AI] Invalid model string "${modelString}". Expected "provider/model" format.`);
|
|
31
|
+
return [modelString.slice(0, slash), modelString.slice(slash + 1)];
|
|
32
|
+
}
|
|
33
|
+
/** Resolve a provider/model string to a ProviderAdapter */
|
|
34
|
+
static resolve(modelString) {
|
|
35
|
+
const [providerName, model] = this.parseModelString(modelString);
|
|
36
|
+
const factory = this.getFactory(providerName);
|
|
37
|
+
return factory.create(model);
|
|
38
|
+
}
|
|
39
|
+
/** Set available models for user selection */
|
|
40
|
+
static setModels(models) {
|
|
41
|
+
this._models = models;
|
|
42
|
+
}
|
|
43
|
+
/** Get available models */
|
|
44
|
+
static getModels() {
|
|
45
|
+
return this._models;
|
|
46
|
+
}
|
|
47
|
+
/** @internal — reset for testing */
|
|
48
|
+
static reset() {
|
|
49
|
+
this.factories.clear();
|
|
50
|
+
this._default = null;
|
|
51
|
+
this._models = [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,UAAU;IACb,MAAM,CAAU,SAAS,GAAG,IAAI,GAAG,EAA2B,CAAA;IAC9D,MAAM,CAAC,QAAQ,GAAkB,IAAI,CAAA;IACrC,MAAM,CAAC,OAAO,GAAoB,EAAE,CAAA;IAE5C,kCAAkC;IAClC,MAAM,CAAC,QAAQ,CAAC,OAAwB;QACtC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC3C,CAAC;IAED,gDAAgD;IAChD,MAAM,CAAC,UAAU,CAAC,IAAY;QAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAClC,IAAI,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,IAAI,uBAAuB,CAAC,CAAA;QAC1F,OAAO,CAAC,CAAA;IACV,CAAC;IAED,4CAA4C;IAC5C,MAAM,CAAC,UAAU,CAAC,WAAmB;QACnC,IAAI,CAAC,QAAQ,GAAG,WAAW,CAAA;IAC7B,CAAC;IAED,4CAA4C;IAC5C,MAAM,CAAC,UAAU;QACf,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAA;QAC/G,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,iEAAiE;IACjE,MAAM,CAAC,gBAAgB,CAAC,WAAmB;QACzC,MAAM,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QACtC,IAAI,KAAK,KAAK,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,WAAW,sCAAsC,CAAC,CAAA;QAC3H,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,WAAW,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAA;IACpE,CAAC;IAED,2DAA2D;IAC3D,MAAM,CAAC,OAAO,CAAC,WAAmB;QAChC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAA;QAChE,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,CAAA;QAC7C,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED,8CAA8C;IAC9C,MAAM,CAAC,SAAS,CAAC,MAAuB;QACtC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAED,2BAA2B;IAC3B,MAAM,CAAC,SAAS;QACd,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;IAED,oCAAoC;IACpC,MAAM,CAAC,KAAK;QACV,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAA;QACtB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACpB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;IACnB,CAAC"}
|
package/dist/tool.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { Tool, ToolCall, ToolCallContext, ToolDefinitionOptions, ToolDefinitionSchema, ToolExecuteFn } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Control chunk a server tool can `yield` from an `async function*`
|
|
5
|
+
* execute to pause the enclosing agent loop and surface a set of CLIENT
|
|
6
|
+
* tool calls to the caller — as if the model itself had emitted them.
|
|
7
|
+
*
|
|
8
|
+
* This is the sanctioned way for a server tool to inject client-tool
|
|
9
|
+
* calls into the parent loop's pending list. The agent loop iterating
|
|
10
|
+
* the execute generator recognizes the shape via the reserved `__rudderjs`
|
|
11
|
+
* discriminator, appends `toolCalls` to the parent's
|
|
12
|
+
* `pendingClientToolCalls`, sets the stop-for-client-tools flag, and —
|
|
13
|
+
* critically — does NOT push an error tool_result / tool message for the
|
|
14
|
+
* yielding tool call. The yielding tool's own call remains orphaned in
|
|
15
|
+
* the parent's message history until the caller resolves it on
|
|
16
|
+
* continuation (typically by computing a final result from whatever the
|
|
17
|
+
* client-side tool execution returned).
|
|
18
|
+
*
|
|
19
|
+
* Why a yield instead of a throw:
|
|
20
|
+
* - Symmetry with the existing `tool-update` yield protocol — no parallel
|
|
21
|
+
* catch-based control path.
|
|
22
|
+
* - Middleware can observe pauses through `runOnChunk`; a thrown error
|
|
23
|
+
* would route through `onError` and muddle telemetry.
|
|
24
|
+
* - Exceptions signal "something went wrong"; this is not an error.
|
|
25
|
+
* - Platform-level feature: any server tool can yield this, not just
|
|
26
|
+
* nested agent runners. E.g., a tool that wants the browser's
|
|
27
|
+
* geolocation, clipboard, or a user file upload.
|
|
28
|
+
*
|
|
29
|
+
* **Primary use case today:** nested agent runners (`run_agent` in
|
|
30
|
+
* panels). A runner that streams a sub-agent server-side can hit a pause
|
|
31
|
+
* point when the sub-agent's model calls a client tool. Those calls have
|
|
32
|
+
* to execute in the browser, not server-side, so the parent loop must
|
|
33
|
+
* surface them to its own caller.
|
|
34
|
+
*
|
|
35
|
+
* Tools that yield this chunk are responsible for persisting any state
|
|
36
|
+
* they need to resume the inner work on continuation (usually in a cache
|
|
37
|
+
* or runStore). `@rudderjs/ai` does not know or care about that state —
|
|
38
|
+
* it just propagates the pause. The optional `resumeHandle` is an opaque
|
|
39
|
+
* string the tool author owns; the agent loop never inspects it.
|
|
40
|
+
*
|
|
41
|
+
* Tool authors should construct this via {@link pauseForClientTools},
|
|
42
|
+
* not by hand, so future shape changes stay source-compatible.
|
|
43
|
+
*/
|
|
44
|
+
export interface PauseForClientToolsChunk {
|
|
45
|
+
/** Reserved discriminator. Namespaced to avoid colliding with user data. */
|
|
46
|
+
readonly __rudderjs: 'pause_for_client_tools';
|
|
47
|
+
readonly toolCalls: ToolCall[];
|
|
48
|
+
readonly resumeHandle?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Construct a pause control chunk for `yield`ing from a server tool's
|
|
52
|
+
* async-generator execute.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* .server(async function* (input, ctx) {
|
|
56
|
+
* const subRunId = await persistSubRunState(...)
|
|
57
|
+
* yield pauseForClientTools(subAgentPending, subRunId)
|
|
58
|
+
* // Unreachable — the loop halts iteration after the pause chunk.
|
|
59
|
+
* return undefined as never
|
|
60
|
+
* })
|
|
61
|
+
*/
|
|
62
|
+
export declare function pauseForClientTools(toolCalls: ToolCall[], resumeHandle?: string): PauseForClientToolsChunk;
|
|
63
|
+
/**
|
|
64
|
+
* Structural typeguard for a pause chunk. Used by the agent loop to
|
|
65
|
+
* detect pauses mid-execute without requiring tool authors to import any
|
|
66
|
+
* `@rudderjs/ai` symbol at the yield site — they can yield via
|
|
67
|
+
* {@link pauseForClientTools} or construct the object literal directly.
|
|
68
|
+
*/
|
|
69
|
+
export declare function isPauseForClientToolsChunk(value: unknown): value is PauseForClientToolsChunk;
|
|
70
|
+
/**
|
|
71
|
+
* Builder returned by {@link toolDefinition}. The builder itself is a valid
|
|
72
|
+
* `Tool` — call `.server(execute)` to attach a server-side handler, or use
|
|
73
|
+
* the builder as-is to register a client tool (no `execute`).
|
|
74
|
+
*/
|
|
75
|
+
export declare class ToolBuilder<TInput extends z.ZodType = z.ZodType, TOutput extends z.ZodType = z.ZodType> implements Tool<z.infer<TInput>, never> {
|
|
76
|
+
readonly options: ToolDefinitionOptions<TInput, TOutput>;
|
|
77
|
+
readonly definition: ToolDefinitionOptions;
|
|
78
|
+
/** Builders are valid client tools — `execute` is intentionally absent. */
|
|
79
|
+
readonly execute?: undefined;
|
|
80
|
+
constructor(options: ToolDefinitionOptions<TInput, TOutput>);
|
|
81
|
+
/**
|
|
82
|
+
* Attach a server-side handler.
|
|
83
|
+
*
|
|
84
|
+
* The handler may be a regular async function (single return value) or an
|
|
85
|
+
* async generator (`async function*`). Generators can `yield` preliminary
|
|
86
|
+
* progress payloads — each yield surfaces as a `tool-update` stream chunk
|
|
87
|
+
* while the tool runs. The generator's `return` value becomes the final
|
|
88
|
+
* tool result. The same tool definition works in both `agent.prompt()`
|
|
89
|
+
* (yields are silently drained) and `agent.stream()`.
|
|
90
|
+
*/
|
|
91
|
+
server<TReturn = unknown, TUpdate = unknown>(execute: (input: z.infer<TInput>, ctx?: ToolCallContext) => AsyncGenerator<TUpdate, TReturn, void>): ServerToolBuilder<z.infer<TInput>, TReturn>;
|
|
92
|
+
server<TReturn = unknown>(execute: (input: z.infer<TInput>, ctx?: ToolCallContext) => TReturn | Promise<TReturn>): ServerToolBuilder<z.infer<TInput>, TReturn>;
|
|
93
|
+
/** Convert to provider-friendly JSON Schema format */
|
|
94
|
+
toSchema(): ToolDefinitionSchema;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Builder returned by {@link ToolBuilder.server}. Itself a valid `Tool`, so
|
|
98
|
+
* it can be passed directly into `tools()` arrays — `.toModelOutput(...)` is
|
|
99
|
+
* an optional chained refinement, not a required `.build()` step.
|
|
100
|
+
*
|
|
101
|
+
* Future per-tool refinements (`.onError(...)`, `.middleware(...)`) would slot
|
|
102
|
+
* in here in the same chained-builder style.
|
|
103
|
+
*/
|
|
104
|
+
export declare class ServerToolBuilder<TInput = unknown, TOutput = unknown> implements Tool<TInput, TOutput> {
|
|
105
|
+
readonly definition: ToolDefinitionOptions;
|
|
106
|
+
readonly execute: ToolExecuteFn<TInput, TOutput, unknown>;
|
|
107
|
+
readonly toModelOutput?: ((result: TOutput) => string | Promise<string>) | undefined;
|
|
108
|
+
constructor(definition: ToolDefinitionOptions, execute: ToolExecuteFn<TInput, TOutput, unknown>, toModelOutput?: (result: TOutput) => string | Promise<string>);
|
|
109
|
+
/**
|
|
110
|
+
* Declare a transform from this tool's structured result to the string the
|
|
111
|
+
* **model** sees on its next step. The UI continues to receive the original
|
|
112
|
+
* structured result.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* toolDefinition({...})
|
|
116
|
+
* .server(async (input) => bigStructuredResult)
|
|
117
|
+
* .modelOutput((result) => `${result.items.length} items found`)
|
|
118
|
+
*/
|
|
119
|
+
modelOutput(fn: (result: TOutput) => string | Promise<string>): ServerToolBuilder<TInput, TOutput>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Define a tool with typed input/output schemas.
|
|
123
|
+
*
|
|
124
|
+
* @example Server tool
|
|
125
|
+
* const weatherTool = toolDefinition({
|
|
126
|
+
* name: 'get_weather',
|
|
127
|
+
* description: 'Get weather for a location',
|
|
128
|
+
* inputSchema: z.object({ location: z.string() }),
|
|
129
|
+
* }).server(async ({ location }) => ({ temp: 72, unit: 'F' }))
|
|
130
|
+
*
|
|
131
|
+
* @example Client tool (no `.server()`)
|
|
132
|
+
* const readFormStateTool = toolDefinition({
|
|
133
|
+
* name: 'read_form_state',
|
|
134
|
+
* description: 'Read the user\'s current local form values.',
|
|
135
|
+
* inputSchema: z.object({ fields: z.array(z.string()).optional() }),
|
|
136
|
+
* })
|
|
137
|
+
*/
|
|
138
|
+
export declare function toolDefinition<TInput extends z.ZodType, TOutput extends z.ZodType = z.ZodUndefined>(options: ToolDefinitionOptions<TInput, TOutput>): ToolBuilder<TInput, TOutput>;
|
|
139
|
+
/**
|
|
140
|
+
* Build a tool whose input/output types are not known at compile time.
|
|
141
|
+
*
|
|
142
|
+
* Use this when the tool's schema is constructed dynamically from user data
|
|
143
|
+
* (e.g. a resource agent that exposes one tool per agent definition).
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const t = dynamicTool({
|
|
147
|
+
* name: agentDef.slug,
|
|
148
|
+
* description: agentDef.description,
|
|
149
|
+
* inputSchema: z.object({}),
|
|
150
|
+
* }).server(async () => agentDef.run())
|
|
151
|
+
*/
|
|
152
|
+
export declare function dynamicTool(options: ToolDefinitionOptions): ToolBuilder;
|
|
153
|
+
/** Convert any Tool to a ToolDefinitionSchema for provider consumption */
|
|
154
|
+
export declare function toolToSchema(tool: {
|
|
155
|
+
definition: ToolDefinitionOptions;
|
|
156
|
+
}): ToolDefinitionSchema;
|
|
157
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,KAAK,EACV,IAAI,EACJ,QAAQ,EACR,eAAe,EACf,qBAAqB,EACrB,oBAAoB,EACpB,aAAa,EACd,MAAM,YAAY,CAAA;AAInB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,WAAW,wBAAwB;IACvC,4EAA4E;IAC5E,QAAQ,CAAC,UAAU,EAAE,wBAAwB,CAAA;IAC7C,QAAQ,CAAC,SAAS,EAAG,QAAQ,EAAE,CAAA;IAC/B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,QAAQ,EAAE,EACrB,YAAY,CAAC,EAAE,MAAM,GACpB,wBAAwB,CAK1B;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,wBAAwB,CAI5F;AAED;;;;GAIG;AACH,qBAAa,WAAW,CACtB,MAAM,SA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IAC1C,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;IACzD,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAA;gBAGlF,UAAU,EAAE,qBAAqB,EACjC,OAAO,EAAE,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EAChD,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAO/D;;;;;;;;;OASG;IACH,WAAW,CACT,EAAE,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAChD,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC;CAGtC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAC5B,MAAM,SAAS,CAAC,CAAC,OAAO,EACxB,OAAO,SAAS,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,YAAY,EAC1C,OAAO,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAE/E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,qBAAqB,GAC7B,WAAW,CAEb;AAED,0EAA0E;AAC1E,wBAAgB,YAAY,CAAC,IAAI,EAAE;IAAE,UAAU,EAAE,qBAAqB,CAAA;CAAE,GAAG,oBAAoB,CAM9F"}
|
package/dist/tool.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { zodToJsonSchema } from './zod-to-json-schema.js';
|
|
2
|
+
/**
|
|
3
|
+
* Construct a pause control chunk for `yield`ing from a server tool's
|
|
4
|
+
* async-generator execute.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* .server(async function* (input, ctx) {
|
|
8
|
+
* const subRunId = await persistSubRunState(...)
|
|
9
|
+
* yield pauseForClientTools(subAgentPending, subRunId)
|
|
10
|
+
* // Unreachable — the loop halts iteration after the pause chunk.
|
|
11
|
+
* return undefined as never
|
|
12
|
+
* })
|
|
13
|
+
*/
|
|
14
|
+
export function pauseForClientTools(toolCalls, resumeHandle) {
|
|
15
|
+
const chunk = resumeHandle !== undefined
|
|
16
|
+
? { __rudderjs: 'pause_for_client_tools', toolCalls, resumeHandle }
|
|
17
|
+
: { __rudderjs: 'pause_for_client_tools', toolCalls };
|
|
18
|
+
return chunk;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Structural typeguard for a pause chunk. Used by the agent loop to
|
|
22
|
+
* detect pauses mid-execute without requiring tool authors to import any
|
|
23
|
+
* `@rudderjs/ai` symbol at the yield site — they can yield via
|
|
24
|
+
* {@link pauseForClientTools} or construct the object literal directly.
|
|
25
|
+
*/
|
|
26
|
+
export function isPauseForClientToolsChunk(value) {
|
|
27
|
+
if (value === null || typeof value !== 'object')
|
|
28
|
+
return false;
|
|
29
|
+
const v = value;
|
|
30
|
+
return v.__rudderjs === 'pause_for_client_tools' && Array.isArray(v.toolCalls);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Builder returned by {@link toolDefinition}. The builder itself is a valid
|
|
34
|
+
* `Tool` — call `.server(execute)` to attach a server-side handler, or use
|
|
35
|
+
* the builder as-is to register a client tool (no `execute`).
|
|
36
|
+
*/
|
|
37
|
+
export class ToolBuilder {
|
|
38
|
+
options;
|
|
39
|
+
definition;
|
|
40
|
+
/** Builders are valid client tools — `execute` is intentionally absent. */
|
|
41
|
+
execute;
|
|
42
|
+
constructor(options) {
|
|
43
|
+
this.options = options;
|
|
44
|
+
this.definition = options;
|
|
45
|
+
}
|
|
46
|
+
server(execute) {
|
|
47
|
+
return new ServerToolBuilder(this.options, execute);
|
|
48
|
+
}
|
|
49
|
+
/** Convert to provider-friendly JSON Schema format */
|
|
50
|
+
toSchema() {
|
|
51
|
+
return {
|
|
52
|
+
name: this.options.name,
|
|
53
|
+
description: this.options.description,
|
|
54
|
+
parameters: zodToJsonSchema(this.options.inputSchema),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Builder returned by {@link ToolBuilder.server}. Itself a valid `Tool`, so
|
|
60
|
+
* it can be passed directly into `tools()` arrays — `.toModelOutput(...)` is
|
|
61
|
+
* an optional chained refinement, not a required `.build()` step.
|
|
62
|
+
*
|
|
63
|
+
* Future per-tool refinements (`.onError(...)`, `.middleware(...)`) would slot
|
|
64
|
+
* in here in the same chained-builder style.
|
|
65
|
+
*/
|
|
66
|
+
export class ServerToolBuilder {
|
|
67
|
+
definition;
|
|
68
|
+
execute;
|
|
69
|
+
toModelOutput;
|
|
70
|
+
constructor(definition, execute, toModelOutput) {
|
|
71
|
+
this.definition = definition;
|
|
72
|
+
this.execute = execute;
|
|
73
|
+
if (toModelOutput)
|
|
74
|
+
this.toModelOutput = toModelOutput;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Declare a transform from this tool's structured result to the string the
|
|
78
|
+
* **model** sees on its next step. The UI continues to receive the original
|
|
79
|
+
* structured result.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* toolDefinition({...})
|
|
83
|
+
* .server(async (input) => bigStructuredResult)
|
|
84
|
+
* .modelOutput((result) => `${result.items.length} items found`)
|
|
85
|
+
*/
|
|
86
|
+
modelOutput(fn) {
|
|
87
|
+
return new ServerToolBuilder(this.definition, this.execute, fn);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Define a tool with typed input/output schemas.
|
|
92
|
+
*
|
|
93
|
+
* @example Server tool
|
|
94
|
+
* const weatherTool = toolDefinition({
|
|
95
|
+
* name: 'get_weather',
|
|
96
|
+
* description: 'Get weather for a location',
|
|
97
|
+
* inputSchema: z.object({ location: z.string() }),
|
|
98
|
+
* }).server(async ({ location }) => ({ temp: 72, unit: 'F' }))
|
|
99
|
+
*
|
|
100
|
+
* @example Client tool (no `.server()`)
|
|
101
|
+
* const readFormStateTool = toolDefinition({
|
|
102
|
+
* name: 'read_form_state',
|
|
103
|
+
* description: 'Read the user\'s current local form values.',
|
|
104
|
+
* inputSchema: z.object({ fields: z.array(z.string()).optional() }),
|
|
105
|
+
* })
|
|
106
|
+
*/
|
|
107
|
+
export function toolDefinition(options) {
|
|
108
|
+
return new ToolBuilder(options);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Build a tool whose input/output types are not known at compile time.
|
|
112
|
+
*
|
|
113
|
+
* Use this when the tool's schema is constructed dynamically from user data
|
|
114
|
+
* (e.g. a resource agent that exposes one tool per agent definition).
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* const t = dynamicTool({
|
|
118
|
+
* name: agentDef.slug,
|
|
119
|
+
* description: agentDef.description,
|
|
120
|
+
* inputSchema: z.object({}),
|
|
121
|
+
* }).server(async () => agentDef.run())
|
|
122
|
+
*/
|
|
123
|
+
export function dynamicTool(options) {
|
|
124
|
+
return new ToolBuilder(options);
|
|
125
|
+
}
|
|
126
|
+
/** Convert any Tool to a ToolDefinitionSchema for provider consumption */
|
|
127
|
+
export function toolToSchema(tool) {
|
|
128
|
+
return {
|
|
129
|
+
name: tool.definition.name,
|
|
130
|
+
description: tool.definition.description,
|
|
131
|
+
parameters: zodToJsonSchema(tool.definition.inputSchema),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=tool.js.map
|
package/dist/tool.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AA4DzD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBA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E,UAAiC,EACjC,OAAgD,EAChD,aAA6D;QAE7D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,aAAa;YAAE,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;IACvD,CAAC;IAED;;;;;;;;;OASG;IACH,WAAW,CACT,EAAiD;QAEjD,OAAO,IAAI,iBAAiB,CAAkB,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IAClF,CAAC;CACF;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAG5B,OAA+C;IAC/C,OAAO,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;AACjC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CACzB,OAA8B;IAE9B,OAAO,IAAI,WAAW,CAAC,OAAsD,CAAC,CAAA;AAChF,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,YAAY,CAAC,IAA2C;IACtE,OAAO;QACL,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI;QAC1B,WAAW,EAAE,IAAI,CAAC,UAAU,CAAC,WAAW;QACxC,UAAU,EAAE,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;KACzD,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SpeechToTextResult } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Fluent builder for speech-to-text transcription.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const result = await Transcription.fromPath('./audio.mp3').generate()
|
|
7
|
+
* const result = await Transcription.fromBuffer(buf).language('en').generate()
|
|
8
|
+
*/
|
|
9
|
+
export declare class Transcription {
|
|
10
|
+
private readonly _audio;
|
|
11
|
+
private _model?;
|
|
12
|
+
private _language?;
|
|
13
|
+
private _prompt?;
|
|
14
|
+
private constructor();
|
|
15
|
+
/** Create a Transcription from a file path */
|
|
16
|
+
static fromPath(path: string): Transcription;
|
|
17
|
+
/** Create a Transcription from a Buffer */
|
|
18
|
+
static fromBuffer(buffer: Buffer): Transcription;
|
|
19
|
+
/** Set the STT model (e.g. 'openai/whisper-1') */
|
|
20
|
+
model(m: string): this;
|
|
21
|
+
/** Set the language hint (ISO-639-1, e.g. 'en', 'es', 'fr') */
|
|
22
|
+
language(l: string): this;
|
|
23
|
+
/** Set an optional prompt to guide transcription style */
|
|
24
|
+
prompt(p: string): this;
|
|
25
|
+
/** Run the transcription */
|
|
26
|
+
generate(): Promise<SpeechToTextResult>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=transcription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription.d.ts","sourceRoot":"","sources":["../src/transcription.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEpD;;;;;;GAMG;AACH,qBAAa,aAAa;IAKJ,OAAO,CAAC,QAAQ,CAAC,MAAM;IAJ3C,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,SAAS,CAAC,CAAQ;IAC1B,OAAO,CAAC,OAAO,CAAC,CAAQ;IAExB,OAAO;IAEP,8CAA8C;IAC9C,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa;IAI5C,2CAA2C;IAC3C,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,aAAa;IAIhD,kDAAkD;IAClD,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAKtB,+DAA+D;IAC/D,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAKzB,0DAA0D;IAC1D,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAKvB,4BAA4B;IACtB,QAAQ,IAAI,OAAO,CAAC,kBAAkB,CAAC;CAyB9C"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { AiRegistry } from './registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Fluent builder for speech-to-text transcription.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const result = await Transcription.fromPath('./audio.mp3').generate()
|
|
8
|
+
* const result = await Transcription.fromBuffer(buf).language('en').generate()
|
|
9
|
+
*/
|
|
10
|
+
export class Transcription {
|
|
11
|
+
_audio;
|
|
12
|
+
_model;
|
|
13
|
+
_language;
|
|
14
|
+
_prompt;
|
|
15
|
+
constructor(_audio) {
|
|
16
|
+
this._audio = _audio;
|
|
17
|
+
}
|
|
18
|
+
/** Create a Transcription from a file path */
|
|
19
|
+
static fromPath(path) {
|
|
20
|
+
return new Transcription(path);
|
|
21
|
+
}
|
|
22
|
+
/** Create a Transcription from a Buffer */
|
|
23
|
+
static fromBuffer(buffer) {
|
|
24
|
+
return new Transcription(buffer);
|
|
25
|
+
}
|
|
26
|
+
/** Set the STT model (e.g. 'openai/whisper-1') */
|
|
27
|
+
model(m) {
|
|
28
|
+
this._model = m;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
/** Set the language hint (ISO-639-1, e.g. 'en', 'es', 'fr') */
|
|
32
|
+
language(l) {
|
|
33
|
+
this._language = l;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/** Set an optional prompt to guide transcription style */
|
|
37
|
+
prompt(p) {
|
|
38
|
+
this._prompt = p;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/** Run the transcription */
|
|
42
|
+
async generate() {
|
|
43
|
+
const modelString = this._model ?? AiRegistry.getDefault();
|
|
44
|
+
const [providerName, modelId] = AiRegistry.parseModelString(modelString);
|
|
45
|
+
const factory = AiRegistry.getFactory(providerName);
|
|
46
|
+
if (!factory.createStt) {
|
|
47
|
+
throw new Error(`[RudderJS AI] Provider "${providerName}" does not support speech-to-text. ` +
|
|
48
|
+
`Use a provider that implements createStt() (e.g. openai).`);
|
|
49
|
+
}
|
|
50
|
+
// Resolve audio: if string (path), read into Buffer
|
|
51
|
+
const audioBuffer = typeof this._audio === 'string'
|
|
52
|
+
? readFileSync(this._audio)
|
|
53
|
+
: this._audio;
|
|
54
|
+
const adapter = factory.createStt(modelId);
|
|
55
|
+
return adapter.transcribe({
|
|
56
|
+
audio: audioBuffer,
|
|
57
|
+
model: modelId,
|
|
58
|
+
language: this._language,
|
|
59
|
+
prompt: this._prompt,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=transcription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription.js","sourceRoot":"","sources":["../src/transcription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG1C;;;;;;GAMG;AACH,MAAM,OAAO,aAAa;IAKa;IAJ7B,MAAM,CAAS;IACf,SAAS,CAAS;IAClB,OAAO,CAAS;IAExB,YAAqC,MAAuB;QAAvB,WAAM,GAAN,MAAM,CAAiB;IAAG,CAAC;IAEhE,8CAA8C;IAC9C,MAAM,CAAC,QAAQ,CAAC,IAAY;QAC1B,OAAO,IAAI,aAAa,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAED,2CAA2C;IAC3C,MAAM,CAAC,UAAU,CAAC,MAAc;QAC9B,OAAO,IAAI,aAAa,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;IAED,kDAAkD;IAClD,KAAK,CAAC,CAAS;QACb,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA;QACf,OAAO,IAAI,CAAA;IACb,CAAC;IAED,+DAA+D;IAC/D,QAAQ,CAAC,CAAS;QAChB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAA;QAClB,OAAO,IAAI,CAAA;IACb,CAAC;IA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