rystem.playframework.client 0.1.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/README.md +265 -0
- package/lib/engine/ClientInteractionRegistry.d.ts +53 -0
- package/lib/engine/ClientInteractionRegistry.js +105 -0
- package/lib/engine/PlayFrameworkClient.d.ts +45 -0
- package/lib/engine/PlayFrameworkClient.js +199 -0
- package/lib/hooks/hooks.d.ts +27 -0
- package/lib/hooks/hooks.js +32 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +19 -0
- package/lib/models/AiSceneResponse.d.ts +118 -0
- package/lib/models/AiSceneResponse.js +2 -0
- package/lib/models/ClientInteractionRequest.d.ts +32 -0
- package/lib/models/ClientInteractionRequest.js +2 -0
- package/lib/models/ClientInteractionResult.d.ts +33 -0
- package/lib/models/ClientInteractionResult.js +2 -0
- package/lib/models/ContentItem.d.ts +30 -0
- package/lib/models/ContentItem.js +2 -0
- package/lib/models/PlayFrameworkRequest.d.ts +77 -0
- package/lib/models/PlayFrameworkRequest.js +2 -0
- package/lib/servicecollection/PlayFrameworkServices.d.ts +67 -0
- package/lib/servicecollection/PlayFrameworkServices.js +115 -0
- package/lib/servicecollection/PlayFrameworkSettings.d.ts +56 -0
- package/lib/servicecollection/PlayFrameworkSettings.js +116 -0
- package/lib/utils/AIContentConverter.d.ts +68 -0
- package/lib/utils/AIContentConverter.js +170 -0
- package/package.json +44 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePlayFramework = usePlayFramework;
|
|
4
|
+
const PlayFrameworkServices_1 = require("../servicecollection/PlayFrameworkServices");
|
|
5
|
+
/**
|
|
6
|
+
* Get a PlayFrameworkClient instance by optional factory name.
|
|
7
|
+
*
|
|
8
|
+
* If `name` is provided, returns the client for that specific factory.
|
|
9
|
+
* If `name` is omitted, returns the default (first configured) client.
|
|
10
|
+
*
|
|
11
|
+
* Framework-agnostic — works with React, Vue, Angular, Svelte, or plain TypeScript.
|
|
12
|
+
*
|
|
13
|
+
* @param name - Optional factory name. Omit to use the default client.
|
|
14
|
+
* @returns PlayFrameworkClient instance.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Use default (first configured) client
|
|
19
|
+
* const client = usePlayFramework();
|
|
20
|
+
*
|
|
21
|
+
* // Use a specific factory
|
|
22
|
+
* const chatClient = usePlayFramework("chat");
|
|
23
|
+
*
|
|
24
|
+
* // Execute
|
|
25
|
+
* for await (const step of client.executeStepByStep({ message: "Hello" })) {
|
|
26
|
+
* console.log(step.message);
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
function usePlayFramework(name) {
|
|
31
|
+
return PlayFrameworkServices_1.PlayFrameworkServices.resolve(name);
|
|
32
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type { ContentItem } from "./models/ContentItem";
|
|
2
|
+
export type { PlayFrameworkRequest, SceneRequestSettings, SceneExecutionMode, CacheBehavior } from "./models/PlayFrameworkRequest";
|
|
3
|
+
export type { AiSceneResponse, AiResponseStatus, SSEEvent, CompletionMarker, ErrorMarker } from "./models/AiSceneResponse";
|
|
4
|
+
export type { ClientInteractionRequest } from "./models/ClientInteractionRequest";
|
|
5
|
+
export type { ClientInteractionResult, AIContent } from "./models/ClientInteractionResult";
|
|
6
|
+
export { PlayFrameworkClient } from "./engine/PlayFrameworkClient";
|
|
7
|
+
export { ClientInteractionRegistry } from "./engine/ClientInteractionRegistry";
|
|
8
|
+
export type { ClientTool } from "./engine/ClientInteractionRegistry";
|
|
9
|
+
export { AIContentConverter } from "./utils/AIContentConverter";
|
|
10
|
+
export { PlayFrameworkSettings } from "./servicecollection/PlayFrameworkSettings";
|
|
11
|
+
export { PlayFrameworkServices } from "./servicecollection/PlayFrameworkServices";
|
|
12
|
+
export { usePlayFramework } from "./hooks/hooks";
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePlayFramework = exports.PlayFrameworkServices = exports.PlayFrameworkSettings = exports.AIContentConverter = exports.ClientInteractionRegistry = exports.PlayFrameworkClient = void 0;
|
|
4
|
+
// Engine
|
|
5
|
+
var PlayFrameworkClient_1 = require("./engine/PlayFrameworkClient");
|
|
6
|
+
Object.defineProperty(exports, "PlayFrameworkClient", { enumerable: true, get: function () { return PlayFrameworkClient_1.PlayFrameworkClient; } });
|
|
7
|
+
var ClientInteractionRegistry_1 = require("./engine/ClientInteractionRegistry");
|
|
8
|
+
Object.defineProperty(exports, "ClientInteractionRegistry", { enumerable: true, get: function () { return ClientInteractionRegistry_1.ClientInteractionRegistry; } });
|
|
9
|
+
// Utilities
|
|
10
|
+
var AIContentConverter_1 = require("./utils/AIContentConverter");
|
|
11
|
+
Object.defineProperty(exports, "AIContentConverter", { enumerable: true, get: function () { return AIContentConverter_1.AIContentConverter; } });
|
|
12
|
+
// Service Collection
|
|
13
|
+
var PlayFrameworkSettings_1 = require("./servicecollection/PlayFrameworkSettings");
|
|
14
|
+
Object.defineProperty(exports, "PlayFrameworkSettings", { enumerable: true, get: function () { return PlayFrameworkSettings_1.PlayFrameworkSettings; } });
|
|
15
|
+
var PlayFrameworkServices_1 = require("./servicecollection/PlayFrameworkServices");
|
|
16
|
+
Object.defineProperty(exports, "PlayFrameworkServices", { enumerable: true, get: function () { return PlayFrameworkServices_1.PlayFrameworkServices; } });
|
|
17
|
+
// Hooks
|
|
18
|
+
var hooks_1 = require("./hooks/hooks");
|
|
19
|
+
Object.defineProperty(exports, "usePlayFramework", { enumerable: true, get: function () { return hooks_1.usePlayFramework; } });
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ClientInteractionRequest } from "./ClientInteractionRequest";
|
|
2
|
+
/**
|
|
3
|
+
* Response status from PlayFramework execution.
|
|
4
|
+
* Matches all 18 C# AiResponseStatus enum values (serialized as string via JsonStringEnumConverter).
|
|
5
|
+
*/
|
|
6
|
+
export type AiResponseStatus = "Initializing" | "LoadingCache" | "ExecutingMainActors" | "Planning" | "ExecutingScene" | "FunctionRequest" | "FunctionCompleted" | "ToolSkipped" | "Streaming" | "Running" | "Summarizing" | "DirectorDecision" | "GeneratingFinalResponse" | "SavingCache" | "AwaitingClient" | "Completed" | "BudgetExceeded" | "Error";
|
|
7
|
+
/**
|
|
8
|
+
* Single step response from PlayFramework (used in step-by-step streaming).
|
|
9
|
+
* Matches C# AiSceneResponse contract.
|
|
10
|
+
*/
|
|
11
|
+
export interface AiSceneResponse {
|
|
12
|
+
/**
|
|
13
|
+
* Execution status.
|
|
14
|
+
*/
|
|
15
|
+
status: AiResponseStatus;
|
|
16
|
+
/**
|
|
17
|
+
* Scene name (if applicable).
|
|
18
|
+
*/
|
|
19
|
+
sceneName?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Function/tool name that was called (if applicable).
|
|
22
|
+
*/
|
|
23
|
+
functionName?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Function/tool arguments (if applicable).
|
|
26
|
+
*/
|
|
27
|
+
functionArguments?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Response message/text.
|
|
30
|
+
*/
|
|
31
|
+
message?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Streaming chunk — the new piece of text received in this streaming event.
|
|
34
|
+
* Only populated when status is "Streaming".
|
|
35
|
+
*/
|
|
36
|
+
streamingChunk?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Indicates if this is the last chunk in a streaming response.
|
|
39
|
+
*/
|
|
40
|
+
isStreamingComplete?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Error message (if status === "Error").
|
|
43
|
+
*/
|
|
44
|
+
errorMessage?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Input tokens used in this operation.
|
|
47
|
+
*/
|
|
48
|
+
inputTokens?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Cached input tokens (reduced cost).
|
|
51
|
+
*/
|
|
52
|
+
cachedInputTokens?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Output tokens generated in this operation.
|
|
55
|
+
*/
|
|
56
|
+
outputTokens?: number;
|
|
57
|
+
/**
|
|
58
|
+
* Total tokens (input + cached + output).
|
|
59
|
+
*/
|
|
60
|
+
totalTokens?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Cost of this specific operation.
|
|
63
|
+
*/
|
|
64
|
+
cost?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Cumulative cost so far in the execution.
|
|
67
|
+
*/
|
|
68
|
+
totalCost?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Conversation key for resuming execution.
|
|
71
|
+
*/
|
|
72
|
+
conversationKey?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Continuation token for resuming after client interaction.
|
|
75
|
+
* Present when status === "AwaitingClient".
|
|
76
|
+
*/
|
|
77
|
+
continuationToken?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Client interaction request when status === "AwaitingClient".
|
|
80
|
+
* Contains tool name, arguments, and schema for client execution.
|
|
81
|
+
*/
|
|
82
|
+
clientInteractionRequest?: ClientInteractionRequest;
|
|
83
|
+
/**
|
|
84
|
+
* Timestamp of this response (ISO 8601).
|
|
85
|
+
*/
|
|
86
|
+
timestamp?: string;
|
|
87
|
+
/**
|
|
88
|
+
* Additional metadata.
|
|
89
|
+
*/
|
|
90
|
+
metadata?: Record<string, any>;
|
|
91
|
+
/**
|
|
92
|
+
* Multi-modal output contents (images, audio, files generated by LLM or tools).
|
|
93
|
+
* Each item has { type, text, data, mediaType } matching ClientContentItem.
|
|
94
|
+
*/
|
|
95
|
+
contents?: Array<{
|
|
96
|
+
type: string;
|
|
97
|
+
text?: string;
|
|
98
|
+
data?: string;
|
|
99
|
+
mediaType?: string;
|
|
100
|
+
}>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Completion marker for SSE streaming.
|
|
104
|
+
*/
|
|
105
|
+
export interface CompletionMarker {
|
|
106
|
+
status: "completed";
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Error marker for SSE streaming.
|
|
110
|
+
*/
|
|
111
|
+
export interface ErrorMarker {
|
|
112
|
+
status: "error";
|
|
113
|
+
errorMessage: string;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* SSE event types.
|
|
117
|
+
*/
|
|
118
|
+
export type SSEEvent = AiSceneResponse | CompletionMarker | ErrorMarker;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request for client-side tool execution.
|
|
3
|
+
* Server sends this when it detects a tool that must run on client (camera, geolocation, file picker, etc.).
|
|
4
|
+
*/
|
|
5
|
+
export interface ClientInteractionRequest {
|
|
6
|
+
/**
|
|
7
|
+
* Unique interaction ID (GUID).
|
|
8
|
+
*/
|
|
9
|
+
interactionId: string;
|
|
10
|
+
/**
|
|
11
|
+
* Tool name registered via OnClient().
|
|
12
|
+
*/
|
|
13
|
+
toolName: string;
|
|
14
|
+
/**
|
|
15
|
+
* Human-readable description.
|
|
16
|
+
*/
|
|
17
|
+
description?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Arguments from LLM (if any).
|
|
20
|
+
* Type is validated against argumentsSchema.
|
|
21
|
+
*/
|
|
22
|
+
arguments?: Record<string, any>;
|
|
23
|
+
/**
|
|
24
|
+
* JSON Schema of arguments for validation.
|
|
25
|
+
* Generated from C# type T via System.Text.Json introspection.
|
|
26
|
+
*/
|
|
27
|
+
argumentsSchema?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum execution time in seconds.
|
|
30
|
+
*/
|
|
31
|
+
timeoutSeconds: number;
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AIContent types that can be sent to server.
|
|
3
|
+
* Simplified to 2 types: DataContent (Base64) and TextContent.
|
|
4
|
+
*/
|
|
5
|
+
export interface AIContent {
|
|
6
|
+
type: "text" | "data";
|
|
7
|
+
text?: string;
|
|
8
|
+
data?: string;
|
|
9
|
+
mediaType?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Result from client-side tool execution.
|
|
13
|
+
* Client sends this back to server to resume execution.
|
|
14
|
+
*/
|
|
15
|
+
export interface ClientInteractionResult {
|
|
16
|
+
/**
|
|
17
|
+
* Unique interaction ID matching the request.
|
|
18
|
+
*/
|
|
19
|
+
interactionId: string;
|
|
20
|
+
/**
|
|
21
|
+
* Multi-modal contents returned by tool.
|
|
22
|
+
* Can include images (Base64), audio, video, files, text.
|
|
23
|
+
*/
|
|
24
|
+
contents: AIContent[];
|
|
25
|
+
/**
|
|
26
|
+
* Error message if execution failed.
|
|
27
|
+
*/
|
|
28
|
+
error?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Timestamp when tool was executed (ISO 8601).
|
|
31
|
+
*/
|
|
32
|
+
executedAt: string;
|
|
33
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-modal content item for PlayFramework requests.
|
|
3
|
+
* Matches C# ContentItem contract.
|
|
4
|
+
*/
|
|
5
|
+
export interface ContentItem {
|
|
6
|
+
/**
|
|
7
|
+
* Content type: "text", "image", "audio", "video", "file", "uri"
|
|
8
|
+
*/
|
|
9
|
+
type: "text" | "image" | "audio" | "video" | "file" | "uri";
|
|
10
|
+
/**
|
|
11
|
+
* Text content (for type="text")
|
|
12
|
+
*/
|
|
13
|
+
text?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Base64-encoded data (for type="image", "audio", "video", "file")
|
|
16
|
+
*/
|
|
17
|
+
data?: string;
|
|
18
|
+
/**
|
|
19
|
+
* URI/URL (for type="image", "audio", "video", "file", "uri")
|
|
20
|
+
*/
|
|
21
|
+
uri?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Media type (MIME type)
|
|
24
|
+
*/
|
|
25
|
+
mediaType?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Optional name/filename.
|
|
28
|
+
*/
|
|
29
|
+
name?: string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ContentItem } from "./ContentItem";
|
|
2
|
+
import { ClientInteractionResult } from "./ClientInteractionResult";
|
|
3
|
+
/**
|
|
4
|
+
* Execution mode for the scene.
|
|
5
|
+
*/
|
|
6
|
+
export type SceneExecutionMode = "Direct" | "Planning" | "DynamicChaining" | "Scene";
|
|
7
|
+
/**
|
|
8
|
+
* Cache behavior for the request.
|
|
9
|
+
*/
|
|
10
|
+
export type CacheBehavior = "Default" | "Avoidable" | "Forever";
|
|
11
|
+
/**
|
|
12
|
+
* Request-specific settings matching C# SceneRequestSettings.
|
|
13
|
+
*/
|
|
14
|
+
export interface SceneRequestSettings {
|
|
15
|
+
/** Maximum recursion depth for planning (default: 5). */
|
|
16
|
+
maxRecursionDepth?: number;
|
|
17
|
+
/** Execution mode: Direct, Planning, DynamicChaining, Scene. */
|
|
18
|
+
executionMode?: SceneExecutionMode;
|
|
19
|
+
/** Whether to enable summarization. */
|
|
20
|
+
enableSummarization?: boolean;
|
|
21
|
+
/** Whether to enable director for multi-scene orchestration. */
|
|
22
|
+
enableDirector?: boolean;
|
|
23
|
+
/** Model to use for this request (overrides default). */
|
|
24
|
+
modelId?: string;
|
|
25
|
+
/** Temperature for LLM calls (0.0 - 2.0). */
|
|
26
|
+
temperature?: number;
|
|
27
|
+
/** Maximum tokens to generate. */
|
|
28
|
+
maxTokens?: number;
|
|
29
|
+
/** Cache behavior for this request. */
|
|
30
|
+
cacheBehavior?: CacheBehavior;
|
|
31
|
+
/** Maximum budget for this request. Null for unlimited. */
|
|
32
|
+
maxBudget?: number;
|
|
33
|
+
/** Enable streaming for text responses. */
|
|
34
|
+
enableStreaming?: boolean;
|
|
35
|
+
/** Maximum number of scenes in dynamic chaining mode (default: 5). */
|
|
36
|
+
maxDynamicScenes?: number;
|
|
37
|
+
/** Unique key for this conversation (used by cache and memory). */
|
|
38
|
+
conversationKey?: string;
|
|
39
|
+
/** Continuation token for resuming execution after client interaction. */
|
|
40
|
+
continuationToken?: string;
|
|
41
|
+
/** Name of the scene to execute directly (used with SceneExecutionMode "Scene"). */
|
|
42
|
+
sceneName?: string;
|
|
43
|
+
/** Results from client-side tool executions. */
|
|
44
|
+
clientInteractionResults?: ClientInteractionResult[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Request model for PlayFramework HTTP API.
|
|
48
|
+
* Matches C# PlayFrameworkRequest contract.
|
|
49
|
+
*/
|
|
50
|
+
export interface PlayFrameworkRequest {
|
|
51
|
+
/**
|
|
52
|
+
* User message (text).
|
|
53
|
+
*/
|
|
54
|
+
message?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Multi-modal content items (images, audio, video, files, URIs).
|
|
57
|
+
*/
|
|
58
|
+
contents?: ContentItem[];
|
|
59
|
+
/**
|
|
60
|
+
* Custom metadata (userId, sessionId, etc.).
|
|
61
|
+
*/
|
|
62
|
+
metadata?: Record<string, any>;
|
|
63
|
+
/**
|
|
64
|
+
* Request-specific settings (override defaults).
|
|
65
|
+
*/
|
|
66
|
+
settings?: SceneRequestSettings;
|
|
67
|
+
/**
|
|
68
|
+
* Continuation token for resuming execution after client interaction.
|
|
69
|
+
* Convenience field — also available inside settings.continuationToken.
|
|
70
|
+
*/
|
|
71
|
+
continuationToken?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Results from client-side tool executions.
|
|
74
|
+
* Convenience field — also available inside settings.clientInteractionResults.
|
|
75
|
+
*/
|
|
76
|
+
clientInteractionResults?: ClientInteractionResult[];
|
|
77
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PlayFrameworkClient } from "../engine/PlayFrameworkClient";
|
|
2
|
+
import { PlayFrameworkSettings } from "./PlayFrameworkSettings";
|
|
3
|
+
/**
|
|
4
|
+
* Service collection for PlayFramework clients (Dependency Injection pattern).
|
|
5
|
+
*/
|
|
6
|
+
export declare class PlayFrameworkServices {
|
|
7
|
+
private static clients;
|
|
8
|
+
private static settings;
|
|
9
|
+
/**
|
|
10
|
+
* Configure a PlayFramework client factory.
|
|
11
|
+
* Always returns a Promise for consistency (even if configure is sync).
|
|
12
|
+
*
|
|
13
|
+
* @param name - Factory name (unique identifier).
|
|
14
|
+
* @param baseUrl - Base API URL (e.g., "https://api.example.com/api/ai").
|
|
15
|
+
* @param configure - Optional configuration callback (sync or async).
|
|
16
|
+
* @returns Promise resolving to PlayFrameworkSettings instance.
|
|
17
|
+
*/
|
|
18
|
+
static configure(name: string, baseUrl: string, configure?: (settings: PlayFrameworkSettings) => void | Promise<void>): Promise<PlayFrameworkSettings>;
|
|
19
|
+
/**
|
|
20
|
+
* Get PlayFramework client by factory name.
|
|
21
|
+
*
|
|
22
|
+
* @param name - Factory name.
|
|
23
|
+
* @returns PlayFrameworkClient instance.
|
|
24
|
+
* @throws Error if factory not configured.
|
|
25
|
+
*/
|
|
26
|
+
static getClient(name: string): PlayFrameworkClient;
|
|
27
|
+
/**
|
|
28
|
+
* Get PlayFramework settings by factory name.
|
|
29
|
+
*
|
|
30
|
+
* @param name - Factory name.
|
|
31
|
+
* @returns PlayFrameworkSettings instance.
|
|
32
|
+
* @throws Error if factory not configured.
|
|
33
|
+
*/
|
|
34
|
+
static getSettings(name: string): PlayFrameworkSettings;
|
|
35
|
+
/**
|
|
36
|
+
* Check if a factory is configured.
|
|
37
|
+
*
|
|
38
|
+
* @param name - Factory name.
|
|
39
|
+
* @returns True if configured, false otherwise.
|
|
40
|
+
*/
|
|
41
|
+
static isConfigured(name: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Remove a configured factory.
|
|
44
|
+
*
|
|
45
|
+
* @param name - Factory name.
|
|
46
|
+
*/
|
|
47
|
+
static remove(name: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Clear all configured factories.
|
|
50
|
+
*/
|
|
51
|
+
static clear(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Get the default (first configured) client.
|
|
54
|
+
* Useful when only one factory is configured.
|
|
55
|
+
*
|
|
56
|
+
* @returns PlayFrameworkClient instance.
|
|
57
|
+
* @throws Error if no factory is configured.
|
|
58
|
+
*/
|
|
59
|
+
static getDefaultClient(): PlayFrameworkClient;
|
|
60
|
+
/**
|
|
61
|
+
* Get client by optional name. If name is omitted, returns the default (first configured) client.
|
|
62
|
+
*
|
|
63
|
+
* @param name - Optional factory name.
|
|
64
|
+
* @returns PlayFrameworkClient instance.
|
|
65
|
+
*/
|
|
66
|
+
static resolve(name?: string): PlayFrameworkClient;
|
|
67
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlayFrameworkServices = void 0;
|
|
4
|
+
const PlayFrameworkClient_1 = require("../engine/PlayFrameworkClient");
|
|
5
|
+
const PlayFrameworkSettings_1 = require("./PlayFrameworkSettings");
|
|
6
|
+
/**
|
|
7
|
+
* Service collection for PlayFramework clients (Dependency Injection pattern).
|
|
8
|
+
*/
|
|
9
|
+
class PlayFrameworkServices {
|
|
10
|
+
static clients = new Map();
|
|
11
|
+
static settings = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* Configure a PlayFramework client factory.
|
|
14
|
+
* Always returns a Promise for consistency (even if configure is sync).
|
|
15
|
+
*
|
|
16
|
+
* @param name - Factory name (unique identifier).
|
|
17
|
+
* @param baseUrl - Base API URL (e.g., "https://api.example.com/api/ai").
|
|
18
|
+
* @param configure - Optional configuration callback (sync or async).
|
|
19
|
+
* @returns Promise resolving to PlayFrameworkSettings instance.
|
|
20
|
+
*/
|
|
21
|
+
static async configure(name, baseUrl, configure) {
|
|
22
|
+
const settings = new PlayFrameworkSettings_1.PlayFrameworkSettings(name, baseUrl);
|
|
23
|
+
try {
|
|
24
|
+
if (configure) {
|
|
25
|
+
await configure(settings);
|
|
26
|
+
}
|
|
27
|
+
this.settings.set(name, settings);
|
|
28
|
+
this.clients.set(name, new PlayFrameworkClient_1.PlayFrameworkClient(settings));
|
|
29
|
+
return settings;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
// Clean up partial state on failure
|
|
33
|
+
this.settings.delete(name);
|
|
34
|
+
this.clients.delete(name);
|
|
35
|
+
throw new Error(`PlayFramework configuration failed for '${name}': ${error instanceof Error ? error.message : String(error)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get PlayFramework client by factory name.
|
|
40
|
+
*
|
|
41
|
+
* @param name - Factory name.
|
|
42
|
+
* @returns PlayFrameworkClient instance.
|
|
43
|
+
* @throws Error if factory not configured.
|
|
44
|
+
*/
|
|
45
|
+
static getClient(name) {
|
|
46
|
+
const client = this.clients.get(name);
|
|
47
|
+
if (!client) {
|
|
48
|
+
throw new Error(`PlayFramework client '${name}' not configured. Call PlayFrameworkServices.configure(name, baseUrl) first.`);
|
|
49
|
+
}
|
|
50
|
+
return client;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get PlayFramework settings by factory name.
|
|
54
|
+
*
|
|
55
|
+
* @param name - Factory name.
|
|
56
|
+
* @returns PlayFrameworkSettings instance.
|
|
57
|
+
* @throws Error if factory not configured.
|
|
58
|
+
*/
|
|
59
|
+
static getSettings(name) {
|
|
60
|
+
const settings = this.settings.get(name);
|
|
61
|
+
if (!settings) {
|
|
62
|
+
throw new Error(`PlayFramework settings '${name}' not configured. Call PlayFrameworkServices.configure(name, baseUrl) first.`);
|
|
63
|
+
}
|
|
64
|
+
return settings;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if a factory is configured.
|
|
68
|
+
*
|
|
69
|
+
* @param name - Factory name.
|
|
70
|
+
* @returns True if configured, false otherwise.
|
|
71
|
+
*/
|
|
72
|
+
static isConfigured(name) {
|
|
73
|
+
return this.clients.has(name);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Remove a configured factory.
|
|
77
|
+
*
|
|
78
|
+
* @param name - Factory name.
|
|
79
|
+
*/
|
|
80
|
+
static remove(name) {
|
|
81
|
+
this.clients.delete(name);
|
|
82
|
+
this.settings.delete(name);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Clear all configured factories.
|
|
86
|
+
*/
|
|
87
|
+
static clear() {
|
|
88
|
+
this.clients.clear();
|
|
89
|
+
this.settings.clear();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get the default (first configured) client.
|
|
93
|
+
* Useful when only one factory is configured.
|
|
94
|
+
*
|
|
95
|
+
* @returns PlayFrameworkClient instance.
|
|
96
|
+
* @throws Error if no factory is configured.
|
|
97
|
+
*/
|
|
98
|
+
static getDefaultClient() {
|
|
99
|
+
const first = this.clients.values().next();
|
|
100
|
+
if (first.done) {
|
|
101
|
+
throw new Error("No PlayFramework client configured. Call PlayFrameworkServices.configure(name, baseUrl) first.");
|
|
102
|
+
}
|
|
103
|
+
return first.value;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get client by optional name. If name is omitted, returns the default (first configured) client.
|
|
107
|
+
*
|
|
108
|
+
* @param name - Optional factory name.
|
|
109
|
+
* @returns PlayFrameworkClient instance.
|
|
110
|
+
*/
|
|
111
|
+
static resolve(name) {
|
|
112
|
+
return name ? this.getClient(name) : this.getDefaultClient();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.PlayFrameworkServices = PlayFrameworkServices;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for PlayFramework client.
|
|
3
|
+
*/
|
|
4
|
+
export declare class PlayFrameworkSettings {
|
|
5
|
+
/**
|
|
6
|
+
* Factory name (for multi-factory endpoints).
|
|
7
|
+
*/
|
|
8
|
+
factoryName: string;
|
|
9
|
+
/**
|
|
10
|
+
* Base API URL (e.g., "https://api.example.com/api/ai").
|
|
11
|
+
*/
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
/**
|
|
14
|
+
* Default headers to include in all requests.
|
|
15
|
+
*/
|
|
16
|
+
defaultHeaders: HeadersInit;
|
|
17
|
+
/**
|
|
18
|
+
* Request timeout in milliseconds (default: 60000ms = 60s).
|
|
19
|
+
*/
|
|
20
|
+
timeout: number;
|
|
21
|
+
/**
|
|
22
|
+
* Maximum number of SSE reconnection attempts on network failure (default: 3).
|
|
23
|
+
* Set to 0 to disable automatic reconnection.
|
|
24
|
+
*/
|
|
25
|
+
maxReconnectAttempts: number;
|
|
26
|
+
/**
|
|
27
|
+
* Base delay in milliseconds between reconnection attempts (default: 1000ms).
|
|
28
|
+
* Uses exponential backoff: delay * 2^attempt.
|
|
29
|
+
*/
|
|
30
|
+
reconnectBaseDelay: number;
|
|
31
|
+
/**
|
|
32
|
+
* Header enrichers (e.g., add Authorization header dynamically).
|
|
33
|
+
*/
|
|
34
|
+
private headersEnrichers;
|
|
35
|
+
/**
|
|
36
|
+
* Error handlers for retry logic.
|
|
37
|
+
*/
|
|
38
|
+
private errorHandlers;
|
|
39
|
+
constructor(factoryName: string, baseUrl: string);
|
|
40
|
+
/**
|
|
41
|
+
* Add a header enricher function (e.g., dynamic Authorization token).
|
|
42
|
+
*/
|
|
43
|
+
addHeadersEnricher(enricher: (url: string, method: string, headers: HeadersInit, body: any) => Promise<HeadersInit>): this;
|
|
44
|
+
/**
|
|
45
|
+
* Add an error handler for retry logic.
|
|
46
|
+
*/
|
|
47
|
+
addErrorHandler(handler: (url: string, method: string, headers: HeadersInit, body: any, error: any) => Promise<boolean>): this;
|
|
48
|
+
/**
|
|
49
|
+
* Enrich headers with all registered enrichers.
|
|
50
|
+
*/
|
|
51
|
+
enrichHeaders(url: string, method: string, headers: HeadersInit | undefined, body: any): Promise<HeadersInit>;
|
|
52
|
+
/**
|
|
53
|
+
* Manage errors with all registered handlers. Returns true if should retry.
|
|
54
|
+
*/
|
|
55
|
+
manageError(url: string, method: string, headers: HeadersInit, body: any, error: any): Promise<boolean>;
|
|
56
|
+
}
|