Skip to content

Commit 53e799e

Browse files
authored
feat!: replace output_text property with getResponseOutputText helper (#48)
Adds a simple helper function to extract aggregated text output from ResponseObject instances. This **replaces** the automatic `output_text` property approach from PR #42 with an explicit helper function, providing a cleaner API without prototype patching or side-effectful imports. ## Changes - **Removes** `src/lib/init.ts` (automatic property patching) - **Adds** `getResponseOutputText()` helper function - Works for both streaming and non-streaming responses ## Usage ### Streaming responses ```typescript import { getResponseOutputText } from 'llama-stack-client'; const stream = await client.responses.create({ stream: true, ... }); for await (const chunk of stream) { if (chunk.type === 'response.completed') { const text = getResponseOutputText(chunk.response); console.log(text); } } ``` ### Non-streaming responses ```typescript import { getResponseOutputText } from 'llama-stack-client'; const response = await client.responses.create({ stream: false, ... }); const text = getResponseOutputText(response); ``` This provides a clean, explicit API for accessing aggregated text from responses without relying on magic property injection.
1 parent bced728 commit 53e799e

File tree

3 files changed

+61
-129
lines changed

3 files changed

+61
-129
lines changed

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ import {
151151
VectorStoresOpenAICursorPage,
152152
} from './resources/vector-stores/vector-stores';
153153

154-
// MANUAL: Auto-install response helpers (preserve across regeneration)
155-
import './lib/init';
156-
157154
export interface ClientOptions {
158155
/**
159156
* Defaults to process.env['LLAMA_STACK_CLIENT_API_KEY'].
@@ -573,6 +570,7 @@ export declare namespace LlamaStackClient {
573570
export type SystemMessage = API.SystemMessage;
574571
}
575572

573+
export { getResponseOutputText } from './lib/response-helpers';
576574
export { toFile, fileFromPath } from './uploads';
577575
export {
578576
LlamaStackClientError,

src/lib/init.ts

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/lib/response-helpers.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// All rights reserved.
3+
//
4+
// This source code is licensed under the terms described in the LICENSE file in
5+
// the root directory of this source tree.
6+
7+
/**
8+
* Helper utilities for working with response objects.
9+
*/
10+
11+
import type { ResponseObject } from '../resources/responses/responses';
12+
13+
/**
14+
* Extracts aggregated text output from a ResponseObject.
15+
* This concatenates all `output_text` entries from the response's output array.
16+
*
17+
* Useful for streaming responses where you want to get the final text from chunk.response:
18+
*
19+
* @example
20+
* ```ts
21+
* const stream = await client.responses.create({ stream: true, ... });
22+
* for await (const chunk of stream) {
23+
* if (chunk.type === 'response.completed') {
24+
* const text = getResponseOutputText(chunk.response);
25+
* console.log(text);
26+
* }
27+
* }
28+
* ```
29+
*/
30+
export function getResponseOutputText(response: ResponseObject): string {
31+
const pieces: string[] = [];
32+
33+
for (const output of response.output ?? []) {
34+
if (!output || output.type !== 'message') {
35+
continue;
36+
}
37+
38+
const content = output.content;
39+
if (typeof content === 'string') {
40+
pieces.push(content);
41+
continue;
42+
}
43+
44+
if (!Array.isArray(content)) {
45+
continue;
46+
}
47+
48+
for (const item of content) {
49+
if (typeof item === 'string') {
50+
pieces.push(item);
51+
continue;
52+
}
53+
if (item && item.type === 'output_text' && 'text' in item && typeof item.text === 'string') {
54+
pieces.push(item.text);
55+
}
56+
}
57+
}
58+
59+
return pieces.join('');
60+
}

0 commit comments

Comments
 (0)