Skip to content

Commit 9f6ec32

Browse files
committed
script
1 parent 1929574 commit 9f6ec32

File tree

325 files changed

+6380
-7204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

325 files changed

+6380
-7204
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AppIntentManager, AppIntentProtocol, Widget, } from "scripting"
2+
import { store } from "./store"
3+
4+
/**
5+
* Use in a Toggle view of `widget.tsx`.
6+
* When the toggle was tapped, the perform function will be called and will toggle the read state of that doc.
7+
*/
8+
export const ToggleReadIntent = AppIntentManager.register({
9+
name: "ToggleReadIntent",
10+
protocol: AppIntentProtocol.AppIntent,
11+
perform: async (title: string) => {
12+
store.toggleRead(title)
13+
}
14+
})
15+
16+
/**
17+
* Use in a Button view of `widget.tsx`.
18+
* When the button was tapped, the perform function will be called and refresh the doc list and reload the widget.
19+
*/
20+
export const RefreshDocsIntent = AppIntentManager.register({
21+
name: "RefreshDocsIntent",
22+
protocol: AppIntentProtocol.AppIntent,
23+
perform: async () => {
24+
store.saveRandomDocsToRead()
25+
Widget.reloadAll()
26+
}
27+
})
Lines changed: 63 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,105 @@
1-
# Assistant
2-
31
The `Assistant` module provides a powerful API that allows users to request structured JSON data from the assistant. This functionality can be used for automation tasks such as extracting bill details, categorizing expenses, or parsing text data.
42

53
## `isAvailable` Variable
64

75
### Description
8-
96
Indicates whether the Assistant API is available.
10-
11-
- This status depends on the selected AI provider and whether a valid API Key is configured.
12-
- If the appropriate API Key is not provided, the Assistant API will be unavailable.
7+
* This status depends on the selected AI provider and whether a valid API Key is configured.
8+
* If the appropriate API Key is not provided, the Assistant API will be unavailable.
139

1410
## `requestStructuredData` Method
1511

1612
### Description
17-
1813
`requestStructuredData` allows users to send a text prompt to the assistant and receive structured data in JSON format based on a defined schema.
1914

2015
### Syntax
21-
2216
```ts
23-
function requestStructuredData<R>(prompt: string, schema: JSONSchemaArray | JSONSchemaObject): Promise<R>;
17+
function requestStructuredData<R>(
18+
prompt: string,
19+
schema: JSONSchemaArray | JSONSchemaObject
20+
): Promise<R>
2421
```
2522

2623
### Parameters
27-
28-
- `prompt` (`string`): The input prompt describing the content to be parsed.
29-
- `schema` (`JSONSchemaArray | JSONSchemaObject`): The expected output JSON schema, defining the structure of the returned data.
24+
- `prompt` (`string`): The input prompt describing the content to be parsed.
25+
- `schema` (`JSONSchemaArray | JSONSchemaObject`): The expected output JSON schema, defining the structure of the returned data.
3026

3127
### Return Value
32-
3328
Returns a `Promise` resolving to structured JSON data that matches the `schema` definition, with a type of `R`.
3429

3530
---
3631

3732
## JSON Schema Definition
38-
3933
The `schema` parameter in the `requestStructuredData` method defines the structure of the returned JSON data, with the following types:
4034

4135
### `JSONSchemaType`
42-
4336
```ts
44-
type JSONSchemaType = JSONSchemaPrimitive | JSONSchemaArray | JSONSchemaObject;
37+
type JSONSchemaType = JSONSchemaPrimitive | JSONSchemaArray | JSONSchemaObject
4538
```
4639

