Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/studio.giselles.ai/app/giselle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ if (
throw new Error("missing github credentials");
}

type TeamForPlan = Pick<CurrentTeam, "id" | "activeSubscriptionId" | "plan">;
type TeamForPlan = Pick<
CurrentTeam,
"id" | "activeSubscriptionId" | "activeCustomerId" | "plan"
>;

async function traceEmbeddingForTeam(args: {
metrics: EmbeddingMetrics;
Expand All @@ -94,6 +97,7 @@ async function traceEmbeddingForTeam(args: {
teamPlan,
userId: args.userId,
subscriptionId: args.team.activeSubscriptionId ?? "",
customerId: args.team.activeCustomerId ?? "",
resourceProvider: queryContext.provider,
workspaceId: queryContext.workspaceId,
embeddingProfileId: queryContext.embeddingProfileId,
Expand Down Expand Up @@ -276,8 +280,9 @@ export const giselle = NextGiselle({
userId: parsedMetadata.userId,
team: {
id: parsedMetadata.team.id,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
plan: parsedMetadata.team.plan,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
activeCustomerId: parsedMetadata.team.activeCustomerId,
},
});
return;
Expand Down Expand Up @@ -413,6 +418,7 @@ if (generateContentProcessor === "trigger.dev") {
id: team.id,
subscriptionId: team.activeSubscriptionId,
plan: team.plan,
activeCustomerId: team.activeCustomerId,
},
});
break;
Expand All @@ -435,6 +441,7 @@ if (generateContentProcessor === "trigger.dev") {
id: currentTeam.id,
subscriptionId: currentTeam.activeSubscriptionId,
plan: currentTeam.plan,
activeCustomerId: currentTeam.activeCustomerId,
},
});
break;
Expand Down
1 change: 1 addition & 0 deletions apps/studio.giselles.ai/lib/generation-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export const GenerationMetadata = z.object({
id: z.string<`tm_${string}`>(),
subscriptionId: z.string().nullable(),
plan: z.enum(["free", "pro", "team", "enterprise", "internal"]),
activeCustomerId: z.string().nullable(),
}),
});
6 changes: 5 additions & 1 deletion apps/studio.giselles.ai/lib/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type {
import type { ModelMessage, ProviderMetadata } from "ai";
import type { CurrentTeam } from "@/services/teams";

type TeamForPlan = Pick<CurrentTeam, "id" | "activeSubscriptionId" | "plan">;
type TeamForPlan = Pick<
CurrentTeam,
"id" | "activeSubscriptionId" | "activeCustomerId" | "plan"
>;

export async function traceGenerationForTeam(args: {
generation: CompletedGeneration | FailedGeneration;
Expand All @@ -33,6 +36,7 @@ export async function traceGenerationForTeam(args: {
teamPlan,
userId: args.userId,
subscriptionId: args.team.activeSubscriptionId ?? "",
customerId: args.team.activeCustomerId ?? "",
providerMetadata: args.providerMetadata,
requestId: args.requestId,
workspaceId: args.generation.context.origin.workspaceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface GenerateEmbeddingsOptions {
maxBatchSize?: number;
signal?: AbortSignal;
embeddingComplete?: EmbeddingCompleteCallback;
xStripeCustomerId?: string;
}

interface EmbeddingResult {
Expand Down Expand Up @@ -54,6 +55,7 @@ export async function generateEmbeddings(
maxBatchSize = DEFAULT_MAX_BATCH_SIZE,
signal,
embeddingComplete,
xStripeCustomerId,
} = options;

if (!Number.isInteger(maxBatchSize) || maxBatchSize <= 0) {
Expand Down Expand Up @@ -117,7 +119,16 @@ export async function generateEmbeddings(
signal?.throwIfAborted();

const batch = chunks.slice(i, i + maxBatchSize);
const batchEmbeddings = await embedder.embedMany(batch);
const batchEmbeddings = await embedder.embedMany(
batch,
xStripeCustomerId
? {
headers: {
Comment on lines +122 to +126

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge embedMany call now violates @giselles-ai/rag signature

The new call to embedder.embedMany(batch, { headers: ... }) uses a second argument, but the public EmbedderFunction exported by @giselles-ai/rag still declares embedMany(texts: string[]): Promise<number[][]> (see packages/rag/dist/index.d.ts lines 173‑185). With the current package version in the workspace, this change will fail TypeScript compilation for the studio app with an “Expected 1 arguments, but got 2” error, blocking builds until the rag types are updated or the extra parameter is removed.

Useful? React with 👍 / 👎.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

The EmbedderFunction type was updated in PR #XXX (PR2) to include the optional EmbeddingOptions parameter on both embed and embedMany methods (see packages/rag/src/embedder/types.ts).

The build artifacts in packages/rag/dist/index.d.ts should reflect this change after running pnpm build-sdk. All type checks pass successfully:

  • ✅ pnpm build-sdk
  • ✅ pnpm check-types
  • ✅ pnpm test

If you're seeing the old signature, it might be because the dist files need to be regenerated or the PR branch needs to be rebased.

"X-Stripe-Customer-ID": xStripeCustomerId,
},
}
: undefined,
);

if (batchEmbeddings.length !== batch.length) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ export async function ingestDocument(
embeddingProfileId,
signal,
embeddingComplete,
xStripeCustomerId:
telemetryContext?.team.activeCustomerId ?? undefined,
});

signal?.throwIfAborted();
Expand Down
7 changes: 5 additions & 2 deletions apps/studio.giselles.ai/trigger/generate-content-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const generateContentJob = schemaJob({
id: z.string<`tm_${string}`>(),
subscriptionId: z.string().nullable(),
plan: z.enum(["free", "pro", "team", "enterprise", "internal"]),
activeCustomerId: z.string().nullable(),
}),
}),
run: async (payload) => {
Expand Down Expand Up @@ -42,8 +43,9 @@ export const generateContentJob = schemaJob({
sessionId: generation.context.origin.taskId,
team: {
id: parsedMetadata.team.id,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
plan: parsedMetadata.team.plan,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
activeCustomerId: parsedMetadata.team.activeCustomerId,
},
});
},
Expand All @@ -57,8 +59,9 @@ export const generateContentJob = schemaJob({
sessionId: generation.context.origin.taskId,
team: {
id: parsedMetadata.team.id,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
plan: parsedMetadata.team.plan,
activeSubscriptionId: parsedMetadata.team.subscriptionId,
activeCustomerId: parsedMetadata.team.activeCustomerId,
},
});
},
Expand Down
18 changes: 16 additions & 2 deletions packages/giselle/src/generations/generate-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ export function generateContent({

const model = generationModel(
operationNode.content.llm,
context.aiGateway,
context.aiGateway
? {
...context.aiGateway,
xStripeCustomerId:
(metadata?.team as { activeCustomerId?: string | null })
?.activeCustomerId ?? undefined,
}
: undefined,
);
let generationError: unknown | undefined;
const textGenerationStartTime = Date.now();
Expand Down Expand Up @@ -498,7 +505,11 @@ function getProviderOptions(languageModelData: TextGenerationLanguageModelData):

function generationModel(
languageModel: TextGenerationLanguageModelData,
gatewayOptions?: { httpReferer: string; xTitle: string },
gatewayOptions?: {
httpReferer: string;
xTitle: string;
xStripeCustomerId?: string;
},
) {
const llmProvider = languageModel.provider;
const gateway = createGateway(
Expand All @@ -508,6 +519,9 @@ function generationModel(
headers: {
"http-referer": gatewayOptions.httpReferer,
"x-title": gatewayOptions.xTitle,
...(gatewayOptions.xStripeCustomerId
? { "X-Stripe-Customer-ID": gatewayOptions.xStripeCustomerId }
: {}),
},
},
);
Expand Down
15 changes: 12 additions & 3 deletions packages/rag/src/embedder/ai-sdk-embedder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { EmbeddingProfile } from "@giselles-ai/protocol";
import { type EmbeddingModel, embed, embedMany } from "ai";
import { ConfigurationError, EmbeddingError } from "../errors";
import type { EmbedderFunction, EmbeddingCompleteCallback } from "./types";
import type {
EmbedderFunction,
EmbeddingCompleteCallback,
EmbeddingOptions,
} from "./types";

export interface EmbedderConfig {
apiKey: string;
Expand Down Expand Up @@ -46,13 +50,14 @@ export function createAiSdkEmbedder(
};

return {
async embed(text: string): Promise<number[]> {
async embed(text: string, options?: EmbeddingOptions): Promise<number[]> {
try {
const startTime = new Date();
const result = await embed({
model: getModel(model),
maxRetries,
value: text,
headers: options?.headers,
});

if (config.embeddingComplete) {
Expand Down Expand Up @@ -85,13 +90,17 @@ export function createAiSdkEmbedder(
}
},

async embedMany(texts: string[]): Promise<number[][]> {
async embedMany(
texts: string[],
options?: EmbeddingOptions,
): Promise<number[][]> {
try {
const startTime = new Date();
const result = await embedMany({
model: getModel(model),
maxRetries,
values: texts,
headers: options?.headers,
});

if (config.embeddingComplete) {
Expand Down
16 changes: 14 additions & 2 deletions packages/rag/src/embedder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,35 @@ export type EmbeddingCompleteCallback = (
metrics: EmbeddingMetrics,
) => void | Promise<void>;

/**
* Options for embedding operations
*/
export interface EmbeddingOptions {
/**
* Additional HTTP headers to be sent with the request
*/
headers?: Record<string, string>;
}

/**
* Function type for embedding operations
*/
export type EmbedderFunction = {
/**
* Convert text to an embedding vector
* @param text The text to embed
* @param options Optional configuration for the embedding request
* @returns The embedding vector
*/
embed(text: string): Promise<number[]>;
embed(text: string, options?: EmbeddingOptions): Promise<number[]>;

/**
* Embed multiple texts at once
* @param texts The array of texts to embed
* @param options Optional configuration for the embedding request
* @returns The array of embedding vectors
*/
embedMany(texts: string[]): Promise<number[][]>;
embedMany(texts: string[], options?: EmbeddingOptions): Promise<number[][]>;

/**
* Optional callback invoked after embedding completion
Expand Down
Loading