Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
feat: prototype runtime (COR-000) (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
z4o4z authored Sep 17, 2024
1 parent 04dcd26 commit 1ad51a1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const useRuntimeState = ({ assistant, config, traceHandlers }: Settings)
isStatus,
reset,
getTurns,
setIndicator,

// these are meant to be static, so bundling them with the API
assistant,
Expand Down
17 changes: 10 additions & 7 deletions packages/react-chat/src/dtos/ChatConfig.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseRequest } from '@voiceflow/dtos-interact';
import type { PublicVerify, RuntimeOptions as SDKRuntimeOptions } from '@voiceflow/sdk-runtime';
import type { PrototypeVerify, PublicVerify, RuntimeOptions as SDKRuntimeOptions } from '@voiceflow/sdk-runtime';
import { z } from 'zod';

import type { RawAssistantOptions } from './AssistantOptions.dto';
Expand All @@ -19,9 +19,10 @@ export type VerifyOptions = z.infer<typeof VerifyOptions>;
export type UserOptions = z.infer<typeof UserOptions>;
export type LaunchOptions = z.infer<typeof LaunchOptions>;

export const VerifyOptions = z.object({
projectID: z.string(),
});
export const VerifyOptions = z.union([
z.object({ projectID: z.string() }),
z.object({ projectID: z.string(), versionID: z.string(), prototype: z.literal(true) }),
]);

export const LaunchOptions = z
.object({
Expand All @@ -39,10 +40,12 @@ export const UserOptions = z
})
.partial();

export interface ChatConfig extends z.infer<typeof ChatConfig>, SDKRuntimeOptions<PublicVerify> {}
export interface RawChatConfig extends z.input<typeof ChatConfig>, Omit<SDKRuntimeOptions<PublicVerify>, 'url'> {}
type Config<T extends SDKRuntimeOptions<PublicVerify | PrototypeVerify>> = T;

export interface LoadConfig extends RawChatConfig {
export interface ChatConfig extends Config<z.infer<typeof ChatConfig>> {}

export interface LoadConfig extends Omit<ChatConfig, 'url'> {
url?: ChatConfig['url'];
assistant?: RawAssistantOptions;
}

Expand Down
1 change: 1 addition & 0 deletions packages/sdk-runtime/src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './runtime.interface';
export * from './runtime.service';
export * from './runtime.service.auth';
export * from './runtime.service.prototype';
export * from './runtime.service.public';
29 changes: 22 additions & 7 deletions packages/sdk-runtime/src/runtime/runtime.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export { Trace } from '@voiceflow/base-types';
// Super broad type so that differing fetch types (ex. builtin, node-fetch, etc.) don't conflict with it
type FetchFn = (...parameters: any[]) => Promise<any>;

export interface RuntimeOptions<V = AuthVerify | PublicVerify> {
export interface RuntimeOptions<V = AuthVerify | PublicVerify | PrototypeVerify> {
url: string;
verify: V;

Expand All @@ -24,18 +24,32 @@ export interface PublicVerify {
projectID: string;
}

export const isAuthRuntimeOptions = (
options: RuntimeOptions<Partial<AuthVerify & PublicVerify>>
): options is RuntimeOptions<AuthVerify> => {
export interface PrototypeVerify {
projectID: string;
versionID: string;
prototype: true;
}

export type AnyVerify = Partial<AuthVerify & PublicVerify & PrototypeVerify>;

export const isAuthRuntimeOptions = (options: RuntimeOptions<AnyVerify>): options is RuntimeOptions<AuthVerify> => {
return !!options?.verify?.authorization;
};

export const isPublicRuntimeOptions = (
options: RuntimeOptions<Partial<AuthVerify & PublicVerify>>
): options is RuntimeOptions<PublicVerify> => {
export const isPublicRuntimeOptions = (options: RuntimeOptions<AnyVerify>): options is RuntimeOptions<PublicVerify> => {
return typeof options?.verify?.projectID === 'string';
};

export const isPrototypeRuntimeOptions = (
options: RuntimeOptions<AnyVerify>
): options is RuntimeOptions<PrototypeVerify> => {
return (
options?.verify?.prototype === true &&
typeof options?.verify?.versionID === 'string' &&
typeof options?.verify?.projectID === 'string'
);
};

export interface RuntimeInteractRequest {
action: RuntimeAction;
sessionID: string;
Expand Down Expand Up @@ -85,6 +99,7 @@ export const createIntentAction = (payload: BaseRequest.IntentRequestPayload): B
});

export interface RuntimeState {
turn: Record<string, any>;
storage: Record<string, any>;
variables: Record<string, any>;
}
Expand Down
52 changes: 52 additions & 0 deletions packages/sdk-runtime/src/runtime/runtime.service.prototype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type {
PrototypeVerify,
RuntimeInteractRequest,
RuntimeInteractResponse,
RuntimeOptions,
RuntimeState,
} from '@/runtime/runtime.interface';
import { RuntimeService } from '@/runtime/runtime.service';

export class PrototypeRuntimeService extends RuntimeService {
private state: RuntimeState = {
turn: {},
storage: {},
variables: {},
};

private readonly versionID: string;

public constructor(options: RuntimeOptions<PrototypeVerify>) {
super(options);

this.versionID = options.verify.versionID;
}

public async interact(request: RuntimeInteractRequest): Promise<RuntimeInteractResponse> {
const { action, config, sessionID } = request;

const result = await this.send<RuntimeInteractResponse>(`interact/${this.versionID}`, {
body: { state: this.state, request: action, config },
method: 'POST',
headers: { sessionID, platform: 'chat-prototype' },
});

this.state = result.state;

return result;
}

public async feedback() {
return Promise.reject(new Error('not implemented'));
}

// TODO: expose authenticated publishing
public getPublishing() {
return Promise.reject(new Error('not implemented'));
}

// TODO: expose authenticated createTranscript
public createTranscript() {
return Promise.reject(new Error('not implemented'));
}
}
13 changes: 11 additions & 2 deletions packages/sdk-runtime/src/sdk/sdk.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { RuntimeFeedbackRequest, RuntimeInteractRequest, RuntimeService } from '@/runtime';
import { AuthRuntimeService, isAuthRuntimeOptions, isPublicRuntimeOptions, PublicRuntimeService } from '@/runtime';
import {
AuthRuntimeService,
isAuthRuntimeOptions,
isPrototypeRuntimeOptions,
isPublicRuntimeOptions,
PrototypeRuntimeService,
PublicRuntimeService,
} from '@/runtime';
import type { TraceDeclaration } from '@/trace/trace.interface';
import { TraceService } from '@/trace/trace.service';

Expand All @@ -13,7 +20,9 @@ export class VoiceflowRuntime<T> {
public constructor(options: VoiceflowRuntimeOptions<T>) {
this.trace = new TraceService(options);

if (isAuthRuntimeOptions(options)) {
if (isPrototypeRuntimeOptions(options)) {
this.runtime = new PrototypeRuntimeService(options);
} else if (isAuthRuntimeOptions(options)) {
this.runtime = new AuthRuntimeService(options);
} else if (isPublicRuntimeOptions(options)) {
this.runtime = new PublicRuntimeService(options);
Expand Down

0 comments on commit 1ad51a1

Please sign in to comment.