Skip to content

Commit 1ad51a1

Browse files
authored
feat: prototype runtime (COR-000) (#171)
1 parent 04dcd26 commit 1ad51a1

File tree

6 files changed

+97
-16
lines changed

6 files changed

+97
-16
lines changed

packages/react-chat/src/contexts/RuntimeContext/useRuntimeState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export const useRuntimeState = ({ assistant, config, traceHandlers }: Settings)
158158
isStatus,
159159
reset,
160160
getTurns,
161+
setIndicator,
161162

162163
// these are meant to be static, so bundling them with the API
163164
assistant,

packages/react-chat/src/dtos/ChatConfig.dto.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { BaseRequest } from '@voiceflow/dtos-interact';
2-
import type { PublicVerify, RuntimeOptions as SDKRuntimeOptions } from '@voiceflow/sdk-runtime';
2+
import type { PrototypeVerify, PublicVerify, RuntimeOptions as SDKRuntimeOptions } from '@voiceflow/sdk-runtime';
33
import { z } from 'zod';
44

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

22-
export const VerifyOptions = z.object({
23-
projectID: z.string(),
24-
});
22+
export const VerifyOptions = z.union([
23+
z.object({ projectID: z.string() }),
24+
z.object({ projectID: z.string(), versionID: z.string(), prototype: z.literal(true) }),
25+
]);
2526

2627
export const LaunchOptions = z
2728
.object({
@@ -39,10 +40,12 @@ export const UserOptions = z
3940
})
4041
.partial();
4142

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

45-
export interface LoadConfig extends RawChatConfig {
45+
export interface ChatConfig extends Config<z.infer<typeof ChatConfig>> {}
46+
47+
export interface LoadConfig extends Omit<ChatConfig, 'url'> {
48+
url?: ChatConfig['url'];
4649
assistant?: RawAssistantOptions;
4750
}
4851

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './runtime.interface';
22
export * from './runtime.service';
33
export * from './runtime.service.auth';
4+
export * from './runtime.service.prototype';
45
export * from './runtime.service.public';

packages/sdk-runtime/src/runtime/runtime.interface.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { Trace } from '@voiceflow/base-types';
55
// Super broad type so that differing fetch types (ex. builtin, node-fetch, etc.) don't conflict with it
66
type FetchFn = (...parameters: any[]) => Promise<any>;
77

8-
export interface RuntimeOptions<V = AuthVerify | PublicVerify> {
8+
export interface RuntimeOptions<V = AuthVerify | PublicVerify | PrototypeVerify> {
99
url: string;
1010
verify: V;
1111

@@ -24,18 +24,32 @@ export interface PublicVerify {
2424
projectID: string;
2525
}
2626

27-
export const isAuthRuntimeOptions = (
28-
options: RuntimeOptions<Partial<AuthVerify & PublicVerify>>
29-
): options is RuntimeOptions<AuthVerify> => {
27+
export interface PrototypeVerify {
28+
projectID: string;
29+
versionID: string;
30+
prototype: true;
31+
}
32+
33+
export type AnyVerify = Partial<AuthVerify & PublicVerify & PrototypeVerify>;
34+
35+
export const isAuthRuntimeOptions = (options: RuntimeOptions<AnyVerify>): options is RuntimeOptions<AuthVerify> => {
3036
return !!options?.verify?.authorization;
3137
};
3238

33-
export const isPublicRuntimeOptions = (
34-
options: RuntimeOptions<Partial<AuthVerify & PublicVerify>>
35-
): options is RuntimeOptions<PublicVerify> => {
39+
export const isPublicRuntimeOptions = (options: RuntimeOptions<AnyVerify>): options is RuntimeOptions<PublicVerify> => {
3640
return typeof options?.verify?.projectID === 'string';
3741
};
3842

43+
export const isPrototypeRuntimeOptions = (
44+
options: RuntimeOptions<AnyVerify>
45+
): options is RuntimeOptions<PrototypeVerify> => {
46+
return (
47+
options?.verify?.prototype === true &&
48+
typeof options?.verify?.versionID === 'string' &&
49+
typeof options?.verify?.projectID === 'string'
50+
);
51+
};
52+
3953
export interface RuntimeInteractRequest {
4054
action: RuntimeAction;
4155
sessionID: string;
@@ -85,6 +99,7 @@ export const createIntentAction = (payload: BaseRequest.IntentRequestPayload): B
8599
});
86100

87101
export interface RuntimeState {
102+
turn: Record<string, any>;
88103
storage: Record<string, any>;
89104
variables: Record<string, any>;
90105
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type {
2+
PrototypeVerify,
3+
RuntimeInteractRequest,
4+
RuntimeInteractResponse,
5+
RuntimeOptions,
6+
RuntimeState,
7+
} from '@/runtime/runtime.interface';
8+
import { RuntimeService } from '@/runtime/runtime.service';
9+
10+
export class PrototypeRuntimeService extends RuntimeService {
11+
private state: RuntimeState = {
12+
turn: {},
13+
storage: {},
14+
variables: {},
15+
};
16+
17+
private readonly versionID: string;
18+
19+
public constructor(options: RuntimeOptions<PrototypeVerify>) {
20+
super(options);
21+
22+
this.versionID = options.verify.versionID;
23+
}
24+
25+
public async interact(request: RuntimeInteractRequest): Promise<RuntimeInteractResponse> {
26+
const { action, config, sessionID } = request;
27+
28+
const result = await this.send<RuntimeInteractResponse>(`interact/${this.versionID}`, {
29+
body: { state: this.state, request: action, config },
30+
method: 'POST',
31+
headers: { sessionID, platform: 'chat-prototype' },
32+
});
33+
34+
this.state = result.state;
35+
36+
return result;
37+
}
38+
39+
public async feedback() {
40+
return Promise.reject(new Error('not implemented'));
41+
}
42+
43+
// TODO: expose authenticated publishing
44+
public getPublishing() {
45+
return Promise.reject(new Error('not implemented'));
46+
}
47+
48+
// TODO: expose authenticated createTranscript
49+
public createTranscript() {
50+
return Promise.reject(new Error('not implemented'));
51+
}
52+
}

packages/sdk-runtime/src/sdk/sdk.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import type { RuntimeFeedbackRequest, RuntimeInteractRequest, RuntimeService } from '@/runtime';
2-
import { AuthRuntimeService, isAuthRuntimeOptions, isPublicRuntimeOptions, PublicRuntimeService } from '@/runtime';
2+
import {
3+
AuthRuntimeService,
4+
isAuthRuntimeOptions,
5+
isPrototypeRuntimeOptions,
6+
isPublicRuntimeOptions,
7+
PrototypeRuntimeService,
8+
PublicRuntimeService,
9+
} from '@/runtime';
310
import type { TraceDeclaration } from '@/trace/trace.interface';
411
import { TraceService } from '@/trace/trace.service';
512

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

16-
if (isAuthRuntimeOptions(options)) {
23+
if (isPrototypeRuntimeOptions(options)) {
24+
this.runtime = new PrototypeRuntimeService(options);
25+
} else if (isAuthRuntimeOptions(options)) {
1726
this.runtime = new AuthRuntimeService(options);
1827
} else if (isPublicRuntimeOptions(options)) {
1928
this.runtime = new PublicRuntimeService(options);

0 commit comments

Comments
 (0)