4740
Primitive data type definition:
48-
4941
```ts
5042
type JSONSchemaPrimitive = {
51-
type: "string" | "number" | "boolean";
52-
required?: boolean;
53-
description: string;
54-
};
43+
type: "string" | "number" | "boolean"
44+
required?: boolean
45+
description: string
46+
}
5547
```
5648
5749
Array type definition:
58-
5950
```ts
6051
type JSONSchemaArray = {
61-
type: "array";
62-
items: JSONSchemaType;
63-
required?: boolean;
64-
description: string;
65-
};
52+
type: "array"
53+
items: JSONSchemaType
54+
required?: boolean
55+
description: string
56+
}
6657
```
6758
6859
Object type definition:
69-
7060
```ts
7161
type JSONSchemaObject = {
72-
type: "object";
73-
properties: Record<string, JSONSchemaType>;
74-
required?: boolean;
75-
description: string;
76-
};
62+
type: "object"
63+
properties: Record<string, JSONSchemaType>
64+
required?: boolean
65+
description: string
66+
}
7767
```
7868
7969
### Example
80-
8170
```ts
8271
const schema: JSONSchemaObject = {
83-
type: "object",
84-
properties: {
85-
totalAmount: {
86-
type: "number",
87-
required: true,
88-
description: "Total bill amount",
89-
},
90-
category: {
91-
type: "string",
92-
required: true,
93-
description: "Bill category",
94-
},
95-
date: {
96-
type: "string",
97-
required: false,
98-
description: "Bill date",
99-
},
100-
location: {
101-
type: "string",
102-
required: false,
103-
description: "Bill location",
104-
},
72+
type: "object",
73+
properties: {
74+
totalAmount: {
75+
type: "number",
76+
required: true,
77+
description: "Total bill amount"
78+
},
79+
category: {
80+
type: "string",
81+
required: true,
82+
description: "Bill category"
83+
},
84+
date: {
85+
type: "string",
86+
required: false,
87+
description: "Bill date"
10588
},
106-
};
89+
location: {
90+
type: "string",
91+
required: false,
92+
description: "Bill location"
93+
}
94+
}
95+
}
10796
```
10897

10998
---
11099

111100
## Example Usage
112101

113102
### Parsing Bill Information
114-
115103
Suppose we have a bill, and we need to extract its amount, date, category, and location.
116104

117105
```ts
@@ -120,41 +108,44 @@ const someBillDetails = `
120108
- Date: 2024-03-11 14:30
121109
- Location: City Center Parking Lot
122110
- Category: Parking
123-
`;
111+
`
124112

125-
const prompt = `Please parse the following bill and output the structured data: ${someBillDetails}`;
113+
const prompt = `Please parse the following bill and output the structured data: ${someBillDetails}`
126114

127-
const data = await Assistant.requestStructuredData(prompt, schema);
128-
console.log(data);
115+
const data = await Assistant.requestStructuredData(
116+
prompt,
117+
schema
118+
)
119+
console.log(data)
129120
```
130121

131122
### Possible Output
132-
133123
```json
134124
{
135-
"totalAmount": 15.0,
136-
"category": "Parking",
137-
"date": "2024-03-11 14:30",
138-
"location": "City Center Parking Lot"
125+
"totalAmount": 15.00,
126+
"category": "Parking",
127+
"date": "2024-03-11 14:30",
128+
"location": "City Center Parking Lot"
139129
}
140130
```
141131

142132
---
143133

144134
## Usage Considerations
145-
146135
1. **Ensure the `schema` is correctly defined**: The JSON schema should match the expected data format.
147136
2. **Use `required` attributes carefully**: If a field is essential, set `required: true`.
148137
3. **Provide a clear `prompt`**: A detailed `prompt` improves the accuracy of the assistant's response.
149138
4. **Error Handling**: Since `requestStructuredData` returns a `Promise`, handle potential errors using `try-catch`.
150139

151140
Example error handling:
152-
153141
```ts
154142
try {
155-
const data = await Assistant.requestStructuredData(prompt, schema);
156-
console.log("Parsed result:", data);
143+
const data = await Assistant.requestStructuredData(
144+
prompt,
145+
schema
146+
)
147+
console.log("Parsed result:", data)
157148
} catch (error) {
158-
console.error("Parsing failed:", error);
149+
console.error("Parsing failed:", error)
159150
}
160151
```

0 commit comments

Comments
 (0)