@openrouter/agent 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 +367 -0
- package/esm/api-shape-helpers/claude-message.d.ts +218 -0
- package/esm/api-shape-helpers/claude-message.d.ts.map +1 -0
- package/esm/api-shape-helpers/claude-message.js +6 -0
- package/esm/api-shape-helpers/claude-message.js.map +1 -0
- package/esm/index.d.ts +22 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +27 -0
- package/esm/index.js.map +1 -0
- package/esm/inner-loop/call-model.d.ts +67 -0
- package/esm/inner-loop/call-model.d.ts.map +1 -0
- package/esm/inner-loop/call-model.js +116 -0
- package/esm/inner-loop/call-model.js.map +1 -0
- package/esm/lib/anthropic-compat.d.ts +51 -0
- package/esm/lib/anthropic-compat.d.ts.map +1 -0
- package/esm/lib/anthropic-compat.js +216 -0
- package/esm/lib/anthropic-compat.js.map +1 -0
- package/esm/lib/anthropic-compat.test.d.ts +2 -0
- package/esm/lib/anthropic-compat.test.d.ts.map +1 -0
- package/esm/lib/anthropic-compat.test.js +668 -0
- package/esm/lib/anthropic-compat.test.js.map +1 -0
- package/esm/lib/async-params.d.ts +107 -0
- package/esm/lib/async-params.d.ts.map +1 -0
- package/esm/lib/async-params.js +94 -0
- package/esm/lib/async-params.js.map +1 -0
- package/esm/lib/chat-compat.d.ts +46 -0
- package/esm/lib/chat-compat.d.ts.map +1 -0
- package/esm/lib/chat-compat.js +111 -0
- package/esm/lib/chat-compat.js.map +1 -0
- package/esm/lib/chat-compat.test.d.ts +2 -0
- package/esm/lib/chat-compat.test.d.ts.map +1 -0
- package/esm/lib/chat-compat.test.js +405 -0
- package/esm/lib/chat-compat.test.js.map +1 -0
- package/esm/lib/claude-constants.d.ts +22 -0
- package/esm/lib/claude-constants.d.ts.map +1 -0
- package/esm/lib/claude-constants.js +20 -0
- package/esm/lib/claude-constants.js.map +1 -0
- package/esm/lib/claude-type-guards.d.ts +10 -0
- package/esm/lib/claude-type-guards.d.ts.map +1 -0
- package/esm/lib/claude-type-guards.js +68 -0
- package/esm/lib/claude-type-guards.js.map +1 -0
- package/esm/lib/conversation-state.d.ts +61 -0
- package/esm/lib/conversation-state.d.ts.map +1 -0
- package/esm/lib/conversation-state.js +230 -0
- package/esm/lib/conversation-state.js.map +1 -0
- package/esm/lib/model-result.d.ts +370 -0
- package/esm/lib/model-result.d.ts.map +1 -0
- package/esm/lib/model-result.js +1483 -0
- package/esm/lib/model-result.js.map +1 -0
- package/esm/lib/next-turn-params.d.ts +30 -0
- package/esm/lib/next-turn-params.d.ts.map +1 -0
- package/esm/lib/next-turn-params.js +129 -0
- package/esm/lib/next-turn-params.js.map +1 -0
- package/esm/lib/reusable-stream.d.ts +39 -0
- package/esm/lib/reusable-stream.d.ts.map +1 -0
- package/esm/lib/reusable-stream.js +192 -0
- package/esm/lib/reusable-stream.js.map +1 -0
- package/esm/lib/stop-conditions.d.ts +80 -0
- package/esm/lib/stop-conditions.d.ts.map +1 -0
- package/esm/lib/stop-conditions.js +104 -0
- package/esm/lib/stop-conditions.js.map +1 -0
- package/esm/lib/stream-transformers.d.ts +109 -0
- package/esm/lib/stream-transformers.d.ts.map +1 -0
- package/esm/lib/stream-transformers.js +856 -0
- package/esm/lib/stream-transformers.js.map +1 -0
- package/esm/lib/stream-type-guards.d.ts +29 -0
- package/esm/lib/stream-type-guards.d.ts.map +1 -0
- package/esm/lib/stream-type-guards.js +85 -0
- package/esm/lib/stream-type-guards.js.map +1 -0
- package/esm/lib/tool-context.d.ts +68 -0
- package/esm/lib/tool-context.d.ts.map +1 -0
- package/esm/lib/tool-context.js +188 -0
- package/esm/lib/tool-context.js.map +1 -0
- package/esm/lib/tool-event-broadcaster.d.ts +44 -0
- package/esm/lib/tool-event-broadcaster.d.ts.map +1 -0
- package/esm/lib/tool-event-broadcaster.js +162 -0
- package/esm/lib/tool-event-broadcaster.js.map +1 -0
- package/esm/lib/tool-executor.d.ts +73 -0
- package/esm/lib/tool-executor.d.ts.map +1 -0
- package/esm/lib/tool-executor.js +267 -0
- package/esm/lib/tool-executor.js.map +1 -0
- package/esm/lib/tool-orchestrator.d.ts +50 -0
- package/esm/lib/tool-orchestrator.d.ts.map +1 -0
- package/esm/lib/tool-orchestrator.js +180 -0
- package/esm/lib/tool-orchestrator.js.map +1 -0
- package/esm/lib/tool-types.d.ts +572 -0
- package/esm/lib/tool-types.d.ts.map +1 -0
- package/esm/lib/tool-types.js +80 -0
- package/esm/lib/tool-types.js.map +1 -0
- package/esm/lib/tool.d.ts +108 -0
- package/esm/lib/tool.d.ts.map +1 -0
- package/esm/lib/tool.js +84 -0
- package/esm/lib/tool.js.map +1 -0
- package/esm/lib/turn-context.d.ts +50 -0
- package/esm/lib/turn-context.d.ts.map +1 -0
- package/esm/lib/turn-context.js +61 -0
- package/esm/lib/turn-context.js.map +1 -0
- package/package.json +125 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type * as models from '@openrouter/sdk/models';
|
|
2
|
+
import type { OpenResponsesResult } from '@openrouter/sdk/models';
|
|
3
|
+
import type { ContextInput } from './tool-context.js';
|
|
4
|
+
import type { ParsedToolCall, StateAccessor, StopWhen, Tool, ToolContextMapWithShared, TurnContext } from './tool-types.js';
|
|
5
|
+
export type { Tool } from './tool-types.js';
|
|
6
|
+
/**
|
|
7
|
+
* A field can be either a value of type T or a function that computes T
|
|
8
|
+
*/
|
|
9
|
+
export type FieldOrAsyncFunction<T> = T | ((context: TurnContext) => T | Promise<T>);
|
|
10
|
+
/**
|
|
11
|
+
* Base input type for callModel without approval-related fields
|
|
12
|
+
*/
|
|
13
|
+
type BaseCallModelInput<TTools extends readonly Tool[] = readonly Tool[], TShared extends Record<string, unknown> = Record<string, never>> = {
|
|
14
|
+
[K in keyof Omit<models.ResponsesRequest, 'stream' | 'tools'>]?: FieldOrAsyncFunction<models.ResponsesRequest[K]>;
|
|
15
|
+
} & {
|
|
16
|
+
tools?: TTools;
|
|
17
|
+
stopWhen?: StopWhen<TTools>;
|
|
18
|
+
/** Typed context data passed to tools via contextSchema. Includes optional `shared` key. */
|
|
19
|
+
context?: ContextInput<ToolContextMapWithShared<TTools, TShared>>;
|
|
20
|
+
/**
|
|
21
|
+
* Call-level approval check - overrides tool-level requireApproval setting
|
|
22
|
+
* Receives the tool call and turn context, can be sync or async
|
|
23
|
+
*/
|
|
24
|
+
requireApproval?: (toolCall: ParsedToolCall<TTools[number]>, context: TurnContext) => boolean | Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* Callback invoked at the start of each tool execution turn
|
|
27
|
+
* Receives the turn context with the current turn number
|
|
28
|
+
*/
|
|
29
|
+
onTurnStart?: (context: TurnContext) => void | Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Callback invoked at the end of each tool execution turn
|
|
32
|
+
* Receives the turn context and the completed response for that turn
|
|
33
|
+
*/
|
|
34
|
+
onTurnEnd?: (context: TurnContext, response: OpenResponsesResult) => void | Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Approval params when state is provided (allows approve/reject)
|
|
38
|
+
*/
|
|
39
|
+
type ApprovalParamsWithState<TTools extends readonly Tool[] = readonly Tool[]> = {
|
|
40
|
+
/** State accessor for multi-turn persistence and approval gates */
|
|
41
|
+
state: StateAccessor<TTools>;
|
|
42
|
+
/** Tool call IDs to approve (for resuming from awaiting_approval status) */
|
|
43
|
+
approveToolCalls?: string[];
|
|
44
|
+
/** Tool call IDs to reject (for resuming from awaiting_approval status) */
|
|
45
|
+
rejectToolCalls?: string[];
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Approval params when state is NOT provided (forbids approve/reject)
|
|
49
|
+
*/
|
|
50
|
+
type ApprovalParamsWithoutState = {
|
|
51
|
+
/** State accessor for multi-turn persistence and approval gates */
|
|
52
|
+
state?: undefined;
|
|
53
|
+
/** Not allowed without state - will cause type error */
|
|
54
|
+
approveToolCalls?: never;
|
|
55
|
+
/** Not allowed without state - will cause type error */
|
|
56
|
+
rejectToolCalls?: never;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Input type for callModel function
|
|
60
|
+
* Each field can independently be a static value or a function that computes the value
|
|
61
|
+
* Generic over TTools to enable proper type inference for stopWhen conditions
|
|
62
|
+
*
|
|
63
|
+
* Type enforcement:
|
|
64
|
+
* - `approveToolCalls` and `rejectToolCalls` are only valid when `state` is provided
|
|
65
|
+
* - Using these without `state` will cause a TypeScript error
|
|
66
|
+
*/
|
|
67
|
+
export type CallModelInput<TTools extends readonly Tool[] = readonly Tool[], TShared extends Record<string, unknown> = Record<string, never>> = BaseCallModelInput<TTools, TShared> & (ApprovalParamsWithState<TTools> | ApprovalParamsWithoutState);
|
|
68
|
+
/**
|
|
69
|
+
* CallModelInput variant that requires state - use when approval workflows are needed
|
|
70
|
+
*/
|
|
71
|
+
export type CallModelInputWithState<TTools extends readonly Tool[] = readonly Tool[], TShared extends Record<string, unknown> = Record<string, never>> = BaseCallModelInput<TTools, TShared> & ApprovalParamsWithState<TTools>;
|
|
72
|
+
/**
|
|
73
|
+
* Resolved CallModelInput (all functions evaluated to values)
|
|
74
|
+
* This is the type after all async functions have been resolved to their values
|
|
75
|
+
*/
|
|
76
|
+
export type ResolvedCallModelInput = Omit<models.ResponsesRequest, 'stream' | 'tools'> & {
|
|
77
|
+
tools?: never;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Resolve all async functions in CallModelInput to their values
|
|
81
|
+
*
|
|
82
|
+
* @param input - Input with possible functions
|
|
83
|
+
* @param context - Turn context for function execution
|
|
84
|
+
* @returns Resolved input with all values (no functions)
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const resolved = await resolveAsyncFunctions(
|
|
89
|
+
* {
|
|
90
|
+
* model: 'gpt-4',
|
|
91
|
+
* temperature: (ctx) => ctx.numberOfTurns * 0.1,
|
|
92
|
+
* input: 'Hello',
|
|
93
|
+
* },
|
|
94
|
+
* { numberOfTurns: 2 }
|
|
95
|
+
* );
|
|
96
|
+
* // resolved.temperature === 0.2
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function resolveAsyncFunctions<TTools extends readonly Tool[] = readonly Tool[]>(input: CallModelInput<TTools>, context: TurnContext): Promise<ResolvedCallModelInput>;
|
|
100
|
+
/**
|
|
101
|
+
* Check if input has any async functions that need resolution
|
|
102
|
+
*
|
|
103
|
+
* @param input - Input to check
|
|
104
|
+
* @returns True if any field is a function
|
|
105
|
+
*/
|
|
106
|
+
export declare function hasAsyncFunctions(input: unknown): boolean;
|
|
107
|
+
//# sourceMappingURL=async-params.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-params.d.ts","sourceRoot":"","sources":["../../src/lib/async-params.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,QAAQ,EACR,IAAI,EACJ,wBAAwB,EACxB,WAAW,EACZ,MAAM,iBAAiB,CAAC;AAGzB,YAAY,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AA6B5C;;GAEG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,WAAW,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAErF;;GAEG;AACH,KAAK,kBAAkB,CACrB,MAAM,SAAS,SAAS,IAAI,EAAE,GAAG,SAAS,IAAI,EAAE,EAChD,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAC7D;KACD,CAAC,IAAI,M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wD;IACxD,eAAe,CAAC,EAAE,KAAK,CAAC;CACzB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc,CACxB,MAAM,SAAS,SAAS,IAAI,EAAE,GAAG,SAAS,IAAI,EAAE,EAChD,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAC7D,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,GACrC,CAAC,uBAAuB,CAAC,MAAM,CAAC,GAAG,0BAA0B,CAAC,CAAC;AAEjE;;GAEG;AACH,MAAM,MAAM,uBAAuB,CACjC,MAAM,SAAS,SAAS,IAAI,EAAE,GAAG,SAAS,IAAI,EAAE,EAChD,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAC7D,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,uBAAuB,CAAC,MAAM,CAAC,CAAC;AAE1E;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG;IACvF,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,qBAAqB,CAAC,MAAM,SAAS,SAAS,IAAI,EAAE,GAAG,SAAS,IAAI,EAAE,EAC1F,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,WAAW,GACnB,OAAO,CAAC,sBAAsB,CAAC,CAwDjC;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAKzD"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is a parameter function
|
|
3
|
+
* Parameter functions take TurnContext and return a value or promise
|
|
4
|
+
*/
|
|
5
|
+
function isParameterFunction(value) {
|
|
6
|
+
return typeof value === 'function';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Build a resolved request object from entries
|
|
10
|
+
* This validates the structure matches the expected ResolvedCallModelInput shape
|
|
11
|
+
*/
|
|
12
|
+
function buildResolvedRequest(entries) {
|
|
13
|
+
const obj = Object.fromEntries(entries);
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Resolve all async functions in CallModelInput to their values
|
|
18
|
+
*
|
|
19
|
+
* @param input - Input with possible functions
|
|
20
|
+
* @param context - Turn context for function execution
|
|
21
|
+
* @returns Resolved input with all values (no functions)
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const resolved = await resolveAsyncFunctions(
|
|
26
|
+
* {
|
|
27
|
+
* model: 'gpt-4',
|
|
28
|
+
* temperature: (ctx) => ctx.numberOfTurns * 0.1,
|
|
29
|
+
* input: 'Hello',
|
|
30
|
+
* },
|
|
31
|
+
* { numberOfTurns: 2 }
|
|
32
|
+
* );
|
|
33
|
+
* // resolved.temperature === 0.2
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export async function resolveAsyncFunctions(input, context) {
|
|
37
|
+
// Build array of resolved entries
|
|
38
|
+
const resolvedEntries = [];
|
|
39
|
+
// Fields that should not be sent to the API (client-side only)
|
|
40
|
+
const clientOnlyFields = new Set([
|
|
41
|
+
'stopWhen', // Handled separately in ModelResult
|
|
42
|
+
'state', // Client-side state management
|
|
43
|
+
'requireApproval', // Client-side approval check function
|
|
44
|
+
'approveToolCalls', // Client-side approval decisions
|
|
45
|
+
'rejectToolCalls', // Client-side rejection decisions
|
|
46
|
+
'context', // Passed through via GetResponseOptions, not sent to API
|
|
47
|
+
'sharedContextSchema', // Client-side schema for shared context validation
|
|
48
|
+
'onTurnStart', // Client-side turn start callback
|
|
49
|
+
'onTurnEnd', // Client-side turn end callback
|
|
50
|
+
]);
|
|
51
|
+
// Iterate over all keys in the input
|
|
52
|
+
for (const [key, value] of Object.entries(input)) {
|
|
53
|
+
// Skip client-only fields - they're handled separately and shouldn't be sent to the API
|
|
54
|
+
// Note: tools are already in API format at this point (converted in callModel()), so we include them
|
|
55
|
+
if (clientOnlyFields.has(key)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (isParameterFunction(value)) {
|
|
59
|
+
try {
|
|
60
|
+
// Execute the function with context and store the result
|
|
61
|
+
const result = await Promise.resolve(value(context));
|
|
62
|
+
resolvedEntries.push([
|
|
63
|
+
key,
|
|
64
|
+
result,
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
// Wrap errors with context about which field failed
|
|
69
|
+
throw new Error(`Failed to resolve async function for field "${key}": ${error instanceof Error ? error.message : String(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// Not a function, use as-is
|
|
74
|
+
resolvedEntries.push([
|
|
75
|
+
key,
|
|
76
|
+
value,
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return buildResolvedRequest(resolvedEntries);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if input has any async functions that need resolution
|
|
84
|
+
*
|
|
85
|
+
* @param input - Input to check
|
|
86
|
+
* @returns True if any field is a function
|
|
87
|
+
*/
|
|
88
|
+
export function hasAsyncFunctions(input) {
|
|
89
|
+
if (!input || typeof input !== 'object') {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return Object.values(input).some((value) => typeof value === 'function');
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=async-params.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-params.js","sourceRoot":"","sources":["../../src/lib/async-params.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,SAAS,mBAAmB,CAC1B,KAAc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MAAM,IAAI,KAAK,CACb,+CAA+C,GAAG,MAChD,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CACvD,EAAE,CACH,CAAC;YACJ,CAAC;QACH,CAAC;aAAM,CAAC;YACN,4BAA4B;YAC5B,eAAe,CAAC,IAAI,CAAC;gBACnB,GAAG;gBACH,KAAK;aACG,CAAC,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,oBAAoB,CAAC,eAAe,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC;AAC3E,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type * as models from '@openrouter/sdk/models';
|
|
2
|
+
import { extractMessageFromResponse } from './stream-transformers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Convert OpenAI chat-style messages to OpenResponses input format.
|
|
5
|
+
*
|
|
6
|
+
* This function transforms Message[] (OpenAI chat format) to OpenResponsesInput
|
|
7
|
+
* format that can be passed directly to callModel().
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { fromChatMessages } from '@openrouter/sdk';
|
|
12
|
+
*
|
|
13
|
+
* const chatMessages = [
|
|
14
|
+
* { role: "system", content: "You are a helpful assistant." },
|
|
15
|
+
* { role: "user", content: "Hello!" },
|
|
16
|
+
* ];
|
|
17
|
+
*
|
|
18
|
+
* const response = openrouter.callModel({
|
|
19
|
+
* model: "openai/gpt-4",
|
|
20
|
+
* input: fromChatMessages(chatMessages),
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function fromChatMessages(messages: models.ChatMessages[]): models.InputsUnion;
|
|
25
|
+
/**
|
|
26
|
+
* Convert an OpenResponses response to OpenAI chat message format.
|
|
27
|
+
*
|
|
28
|
+
* This function transforms OpenResponsesResult to ChatAssistantMessage
|
|
29
|
+
* (OpenAI chat format) for compatibility with code expecting chat responses.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { toChatMessage } from '@openrouter/sdk';
|
|
34
|
+
*
|
|
35
|
+
* const response = await openrouter.callModel({
|
|
36
|
+
* model: "openai/gpt-4",
|
|
37
|
+
* input: "Hello!",
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* const openResponsesResult = await response.getResponse();
|
|
41
|
+
* const chatMessage = toChatMessage(openResponsesResult);
|
|
42
|
+
* // chatMessage is now { role: "assistant", content: "..." }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const toChatMessage: typeof extractMessageFromResponse;
|
|
46
|
+
//# sourceMappingURL=chat-compat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-compat.d.ts","sourceRoot":"","sources":["../../src/lib/chat-compat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,MAAM,wBAAwB,CAAC;AAQtD,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAoDtE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,YAAY,EAAE,GAAG,MAAM,CAAC,WAAW,CAuBpF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,aAAa,mCAA6B,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { EasyInputMessageRoleAssistant, EasyInputMessageRoleDeveloper, EasyInputMessageRoleSystem, EasyInputMessageRoleUser, } from '@openrouter/sdk/models/easyinputmessage';
|
|
2
|
+
import { extractMessageFromResponse } from './stream-transformers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Type guard for ChatToolMessage
|
|
5
|
+
*/
|
|
6
|
+
function isToolResponseMessage(msg) {
|
|
7
|
+
return msg.role === 'tool';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for ChatAssistantMessage
|
|
11
|
+
*/
|
|
12
|
+
function isAssistantMessage(msg) {
|
|
13
|
+
return msg.role === 'assistant';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Maps chat role strings to OpenResponses role types
|
|
17
|
+
*/
|
|
18
|
+
function mapChatRole(role) {
|
|
19
|
+
switch (role) {
|
|
20
|
+
case 'user':
|
|
21
|
+
return EasyInputMessageRoleUser.User;
|
|
22
|
+
case 'system':
|
|
23
|
+
return EasyInputMessageRoleSystem.System;
|
|
24
|
+
case 'assistant':
|
|
25
|
+
return EasyInputMessageRoleAssistant.Assistant;
|
|
26
|
+
case 'developer':
|
|
27
|
+
return EasyInputMessageRoleDeveloper.Developer;
|
|
28
|
+
default: {
|
|
29
|
+
const exhaustiveCheck = role;
|
|
30
|
+
throw new Error(`Unhandled role type: ${exhaustiveCheck}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Convert message content to a string representation.
|
|
36
|
+
* Handles string, null, undefined, and object content types.
|
|
37
|
+
*/
|
|
38
|
+
function contentToString(content) {
|
|
39
|
+
if (typeof content === 'string') {
|
|
40
|
+
return content;
|
|
41
|
+
}
|
|
42
|
+
if (content === null || content === undefined) {
|
|
43
|
+
return '';
|
|
44
|
+
}
|
|
45
|
+
return JSON.stringify(content);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convert OpenAI chat-style messages to OpenResponses input format.
|
|
49
|
+
*
|
|
50
|
+
* This function transforms Message[] (OpenAI chat format) to OpenResponsesInput
|
|
51
|
+
* format that can be passed directly to callModel().
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { fromChatMessages } from '@openrouter/sdk';
|
|
56
|
+
*
|
|
57
|
+
* const chatMessages = [
|
|
58
|
+
* { role: "system", content: "You are a helpful assistant." },
|
|
59
|
+
* { role: "user", content: "Hello!" },
|
|
60
|
+
* ];
|
|
61
|
+
*
|
|
62
|
+
* const response = openrouter.callModel({
|
|
63
|
+
* model: "openai/gpt-4",
|
|
64
|
+
* input: fromChatMessages(chatMessages),
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function fromChatMessages(messages) {
|
|
69
|
+
return messages.map((msg) => {
|
|
70
|
+
if (isToolResponseMessage(msg)) {
|
|
71
|
+
return {
|
|
72
|
+
type: 'function_call_output',
|
|
73
|
+
callId: msg.toolCallId,
|
|
74
|
+
output: contentToString(msg.content),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (isAssistantMessage(msg)) {
|
|
78
|
+
return {
|
|
79
|
+
role: mapChatRole('assistant'),
|
|
80
|
+
content: contentToString(msg.content),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// System, user, developer messages
|
|
84
|
+
return {
|
|
85
|
+
role: mapChatRole(msg.role),
|
|
86
|
+
content: contentToString(msg.content),
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Convert an OpenResponses response to OpenAI chat message format.
|
|
92
|
+
*
|
|
93
|
+
* This function transforms OpenResponsesResult to ChatAssistantMessage
|
|
94
|
+
* (OpenAI chat format) for compatibility with code expecting chat responses.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { toChatMessage } from '@openrouter/sdk';
|
|
99
|
+
*
|
|
100
|
+
* const response = await openrouter.callModel({
|
|
101
|
+
* model: "openai/gpt-4",
|
|
102
|
+
* input: "Hello!",
|
|
103
|
+
* });
|
|
104
|
+
*
|
|
105
|
+
* const openResponsesResult = await response.getResponse();
|
|
106
|
+
* const chatMessage = toChatMessage(openResponsesResult);
|
|
107
|
+
* // chatMessage is now { role: "assistant", content: "..." }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export const toChatMessage = extractMessageFromResponse;
|
|
111
|
+
//# sourceMappingURL=chat-compat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-compat.js","sourceRoot":"","sources":["../../src/lib/chat-compat.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,6BAA6B,EAC7B,6BAA6B,EAC7B,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAEtE;;GAEG;AACH,SAAS,qBAAqB,CAAC,GAAwB;IACrD,OAAO,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,SAAS,kBAAkB,CAAC,GAAwB;IAClD,OAAO,GAAG,CAAC,IAAI,KAAK,WAAW,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAS,WAAW,CAClB,IAAmD;IAEnD,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,MAAM;YACT,OAAO,wBAAwB,CAAC,IAAI,CAAC;QACvC,KAAK,QAAQ;YACX,OAAO,0BAA0B,CAAC,MAAM,CAAC;QAC3C,KAAK,WAAW;YACd,OAAO,6BAA6B,CAAC,SAAS,CAAC;QACjD,KAAK,WAAW;YACd,OAAO,6BAA6B,CAAC,SAAS,CAAC;QACjD,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,eAAe,GAAU,IAAI,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,wBAAwB,eAAe,EAAE,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;GAGG;AAC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